From 7ba56bf81bbf1390409d9eec990b7dcfd7e51d40 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sat, 3 Jan 2026 20:26:21 +0100 Subject: [PATCH] feat(new tool): HTML to XHTML --- package.json | 1 + pnpm-lock.yaml | 28 +++++ .../html-to-xhtml.service.test.ts | 110 ++++++++++++++++++ .../html-to-xhtml/html-to-xhtml.service.ts | 83 +++++++++++++ src/tools/html-to-xhtml/html-to-xhtml.vue | 55 +++++++++ src/tools/html-to-xhtml/index.ts | 13 +++ 6 files changed, 290 insertions(+) create mode 100644 src/tools/html-to-xhtml/html-to-xhtml.service.test.ts create mode 100644 src/tools/html-to-xhtml/html-to-xhtml.service.ts create mode 100644 src/tools/html-to-xhtml/html-to-xhtml.vue create mode 100644 src/tools/html-to-xhtml/index.ts diff --git a/package.json b/package.json index 3265e4b1..dccf6fdd 100644 --- a/package.json +++ b/package.json @@ -182,6 +182,7 @@ "highlight.js": "^11.11.1", "html-to-text": "^9.0.5", "htmlfy": "^1.0.0", + "htmlparser2": "^10.0.0", "hyparquet": "^1.9.0", "hyparquet-compressors": "^1.0.0", "ibantools": "^4.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d8be54c3..f2078d5f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -435,6 +435,9 @@ importers: htmlfy: specifier: ^1.0.0 version: 1.0.0 + htmlparser2: + specifier: ^10.0.0 + version: 10.0.0 hyparquet: specifier: ^1.9.0 version: 1.9.0 @@ -6321,6 +6324,9 @@ packages: domutils@3.1.0: resolution: {integrity: sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==} + domutils@3.2.2: + resolution: {integrity: sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==} + dot-case@2.1.1: resolution: {integrity: sha512-HnM6ZlFqcajLsyudHq7LeeLDr2rFAVYtDv/hV5qchQEidSck8j9OPUsXY9KwJv/lHMtYlX4DjRQqwFYa+0r8Ug==} @@ -6440,6 +6446,10 @@ packages: resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==} engines: {node: '>=0.12'} + entities@6.0.1: + resolution: {integrity: sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==} + engines: {node: '>=0.12'} + env-paths@3.0.0: resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} @@ -7363,6 +7373,9 @@ packages: htmlfy@1.0.0: resolution: {integrity: sha512-XM6zkMWIekSHqT55GhXXuf9BbIIsxidbIpj9uwY2I5MsnQPw4llWL1QD2BbzsmD5ozyi+t5gJQsuFIJj+shOYQ==} + htmlparser2@10.0.0: + resolution: {integrity: sha512-TwAZM+zE5Tq3lrEHvOlvwgj1XLWQCtaaibSN11Q+gGBAS7Y1uZSWwXXRe4iF6OXnaq1riyQAPFOBtYc77Mxq0g==} + htmlparser2@8.0.2: resolution: {integrity: sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==} @@ -17791,6 +17804,12 @@ snapshots: domelementtype: 2.3.0 domhandler: 5.0.3 + domutils@3.2.2: + dependencies: + dom-serializer: 2.0.0 + domelementtype: 2.3.0 + domhandler: 5.0.3 + dot-case@2.1.1: dependencies: no-case: 2.3.2 @@ -17916,6 +17935,8 @@ snapshots: entities@4.5.0: {} + entities@6.0.1: {} + env-paths@3.0.0: {} errno@0.1.8: @@ -19060,6 +19081,13 @@ snapshots: htmlfy@1.0.0: {} + htmlparser2@10.0.0: + dependencies: + domelementtype: 2.3.0 + domhandler: 5.0.3 + domutils: 3.2.2 + entities: 6.0.1 + htmlparser2@8.0.2: dependencies: domelementtype: 2.3.0 diff --git a/src/tools/html-to-xhtml/html-to-xhtml.service.test.ts b/src/tools/html-to-xhtml/html-to-xhtml.service.test.ts new file mode 100644 index 00000000..36fed71b --- /dev/null +++ b/src/tools/html-to-xhtml/html-to-xhtml.service.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from 'vitest'; +import { toStrictXhtml } from './html-to-xhtml.service'; + +describe('toStrictXhtml', () => { + it('self-closes all void elements', () => { + const html = ` + +
+
+ + + + + + + + + + + `; + + const out = toStrictXhtml(html); + + const expected = [ + '', + '
', + '
', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + ]; + + for (const line of expected) { + expect(out).toContain(line); + } + }); + + it('injects namespace on root element when requested', () => { + const html = '

Hello

'; + + const out = toStrictXhtml(html, { + addNamespace: true, + }); + + expect(out).toContain(''); + }); + + it('applies indentation when indent option is provided', () => { + const html = '

Hello

'; + + const out = toStrictXhtml(html, { indent: 2 }); + + expect(out).toBe( +`
+

+ Hello +

+
`, + ); + }); + + it('escapes XML entities in text and attributes', () => { + const html = '

5 > 3 & 2 < 4

'; + + const out = toStrictXhtml(html); + + expect(out).toContain('title="5 > 3 & 2 < 4"'); + expect(out).toContain('5 > 3 & 2 < 4'); + }); + + it('lowercases tag and attribute names', () => { + const html = '
'; + + const out = toStrictXhtml(html); + + expect(out).toContain('
'); + expect(out).toContain(''); + }); + + it('serializes comments correctly', () => { + const html = '
'; + + const out = toStrictXhtml(html); + expect(out).toContain(''); + }); + + it('handles nested structures deterministically', () => { + const html = ''; + + const out = toStrictXhtml(html, { indent: 2 }); + + expect(out).toBe( +``, + ); + }); +}); diff --git a/src/tools/html-to-xhtml/html-to-xhtml.service.ts b/src/tools/html-to-xhtml/html-to-xhtml.service.ts new file mode 100644 index 00000000..0de70294 --- /dev/null +++ b/src/tools/html-to-xhtml/html-to-xhtml.service.ts @@ -0,0 +1,83 @@ +import { DomUtils, parseDocument } from 'htmlparser2'; + +export interface XhtmlOptions { + addNamespace?: boolean // add http://www.w3.org/1999/xhtml + indent?: number // number of spaces per indent level +} + +export function toStrictXhtml(html: string, options: XhtmlOptions = {}): string { + // FULL canonical void element list + const voidElements = new Set([ + 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', + 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr', + ]); + + const escapeXml = (value: string) => + value + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); + + const indentSize = options.indent ?? 0; + const indentStr = (level: number) => + indentSize > 0 ? ' '.repeat(level * indentSize) : ''; + + const serialize = (node: any, level = 0): string => { + if (DomUtils.isText(node)) { + const text = escapeXml(node.data); + return indentSize > 0 ? indentStr(level) + text : text; + } + + if (DomUtils.isComment(node)) { + const comment = ``; + return indentSize > 0 ? indentStr(level) + comment : comment; + } + + if (DomUtils.isTag(node)) { + const tag = node.name.toLowerCase(); + + // Attributes + const attrsObj = { ...(node.attribs || {}) }; + + // Inject namespace on root element if requested + if (options.addNamespace && node.parent?.type === 'root') { + attrsObj.xmlns = 'http://www.w3.org/1999/xhtml'; + } + + const attrs = Object.entries(attrsObj) + .map(([k, v]) => `${k.toLowerCase()}="${escapeXml(String(v || k.toLowerCase()))}"`) + .join(' '); + + const open = attrs ? `<${tag} ${attrs}` : `<${tag}`; + + // VOID ELEMENT → self-close + if (voidElements.has(tag)) { + const line = `${open} />`; + return indentSize > 0 ? indentStr(level) + line : line; + } + + // NORMAL ELEMENT + const children = (node.children || []) + .map(child => serialize(child, level + 1)) + .join(indentSize > 0 ? '\n' : ''); + + if (indentSize > 0) { + if (children.trim().length === 0) { + return `${indentStr(level)}${open}>`; + } + return `${indentStr(level)}${open}>\n${children}\n${indentStr(level)}`; + } + + return `${open}>${children}`; + } + + return ''; + }; + + const doc = parseDocument(html); + + return doc.children + .map(node => serialize(node, 0)) + .join(indentSize > 0 ? '\n' : ''); +} diff --git a/src/tools/html-to-xhtml/html-to-xhtml.vue b/src/tools/html-to-xhtml/html-to-xhtml.vue new file mode 100644 index 00000000..c25ac447 --- /dev/null +++ b/src/tools/html-to-xhtml/html-to-xhtml.vue @@ -0,0 +1,55 @@ + + + diff --git a/src/tools/html-to-xhtml/index.ts b/src/tools/html-to-xhtml/index.ts new file mode 100644 index 00000000..d723df1e --- /dev/null +++ b/src/tools/html-to-xhtml/index.ts @@ -0,0 +1,13 @@ +import { BrandHtml5 } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'HTML to XHTML', + path: '/html-to-xhtml', + description: 'Convert HTML to strict XHTML (XML parsable, self closing tags...)', + keywords: ['html', 'xhtml'], + component: () => import('./html-to-xhtml.vue'), + icon: BrandHtml5, + createdAt: new Date('2026-01-03'), + category: 'Web', +});