Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/fuzz/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="data:" />
<title>dhtml fuzzer</title>
<script type="importmap">
{
"imports": {
"dhtml": "./node_modules/dhtml/index.js",
"dhtml/client": "./node_modules/dhtml/client.js",
"dhtml/server": "./node_modules/dhtml/server.js"
}
}
</script>
<script type="module" src="main.js"></script>
</head>
<body></body>
</html>
187 changes: 187 additions & 0 deletions examples/fuzz/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { html } from 'dhtml'
import { createRoot } from 'dhtml/client'
import { renderToString } from 'dhtml/server'

function canonicalize(str) {
const t = document.createElement('template')
t.innerHTML = str
return t.innerHTML
}

/**
* @param {...string} strings
* @returns {TemplateStringsArray}
*/
function tsa(...strings) {
return Object.assign(strings, { raw: strings })
}

function tag(name) {
const child = random()

return {
render() {
return html(tsa(`<${name}>`, `</${name}>`), child)
},
toString() {
return `<${name}>${child}</${name}>`
},
}
}
function voidTag(name) {
return {
render() {
return html(tsa(`<${name}>`))
},
toString() {
return `<${name}>`
},
}
}
function text(raw) {
return {
render() {
return raw
},
toString() {
return raw
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#39;')
},
}
}
function sequence(length) {
const items = Array.from({ length }, () => random())
return {
render() {
return items
},
toString() {
return items.join('')
},
}
}

const choices = [
() => tag('a'),
() => tag('p'),
() => tag('span'),
() => tag('div'),
() => tag('table'),
() => tag('tbody'),
() => tag('tr'),
() => tag('td'),
() => tag('form'),
() => tag('button'),
() => tag('input'),
() => voidTag('br'),
() => tag('h1'),
() => tag('h2'),
() => tag('h3'),
() => tag('ul'),
() => tag('ol'),
() => tag('li'),
() => tag('section'),
() => tag('article'),
() => tag('header'),
() => tag('footer'),
() => tag('nav'),
() => tag('main'),
() => tag('aside'),
() => tag('strong'),
() => tag('em'),
() => tag('code'),
() => tag('pre'),
() => tag('blockquote'),
() => tag('label'),
() => tag('select'),
() => tag('option'),
// () => tag('textarea'), // causes issues
() => tag('fieldset'),
() => tag('legend'),
() => tag('figure'),
() => tag('figcaption'),
() => tag('caption'),
() => tag('thead'),
() => tag('tfoot'),
() => tag('th'),
() => tag('small'),
() => tag('b'),
() => tag('i'),
() => tag('u'),
() => tag('sub'),
() => tag('sup'),
() => tag('mark'),
() => tag('del'),
() => tag('ins'),
() => tag('abbr'),
() => tag('cite'),
() => tag('q'),
() => tag('dfn'),
() => tag('time'),
() => tag('address'),
() => tag('details'),
() => tag('summary'),
() => tag('dialog'),
() => tag('data'),
() => tag('output'),
() => tag('progress'),
() => tag('meter'),
() => voidTag('hr'),
() => voidTag('img'),
() => voidTag('area'),
() => voidTag('base'),
() => voidTag('col'),
() => voidTag('embed'),
() => voidTag('link'),
() => voidTag('meta'),
() => voidTag('param'),
() => voidTag('source'),
() => voidTag('track'),
() => voidTag('wbr'),
() => text('text'),
() => text(''),
() => text('<hello>'),
() => sequence(2),
() => sequence(3),
() => sequence(4),
() => sequence(5),
() => ({
render: () => null,
toString: () => '',
}),
]
function random() {
return choices[Math.floor(Math.random() * choices.length)]()
}

for (let i = 0; i < 10000; i++) {
const app = random()

const el = document.createElement('div')
const root = createRoot(el)
const str = app.toString()

try {
root.render(app)
} catch (error) {
console.warn(str, app)
throw error
}

const str2 = renderToString(app)

if (str !== str2) {
console.log('ssr mismatch, expected:', str, 'got:', str2)
}

if (canonicalize(str) !== str) continue

if (el.innerHTML !== str) {
console.log('rendering mismatch, expected:', str, 'got:', el.innerHTML)
}
}
console.log('done')
14 changes: 14 additions & 0 deletions examples/fuzz/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@dhtml-examples/fuzz",
"private": true,
"type": "module",
"scripts": {
"check": "tsc"
},
"devDependencies": {
"typescript": "~5.8.3"
},
"dependencies": {
"dhtml": "file:../../dist"
}
}
11 changes: 11 additions & 0 deletions examples/fuzz/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleResolution": "bundler",
"module": "preserve",
"target": "es2020"
}
}
Loading