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

feat(parser): context free tag mode (#165)

* feat(parser): initial context free tag mode

* fix: tests coverage

* chore: update readme

* chore: remove unused badge from readme
This commit is contained in:
Nikolay Kost
2023-01-27 05:26:09 +02:00
committed by GitHub
parent 6b2810fcf4
commit 19e8dd659e
11 changed files with 215 additions and 79 deletions
+9
View File
@@ -0,0 +1,9 @@
import { TagNode } from "../src/index";
describe('index', () => {
test('tag with content and params', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
expect(String(tagNode)).toBe('[test test="1"]Hello[/test]');
});
})
+19
View File
@@ -14,6 +14,7 @@ const TYPE_NAMES = Object.fromEntries(Object.keys(TYPE).map(key => [TYPE[key], k
const tokenize = input => (createLexer(input).tokenize());
const tokenizeEscape = input => (createLexer(input, { enableEscapeTags: true }).tokenize());
const tokenizeContextFreeTags = (input, tags = []) => (createLexer(input, { contextFreeTags: tags }).tokenize());
describe('lexer', () => {
expect.extend({
@@ -463,6 +464,24 @@ describe('lexer', () => {
expect(tokens).toBeMantchOutput(output);
});
test('context free tag [code]', () => {
const input = '[code] [b]some string[/b][/code]'
const tokens = tokenizeContextFreeTags(input, ['code']);
const output = [
[TYPE.TAG, 'code', 0, 0],
[TYPE.SPACE, ' ', 0, 0],
[TYPE.WORD, '[', 0, 0],
[TYPE.WORD, 'b]some', 0, 0],
[TYPE.SPACE, ' ', 0, 0],
[TYPE.WORD, 'string', 0, 0],
[TYPE.WORD, '[', 0, 0],
[TYPE.WORD, '/b]', 0, 0],
[TYPE.TAG, '/code', 0, 0],
]
expect(tokens).toBeMantchOutput(output);
})
test('bad closed tag with escaped backslash', () => {
const input = `[b]test[\\b]`;
const tokens = tokenizeEscape(input);
+42
View File
@@ -25,6 +25,23 @@ describe('Parser', () => {
expectOutput(ast, output);
});
test('parse paired tags tokens 2', () => {
const ast = parse('[bar]Foo Bar[/bar]');
const output = [
{
tag: 'bar',
attrs: {},
content: [
'Foo',
' ',
'Bar',
],
},
];
expectOutput(ast, output);
});
describe('onlyAllowTags', () => {
test('parse only allowed tags', () => {
const ast = parse('[h1 name=value]Foo [Bar] [/h1]', {
@@ -126,6 +143,31 @@ describe('Parser', () => {
});
})
describe('contextFreeTags', () => {
test('context free tag [code]', () => {
const ast = parse('[code] [b]some string[/b][/code]', {
contextFreeTags: ['code']
});
const output = [
{
tag: 'code',
attrs: {},
content: [
' ',
'[',
'b]some',
' ',
'string',
'[',
'/b]'
]
}
]
expectOutput(ast, output);
})
})
test('parse inconsistent tags', () => {
const ast = parse('[h1 name=value]Foo [Bar] /h1]');
const output = [