import test from 'ava'; import { parser } from '../src'; test('should be parse doctype in uppercase', t => { const tree = parser(''); const expected = ['']; t.deepEqual(tree, expected); }); test('should be parse comment', t => { const tree = parser(''); const expected = ['']; t.deepEqual(tree, expected); }); test('should be parse CDATA', t => { const tree = parser('', { xmlMode: true }); const expected = [{ tag: 'script', content: ['console.log(1);'] }]; t.deepEqual(tree, expected); }); test('should be parse tag with escape object in attribute', t => { const html = ''; const tree = parser(html); const expected = [ { tag: 'button', attrs: { type: 'submit', 'data-bem': '{"button":{"checkedView":"extra"}}' } } ]; t.deepEqual(tree, expected); }); test.skip('should be parse tag with object in attribute data witchout escape', t => { const html = ''; const tree = parser(html); const expected = [ { tag: 'button', attrs: { type: 'submit', 'data-bem': '{"button":{"checkedView":"extra"}}' } } ]; t.deepEqual(tree, expected); }); test.skip('should be parse tag with object in attribute data escape', t => { const json = JSON.stringify({ button: { checkedView: 'extra' } }); const html = ''; const tree = parser(html); const expected = [ { tag: 'button', attrs: { type: 'submit', 'data-bem': '{"button":{"checkedView":"extra"}}' } } ]; t.deepEqual(tree, expected); }); test('should be parse isolated comment', t => { const tree = parser('
'); const expected = [{ tag: 'div', content: [''] }]; t.deepEqual(tree, expected); }); test('should be parse comment before text content', t => { const tree = parser('
Text after comment
'); const expected = [{ tag: 'div', content: ['', 'Text after comment'] }]; t.deepEqual(tree, expected); }); test('should be parse comment after text content', t => { const tree = parser('
Text before comment.
'); const expected = [{ tag: 'div', content: ['Text before comment.', ''] }]; t.deepEqual(tree, expected); }); test('should be parse comment in the middle of text content', t => { const tree = parser('
Text surrounding a comment.
'); const expected = [{ tag: 'div', content: ['Text surrounding ', '', ' a comment.'] }]; t.deepEqual(tree, expected); }); test('should be parse doctype', t => { const tree = parser(''); const expected = ['']; t.deepEqual(tree, expected); }); test('should be parse directive', t => { const options = { directives: [ { name: '?php', start: '<', end: '>' } ] }; const tree = parser('', options); const expected = ['']; t.deepEqual(tree, expected); }); test('should be parse regular expression directive', t => { const options = { directives: [ { name: /\?(php|=).*/, start: '<', end: '>' } ] }; const tree1 = parser('', options); const expected1 = ['']; const tree2 = parser('', options); const expected2 = ['']; t.deepEqual(tree1, expected1); t.deepEqual(tree2, expected2); }); test('should be parse directives and tag', t => { const options = { directives: [ { name: '!doctype', start: '<', end: '>' }, { name: '?php', start: '<', end: '>' } ] }; const html = '
{{%njk test %}}'; const tree = parser(html, options); const expected = [ '', { content: [''], tag: 'header' }, { content: ['{{%njk test %}}'], tag: 'body' } ]; t.deepEqual(tree, expected); }); test('should be parse tag', t => { const tree = parser(''); const expected = [{ tag: 'html' }]; t.deepEqual(tree, expected); }); test('should be parse doctype and tag', t => { const tree = parser(''); const expected = ['', { tag: 'html' }]; t.deepEqual(tree, expected); }); test('should be parse tag attrs', t => { const tree = parser('
'); const expected = [{ tag: 'div', attrs: { id: 'id', class: 'class' } }]; t.deepEqual(tree, expected); }); test('should be parse text', t => { const tree = parser('Text'); const expected = ['Text']; t.deepEqual(tree, expected); }); test('should be parse text in content', t => { const tree = parser('
Text
'); const expected = [{ tag: 'div', content: ['Text'] }]; t.deepEqual(tree, expected); }); test('should be parse not a single node in tree', t => { const tree = parser('Text1Text2Text3'); const expected = [ { tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3' ]; t.deepEqual(tree, expected); }); test('should be parse not a single node in parent content', t => { const tree = parser('
Text1Text2Text3
'); const expected = [ { tag: 'div', content: [{ tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3'] } ]; t.deepEqual(tree, expected); }); test('should be parse camelCase tag name', t => { const tree = parser(''); const expected = [ { tag: 'mySuperTag' } ]; t.deepEqual(tree, expected); }); test('should be parse simple contents are split with "<" in comment', t => { const html = ' /* width < 800px */
test
'; const tree = parser(html); const expected = [ { tag: 'a', content: [' /* width < 800px */ ', { tag: 'hr' }, ' test'] } ]; t.deepEqual(tree, expected); }); test('should be parse style contents are split with "<" in comment', t => { const html = ''; const tree = parser(html); const expected = [ { tag: 'style', content: [' /* width < 800px */ @media (max-width: 800px) { /* selectors */} '] } ]; t.deepEqual(tree, expected); }); test('should be parse script contents are split with "<" in comment', t => { const html = ''; const tree = parser(html); const expected = [ { tag: 'script', content: [ ' var str = \'hey { const html = '‌ ©'; const tree = parser(html); const expected = ['‌ ©']; t.deepEqual(tree, expected); }); test('should parse with source locations', t => { const html = '

Test

\n

Foo


Bar\n


'; const tree = parser(html, { sourceLocations: true }); const expected = [ { tag: 'h1', content: ['Test'], location: { start: { line: 1, column: 1 }, end: { line: 1, column: 13 } } }, '\n', { tag: 'p', content: [ { tag: 'b', content: ['Foo'], location: { start: { line: 2, column: 4 }, end: { line: 2, column: 13 } } } ], location: { start: { line: 2, column: 1 }, end: { line: 2, column: 13 } } }, { tag: 'hr', location: { start: { line: 2, column: 14 }, end: { line: 2, column: 17 } } }, { tag: 'p', location: { start: { line: 2, column: 18 }, end: { line: 2, column: 21 } } }, { tag: 'p', content: [ 'Bar\n' ], location: { start: { line: 2, column: 22 }, end: { line: 2, column: 28 } } }, { tag: 'hr', location: { start: { line: 3, column: 1 }, end: { line: 3, column: 4 } } } ]; t.deepEqual(tree, expected); }); test('should parse with input in button', t => { const html = ''; const tree = parser(html, { xmlMode: true }); const expected = [ { tag: 'button', content: [ 'Hello ', { tag: 'input', attrs: { type: 'file', 'ng-hide': 'true' } }, 'PostHtml' ] } ]; t.deepEqual(tree, expected); });