/* * @author acrazing joking.young@gmail.com * @since 2017-08-20 17:32:24 */ import * as assert from 'node:assert'; import { parse } from './parse'; import { SyntaxKind } from './types'; import type { IAttribute, IAttributeValue, INode, ITag, IText } from './types'; export let index = 0; export function text(input: string, start = index): IText { return { type: SyntaxKind.Text, start: start, end: (index = input.length + start), value: input, }; } export function tag( input: string, name: string, open: IText, attributes: IAttribute[], body: INode[] | undefined | null, close: IText | undefined | null, start: number, rawName = name, ): ITag { return { start: start, end: (index = start + input.length), type: SyntaxKind.Tag, open: open, name: name, rawName: rawName, attributes: attributes, attributeMap: undefined, body: body, close: close, }; } function attr(name: IText, value?: IAttributeValue): IAttribute { return { start: name.start, end: (index = value ? value.end : name.end), name: name, value: value, }; } function value(input: string, quote: undefined | "'" | '"', start = index): IAttributeValue { return { start: start, end: (index = start + (quote === void 0 ? 0 : 2) + input.length), value: input, quote: quote, }; } const scenes: Array<{ name: string; input: string; nodes: INode[]; }> = [ { name: 'text', input: 'hello world', nodes: [text('hello world', 0)], }, { name: 'text twice', input: 'hello < world', nodes: [text('hello < world', 0)], }, { name: 'single tag', input: '
', nodes: [tag('
', 'div', text('
', 0), [], [], text('
'), 0)], }, { name: 'tag attributes', input: '
', nodes: [ tag( '
', 'div', text('
', 0), [ attr(text('a1', 5)), attr(text('b2', index + 1), value('c3', void 0, index + 1)), attr(text('d4', index + 1), value('e5', void 0, index + 3)), attr(text('f6', index + 1), value('g7', "'", index + 1)), attr(text('h8', index + 1), value('i9', '"', index + 1)), ], void 0, null, 0, ), ], }, { name: 'nested tags', input: `
hello world

h1

span
`, nodes: [ text('\n', 0), tag( `
hello world

h1

span
`, 'div', text('
', 1), [attr(text('id', 6), value('1', '"', 9))], [ text('\n hello world\n ', 13), tag( '

h1

', 'h1', text('

', 30), [attr(text('id', 34), value('h1', '"', 37))], [text('h1', 42)], text('

', 44), 30, ), text('\n ', 49), tag( '', 'img', text('', 52), [attr(text('src', 57), value('/src/index.ts', '"', 61))], void 0, null, 52, ), text('\n ', 77), tag('', 'input', text('', 80), [], void 0, null, 80), text('\n ', 89), tag( `
span
`, 'div', text('
', 92), [attr(text('id', 97), value('2', '"', 100))], [ text('\n ', 104), tag( `
span
`, 'div', text('
', 109), [attr(text('id', 114), value('3', '"', 117))], [ text('\n ', 121), tag( 'span', 'span', text('', 128), [], [text('span', 134)], text('', 138), 128, ), text('\n ', 145), tag( '', 'empty', text('', 152), [], [], text('', 159), 152, ), text('\n ', 167), ], text('
', 172), 109, ), text('\n ', 178), ], text('
', 181), 92, ), text('\n', 187), ], text('
', 188), 1, ), text('\n ', 194), ], }, { name: 'doctype', input: '', nodes: [ tag( '', '!doctype', text('', 0), [attr(text('html', 10))], void 0, null, 0, ), tag('', 'html', text('', 15), [], [], text('', 21), 15), ], }, { name: 'comments', input: '', nodes: [ tag( '', '!--', text('', 20), 0, ), tag( '', '!', text('', 42), 23, ), tag( '', '!', text('', 62), 43, ), tag( '', '', text('<', 63), [], [text('? qm comment ?', 64)], text('>', 78), 63, ), tag( '', '!', text('', 97), 79, ), ], }, { name: 'normal comment special', input: '', nodes: [ tag( '', '!--', text('', 14), 0, ), ], }, { name: 'script', input: '', nodes: [ tag( '', 'script', text('', 22), 0, ), ], }, { name: 'script', input: '', nodes: [ tag( '', 'style', text('', 20), 0, ), ], }, { name: 'tag name', input: '
', nodes: [tag('
', 'div', text('
', 0), [], [], text('
', 5), 0, 'DIV')], }, ]; describe('parse cases', () => { for (const scene of scenes) { it(`case ${JSON.stringify(scene.name)}`, () => { assert.deepStrictEqual(parse(scene.input), scene.nodes); }); } }); describe('parse options', () => { it('should setAttributeMap', () => { const ast = parse(`
`, { setAttributeMap: true, }); const div = tag( '
', 'div', text('
', 0), [ attr(text('same', 5), value('1', '"', index + 1)), attr(text('diff', index + 1), value('2', '"', index + 1)), attr(text('same', index + 1), value('3', '"', index + 1)), ], void 0, null, 0, ); div.attributeMap = { same: div.attributes[2], diff: div.attributes[1], }; expect(ast).toEqual([div]); }); }); describe('void elements', () => { for (const tagName of ['basefont', 'bgsound', 'command', 'frame', 'image', 'keygen']) { it(`parses ${tagName} as a void element`, () => { const input = `<${tagName}>text`; assert.deepStrictEqual(parse(input), [ tag(`<${tagName}>`, tagName, text(`<${tagName}>`, 0), [], void 0, null, 0), text('text', tagName.length + 2), ]); }); } }); function assertTagNode(node: INode): asserts node is ITag { assert.strictEqual(node.type, SyntaxKind.Tag); } function assertTextNode(node: INode): asserts node is IText { assert.strictEqual(node.type, SyntaxKind.Text); } describe('text element modes', () => { const body = 'text'; for (const tagName of [ 'title', 'textarea', 'style', 'xmp', 'iframe', 'noembed', 'noframes', 'script', 'noscript', ]) { it(`parses ${tagName} content as text until its closing tag`, () => { const ast = parse(`<${tagName}>${body}`); assert.strictEqual(ast.length, 1); assertTagNode(ast[0]); assert.strictEqual(ast[0].name, tagName); assert.deepStrictEqual(ast[0].open, text(`<${tagName}>`, 0)); assert.deepStrictEqual(ast[0].close, text(``, tagName.length + body.length + 2)); assert.ok(Array.isArray(ast[0].body)); assert.deepStrictEqual(ast[0].body, [text(body, tagName.length + 2)]); }); } it('parses noscript content as HTML when scripting is disabled', () => { const ast = parse('', { scriptingEnabled: false, }); assert.strictEqual(ast.length, 1); assertTagNode(ast[0]); assert.ok(Array.isArray(ast[0].body)); assert.strictEqual(ast[0].body.length, 1); assertTagNode(ast[0].body[0]); assert.strictEqual(ast[0].body[0].name, 'span'); }); it('ignores a leading LF in textarea content', () => { const ast = parse(''); assert.strictEqual(ast.length, 1); assertTagNode(ast[0]); assert.ok(Array.isArray(ast[0].body)); assert.strictEqual(ast[0].body.length, 1); assertTextNode(ast[0].body[0]); assert.strictEqual(ast[0].body[0].value, 'hello'); assert.strictEqual(ast[0].body[0].start, 11); }); it('parses plaintext content as text until EOF', () => { const body = 'text

after

'; const ast = parse(`${body}`); assert.strictEqual(ast.length, 1); assertTagNode(ast[0]); assert.strictEqual(ast[0].name, 'plaintext'); assert.strictEqual(ast[0].close, null); assert.ok(Array.isArray(ast[0].body)); assert.deepStrictEqual(ast[0].body, [text(body, 11)]); }); });