Skip to content

Commit b9f0122

Browse files
Adjusting special output for HTML.
1 parent d310e56 commit b9f0122

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"runtimeArgs": [
1212
"--inspect-brk",
1313
"${workspaceRoot}/node_modules/jest/bin/jest.js",
14-
"html.test",
14+
"xslt.test",
1515
"--runInBand"
1616
],
1717
"skipFiles": ["<node_internals>/**", "node_modules/**"],

src/dom/xml-functions.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ function xmlElementLogicTrivial(node: XNode, buffer: string[], options: XmlOutpu
248248
let childNodes = node.transformedChildNodes.length > 0 ? node.transformedChildNodes : node.childNodes;
249249
childNodes = childNodes.sort((a, b) => a.siblingPosition - b.siblingPosition);
250250
if (childNodes.length === 0) {
251-
if (options.selfClosingTags || (options.outputMethod === 'html' && ['hr', 'link'].includes(node.nodeName))) {
251+
if (options.outputMethod === 'html' && ['hr', 'link', 'meta'].includes(node.nodeName)) {
252+
buffer.push('>');
253+
} else if (options.selfClosingTags) {
252254
buffer.push('/>');
253255
} else {
254256
buffer.push(`></${xmlFullNodeName(node)}>`);
@@ -278,7 +280,13 @@ function xmlElementLogicMuted(node: XNode, buffer: any[], options: XmlOutputOpti
278280
}
279281
}
280282

281-
function xmlFullNodeName(node: XNode) {
283+
/**
284+
* Gets the full node name.
285+
* When namespace is set, the node name is `namespace:node`.
286+
* @param node The node.
287+
* @returns The full node name as a string.
288+
*/
289+
function xmlFullNodeName(node: XNode): string {
282290
const nodeName = node.transformedNodeName || node.nodeName;
283291
if (node.transformedPrefix && nodeName.indexOf(`${node.transformedPrefix}:`) != 0) {
284292
return `${node.transformedPrefix}:${nodeName}`;
@@ -294,7 +302,7 @@ function xmlFullNodeName(node: XNode) {
294302
* @param s The string to be escaped.
295303
* @returns The escaped string.
296304
*/
297-
export function xmlEscapeText(s: string) {
305+
export function xmlEscapeText(s: string): string {
298306
return `${s}`
299307
.replace(/&/g, '&amp;')
300308
.replace(/&amp;amp;/g, '&amp;')
@@ -310,7 +318,7 @@ export function xmlEscapeText(s: string) {
310318
* @param s The string to be escaped.
311319
* @returns The escaped string.
312320
*/
313-
function xmlEscapeAttr(s: string) {
321+
function xmlEscapeAttr(s: string): string {
314322
return xmlEscapeText(s).replace(/"/g, '&quot;');
315323
}
316324

@@ -324,7 +332,7 @@ function xmlEscapeAttr(s: string) {
324332
* @param name TODO
325333
* @returns TODO
326334
*/
327-
export function xmlGetAttribute(node: XNode, name: string) {
335+
export function xmlGetAttribute(node: XNode, name: string): string {
328336
// TODO(mesch): This should not be necessary if the DOM is working
329337
// correctly. The DOM is responsible for resolving entities, not the
330338
// application.

src/dom/xml-output-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export type XmlOutputOptions = {
22
cData: boolean;
33
escape: boolean;
44
selfClosingTags: boolean;
5-
outputMethod: 'xml' | 'html'
5+
outputMethod: 'xml' | 'html' | 'text' | 'name'
66
}

src/dom/xml-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class XmlParser {
3636
XML11_TAGNAME_REGEXP = new RegExp(`^(${XML11_NAME})`);
3737
XML11_ATTRIBUTE_REGEXP = new RegExp(XML11_ATTRIBUTE, 'g');
3838

39-
lenientHtmlTags = ['hr', 'link'];
39+
lenientHtmlTags = ['hr', 'link', 'meta'];
4040

4141
/**
4242
* The entry point for this parser.

src/xslt/xslt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class Xslt {
7474
decimalFormatSettings: XsltDecimalFormatSettings;
7575

7676
outputDocument: XDocument;
77-
outputMethod: 'xml' | 'html';
77+
outputMethod: 'xml' | 'html' | 'text' | 'name';
7878
outputOmitXmlDeclaration: string;
7979
version: string;
8080

@@ -369,7 +369,7 @@ export class Xslt {
369369
case 'otherwise':
370370
throw `error if here: ${template.localName}`;
371371
case 'output':
372-
this.outputMethod = xmlGetAttribute(template, 'method');
372+
this.outputMethod = (xmlGetAttribute(template, 'method') as 'xml' | 'html' | 'text' | 'name');
373373
this.outputOmitXmlDeclaration = xmlGetAttribute(template, 'omit-xml-declaration');
374374
break;
375375
case 'param':

tests/lmht.test.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,24 +1576,22 @@ describe('LMHT', () => {
15761576
</lmht>
15771577
);
15781578

1579-
const expectedOutString = (
1580-
<html>
1581-
<head>
1582-
<meta name="description" content="LMHT" />
1583-
<meta name="keywords" content="HTML, LMHT, Desenvolvimento, Web" />
1584-
<meta name="author" content="Leonel Sanches da Silva" />
1585-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
1586-
<title>Meu blog</title>
1587-
<link rel="stylesheet" href="/publico/css/teste.css" />
1588-
</head>
1589-
<body>
1590-
<article>
1591-
<h1>Meu primeiro artigo</h1>
1592-
<p>Este é meu primeiro artigo.</p>
1593-
</article>
1594-
</body>
1595-
</html>
1596-
);
1579+
const expectedOutString = `<html>`+
1580+
`<head>`+
1581+
`<meta name="description" content="LMHT">`+
1582+
`<meta name="keywords" content="HTML, LMHT, Desenvolvimento, Web">`+
1583+
`<meta name="author" content="Leonel Sanches da Silva">`+
1584+
`<meta name="viewport" content="width=device-width, initial-scale=1.0">`+
1585+
`<title>Meu blog</title>`+
1586+
`<link rel="stylesheet" href="/publico/css/teste.css">`+
1587+
`</head>`+
1588+
`<body>`+
1589+
`<article>`+
1590+
`<h1>Meu primeiro artigo</h1>`+
1591+
`<p>Este é meu primeiro artigo.</p>`+
1592+
`</article>`+
1593+
`</body>`+
1594+
`</html>`;
15971595

15981596
const xsltClass = new Xslt({ selfClosingTags: false });
15991597
const xmlParser = new XmlParser();

0 commit comments

Comments
 (0)