From 1224e4605316026476081fd71bb457c380d0a50f Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Thu, 29 Aug 2024 21:57:31 +0200 Subject: [PATCH 1/6] fix: eslint --- .vscode/settings.json | 9 ++++++++- eslint.config.js | 27 ++++++++++++++++++++++++--- package.json | 25 +++++++++++++------------ src/html.ts | 6 ++++-- src/tidy.ts | 3 ++- src/utils.ts | 8 ++++++-- src/vcss.spec.tsx | 1 - src/vdom.spec.tsx | 1 - src/vdomparser.ts | 13 +++---------- 9 files changed, 60 insertions(+), 33 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index eae23ff..9f9462b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -39,5 +39,12 @@ "jsonc", "yaml", "toml" - ] + ], + + "github.copilot.enable": { + "*": true, + "plaintext": false, + "markdown": false, + "scminput": false + } } diff --git a/eslint.config.js b/eslint.config.js index f3a5206..af464ae 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,5 +1,26 @@ import antfu from '@antfu/eslint-config' +import { eslintIgnoreDefaults, eslintRulesDefaults } from 'zeed' -export default antfu({ - ignores: ['docs', 'dist', '**/dist/**', 'node_modules', '**/node_modules/**', 'build', '**/build/**', 'tmp', '**/tmp/**', 'demos', '**/demos/**', 'coverage', '**/coverage/**', '_archive', '**/_archive/**', '*.spec.*', '**/*.spec.*/**', 'vitest.config.ts', '**/vitest.config.ts/**', '*.md', '**/*.md/**', '*.yml', '**/*.yml/**'], -}) +export default antfu( + { + typescript: true, + vue: true, + + regexp: false, + + ignores: [ + ...eslintIgnoreDefaults(), + 'docs', 'dist', '**/dist/**', 'node_modules', '**/node_modules/**', 'build', '**/build/**', 'tmp', '**/tmp/**', 'demos', '**/demos/**', 'coverage', '**/coverage/**', '_archive', '**/_archive/**', '*.spec.*', '**/*.spec.*/**', 'vitest.config.ts', '**/vitest.config.ts/**', '*.md', '**/*.md/**', '*.yml', '**/*.yml/**', + ], + }, + {}, + { + rules: { + ...eslintRulesDefaults(), + 'regexp/*': 'off', + 'regexp/confusing-quantifier': 'off', + 'antfu/consistent-list-newline': 'off', + 'eslint-disable-unused-imports/no-unused-imports': 'off', + }, + }, +) diff --git a/package.json b/package.json index 7999e27..306fe0d 100644 --- a/package.json +++ b/package.json @@ -69,19 +69,20 @@ }, "dependencies": { "css-what": "^6.1.0", - "entities": "^4.5.0" + "entities": "^5.0.0", + "zeed": "^0.24.13" }, "devDependencies": { - "@antfu/eslint-config": "^2.18.0", - "@antfu/ni": "^0.21.12", - "@types/node": "^20.12.12", - "@vitest/coverage-v8": "^1.6.0", - "c8": "^9.1.0", - "eslint": "^9.2.0", - "tsup": "^8.0.2", - "typedoc": "^0.25.13", - "typescript": "^5.4.5", - "vite": "^5.2.11", - "vitest": "^1.6.0" + "@antfu/eslint-config": "^3.0.0", + "@antfu/ni": "^0.23.0", + "@types/node": "^22.5.1", + "@vitest/coverage-v8": "^2.0.5", + "c8": "^10.1.2", + "eslint": "^9.9.1", + "tsup": "^8.2.4", + "typedoc": "^0.26.6", + "typescript": "^5.5.4", + "vite": "^5.4.2", + "vitest": "^2.0.5" } } diff --git a/src/html.ts b/src/html.ts index 529eba8..1a754df 100644 --- a/src/html.ts +++ b/src/html.ts @@ -124,10 +124,12 @@ export function markup( (c.startsWith('<') && c.endsWith('>')) || tag === 'script' || tag === 'style' - ) + ) { parts.push(c) - else + } + else { parts.push(escapeHTML(c.toString())) + } } } } diff --git a/src/tidy.ts b/src/tidy.ts index fcf1e65..d7aad8b 100644 --- a/src/tidy.ts +++ b/src/tidy.ts @@ -28,8 +28,9 @@ export function tidyDOM(document: VDocument) { !prev || prev.nodeType !== VNode.TEXT_NODE || !prev.nodeValue?.endsWith('\n') - ) + ) { e.parentNode?.insertBefore(new VTextNode('\n'), e) + } e.parentNode?.insertBefore(new VTextNode(level(e)), e) diff --git a/src/utils.ts b/src/utils.ts index 03dba61..56bd8f2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,8 +6,12 @@ export function removeBodyContainer(body: VNodeQuery): VNodeQuery { const ebody = body.querySelector('body') if (ebody || ehead) { const body = new VDocumentFragment() - ehead && body.appendChild(ehead.childNodes) - ebody && body.appendChild(ebody.children) + if (ehead) { + body.appendChild(ehead.childNodes) + } + if (ebody) { + body.appendChild(ebody.children) + } return body } return body diff --git a/src/vcss.spec.tsx b/src/vcss.spec.tsx index 02dec78..35fea74 100644 --- a/src/vcss.spec.tsx +++ b/src/vcss.spec.tsx @@ -1,4 +1,3 @@ - import { matchSelector } from './vcss' import { createHTMLDocument, h } from './vdom' diff --git a/src/vdom.spec.tsx b/src/vdom.spec.tsx index 02991e4..b85946d 100644 --- a/src/vdom.spec.tsx +++ b/src/vdom.spec.tsx @@ -141,7 +141,6 @@ describe('vDOM', () => { }) it('should JSX components', () => { - // eslint-disable-next-line unused-imports/no-unused-vars function Welcome({ props, h }: any) { return (

diff --git a/src/vdomparser.ts b/src/vdomparser.ts index cee3644..081cba6 100644 --- a/src/vdomparser.ts +++ b/src/vdomparser.ts @@ -4,15 +4,7 @@ import { unescapeHTML } from './encoding' import { SELF_CLOSING_TAGS } from './html' import { HtmlParser } from './htmlparser' import { hasOwn } from './utils' -import { - VDocType, - VDocumentFragment, - VElement, - VHTMLDocument, - VNode, - VTextNode, - document, -} from './vdom' +import { VDocType, VDocumentFragment, VElement, VHTMLDocument, VNode, VTextNode, document } from './vdom' // Makes sure we operate on VNodes export function vdom(obj: VNode | Buffer | string | null = null): VNode { @@ -71,8 +63,9 @@ export function parseHTML(html: string): VDocumentFragment | VHTMLDocument { !( SELF_CLOSING_TAGS.includes(tagName.toLowerCase()) || isSelfClosing ) - ) + ) { stack.push(element) + } } }, endElement(_tagName: string) { From d7f7489546eb75c1d4f040ceff7b5964791299f9 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Thu, 29 Aug 2024 22:13:06 +0200 Subject: [PATCH 2/6] fix: be more permissive with script and style. fixes #13 --- src/htmlparser.spec.ts | 36 ++++++++++++++++++++---------------- src/htmlparser.ts | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/htmlparser.spec.ts b/src/htmlparser.spec.ts index 39310fb..e58e1c9 100644 --- a/src/htmlparser.spec.ts +++ b/src/htmlparser.spec.ts @@ -47,10 +47,11 @@ describe('htmlparser', () => { const html = `` const dom = parseHTML(html) as VHTMLDocument @@ -58,10 +59,11 @@ $('body') " var x = 1 & 4 window.addEventListener('load', function () { - \$('body') - .attr('data-spy', 'scroll') - .attr('data-offset', '88') - .attr('data-target', '#outline') + if (x<1) + $('body') + .attr('data-spy', 'scroll') + .attr('data-offset', '88') + .attr('data-target', '#outline') }) " `) @@ -77,10 +79,11 @@ $('body') "_text": " var x = 1 & 4 window.addEventListener('load', function () { - $('body') - .attr('data-spy', 'scroll') - .attr('data-offset', '88') - .attr('data-target', '#outline') + if (x<1) + $('body') + .attr('data-spy', 'scroll') + .attr('data-offset', '88') + .attr('data-target', '#outline') }) ", "append": [Function], @@ -101,10 +104,11 @@ $('body') "" `) diff --git a/src/htmlparser.ts b/src/htmlparser.ts index b6ab7f1..8a1f027 100644 --- a/src/htmlparser.ts +++ b/src/htmlparser.ts @@ -54,6 +54,7 @@ export class HtmlParser { treatAsChars = true } } + // end tag else if (html.substring(0, 2) === ' Date: Thu, 29 Aug 2024 22:20:33 +0200 Subject: [PATCH 3/6] fix: Incorrect and slow parsing of base64 encoded CSS property values fixes #12 --- src/htmlparser.spec.ts | 21 +++++++++++++++++++++ src/vdom.ts | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/htmlparser.spec.ts b/src/htmlparser.spec.ts index e58e1c9..05decba 100644 --- a/src/htmlparser.spec.ts +++ b/src/htmlparser.spec.ts @@ -43,6 +43,27 @@ describe('htmlparser', () => { expect(dom.render()).toMatchInlineSnapshot(`"

Let's go

"`) }) + it('should handle inline base64', () => { + // https://github.com/holtwick/zeed-dom/issues/12 + + const dom = parseHTML(`
Test
`) as VHTMLDocument + const node = dom.querySelector('#elem') + expect(node).not.toBeNull() + expect(node?.style).toMatchInlineSnapshot(` + { + "background-image": "url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAyNy41LjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCAzMTQ3IDIwMDAiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDMxNDcgMjAwMDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPGc)", + "backgroundImage": "url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAyNy41LjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCAzMTQ3IDIwMDAiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDMxNDcgMjAwMDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPGc)", + "height": "100px", + "width": "200px", + } + `) + }) + it('should ignore escape for script etc.', () => { const html = `