mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
feat(new tool): HTML to XHTML
This commit is contained in:
1 parent
ebad961241
commit
7ba56bf81b
6 files changed
+290
No files matched your search
@@ -182,6 +182,7 @@
|
|||||||
"highlight.js": "^11.11.1",
|
"highlight.js": "^11.11.1",
|
||||||
"html-to-text": "^9.0.5",
|
"html-to-text": "^9.0.5",
|
||||||
"htmlfy": "^1.0.0",
|
"htmlfy": "^1.0.0",
|
||||||
|
"htmlparser2": "^10.0.0",
|
||||||
"hyparquet": "^1.9.0",
|
"hyparquet": "^1.9.0",
|
||||||
"hyparquet-compressors": "^1.0.0",
|
"hyparquet-compressors": "^1.0.0",
|
||||||
"ibantools": "^4.5.1",
|
"ibantools": "^4.5.1",
|
||||||
|
|||||||
Generated
+28
@@ -435,6 +435,9 @@ importers:
|
|||||||
htmlfy:
|
htmlfy:
|
||||||
specifier: ^1.0.0
|
specifier: ^1.0.0
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
htmlparser2:
|
||||||
|
specifier: ^10.0.0
|
||||||
|
version: 10.0.0
|
||||||
hyparquet:
|
hyparquet:
|
||||||
specifier: ^1.9.0
|
specifier: ^1.9.0
|
||||||
version: 1.9.0
|
version: 1.9.0
|
||||||
@@ -6321,6 +6324,9 @@ packages:
|
|||||||
domutils@3.1.0:
|
domutils@3.1.0:
|
||||||
resolution: {integrity: sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==}
|
resolution: {integrity: sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==}
|
||||||
|
|
||||||
|
domutils@3.2.2:
|
||||||
|
resolution: {integrity: sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==}
|
||||||
|
|
||||||
dot-case@2.1.1:
|
dot-case@2.1.1:
|
||||||
resolution: {integrity: sha512-HnM6ZlFqcajLsyudHq7LeeLDr2rFAVYtDv/hV5qchQEidSck8j9OPUsXY9KwJv/lHMtYlX4DjRQqwFYa+0r8Ug==}
|
resolution: {integrity: sha512-HnM6ZlFqcajLsyudHq7LeeLDr2rFAVYtDv/hV5qchQEidSck8j9OPUsXY9KwJv/lHMtYlX4DjRQqwFYa+0r8Ug==}
|
||||||
|
|
||||||
@@ -6440,6 +6446,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==}
|
resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==}
|
||||||
engines: {node: '>=0.12'}
|
engines: {node: '>=0.12'}
|
||||||
|
|
||||||
|
entities@6.0.1:
|
||||||
|
resolution: {integrity: sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==}
|
||||||
|
engines: {node: '>=0.12'}
|
||||||
|
|
||||||
env-paths@3.0.0:
|
env-paths@3.0.0:
|
||||||
resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==}
|
resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==}
|
||||||
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
||||||
@@ -7363,6 +7373,9 @@ packages:
|
|||||||
htmlfy@1.0.0:
|
htmlfy@1.0.0:
|
||||||
resolution: {integrity: sha512-XM6zkMWIekSHqT55GhXXuf9BbIIsxidbIpj9uwY2I5MsnQPw4llWL1QD2BbzsmD5ozyi+t5gJQsuFIJj+shOYQ==}
|
resolution: {integrity: sha512-XM6zkMWIekSHqT55GhXXuf9BbIIsxidbIpj9uwY2I5MsnQPw4llWL1QD2BbzsmD5ozyi+t5gJQsuFIJj+shOYQ==}
|
||||||
|
|
||||||
|
htmlparser2@10.0.0:
|
||||||
|
resolution: {integrity: sha512-TwAZM+zE5Tq3lrEHvOlvwgj1XLWQCtaaibSN11Q+gGBAS7Y1uZSWwXXRe4iF6OXnaq1riyQAPFOBtYc77Mxq0g==}
|
||||||
|
|
||||||
htmlparser2@8.0.2:
|
htmlparser2@8.0.2:
|
||||||
resolution: {integrity: sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==}
|
resolution: {integrity: sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==}
|
||||||
|
|
||||||
@@ -17791,6 +17804,12 @@ snapshots:
|
|||||||
domelementtype: 2.3.0
|
domelementtype: 2.3.0
|
||||||
domhandler: 5.0.3
|
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:
|
dot-case@2.1.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
no-case: 2.3.2
|
no-case: 2.3.2
|
||||||
@@ -17916,6 +17935,8 @@ snapshots:
|
|||||||
|
|
||||||
entities@4.5.0: {}
|
entities@4.5.0: {}
|
||||||
|
|
||||||
|
entities@6.0.1: {}
|
||||||
|
|
||||||
env-paths@3.0.0: {}
|
env-paths@3.0.0: {}
|
||||||
|
|
||||||
errno@0.1.8:
|
errno@0.1.8:
|
||||||
@@ -19060,6 +19081,13 @@ snapshots:
|
|||||||
|
|
||||||
htmlfy@1.0.0: {}
|
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:
|
htmlparser2@8.0.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
domelementtype: 2.3.0
|
domelementtype: 2.3.0
|
||||||
|
|||||||
@@ -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 = `
|
||||||
|
<img src="a.png">
|
||||||
|
<br>
|
||||||
|
<hr>
|
||||||
|
<input disabled>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<source src="x.mp4">
|
||||||
|
<track kind="captions">
|
||||||
|
<area href="#">
|
||||||
|
<base href="/">
|
||||||
|
<col>
|
||||||
|
<embed src="x">
|
||||||
|
<param name="p" value="v">
|
||||||
|
<wbr>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html);
|
||||||
|
|
||||||
|
const expected = [
|
||||||
|
'<img src="a.png" />',
|
||||||
|
'<br />',
|
||||||
|
'<hr />',
|
||||||
|
'<input disabled="disabled" />',
|
||||||
|
'<meta charset="utf-8" />',
|
||||||
|
'<source src="x.mp4" />',
|
||||||
|
'<track kind="captions" />',
|
||||||
|
'<area href="#" />',
|
||||||
|
'<base href="/" />',
|
||||||
|
'<col />',
|
||||||
|
'<embed src="x" />',
|
||||||
|
'<param name="p" value="v" />',
|
||||||
|
'<wbr />',
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const line of expected) {
|
||||||
|
expect(out).toContain(line);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('injects namespace on root element when requested', () => {
|
||||||
|
const html = '<html><body><p>Hello</p></body></html>';
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html, {
|
||||||
|
addNamespace: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(out).toContain('<html xmlns="http://www.w3.org/1999/xhtml">');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('applies indentation when indent option is provided', () => {
|
||||||
|
const html = '<div><p>Hello</p></div>';
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html, { indent: 2 });
|
||||||
|
|
||||||
|
expect(out).toBe(
|
||||||
|
`<div>
|
||||||
|
<p>
|
||||||
|
Hello
|
||||||
|
</p>
|
||||||
|
</div>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes XML entities in text and attributes', () => {
|
||||||
|
const html = '<p title="5 > 3 & 2 < 4">5 > 3 & 2 < 4</p>';
|
||||||
|
|
||||||
|
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 = '<DIV CLASS="X"><IMG SRC="A.PNG"></DIV>';
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html);
|
||||||
|
|
||||||
|
expect(out).toContain('<div class="X">');
|
||||||
|
expect(out).toContain('<img src="A.PNG" />');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('serializes comments correctly', () => {
|
||||||
|
const html = '<div><!-- Test Comment --></div>';
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html);
|
||||||
|
expect(out).toContain('<!-- Test Comment -->');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles nested structures deterministically', () => {
|
||||||
|
const html = '<ul><li>One</li><li>Two</li></ul>';
|
||||||
|
|
||||||
|
const out = toStrictXhtml(html, { indent: 2 });
|
||||||
|
|
||||||
|
expect(out).toBe(
|
||||||
|
`<ul>
|
||||||
|
<li>
|
||||||
|
One
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Two
|
||||||
|
</li>
|
||||||
|
</ul>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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, '>')
|
||||||
|
.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 = `<!--${escapeXml(node.data)}-->`;
|
||||||
|
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}></${tag}>`;
|
||||||
|
}
|
||||||
|
return `${indentStr(level)}${open}>\n${children}\n${indentStr(level)}</${tag}>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${open}>${children}</${tag}>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const doc = parseDocument(html);
|
||||||
|
|
||||||
|
return doc.children
|
||||||
|
.map(node => serialize(node, 0))
|
||||||
|
.join(indentSize > 0 ? '\n' : '');
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { toStrictXhtml } from './html-to-xhtml.service';
|
||||||
|
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||||
|
|
||||||
|
const inputHtml = ref('');
|
||||||
|
const addNamespace = ref(false);
|
||||||
|
const indentation = ref(2);
|
||||||
|
const outputHtml = computed(() => {
|
||||||
|
try {
|
||||||
|
return toStrictXhtml(
|
||||||
|
inputHtml.value,
|
||||||
|
{
|
||||||
|
addNamespace: addNamespace.value,
|
||||||
|
indent: indentation.value,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
return `<!-- ERROR: ${e.toString()} -->`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="inputHtml"
|
||||||
|
multiline raw-text
|
||||||
|
placeholder="Your HTML content..."
|
||||||
|
rows="8"
|
||||||
|
autofocus
|
||||||
|
label="Your HTML to convert to XHTML:"
|
||||||
|
paste-html
|
||||||
|
mb-1
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-space justify="center" mb-2>
|
||||||
|
<n-form-item label="Add XHTML xmlns:" label-placement="left">
|
||||||
|
<n-switch v-model:value="addNamespace" />
|
||||||
|
</n-form-item>
|
||||||
|
<n-form-item label="Indentation:" label-placement="left">
|
||||||
|
<n-input-number v-model:value="indentation" :min="0" />
|
||||||
|
</n-form-item>
|
||||||
|
</n-space>
|
||||||
|
|
||||||
|
<c-card v-if="outputHtml" title="Output XHTML:">
|
||||||
|
<TextareaCopyable
|
||||||
|
:value="outputHtml"
|
||||||
|
multiline
|
||||||
|
language="html"
|
||||||
|
:word-wrap="true"
|
||||||
|
/>
|
||||||
|
</c-card>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -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',
|
||||||
|
});
|
||||||
Reference in new issue
Block a user