Skip to content

Commit

Permalink
Adds tests for minified html
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Sep 22, 2024
1 parent 0aa4b05 commit da72fb0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
21 changes: 10 additions & 11 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ const replacePartials = (source, destination, tag) => {

class Dom {
constructor(html, {minify = true, noscript = 'body'} = {}) {
const jsdom = new JSDOM(html);

const jsdom = new JSDOM(html.trim());
const {window} = jsdom;
const {document} = window;
document.$jsdom = jsdom;

this.noscriptPosition = noscript;
this.minify = minify;
this.html = html;
Expand Down Expand Up @@ -116,18 +114,19 @@ class Dom {
: [...this.bodyElements];

if (head.length > 0) {
const [, match] = /^([^\S\r\n]*)<\/\s*head>/gim.exec(html) || ['', null];
const indent = match === null ? '' : `\n${match}`;
const headContent = `${indent}${this.headIndent.indent}${head.join(`${indent}${this.headIndent.indent}`)}`;
const [, match] = /^([^\S\r\n]*)<\/\s*head>/gim.exec(result) || ['', null];
const nl = match === null ? '' : `\n`;
const headContent = `${this.indent.indent}${this.indent.indent}${head.join(`${nl}${this.indent.indent}${this.indent.indent}`)}`;

result = result.replaceAll(`${indent}</head>`, `${headContent}${indent}</head>`);
result = result.replaceAll(`${match || ''}</head>`, `${headContent}${nl}${this.indent.indent}</head>`);
}

if (body.length > 0) {
const [, match] = /^([^\S\r\n]*)<\/\s*body>/gim.exec(html) || ['', null];
const indent = match === null ? '' : `\n${match}`;
const bodyContent = `${indent}${this.bodyIndent.indent}${body.join(`${indent}${this.bodyIndent.indent}`)}`;
result = result.replaceAll(`${indent}</body>`, `${bodyContent}${indent}</body>`);
const [, match] = /^([^\S\r\n]*)<\/\s*body>/gim.exec(result) || ['', null];
const nl = match === null ? '' : `\n`;
const bodyContent = `${this.indent.indent}${this.indent.indent}${body.join(`${nl}${this.indent.indent}${this.indent.indent}`)}`;

result = result.replaceAll(`${match || ''}</body>`, `${bodyContent}${nl}${this.indent.indent}</body>`);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h4>Bootstrap</h4>
<p>♥ from the Yeoman team</p>
</div>
</div>
<link rel="stylesheet" integrity="sha256-blnB+dDRC2oOhBOEH36T7kD32ztse+CjsSsO8Bo/ImU=" href="css/main.css">
<link rel="stylesheet" integrity="sha256-AVRVPaW6dptaAwENng9sTd1m6Aogc3Buf4IquvM3zFY=" href="bower_components/bootstrap/dist/css/bootstrap.css">
</body>
<link rel="stylesheet" integrity="sha256-blnB+dDRC2oOhBOEH36T7kD32ztse+CjsSsO8Bo/ImU=" href="css/main.css">
<link rel="stylesheet" integrity="sha256-AVRVPaW6dptaAwENng9sTd1m6Aogc3Buf4IquvM3zFY=" href="bower_components/bootstrap/dist/css/bootstrap.css">
</body>
</html>
2 changes: 1 addition & 1 deletion test/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const checkAndDelete = (file) => {
}
};

export const strip = (string) => nn(string.replaceAll(/[\r\n]+/gm, ' ').replaceAll(/\s+/gm, ''));
export const strip = (string, safe) => nn(string.replaceAll(/[\r\n]+/gm, ' ').replaceAll(/\s+/gm, safe ? ' ' : ''));

export const getBinary = async () => {
const {packageJson} = await readPackageUp();
Expand Down
63 changes: 53 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,6 @@ test('Keep existing integrity attribute on style tags with media=print', async (
expect(out.toString()).toBe(expected);
});

test('Keep existing integrity attribute on style tags with media=print', async () => {
const html = await read('fixtures/index-integrity.html');
const css = await read('fixtures/critical.css');

const expected = await read('expected/index-inlined-async-integrity-print-default.html');
const out = inline(html, css);

expect(out.toString()).toBe(expected);
});

test('Replace stylesheets (default)', async () => {
const html = await read('fixtures/replace-stylesheets.html');
const css = await read('fixtures/css/simple.css');
Expand Down Expand Up @@ -808,6 +798,59 @@ test('Replace stylesheets (swap)', async () => {
expect(out.toString()).toBe(expected);
});

test('Replace stylesheets (minified, polyfill)', async () => {
const html = await read('fixtures/replace-stylesheets.html');
const css = await read('fixtures/css/simple.css');

const expected = await read('expected/replace-stylesheets-polyfill.html');
const out = inline(strip(html, true), css, {
strategy: 'polyfill',
replaceStylesheets: ['/css/replaced.css'],
});

expect(strip(out.toString())).toBe(strip(expected));
});

test('Replace stylesheets (minified, body)', async () => {
const html = await read('fixtures/replace-stylesheets.html');
const css = await read('fixtures/css/simple.css');

const expected = await read('expected/replace-stylesheets-body.html');
const out = inline(strip(html, true), css, {
strategy: 'body',
replaceStylesheets: ['/css/replaced.css'],
});

expect(strip(out.toString())).toBe(strip(expected));
});

test('Replace stylesheets (minified, media)', async () => {
const html = await read('fixtures/replace-stylesheets.html');
const css = await read('fixtures/css/simple.css');

const expected = await read('expected/replace-stylesheets-media.html');
const out = inline(strip(html, true), css, {
strategy: 'media',
replaceStylesheets: ['/css/replaced.css'],
});

console.log('DEBUG:', strip(html, true));
expect(strip(out.toString())).toBe(strip(expected));
});

test('Replace stylesheets (minified, swap)', async () => {
const html = await read('fixtures/replace-stylesheets.html');
const css = await read('fixtures/css/simple.css');

const expected = await read('expected/replace-stylesheets-swap.html');
const out = inline(strip(html, true), css, {
strategy: 'swap',
replaceStylesheets: ['/css/replaced.css'],
});

expect(strip(out.toString())).toBe(strip(expected));
});

test('Issue 300', async () => {
const html = await read('fixtures/issue-300.html');
const css = await read('fixtures/critical.css');
Expand Down

0 comments on commit da72fb0

Please sign in to comment.