88import { MockDocument } from '../document'; import { EMPTY_ELEMENTS, serializeNodeToHtml } from '../serialize-node'; describe('serializeNodeToHtml', () => { let doc: MockDocument; beforeEach(() => { doc = new MockDocument(); }); it('remove most whitespace between elements, but not all of it when not in pretty print', () => { const elm = doc.createElement('div'); elm.innerHTML = `
\tvar 88 \tvar 88 88 ; 88 ;', () => {
const elm = doc.createElement('div');
elm.innerHTML = `88
`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`\n 88
\n \n `);
});
it('do not pretty print ', () => {
const elm = doc.createElement('div');
elm.innerHTML = `88
`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`88
`);
});
it('do not pretty print w/ highlights and new', () => {
const elm = doc.createElement('div');
elm.innerHTML = `install cordova-plugin-purchase\nnpx cap update
`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`install cordova-plugin-purchase\nnpx cap update
`);
});
it('do not pretty print w/ html comments', () => {
const elm = doc.createElement('div');
elm.innerHTML = `88c
`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`88c
`);
});
it('do not pretty print `;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(``);
});
it('do not remove whitespace within ', () => {
const elm = doc.createElement('div');
elm.innerHTML = `
var
value
=
88;
`;
const html = serializeNodeToHtml(elm);
expect(html).toBe(`
var
value
=
88;
`);
});
it('pretty print with comments', () => {
const elm = doc.createElement('div');
elm.innerHTML = `
Hello
`;
expect(elm).toEqualHtml(`
Hello
`);
});
it('shadow root to template', () => {
const elm = doc.createElement('cmp-a');
expect(elm.shadowRoot).toEqual(null);
const shadowRoot = elm.attachShadow({ mode: 'open' });
expect(shadowRoot.nodeType).toEqual(11);
expect(elm.shadowRoot.nodeType).toEqual(11);
expect(shadowRoot.host).toEqual(elm);
const shadowTop = doc.createElement('article');
shadowTop.innerHTML = 'shadow top';
shadowRoot.appendChild(shadowTop);
const slot = doc.createElement('slot');
shadowRoot.appendChild(slot);
const shadowBottom = doc.createElement('section');
shadowBottom.innerHTML = 'shadow bottom';
shadowRoot.appendChild(shadowBottom);
elm.innerHTML = 'light dom';
expect(elm).toEqualHtml(`
shadow top
shadow bottom
light dom
`);
});
it('shadow root to template with focus delegation', () => {
const elm = doc.createElement('cmp-a');
expect(elm.shadowRoot).toEqual(null);
const shadowRoot = elm.attachShadow({ mode: 'open', delegatesFocus: true });
expect(shadowRoot.nodeType).toEqual(11);
expect(elm.shadowRoot.nodeType).toEqual(11);
expect(shadowRoot.host).toEqual(elm);
expect(elm.outerHTML).toContain(' {
const elm = doc.createElement('my-tag');
elm.setAttribute('tabindex', '0');
const shadowRoot = elm.attachShadow({ mode: 'open', delegatesFocus: true });
const div = doc.createElement('div');
div.innerHTML = 'test content';
shadowRoot.appendChild(div);
// Test that the serialized output matches the expected format
// This ensures shadowrootdelegatesfocus appears without ="" when compared
expect(elm).toEqualHtml(`
test content
`);
});
it('treat an element with only removed comments as empty, pretty', () => {
const elm = doc.createElement('div');
elm.innerHTML = ``;
const html = serializeNodeToHtml(elm, { prettyHtml: true, removeHtmlComments: true });
expect(html).toBe(``);
});
it('treat an element with only removed comments and whitespace as empty, pretty', () => {
const elm = doc.createElement('div');
elm.innerHTML = `\n \n `;
const html = serializeNodeToHtml(elm, { prettyHtml: true, removeHtmlComments: true });
expect(html).toBe(``);
});
it('treat an element with only removed comments as empty', () => {
const elm = doc.createElement('div');
elm.innerHTML = ``;
const html = serializeNodeToHtml(elm, { removeHtmlComments: true });
expect(html).toBe(``);
});
it('keep content next to removed comments, pretty', () => {
const elm = doc.createElement('div');
elm.innerHTML = `88 `;
const html = serializeNodeToHtml(elm, { prettyHtml: true, removeHtmlComments: true });
expect(html).toBe(`\n 88\n `);
});
it('keep comments when they are not removed, pretty', () => {
const elm = doc.createElement('div');
elm.innerHTML = ``;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`\n \n `);
});
it('shadow root with only removed comments in the light dom, pretty', () => {
const elm = doc.createElement('cmp-a');
const shadowRoot = elm.attachShadow({ mode: 'open' });
shadowRoot.appendChild(doc.createElement('article'));
elm.appendChild(doc.createComment('a'));
const html = serializeNodeToHtml(elm, { prettyHtml: true, removeHtmlComments: true, outerHtml: true });
expect(html).toBe(`\n \n \n \n `);
});
it('style', () => {
const input = ``;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(output).toBe(``);
});
it('template', () => {
const input = `text`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(output).toBe(`text`);
});
it('svg', () => {
const input = ``;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(input).toBe(output);
});
it('remove boolean attributes', () => {
const input = ``;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body, { removeBooleanAttributeQuotes: true });
expect(output).toBe(``);
});
it('do not collapse boolean attributes', () => {
const input = ``;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(input).toBe(output);
});
it('do not remove empty attrs', () => {
const elm = doc.createElement('button');
elm.setAttribute('class', '');
elm.setAttribute('dir', '');
elm.setAttribute('my-attr', '');
elm.setAttribute('id', '');
elm.setAttribute('data-custom', '');
elm.setAttribute('lang', '');
elm.setAttribute('name', '');
elm.setAttribute('title', '');
const html = serializeNodeToHtml(elm, { outerHtml: true, removeEmptyAttributes: false });
expect(html).toBe(``);
});
it('remove empty attrs', () => {
const elm = doc.createElement('button');
elm.setAttribute('class', '');
elm.setAttribute('dir', '');
elm.setAttribute('my-attr', '');
elm.setAttribute('id', '');
elm.setAttribute('data-custom', '');
elm.setAttribute('lang', '');
elm.setAttribute('name', '');
elm.setAttribute('title', '');
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(``);
});
it('set attributes, pretty', () => {
const elm = doc.createElement('button');
elm.setAttribute('type', 'submit');
elm.setAttribute('id', 'btn');
elm.textContent = `Text`;
const html = serializeNodeToHtml(elm, { outerHtml: true, prettyHtml: true });
expect(html).toBe(``);
});
it('set attributes', () => {
const elm = doc.createElement('button');
elm.setAttribute('type', 'submit');
elm.setAttribute('id', 'btn');
elm.textContent = `Text`;
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(``);
});
it('do not escape scripts', () => {
const elm = doc.createElement('script');
elm.innerHTML = `if (true && false) { console.log('hi); }`;
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(``);
});
it('empty document', () => {
const html = serializeNodeToHtml(doc);
expect(html).toBe(``);
});
it('empty document, pretty', () => {
const html = serializeNodeToHtml(doc, { prettyHtml: true });
expect(html).toBe(`
`);
});
it('script innerHTML', () => {
const input = `xb`;
const scriptElm = doc.createElement('script');
scriptElm.innerHTML = input;
expect(scriptElm.innerHTML).toBe(input);
});
it.each([...EMPTY_ELEMENTS])("does not add a closing tag for '%s'", (voidElm) => {
if (voidElm === 'frame') {
// 'frame' is a non-HTML5 compatible/deprecated element.
// Stencil technically still supports it, but it doesn't get rendered as a child element, so let's just skip it
return;
}
const elm = doc.createElement('div');
elm.innerHTML = `<${voidElm}>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<${voidElm}>`);
});
describe('style attribute', () => {
it('escapes double quotes in a style value', () => {
const elm = doc.createElement('div');
const p = doc.createElement('p');
p.style.fontFamily = '"Open Sans", sans-serif';
elm.appendChild(p);
const html = serializeNodeToHtml(elm);
expect(html).toBe(``);
});
it('escapes double quotes in a style value that wraps onto its own line', () => {
const elm = doc.createElement('div');
const p = doc.createElement('p');
p.style.fontFamily = '"Open Sans", sans-serif';
elm.appendChild(p);
const html = serializeNodeToHtml(elm, { approximateLineWidth: 10 });
expect(html).toBe(``);
});
it('keeps escaped quotes from parsed HTML inside the style attribute', () => {
const elm = doc.createElement('div');
elm.innerHTML = `text
`;
const html = serializeNodeToHtml(elm);
expect(html).toBe(`text
`);
expect(elm.querySelector('img')).toBeNull();
});
it('escapes ampersands in a style value', () => {
const elm = doc.createElement('div');
const p = doc.createElement('p');
p.style.backgroundImage = 'url("a.png?x=1&y=2")';
elm.appendChild(p);
const html = serializeNodeToHtml(elm);
expect(html).toBe(``);
});
});
});