/* * @author acrazing joking.young@gmail.com * @since 2026-05-15 15:16:00 */ import * as assert from 'node:assert'; import { parse } from './parse'; import { removeAttribute, setAttribute, stringify } from './stringify'; import { SyntaxKind } from './types'; import type { IAttribute, INode, ITag } from './types'; function assertTagNode(node: INode): asserts node is ITag { assert.strictEqual(node.type, SyntaxKind.Tag); } function attr(name: string, value?: string, quote: "'" | '"' | undefined = '"'): IAttribute { return { start: 0, end: 0, name: { start: 0, end: 0, type: SyntaxKind.Text, value: name, }, value: value === void 0 ? void 0 : { start: 0, end: 0, value, quote, }, }; } describe('stringify', () => { it('serializes parsed HTML back to HTML', () => { const input = '
Text
'; assert.strictEqual(stringify(parse(input)), input); }); it('serializes a single node', () => { const ast = parse('Text'); assert.strictEqual(stringify(ast[0]), 'Text'); }); it('reflects attribute value mutations through attributeMap', () => { const ast = parse('Link', { setAttributeMap: true, }); assertTagNode(ast[0]); ast[0].attributeMap!.href.value!.value = 'after'; assert.strictEqual(stringify(ast), 'Link'); }); it('sets an existing attribute and syncs attributeMap', () => { const ast = parse('Link', { setAttributeMap: true, }); assertTagNode(ast[0]); setAttribute(ast[0], 'href', 'after'); assert.strictEqual(ast[0].attributeMap!.href.value!.value, 'after'); assert.strictEqual(stringify(ast), 'Link'); }); it('adds a new attribute and syncs attributeMap', () => { const ast = parse('Link', { setAttributeMap: true, }); assertTagNode(ast[0]); setAttribute(ast[0], 'target', '_blank'); assert.strictEqual(ast[0].attributeMap!.target.value!.value, '_blank'); assert.strictEqual(stringify(ast), 'Link'); }); it('sets boolean attributes', () => { const ast = parse(''); assertTagNode(ast[0]); setAttribute(ast[0], 'disabled'); assert.strictEqual(stringify(ast), ''); }); it('removes all matching attributes and syncs attributeMap', () => { const ast = parse('Link', { setAttributeMap: true, }); assertTagNode(ast[0]); removeAttribute(ast[0], 'href'); assert.strictEqual(ast[0].attributeMap!.href, void 0); assert.strictEqual(stringify(ast), 'Link'); }); it('serializes attributes added to attributes array', () => { const ast = parse('Link'); assertTagNode(ast[0]); ast[0].attributes.push(attr('target', '_blank')); assert.strictEqual(stringify(ast), 'Link'); }); it('preserves missing close tags from the AST shape', () => { assert.strictEqual(stringify(parse('
Text')), '
Text'); }); it('serializes incomplete open tags without a closing angle bracket', () => { assert.strictEqual(stringify(parse('