Skip to content

Commit 2082351

Browse files
authored
fix(hydrate): output track elements as void elms (#5720)
* fix(hydrate): output track elements as void elms add `track` to the list of "EMPTY" elements (void elements) that is referenced when we serialize nodes. prior to this fix, the output of running `renderToString` as such: ```js const hydrate = require('./hydrate/index.js'); const src = `<video> <source src="x" type="video/mp4"> <track kind="subtitles" src="x" > </video>`; hydrate.renderToString(src, { prettyHtml: true }) .then(result => console.log(result.html)); ``` would result in the following being printed: ```html <!doctype html> <html class="hydrated" data-stencil-build="shc9pw4e"> <head> <meta charset="utf-8"> </head> <body> <video> <source src="x" type="video/mp4"> <!-- NOTE: track has a closing tag --> <track kind="subtitles" src="x"></track> </video> </body> </html> ``` With this fix applied, we print `track` as a void element: ```html <!doctype html> <html class="hydrated" data-stencil-build="z6oqy2b2"> <head> <meta charset="utf-8"> </head> <body> <video> <source src="x" type="video/mp4"> <track kind="subtitles" src="x"> </video> </body> </html> ``` Fixes: #2994 STENCIL-94: Using hydrate.renderToString produces "invalid" HTML with <track> * review(cb): unit tests
1 parent 837a329 commit 2082351

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/mock-doc/serialize-node.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ function isWithinWhitespaceSensitive(node: Node) {
474474
return false;
475475
}
476476

477+
// TODO(STENCIL-1299): Audit this list, remove unsupported/deprecated elements
477478
/*@__PURE__*/ export const NON_ESCAPABLE_CONTENT = new Set([
478479
'STYLE',
479480
'SCRIPT',
@@ -485,6 +486,7 @@ function isWithinWhitespaceSensitive(node: Node) {
485486
'PLAINTEXT',
486487
]);
487488

489+
// TODO(STENCIL-1299): Audit this list, remove unsupported/deprecated elements
488490
/**
489491
* A list of whitespace sensitive tag names, such as `code`, `pre`, etc.
490492
*/
@@ -498,7 +500,8 @@ function isWithinWhitespaceSensitive(node: Node) {
498500
'TEXTAREA',
499501
]);
500502

501-
/*@__PURE__*/ const EMPTY_ELEMENTS = new Set([
503+
// TODO(STENCIL-1299): Audit this list, remove unsupported/deprecated elements
504+
/*@__PURE__*/ export const EMPTY_ELEMENTS = new Set([
502505
'area',
503506
'base',
504507
'basefont',
@@ -516,11 +519,14 @@ function isWithinWhitespaceSensitive(node: Node) {
516519
'param',
517520
'source',
518521
'trace',
522+
'track',
519523
'wbr',
520524
]);
521525

526+
// TODO(STENCIL-1299): Audit this list, remove unsupported/deprecated attr
522527
/*@__PURE__*/ const REMOVE_EMPTY_ATTR = new Set(['class', 'dir', 'id', 'lang', 'name', 'title']);
523528

529+
// TODO(STENCIL-1299): Audit this list, remove unsupported/deprecated attr
524530
/*@__PURE__*/ const BOOLEAN_ATTR = new Set([
525531
'allowfullscreen',
526532
'async',

src/mock-doc/test/serialize-node.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MockDocument } from '../document';
2-
import { serializeNodeToHtml } from '../serialize-node';
2+
import { EMPTY_ELEMENTS, serializeNodeToHtml } from '../serialize-node';
33

44
describe('serializeNodeToHtml', () => {
55
let doc: MockDocument;
@@ -278,4 +278,19 @@ describe('serializeNodeToHtml', () => {
278278
scriptElm.innerHTML = input;
279279
expect(scriptElm.innerHTML).toBe(input);
280280
});
281+
282+
it.each([...EMPTY_ELEMENTS])("does not add a closing tag for '%s'", (voidElm) => {
283+
if (voidElm === 'frame') {
284+
// 'frame' is a non-HTML5 compatible/deprecated element.
285+
// Stencil technically still supports it, but it doesn't get rendered as a child element, so let's just skip it
286+
return;
287+
}
288+
289+
const elm = doc.createElement('div');
290+
291+
elm.innerHTML = `<${voidElm}>`;
292+
293+
const html = serializeNodeToHtml(elm, { prettyHtml: true });
294+
expect(html).toBe(`<${voidElm}>`);
295+
});
281296
});

0 commit comments

Comments
 (0)