2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-14 18:42:24 +03:00

feat(parser): custom open and close tags support, html tags tests (#3)

This commit is contained in:
Nikolay Kostyurin
2018-09-18 23:41:52 +02:00
committed by GitHub
parent f5fd078eca
commit 790825af30
9 changed files with 268 additions and 100 deletions
+86 -14
View File
@@ -14,15 +14,16 @@ const tokenize = input => (createLexer(input).tokenize());
describe('lexer', () => {
const expectOutput = (output, tokens) => {
expect(tokens.length).toBe(output.length);
expect(tokens).toBeInstanceOf(Array);
output.forEach((token, idx) => {
expect(tokens[idx]).toBeInstanceOf(Object);
expect(tokens[idx].type).toEqual(token[0]);
expect(tokens[idx].value).toEqual(token[1]);
tokens.forEach((token, idx) => {
expect(token).toBeInstanceOf(Object);
expect(token.type).toEqual(output[idx][0]);
expect(token.value).toEqual(output[idx][1]);
});
};
test('tokenize single tag', () => {
test('single tag', () => {
const input = '[SingleTag]';
const tokens = tokenize(input);
const output = [
@@ -32,7 +33,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize single tag with spaces', () => {
test('single tag with spaces', () => {
const input = '[Single Tag]';
const tokens = tokenize(input);
@@ -43,7 +44,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize string with quotemarks', () => {
test('string with quotemarks', () => {
const input = '"Someone Like You" by Adele';
const tokens = tokenize(input);
@@ -62,7 +63,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize tags in brakets', () => {
test('tags in brakets', () => {
const input = '[ [h1]G[/h1] ]';
const tokens = tokenize(input);
@@ -79,7 +80,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize tag as param', () => {
test('tag as param', () => {
const input = '[color="#ff0000"]Text[/color]';
const tokens = tokenize(input);
const output = [
@@ -92,7 +93,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize tag with quotemark params with spaces', () => {
test('tag with quotemark params with spaces', () => {
const input = '[url text="Foo Bar" text2="Foo Bar 2"]Text[/url]';
const tokens = tokenize(input);
const output = [
@@ -108,7 +109,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize tag with escaped quotemark param', () => {
test('tag with escaped quotemark param', () => {
const input = `[url text="Foo \\"Bar"]Text[/url]`;
const tokens = tokenize(input);
const output = [
@@ -122,7 +123,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize tag param without quotemarks', () => {
test('tag param without quotemarks', () => {
const input = '[style color=#ff0000]Text[/style]';
const tokens = tokenize(input);
const output = [
@@ -136,7 +137,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize list tag with items', () => {
test('list tag with items', () => {
const input = `[list]
[*] Item 1.
[*] Item 2.
@@ -174,7 +175,7 @@ describe('lexer', () => {
expectOutput(output, tokens);
});
test('tokenize bad tags as texts', () => {
test('bad tags as texts', () => {
const inputs = [
'[]',
'[=]',
@@ -234,4 +235,75 @@ describe('lexer', () => {
expectOutput(asserts[idx], tokens);
});
});
/*
test('bad unclosed tag', () => {
const input = `[Finger tapping; R.H. = Right Hand) Part A [Finger tapping (Right hand -15-, -16-)]`;
const tokens = tokenize(input);
const output = [];
expectOutput(output, tokens);
});
*/
describe('html', () => {
const tokenizeHTML = input => createLexer(input, { openTag: '<', closeTag: '>' }).tokenize();
test('Normal attributes', () => {
const content = `<button id="test0" class="value0" title="value1">class="value0" title="value1"</button>`;
const tokens = tokenizeHTML(content);
const output = [
[TYPE.TAG, 'button', 2, 0],
[TYPE.ATTR_NAME, 'id', 2, 0],
[TYPE.ATTR_VALUE, 'test0', 2, 0],
[TYPE.ATTR_NAME, 'class', 2, 0],
[TYPE.ATTR_VALUE, 'value0', 2, 0],
[TYPE.ATTR_NAME, 'title', 2, 0],
[TYPE.ATTR_VALUE, 'value1', 2, 0],
[TYPE.WORD, "class=\"value0\"", 2, 0],
[TYPE.SPACE, " ", 2, 0],
[TYPE.WORD, "title=\"value1\"", 2, 0],
[TYPE.TAG, '/button', 2, 0]
];
expectOutput(output, tokens);
});
test('Attributes with no quotes or value', () => {
const content = `<button id="test1" class=value2 disabled>class=value2 disabled</button>`;
const tokens = tokenizeHTML(content);
const output = [
[TYPE.TAG, 'button', 2, 0],
[TYPE.ATTR_NAME, 'id', 2, 0],
[TYPE.ATTR_VALUE, 'test1', 2, 0],
[TYPE.ATTR_NAME, 'class', 2, 0],
[TYPE.ATTR_VALUE, 'value2', 2, 0],
[TYPE.ATTR_VALUE, 'disabled', 2, 0],
[TYPE.WORD, "class=value2", 2, 0],
[TYPE.SPACE, " ", 2, 0],
[TYPE.WORD, "disabled", 2, 0],
[TYPE.TAG, '/button', 2, 0]
];
expectOutput(output, tokens);
});
test('Attributes with no space between them. No valid, but accepted by the browser', () => {
const content = `<button id="test2" class="value4"title="value5">class="value4"title="value5"</button>`;
const tokens = tokenizeHTML(content);
const output = [
[TYPE.TAG, 'button', 2, 0],
[TYPE.ATTR_NAME, 'id', 2, 0],
[TYPE.ATTR_VALUE, 'test2', 2, 0],
[TYPE.ATTR_NAME, 'class', 2, 0],
[TYPE.ATTR_VALUE, 'value4', 2, 0],
[TYPE.ATTR_NAME, 'title', 2, 0],
[TYPE.ATTR_VALUE, 'value5', 2, 0],
[TYPE.WORD, "class=\"value4\"title=\"value5\"", 2, 0],
[TYPE.TAG, '/button', 2, 0]
];
expectOutput(output, tokens);
});
})
});
+93 -29
View File
@@ -1,11 +1,15 @@
import { parse } from '../src'
describe('Parser', () => {
const expectOutput = (ast, output) => {
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual(output);
};
test('parse paired tags tokens', () => {
const ast = parse('[best name=value]Foo Bar[/best]');
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual([
expectOutput(ast, [
{
tag: 'best',
attrs: {
@@ -25,8 +29,7 @@ describe('Parser', () => {
onlyAllowTags: ['h1']
});
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual([
expectOutput(ast, [
{
tag: 'h1',
attrs: {
@@ -45,36 +48,32 @@ describe('Parser', () => {
test('parse inconsistent tags', () => {
const ast = parse('[h1 name=value]Foo [Bar] /h1]');
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual(
[
{
attrs: {},
tag: 'h1',
content: []
},
'Foo',
' ',
{
tag: 'Bar',
attrs: {},
content: []
},
' ',
'/h1]',
]
);
expectOutput(ast, [
{
attrs: {},
tag: 'h1',
content: []
},
'Foo',
' ',
{
tag: 'Bar',
attrs: {},
content: []
},
' ',
'/h1]',
]);
});
test('parse tag with value param', () => {
const ast = parse('[url=https://github.com/jilizart/bbob]BBob[/url]');
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual([
expectOutput(ast, [
{
tag: 'url',
attrs: {
url: 'https://github.com/jilizart/bbob',
'https://github.com/jilizart/bbob': 'https://github.com/jilizart/bbob',
},
content: ['BBob'],
},
@@ -84,8 +83,7 @@ describe('Parser', () => {
test('parse tag with quoted param with spaces', () => {
const ast = parse('[url href=https://ru.wikipedia.org target=_blank text="Foo Bar"]Text[/url]');
expect(ast).toBeInstanceOf(Array);
expect(ast).toEqual([
expectOutput(ast, [
{
tag: 'url',
attrs: {
@@ -103,5 +101,71 @@ describe('Parser', () => {
const ast = parse('[c][/c][b]hello[/c][/b][b]', { onError });
expect(onError).toHaveBeenCalled();
})
});
describe('html', () => {
const parseHTML = input => parse(input, { openTag: '<', closeTag: '>' });
test('normal attributes', () => {
const content = `<button id="test0" class="value0" title="value1">class="value0" title="value1"</button>`;
const ast = parseHTML(content);
expectOutput(ast, [
{
"tag": "button",
"attrs": {
"id": "test0",
"class": "value0",
"title": "value1"
},
"content": [
"class=\"value0\"",
" ",
"title=\"value1\""
]
}
]);
});
test('attributes with no quotes or value', () => {
const content = `<button id="test1" class=value2 disabled required>class=value2 disabled</button>`;
const ast = parseHTML(content);
expectOutput(ast, [
{
"tag": "button",
"attrs": {
"id": "test1",
"class": "value2",
"disabled": "disabled",
"required": "required"
},
"content": [
"class=value2",
" ",
"disabled"
]
}
]);
});
test('attributes with no space between them. no valid, but accepted by the browser', () => {
const content = `<button id="test2" class="value4"title="value5">class="value4"title="value5"</button>`;
const ast = parseHTML(content);
expectOutput(ast, [
{
"tag": "button",
"attrs": {
"id": "test2",
"class": "value4",
"title": "value5"
},
"content": [
"class=\"value4\"title=\"value5\""
]
}
]);
});
});
});
+1 -1
View File
@@ -7,7 +7,7 @@ describe('posthtml-render', () => {
const ast = parse('[size=150][b]PostHTML render test[/b][/size]');
const html = render(ast);
expect(html).toBe('<size size="150"><b>PostHTML render test</b></size>')
expect(html).toBe('<size 150="150"><b>PostHTML render test</b></size>')
})
});