diff --git a/.editorconfig b/.editorconfig index c6c8b36..0f17867 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,9 +1,9 @@ root = true [*] -indent_style = space -indent_size = 2 -end_of_line = lf charset = utf-8 -trim_trailing_whitespace = true +end_of_line = lf +indent_size = 2 +indent_style = space insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.github/workflows/bb.yml b/.github/workflows/bb.yml index 0198fc3..3dbfce5 100644 --- a/.github/workflows/bb.yml +++ b/.github/workflows/bb.yml @@ -1,9 +1,3 @@ -name: bb -on: - issues: - types: [opened, reopened, edited, closed, labeled, unlabeled] - pull_request_target: - types: [opened, reopened, edited, closed, labeled, unlabeled] jobs: main: runs-on: ubuntu-latest @@ -11,3 +5,9 @@ jobs: - uses: unifiedjs/beep-boop-beta@main with: repo-token: ${{secrets.GITHUB_TOKEN}} +name: bb +on: + issues: + types: [closed, edited, labeled, opened, reopened, unlabeled] + pull_request_target: + types: [closed, edited, labeled, opened, reopened, unlabeled] diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2ce794f..ade3921 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ jobs: node-version: ${{matrix.node}} - run: npm install - run: npm test - - uses: codecov/codecov-action@v4 + - uses: codecov/codecov-action@v5 strategy: matrix: node: diff --git a/.gitignore b/.gitignore index b5761b8..388388c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ -coverage/ -node_modules/ -*.d.ts.map *.d.ts *.log +*.map *.tsbuildinfo .DS_Store +coverage/ +node_modules/ yarn.lock diff --git a/lib/handle/element.js b/lib/handle/element.js index 17b4713..17d9470 100644 --- a/lib/handle/element.js +++ b/lib/handle/element.js @@ -173,8 +173,8 @@ function serializeAttribute(state, key, value) { if (info.overloadedBoolean && (value === info.attribute || value === '')) { value = true } else if ( - info.boolean || - (info.overloadedBoolean && typeof value !== 'string') + (info.boolean || info.overloadedBoolean) && + (typeof value !== 'string' || value === info.attribute || value === '') ) { value = Boolean(value) } diff --git a/license b/license index 8d8660d..bc8f165 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ (The MIT License) -Copyright (c) 2016 Titus Wormer +Copyright (c) Titus Wormer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/package.json b/package.json index c96fc04..d9e427b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hast-util-to-html", - "version": "9.0.3", + "version": "9.0.4", "description": "hast utility to serialize to HTML", "license": "MIT", "keywords": [ @@ -55,8 +55,7 @@ "remark-preset-wooorm": "^10.0.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", - "unist-builder": "^4.0.0", - "xo": "^0.59.0" + "xo": "^0.60.0" }, "scripts": { "prepack": "npm run build && npm run format", diff --git a/test/attribute.js b/test/attribute.js index 99e2a5e..bdf6366 100644 --- a/test/attribute.js +++ b/test/attribute.js @@ -2,20 +2,29 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`element` attributes', async (t) => { await t.test('should support unknown properties', async function (t) { await t.test('should ignore unknowns set to `false`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {unknown: false}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: false}, + children: [] + }), '' ) }) await t.test('should ignore unknowns set to `null`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {unknown: null}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: null}, + children: [] + }), '' ) }) @@ -24,9 +33,12 @@ test('`element` attributes', async (t) => { 'should ignore unknowns set to `undefined`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {unknown: undefined}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: undefined}, + children: [] + }), '' ) } @@ -34,9 +46,12 @@ test('`element` attributes', async (t) => { await t.test('should ignore unknowns set to `NaN`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {unknown: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: Number.NaN}, + children: [] + }), '' ) }) @@ -45,7 +60,12 @@ test('`element` attributes', async (t) => { 'should serialize unknowns set to `true` without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {unknown: true}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: true}, + children: [] + }), '' ) } @@ -55,9 +75,12 @@ test('`element` attributes', async (t) => { 'should serialize unknowns set to their name as their name', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {unknown: 'unknown'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: 'unknown'}, + children: [] + }), '' ) } @@ -67,9 +90,12 @@ test('`element` attributes', async (t) => { 'should serialize unknown lists as space-separated', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {unknown: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: ['a', 'b']}, + children: [] + }), '' ) } @@ -79,7 +105,12 @@ test('`element` attributes', async (t) => { 'should serialize unknowns set to an integer as it’s string version', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {unknown: 1}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: 1}, + children: [] + }), '' ) } @@ -87,17 +118,25 @@ test('`element` attributes', async (t) => { await t.test('should serialize unknowns set to `0`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {unknown: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {unknown: 0}, + children: [] + }), '' ) }) await t.test('should serialize unknowns set to objects', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u('element', {tagName: 'i', properties: {unknown: {toString}}}, []) - ), + properties: {unknown: {toString}}, + children: [] + }), '' ) }) @@ -108,7 +147,12 @@ test('`element` attributes', async (t) => { 'should ignore known booleans set to `false`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {hidden: false}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: false}, + children: [] + }), '' ) } @@ -116,16 +160,24 @@ test('`element` attributes', async (t) => { await t.test('should ignore falsey known booleans', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {hidden: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: 0}, + children: [] + }), '' ) }) await t.test('should ignore NaN known booleans', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {hidden: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: Number.NaN}, + children: [] + }), '' ) }) @@ -134,7 +186,12 @@ test('`element` attributes', async (t) => { 'should serialize known booleans set to `true` without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {hidden: true}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: true}, + children: [] + }), '' ) } @@ -144,9 +201,12 @@ test('`element` attributes', async (t) => { 'should serialize known booleans set to their name without value', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {hidden: 'hidden'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: 'hidden'}, + children: [] + }), '' ) } @@ -156,11 +216,46 @@ test('`element` attributes', async (t) => { 'should serialize truthy known booleans without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {hidden: 1}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {hidden: 1}, + children: [] + }), '' ) } ) + + await t.test( + 'should serialize known booleans set to arbitrary strings with value', + async function () { + assert.deepEqual( + toHtml(h('div', {selected: 'something'})), + '
' + ) + } + ) + + await t.test( + 'should serialize known booleans set to an empty string without value', + async function () { + assert.deepEqual( + toHtml(h('div', {selected: ''})), + '
' + ) + } + ) + + await t.test( + 'should serialize known overloaded booleans set to arbitrary strings with value', + async function () { + assert.deepEqual( + toHtml(h('div', {download: 'something'})), + '
' + ) + } + ) }) await t.test('should support known overloaded booleans', async function (t) { @@ -168,9 +263,12 @@ test('`element` attributes', async (t) => { 'should ignore known overloaded booleans set to `false`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {download: false}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: false}, + children: [] + }), '' ) } @@ -180,7 +278,12 @@ test('`element` attributes', async (t) => { 'should ignore falsey known overloaded booleans', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'a', properties: {download: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: 0}, + children: [] + }), '' ) } @@ -190,9 +293,12 @@ test('`element` attributes', async (t) => { 'should ignore NaN known overloaded booleans', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {download: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: Number.NaN}, + children: [] + }), '' ) } @@ -202,9 +308,12 @@ test('`element` attributes', async (t) => { 'should serialize known overloaded booleans set to `true` without value', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {download: true}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: true}, + children: [] + }), '' ) } @@ -214,9 +323,12 @@ test('`element` attributes', async (t) => { 'should serialize known overloaded booleans set to their name without value', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {download: 'download'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: 'download'}, + children: [] + }), '' ) } @@ -226,7 +338,12 @@ test('`element` attributes', async (t) => { 'should serialize known overloaded booleans set to an empty string without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'a', properties: {download: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: ''}, + children: [] + }), '' ) } @@ -236,7 +353,12 @@ test('`element` attributes', async (t) => { 'should serialize truthy known overloaded booleans without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'a', properties: {download: 1}}, [])), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: 1}, + children: [] + }), '' ) } @@ -246,9 +368,12 @@ test('`element` attributes', async (t) => { 'should serialize known overloaded booleans set to another string', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {download: 'another'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {download: 'another'}, + children: [] + }), '' ) } @@ -259,7 +384,12 @@ test('`element` attributes', async (t) => { 'should ignore known numbers set to `false`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: false}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: false}, + children: [] + }), '' ) } @@ -267,9 +397,12 @@ test('`element` attributes', async (t) => { await t.test('should ignore NaN known numbers', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {cols: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {cols: Number.NaN}, + children: [] + }), '' ) }) @@ -278,7 +411,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to `0`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: 0}, + children: [] + }), '' ) } @@ -288,7 +426,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to `-1`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: -1}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: -1}, + children: [] + }), '' ) } @@ -298,7 +441,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to `1`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: 1}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: 1}, + children: [] + }), '' ) } @@ -308,7 +456,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to `Math.PI`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: Math.PI}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: Math.PI}, + children: [] + }), '' ) } @@ -318,7 +471,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to `true` as without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: true}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: true}, + children: [] + }), '' ) } @@ -328,7 +486,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to an empty string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: ''}, + children: [] + }), '' ) } @@ -338,7 +501,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to their name', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: 'cols'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: 'cols'}, + children: [] + }), '' ) } @@ -348,9 +516,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to a string', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {cols: 'another'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: 'another'}, + children: [] + }), '' ) } @@ -360,10 +531,13 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to an object', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u('element', {tagName: 'i', properties: {cols: {toString}}}, []) - ), + properties: {cols: {toString}}, + children: [] + }), '' ) } @@ -373,9 +547,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to an array of strings', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {cols: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: ['a', 'b']}, + children: [] + }), '' ) } @@ -385,7 +562,12 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to an array of numbers', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {cols: [0, 50]}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {cols: [0, 50]}, + children: [] + }), '' ) } @@ -395,10 +577,13 @@ test('`element` attributes', async (t) => { 'should serialize known numbers set to an array of booleans', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles booleans in an array. - u('element', {tagName: 'i', properties: {cols: [true, false]}}, []) - ), + properties: {cols: [true, false]}, + children: [] + }), '' ) } @@ -412,9 +597,12 @@ test('`element` attributes', async (t) => { 'should ignore known space-separated lists set to `false`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: false}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: false}, + children: [] + }), '' ) } @@ -424,13 +612,12 @@ test('`element` attributes', async (t) => { 'should ignore NaN known space-separated lists', async function () { assert.deepEqual( - toHtml( - u( - 'element', - {tagName: 'a', properties: {className: Number.NaN}}, - [] - ) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {className: Number.NaN}, + children: [] + }), '' ) } @@ -440,9 +627,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to `0`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: 0}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: 0}, + children: [] + }), '' ) } @@ -452,9 +642,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to `true` as without value', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: true}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: true}, + children: [] + }), '' ) } @@ -464,9 +657,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to an empty string', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: ''}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: ''}, + children: [] + }), '' ) } @@ -476,9 +672,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to their attribute name', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: 'class'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: 'class'}, + children: [] + }), '' ) } @@ -488,13 +687,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to their property name', async function () { assert.deepEqual( - toHtml( - u( - 'element', - {tagName: 'i', properties: {className: 'className'}}, - [] - ) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: 'className'}, + children: [] + }), '' ) } @@ -504,13 +702,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to a string', async function () { assert.deepEqual( - toHtml( - u( - 'element', - {tagName: 'i', properties: {className: 'another'}}, - [] - ) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: 'another'}, + children: [] + }), '' ) } @@ -520,14 +717,13 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to an object', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u( - 'element', - {tagName: 'i', properties: {className: {toString}}}, - [] - ) - ), + properties: {className: {toString}}, + children: [] + }), '' ) } @@ -537,13 +733,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to an array of strings', async function () { assert.deepEqual( - toHtml( - u( - 'element', - {tagName: 'i', properties: {className: ['a', 'b']}}, - [] - ) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: ['a', 'b']}, + children: [] + }), '' ) } @@ -553,9 +748,12 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to an array of numbers', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {className: [0, 50]}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {className: [0, 50]}, + children: [] + }), '' ) } @@ -565,14 +763,13 @@ test('`element` attributes', async (t) => { 'should serialize known space-separated lists set to an array of booleans', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles booleans in an array. - u( - 'element', - {tagName: 'i', properties: {className: [true, false]}}, - [] - ) - ), + properties: {className: [true, false]}, + children: [] + }), '' ) } @@ -587,9 +784,12 @@ test('`element` attributes', async (t) => { 'should ignore known comma-separated lists set to `false`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: false}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: false}, + children: [] + }), '' ) } @@ -599,9 +799,12 @@ test('`element` attributes', async (t) => { 'should ignore NaN known comma-separated lists', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'a', properties: {accept: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'a', + properties: {accept: Number.NaN}, + children: [] + }), '' ) } @@ -611,7 +814,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to `0`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {accept: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: 0}, + children: [] + }), '' ) } @@ -621,9 +829,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to `true` as without value', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: true}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: true}, + children: [] + }), '' ) } @@ -633,7 +844,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to an empty string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {accept: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: ''}, + children: [] + }), '' ) } @@ -643,9 +859,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to their name', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: 'accept'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: 'accept'}, + children: [] + }), '' ) } @@ -655,9 +874,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to a string', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: 'another'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: 'another'}, + children: [] + }), '' ) } @@ -667,10 +889,13 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to an object', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u('element', {tagName: 'i', properties: {accept: {toString}}}, []) - ), + properties: {accept: {toString}}, + children: [] + }), '' ) } @@ -680,9 +905,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to an array of strings', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: ['a', 'b']}, + children: [] + }), '' ) } @@ -692,9 +920,12 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to an array of numbers', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: [0, 50]}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: [0, 50]}, + children: [] + }), '' ) } @@ -704,14 +935,13 @@ test('`element` attributes', async (t) => { 'should serialize known comma-separated lists set to an array of booleans', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles booleans in an array. - u( - 'element', - {tagName: 'i', properties: {accept: [true, false]}}, - [] - ) - ), + properties: {accept: [true, false]}, + children: [] + }), '' ) } @@ -724,7 +954,12 @@ test('`element` attributes', async (t) => { 'should ignore known normals set to `false`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: false}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: false}, + children: [] + }), '' ) } @@ -732,7 +967,12 @@ test('`element` attributes', async (t) => { await t.test('should ignore NaN known normals', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: Number.NaN}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: Number.NaN}, + children: [] + }), '' ) }) @@ -741,7 +981,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to `0`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: 0}, + children: [] + }), '' ) } @@ -751,7 +996,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to `true` as without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: true}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: true}, + children: [] + }), '' ) } @@ -761,7 +1011,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to an empty string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: ''}, + children: [] + }), '' ) } @@ -771,7 +1026,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to their name', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: 'id'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: 'id'}, + children: [] + }), '' ) } @@ -781,7 +1041,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to a string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: 'another'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: 'another'}, + children: [] + }), '' ) } @@ -791,10 +1056,13 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to an object', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u('element', {tagName: 'i', properties: {id: {toString}}}, []) - ), + properties: {id: {toString}}, + children: [] + }), '' ) } @@ -804,9 +1072,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to an array of strings as a space-separated list', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {id: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: ['a', 'b']}, + children: [] + }), '' ) } @@ -816,7 +1087,12 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to an array of numbers as a space-separated list', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: [0, 50]}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: [0, 50]}, + children: [] + }), '' ) } @@ -826,10 +1102,13 @@ test('`element` attributes', async (t) => { 'should serialize known normals set to an array of booleans as a space-separated list', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles booleans in an array. - u('element', {tagName: 'i', properties: {id: [true, false]}}, []) - ), + properties: {id: [true, false]}, + children: [] + }), '' ) } @@ -841,7 +1120,12 @@ test('`element` attributes', async (t) => { 'should ignore data properties set to `false`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {dataId: false}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: false}, + children: [] + }), '' ) } @@ -849,9 +1133,12 @@ test('`element` attributes', async (t) => { await t.test('should ignore NaN data properties', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: Number.NaN}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: Number.NaN}, + children: [] + }), '' ) }) @@ -860,7 +1147,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to `0`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {dataId: 0}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: 0}, + children: [] + }), '' ) } @@ -870,7 +1162,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to `true` as without value', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {dataId: true}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: true}, + children: [] + }), '' ) } @@ -880,7 +1177,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to an empty string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {dataId: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: ''}, + children: [] + }), '' ) } @@ -890,9 +1192,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to their property name', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: 'dataId'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: 'dataId'}, + children: [] + }), '' ) } @@ -902,9 +1207,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to their attribute name', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: 'data-id'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: 'data-id'}, + children: [] + }), '' ) } @@ -914,9 +1222,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to a string', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: 'another'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: 'another'}, + children: [] + }), '' ) } @@ -926,7 +1237,12 @@ test('`element` attributes', async (t) => { 'should serialize numeric-first data properties set to a string', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {data123: 'a'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {data123: 'a'}, + children: [] + }), '' ) } @@ -936,10 +1252,13 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to an object', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles a `toString` method on an object. - u('element', {tagName: 'i', properties: {dataId: {toString}}}, []) - ), + properties: {dataId: {toString}}, + children: [] + }), '' ) } @@ -949,9 +1268,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to an array of strings as a space-separated list', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: ['a', 'b']}, + children: [] + }), '' ) } @@ -961,9 +1283,12 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to an array of numbers as a space-separated list', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {dataId: [0, 50]}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {dataId: [0, 50]}, + children: [] + }), '' ) } @@ -973,14 +1298,13 @@ test('`element` attributes', async (t) => { 'should serialize data properties set to an array of booleans as a space-separated list', async function () { assert.deepEqual( - toHtml( + toHtml({ + type: 'element', + tagName: 'i', // @ts-expect-error: check how the runtime handles booleans in an array. - u( - 'element', - {tagName: 'i', properties: {dataId: [true, false]}}, - [] - ) - ), + properties: {dataId: [true, false]}, + children: [] + }), '' ) } @@ -990,7 +1314,12 @@ test('`element` attributes', async (t) => { await t.test('should support `collapseEmptyAttributes`', async function (t) { await t.test('should show empty string attributes', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: ''}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {id: ''}, + children: [] + }), '' ) }) @@ -999,9 +1328,12 @@ test('`element` attributes', async (t) => { 'should collapse empty string attributes in `collapseEmptyAttributes` mode', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: ''}}, []), { - collapseEmptyAttributes: true - }), + toHtml( + {type: 'element', tagName: 'i', properties: {id: ''}, children: []}, + { + collapseEmptyAttributes: true + } + ), '' ) } @@ -1011,9 +1343,12 @@ test('`element` attributes', async (t) => { await t.test('should support `tightAttributes`', async function (t) { await t.test('should serialize multiple properties', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {title: 'a', id: 'b'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {title: 'a', id: 'b'}, + children: [] + }), '' ) }) @@ -1023,7 +1358,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: 'a', id: 'b'}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: 'a', id: 'b'}, + children: [] + }, { tightAttributes: true } @@ -1039,9 +1379,12 @@ test('`element` attributes', async (t) => { 'should serialize comma-separated attributes', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {accept: ['a', 'b']}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {accept: ['a', 'b']}, + children: [] + }), '' ) } @@ -1052,7 +1395,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {accept: ['a', 'b']}}, []), + { + type: 'element', + tagName: 'i', + properties: {accept: ['a', 'b']}, + children: [] + }, { tightCommaSeparatedLists: true } @@ -1068,7 +1416,12 @@ test('`element` attributes', async (t) => { 'should quote attribute values with double quotes by default', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: 'a'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {title: 'a'}, + children: [] + }), '' ) } @@ -1078,9 +1431,17 @@ test('`element` attributes', async (t) => { "should quote attribute values with single quotes if `quote: '\\''`", async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: 'a'}}, []), { - quote: "'" - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: 'a'}, + children: [] + }, + { + quote: "'" + } + ), "" ) } @@ -1090,9 +1451,17 @@ test('`element` attributes', async (t) => { "should quote attribute values with double quotes if `quote: '\\\"'`", async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: 'a'}}, []), { - quote: '"' - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: 'a'}, + children: [] + }, + { + quote: '"' + } + ), '' ) } @@ -1102,9 +1471,17 @@ test('`element` attributes', async (t) => { "should quote attribute values with single quotes if `quote: '\\''` even if they occur in value", async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: "'a'"}}, []), { - quote: "'" - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: "'a'"}, + children: [] + }, + { + quote: "'" + } + ), "" ) } @@ -1114,9 +1491,17 @@ test('`element` attributes', async (t) => { "should quote attribute values with double quotes if `quote: '\\\"'` even if they occur in value", async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: '"a"'}}, []), { - quote: '"' - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: '"a"'}, + children: [] + }, + { + quote: '"' + } + ), '' ) } @@ -1138,10 +1523,18 @@ test('`element` attributes', async (t) => { 'should quote attribute values with primary quotes by default', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: 'a'}}, []), { - allowDangerousCharacters: true, - quoteSmart: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: 'a'}, + children: [] + }, + { + allowDangerousCharacters: true, + quoteSmart: true + } + ), '' ) } @@ -1151,10 +1544,18 @@ test('`element` attributes', async (t) => { 'should quote attribute values with primary quotes if the alternative occurs', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: "'a'"}}, []), { - allowDangerousCharacters: true, - quoteSmart: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: "'a'"}, + children: [] + }, + { + allowDangerousCharacters: true, + quoteSmart: true + } + ), '' ) } @@ -1165,7 +1566,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: "'\"a'"}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: "'\"a'"}, + children: [] + }, { allowDangerousCharacters: true, quoteSmart: true @@ -1181,7 +1587,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: '"a\''}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: '"a\''}, + children: [] + }, { allowDangerousCharacters: true, quoteSmart: true @@ -1197,7 +1608,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: '"\'a\'"'}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: '"\'a\'"'}, + children: [] + }, { allowDangerousCharacters: true, quoteSmart: true @@ -1212,10 +1628,18 @@ test('`element` attributes', async (t) => { 'should quote attribute values with alternative quotes if the primary occurs', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: '"a"'}}, []), { - allowDangerousCharacters: true, - quoteSmart: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: '"a"'}, + children: [] + }, + { + allowDangerousCharacters: true, + quoteSmart: true + } + ), '' ) } @@ -1226,7 +1650,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: '"\'a"'}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: '"\'a"'}, + children: [] + }, { allowDangerousCharacters: true, quoteSmart: true @@ -1241,9 +1670,12 @@ test('`element` attributes', async (t) => { await t.test('should support `preferUnquoted`', async function (t) { await t.test('should omit quotes in `preferUnquoted`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: 'a'}}, []), { - preferUnquoted: true - }), + toHtml( + {type: 'element', tagName: 'i', properties: {id: 'a'}, children: []}, + { + preferUnquoted: true + } + ), '' ) }) @@ -1252,9 +1684,17 @@ test('`element` attributes', async (t) => { 'should keep quotes in `preferUnquoted` and impossible', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: 'a b'}}, []), { - preferUnquoted: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {id: 'a b'}, + children: [] + }, + { + preferUnquoted: true + } + ), '' ) } @@ -1264,9 +1704,12 @@ test('`element` attributes', async (t) => { 'should not add `=` when omitting quotes on empty values', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {id: ''}}, []), { - preferUnquoted: true - }), + toHtml( + {type: 'element', tagName: 'i', properties: {id: ''}, children: []}, + { + preferUnquoted: true + } + ), '' ) } @@ -1278,7 +1721,12 @@ test('`element` attributes', async (t) => { 'should encode entities in attribute names', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {'3<5\0': 'a'}}, [])), + toHtml({ + type: 'element', + tagName: 'i', + properties: {'3<5\0': 'a'}, + children: [] + }), '' ) } @@ -1288,9 +1736,12 @@ test('`element` attributes', async (t) => { 'should encode entities in attribute values', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'i', properties: {title: '3<5\0'}}, []) - ), + toHtml({ + type: 'element', + tagName: 'i', + properties: {title: '3<5\0'}, + children: [] + }), '' ) } @@ -1300,9 +1751,17 @@ test('`element` attributes', async (t) => { 'should not encode characters in attribute names which cause parse errors, but work, in `allowParseErrors` mode', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {'3=5\0': 'a'}}, []), { - allowParseErrors: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {'3=5\0': 'a'}, + children: [] + }, + { + allowParseErrors: true + } + ), '' ) } @@ -1313,7 +1772,12 @@ test('`element` attributes', async (t) => { async function () { assert.deepEqual( toHtml( - u('element', {tagName: 'i', properties: {title: '3=5\0'}}, []), + { + type: 'element', + tagName: 'i', + properties: {title: '3=5\0'}, + children: [] + }, { allowParseErrors: true } @@ -1327,9 +1791,17 @@ test('`element` attributes', async (t) => { 'should not encode characters which cause XSS issues in older browsers, in `allowDangerousCharacters` mode', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'i', properties: {title: "3'5"}}, []), { - allowDangerousCharacters: true - }), + toHtml( + { + type: 'element', + tagName: 'i', + properties: {title: "3'5"}, + children: [] + }, + { + allowDangerousCharacters: true + } + ), '' ) } diff --git a/test/comment.js b/test/comment.js index eb0004f..ffe7244 100644 --- a/test/comment.js +++ b/test/comment.js @@ -1,22 +1,21 @@ import assert from 'node:assert/strict' import test from 'node:test' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`comment`', async function (t) { await t.test('should serialize `comment`s', async function () { - assert.deepEqual(toHtml(u('comment', 'alpha')), '') + assert.deepEqual(toHtml({type: 'comment', value: 'alpha'}), '') }) await t.test('should not encode `comment`s', async function () { - assert.deepEqual(toHtml(u('comment', 'AT&T')), '') + assert.deepEqual(toHtml({type: 'comment', value: 'AT&T'}), '') }) await t.test( 'should serialize bogus comments (`bogusComments`)', async function () { assert.deepEqual( - toHtml(u('comment', 'asd'), {bogusComments: true}), + toHtml({type: 'comment', value: 'asd'}, {bogusComments: true}), '' ) } @@ -26,7 +25,7 @@ test('`comment`', async function (t) { 'should prevent breaking out of bogus comments (`bogusComments`)', async function () { assert.deepEqual( - toHtml(u('comment', 'ad'), {bogusComments: true}), + toHtml({type: 'comment', value: 'ad'}, {bogusComments: true}), '' ) } @@ -36,6 +35,7 @@ test('`comment`', async function (t) { // Optionally, text, with the additional restriction that the text must not // start with the string `>`, nor start with the string `->`, nor contain the // strings ``, or `--!>`, nor end with the string `} */ const matrix = [ ['>a', '>a'], ['->a', '->a'], @@ -49,19 +49,18 @@ test('`comment`', async function (t) { // Not at end: ['a' + toHtml({type: 'comment', value: open}), + '' ) } ) diff --git a/test/core.js b/test/core.js index 38129e7..c205ba8 100644 --- a/test/core.js +++ b/test/core.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('toHtml()', async function (t) { await t.test('should expose the public api', async function () { @@ -21,7 +20,7 @@ test('toHtml()', async function (t) { await t.test('should throw on unknown nodes', async function () { assert.throws(function () { // @ts-expect-error: check how the runtime handles an unknown node. - toHtml(u('foo', [])) + toHtml({type: 'foo', children: []}) }, /Cannot compile unknown node `foo`/) }) diff --git a/test/doctype.js b/test/doctype.js index 031e23b..d40efb8 100644 --- a/test/doctype.js +++ b/test/doctype.js @@ -1,18 +1,17 @@ import assert from 'node:assert/strict' import test from 'node:test' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`doctype`', async function (t) { await t.test('should serialize doctypes', async function () { - assert.deepEqual(toHtml(u('doctype')), '') + assert.deepEqual(toHtml({type: 'doctype'}), '') }) await t.test( 'should serialize doctypes tightly in `tightDoctype` mode', async function () { assert.deepEqual( - toHtml(u('doctype'), {tightDoctype: true}), + toHtml({type: 'doctype'}, {tightDoctype: true}), '' ) } @@ -22,7 +21,7 @@ test('`doctype`', async function (t) { 'should serialize uppercase doctypes in `upperDoctype` mode', async function () { assert.deepEqual( - toHtml(u('doctype'), {upperDoctype: true}), + toHtml({type: 'doctype'}, {upperDoctype: true}), '' ) } diff --git a/test/omission-closing-body.js b/test/omission-closing-body.js index 8ac965d..241548a 100644 --- a/test/omission-closing-body.js +++ b/test/omission-closing-body.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`body` (closing)', async function (t) { await t.test('should omit tag without following', async function () { @@ -13,7 +12,7 @@ test('`body` (closing)', async function (t) { 'should not omit tag if followed by `comment`', async function () { assert.deepEqual( - toHtml(h('html', [h('body'), u('comment', 'alpha')]), { + toHtml(h('html', [h('body'), {type: 'comment', value: 'alpha'}]), { omitOptionalTags: true }), '' @@ -25,7 +24,7 @@ test('`body` (closing)', async function (t) { 'should omit tag if not followed by `comment`', async function () { assert.deepEqual( - toHtml(h('html', [h('body'), u('text', 'alpha')]), { + toHtml(h('html', [h('body'), {type: 'text', value: 'alpha'}]), { omitOptionalTags: true }), 'alpha' diff --git a/test/omission-closing-caption.js b/test/omission-closing-caption.js index 25b409e..eab6e65 100644 --- a/test/omission-closing-caption.js +++ b/test/omission-closing-caption.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`caption` (closing)', async function (t) { await t.test('should not omit tag without children', async function () { @@ -21,7 +20,7 @@ test('`caption` (closing)', async function (t) { await t.test('should not omit tag followed by `comment`', async function () { assert.deepEqual( - toHtml(h('table', [h('caption'), u('comment', 'alpha')]), { + toHtml(h('table', [h('caption'), {type: 'comment', value: 'alpha'}]), { omitOptionalTags: true }), '
' diff --git a/test/omission-closing-colgroup.js b/test/omission-closing-colgroup.js index c4564f7..2327c68 100644 --- a/test/omission-closing-colgroup.js +++ b/test/omission-closing-colgroup.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`colgroup` (closing)', async function (t) { await t.test('should not omit tag without children', async function () { @@ -21,7 +20,9 @@ test('`colgroup` (closing)', async function (t) { await t.test('should not omit tag if head is not `col`', async function () { assert.deepEqual( - toHtml(h('colgroup', [u('comment', 'alpha')]), {omitOptionalTags: true}), + toHtml(h('colgroup', [{type: 'comment', value: 'alpha'}]), { + omitOptionalTags: true + }), '' ) }) diff --git a/test/omission-closing-head.js b/test/omission-closing-head.js index aae6efc..c5ed5eb 100644 --- a/test/omission-closing-head.js +++ b/test/omission-closing-head.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`head` (closing)', async function (t) { await t.test('should omit tag without following', async function () { @@ -13,7 +12,7 @@ test('`head` (closing)', async function (t) { 'should not omit tag if followed by `comment`', async function () { assert.deepEqual( - toHtml(h('html', [h('head'), u('comment', 'alpha')]), { + toHtml(h('html', [h('head'), {type: 'comment', value: 'alpha'}]), { omitOptionalTags: true }), '' @@ -35,7 +34,7 @@ test('`head` (closing)', async function (t) { 'should omit tag if not followed by `comment`', async function () { assert.deepEqual( - toHtml(h('html', [h('head'), u('text', 'alpha')]), { + toHtml(h('html', [h('head'), {type: 'text', value: 'alpha'}]), { omitOptionalTags: true }), 'alpha' diff --git a/test/omission-closing-html.js b/test/omission-closing-html.js index a14820d..cb19331 100644 --- a/test/omission-closing-html.js +++ b/test/omission-closing-html.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`html` (closing)', async function (t) { await t.test('should omit tag without following', async function () { @@ -13,9 +12,13 @@ test('`html` (closing)', async function (t) { 'should not omit tag if followed by `comment`', async function () { assert.deepEqual( - toHtml(u('root', [h('html'), u('comment', 'alpha')]), { - omitOptionalTags: true - }), + toHtml( + { + type: 'root', + children: [h('html'), {type: 'comment', value: 'alpha'}] + }, + {omitOptionalTags: true} + ), '' ) } @@ -25,9 +28,10 @@ test('`html` (closing)', async function (t) { 'should omit tag if not followed by `comment`', async function () { assert.deepEqual( - toHtml(u('root', [h('html'), u('text', 'alpha')]), { - omitOptionalTags: true - }), + toHtml( + {type: 'root', children: [h('html'), {type: 'text', value: 'alpha'}]}, + {omitOptionalTags: true} + ), 'alpha' ) } diff --git a/test/omission-closing-p.js b/test/omission-closing-p.js index 1c242f4..4c8790c 100644 --- a/test/omission-closing-p.js +++ b/test/omission-closing-p.js @@ -2,40 +2,51 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`p` (closing)', async function (t) { await t.test('should omit tag without following', async function () { assert.deepEqual( - toHtml(u('root', [h('p')]), {omitOptionalTags: true}), + toHtml({type: 'root', children: [h('p')]}, {omitOptionalTags: true}), '

' ) }) await t.test('should omit tag if followed by `address`', async function () { assert.deepEqual( - toHtml(u('root', [h('p'), h('address')]), {omitOptionalTags: true}), + toHtml( + {type: 'root', children: [h('p'), h('address')]}, + {omitOptionalTags: true} + ), '

' ) }) await t.test('should omit tag if followed by `ul`', async function () { assert.deepEqual( - toHtml(u('root', [h('p'), h('ul')]), {omitOptionalTags: true}), + toHtml( + {type: 'root', children: [h('p'), h('ul')]}, + {omitOptionalTags: true} + ), '

    ' ) }) await t.test('should not omit tag if followed by `a`', async function () { assert.deepEqual( - toHtml(u('root', [h('p'), h('a')]), {omitOptionalTags: true}), + toHtml( + {type: 'root', children: [h('p'), h('a')]}, + {omitOptionalTags: true} + ), '

    ' ) }) await t.test('should not omit tag if followed by `xmp`', async function () { assert.deepEqual( - toHtml(u('root', [h('p'), h('xmp')]), {omitOptionalTags: true}), + toHtml( + {type: 'root', children: [h('p'), h('xmp')]}, + {omitOptionalTags: true} + ), '

    ' ) }) diff --git a/test/omission-opening-body.js b/test/omission-opening-body.js index 40a0e67..4a69962 100644 --- a/test/omission-opening-body.js +++ b/test/omission-opening-body.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`body` (opening)', async function (t) { await t.test('should omit tag without children', async function () { @@ -13,7 +12,9 @@ test('`body` (opening)', async function (t) { 'should not omit tag if the head is a `comment`', async function () { assert.deepEqual( - toHtml(h('body', [u('comment', 'alpha')]), {omitOptionalTags: true}), + toHtml(h('body', [{type: 'comment', value: 'alpha'}]), { + omitOptionalTags: true + }), '' ) } diff --git a/test/omission-opening-colgroup.js b/test/omission-opening-colgroup.js index 1c294d2..48c6701 100644 --- a/test/omission-opening-colgroup.js +++ b/test/omission-opening-colgroup.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`colgroup` (opening)', async function (t) { await t.test('should not omit tag without children', async function () { @@ -28,7 +27,7 @@ test('`colgroup` (opening)', async function (t) { await t.test('should not omit tag followed by `comment`', async function () { assert.deepEqual( - toHtml(h('table', [h('colgroup'), u('comment', 'alpha')]), { + toHtml(h('table', [h('colgroup'), {type: 'comment', value: 'alpha'}]), { omitOptionalTags: true }), '
    ' diff --git a/test/omission-opening-html.js b/test/omission-opening-html.js index 94cb6ac..8465b0c 100644 --- a/test/omission-opening-html.js +++ b/test/omission-opening-html.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`html` (opening)', async function (t) { await t.test('should omit tag without first child', async function () { @@ -11,7 +10,7 @@ test('`html` (opening)', async function (t) { await t.test('should not omit tag if head is `comment`', async function () { assert.deepEqual( - toHtml(h('html', [u('comment', 'alpha'), 'bravo']), { + toHtml(h('html', [{type: 'comment', value: 'alpha'}, 'bravo']), { omitOptionalTags: true }), 'bravo' diff --git a/test/raw.js b/test/raw.js index 6b586f5..c9cc1d7 100644 --- a/test/raw.js +++ b/test/raw.js @@ -5,12 +5,11 @@ import assert from 'node:assert/strict' import test from 'node:test' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`raw`', async function (t) { await t.test('should encode `raw`s', async function () { assert.deepEqual( - toHtml(u('raw', '')), + toHtml({type: 'raw', value: ''}), '<script>alert("XSS!")</script>' ) }) @@ -19,9 +18,10 @@ test('`raw`', async function (t) { 'should not encode `raw`s in `allowDangerousHtml` mode', async function () { assert.deepEqual( - toHtml(u('raw', ''), { - allowDangerousHtml: true - }), + toHtml( + {type: 'raw', value: ''}, + {allowDangerousHtml: true} + ), '' ) } diff --git a/test/root.js b/test/root.js index f80541a..afd87c0 100644 --- a/test/root.js +++ b/test/root.js @@ -2,14 +2,18 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('`root`', async function (t) { await t.test('should serialize `root`s', async function () { assert.deepEqual( - toHtml( - u('root', [u('text', 'alpha '), h('i', 'bravo'), u('text', ' charlie')]) - ), + toHtml({ + type: 'root', + children: [ + {type: 'text', value: 'alpha '}, + h('i', 'bravo'), + {type: 'text', value: ' charlie'} + ] + }), 'alpha bravo charlie' ) }) @@ -17,7 +21,7 @@ test('`root`', async function (t) { await t.test('should serialize `root`s w/o children', async function () { assert.deepEqual( // @ts-expect-error: check how the runtime handles missing `children`. - toHtml(u('root')), + toHtml({type: 'root'}), '' ) }) diff --git a/test/security.js b/test/security.js index 0c8ee59..0b2df61 100644 --- a/test/security.js +++ b/test/security.js @@ -2,14 +2,18 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('security', async function (t) { await t.test( 'should make sure comments cannot break out of their context (safe)', async function () { assert.equal( - toHtml(u('root', [u('comment', '-->' ) } @@ -17,7 +21,7 @@ test('security', async function (t) { await t.test('should make sure scripts render (unsafe)', async function () { assert.equal( - toHtml(u('root', [h('script', 'alert(1)')])), + toHtml({type: 'root', children: [h('script', 'alert(1)')]}), '' ) }) @@ -34,7 +38,10 @@ test('security', async function (t) { await t.test('should make sure texts are encoded (safe)', async function () { assert.equal( - toHtml(u('root', u('text', ''))), + toHtml({ + type: 'root', + children: [{type: 'text', value: ''}] + }), '<script>alert(1)</script>' ) }) diff --git a/test/svg.js b/test/svg.js index 2408efe..00af190 100644 --- a/test/svg.js +++ b/test/svg.js @@ -2,7 +2,6 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h, s} from 'hastscript' import {toHtml} from 'hast-util-to-html' -import {u} from 'unist-builder' test('svg', async function (t) { await t.test('should serialize `element`s', async function () { @@ -401,7 +400,12 @@ test('svg', async function (t) { await t.test('should ignore attributes set to `null`', async function () { assert.deepEqual( - toHtml(u('element', {tagName: 'circle', properties: {id: null}}, [])), + toHtml({ + type: 'element', + tagName: 'circle', + properties: {id: null}, + children: [] + }), '' ) }) @@ -410,9 +414,12 @@ test('svg', async function (t) { 'should ignore attributes set to `undefined`', async function () { assert.deepEqual( - toHtml( - u('element', {tagName: 'circle', properties: {id: undefined}}, []) - ), + toHtml({ + type: 'element', + tagName: 'circle', + properties: {id: undefined}, + children: [] + }), '' ) } @@ -473,9 +480,10 @@ test('svg', async function (t) { 'should serialize an HTML tree with embedded HTML', async function () { assert.deepEqual( - toHtml( - u('root', [ - u('doctype', {name: 'html'}), + toHtml({ + type: 'root', + children: [ + {type: 'doctype'}, h('head', h('title', 'The SVG `` element')), h('body', [ s( @@ -487,8 +495,8 @@ test('svg', async function (t) { s('circle', {cx: 120, cy: 120, r: 100}) ) ]) - ]) - ), + ] + }), [ '', 'The SVG `<circle>` element', diff --git a/test/text.js b/test/text.js index 5f69275..a614c3d 100644 --- a/test/text.js +++ b/test/text.js @@ -1,33 +1,40 @@ import assert from 'node:assert/strict' import test from 'node:test' import {h} from 'hastscript' -import {u} from 'unist-builder' import {toHtml} from 'hast-util-to-html' test('`text`', async function (t) { await t.test('should serialize `text`s', async function () { - assert.deepEqual(toHtml(u('text', 'alpha')), 'alpha') + assert.deepEqual(toHtml({type: 'text', value: 'alpha'}), 'alpha') }) await t.test('should encode `text`s', async function () { - assert.deepEqual(toHtml(u('text', '3 < 5 & 7')), '3 < 5 & 7') + assert.deepEqual( + toHtml({type: 'text', value: '3 < 5 & 7'}), + '3 < 5 & 7' + ) }) await t.test('should not encode `text`s in `style`', async function () { assert.deepEqual( - toHtml(h('style', [u('text', '*:before {content: "3 < 5"}')])), + toHtml( + h('style', [{type: 'text', value: '*:before {content: "3 < 5"}'}]) + ), '' ) }) await t.test('should not encode `text`s in `script`', async function () { assert.deepEqual( - toHtml(h('script', [u('text', 'alert("3 < 5")')])), + toHtml(h('script', [{type: 'text', value: 'alert("3 < 5")'}])), '' ) }) await t.test('should encode `text`s in other nodes', async function () { - assert.deepEqual(toHtml(h('b', [u('text', '3 < 5')])), '3 < 5') + assert.deepEqual( + toHtml(h('b', [{type: 'text', value: '3 < 5'}])), + '3 < 5' + ) }) })