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

Test

\n

Foo


Bar\n


'; +test("should parse with source locations", () => { + const html = "

Test

\n

Foo


Bar\n


"; const tree = parser(html, { sourceLocations: true }); const expected = [ { - tag: 'h1', - content: ['Test'], + tag: "h1", + content: ["Test"], location: { start: { line: 1, @@ -286,13 +263,13 @@ test('should parse with source locations', () => { }, }, }, - '\n', + "\n", { - tag: 'p', + tag: "p", content: [ { - tag: 'b', - content: ['Foo'], + tag: "b", + content: ["Foo"], location: { start: { line: 2, @@ -317,7 +294,7 @@ test('should parse with source locations', () => { }, }, { - tag: 'hr', + tag: "hr", location: { start: { line: 2, @@ -330,7 +307,7 @@ test('should parse with source locations', () => { }, }, { - tag: 'p', + tag: "p", location: { start: { line: 2, @@ -343,8 +320,8 @@ test('should parse with source locations', () => { }, }, { - tag: 'p', - content: ['Bar\n'], + tag: "p", + content: ["Bar\n"], location: { start: { line: 2, @@ -357,7 +334,7 @@ test('should parse with source locations', () => { }, }, { - tag: 'hr', + tag: "hr", location: { start: { line: 3, @@ -373,50 +350,49 @@ test('should parse with source locations', () => { expect(tree).eql(expected); }); -test('should parse with input in button', () => { - const html = - ''; +test("should parse with input in button", () => { + const html = ''; const tree = parser(html, { xmlMode: true }); const expected = [ { - tag: 'button', + tag: "button", content: [ - 'Hello ', + "Hello ", { - tag: 'input', + tag: "input", attrs: { - type: 'file', - 'ng-hide': 'true', + type: "file", + "ng-hide": "true", }, }, - 'PostHtml', + "PostHtml", ], }, ]; expect(tree).eql(expected); }); -test('should parse no value attribute as `true` when `recognizeNoValueAttribute` is `true` ', () => { +test("should parse no value attribute as `true` when `recognizeNoValueAttribute` is `true` ", () => { const tree = parser('
Content
', { recognizeNoValueAttribute: true, }); const expected = [ { - tag: 'div', - attrs: { class: 'className', hasClass: true }, - content: ['Content'], + tag: "div", + attrs: { class: "className", hasClass: true }, + content: ["Content"], }, ]; expect(tree).eql(expected); }); -test('should parse no value attribute as empty string when `recognizeNoValueAttribute` is `false` or not set ', () => { +test("should parse no value attribute as empty string when `recognizeNoValueAttribute` is `false` or not set ", () => { const tree = parser('
Content
'); const expected = [ { - tag: 'div', - attrs: { class: 'className', hasClass: '' }, - content: ['Content'], + tag: "div", + attrs: { class: "className", hasClass: "" }, + content: ["Content"], }, ]; expect(tree).eql(expected);