Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing noscript 319 #320

Merged
merged 4 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 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 All @@ -93,7 +91,6 @@ class Dom {
this.bodyElements = [];

this.indent = detectIndent(html);
this.headIndent = detectIndent(this.document.querySelector('head').innerHTML);
}

serialize() {
Expand All @@ -115,11 +112,19 @@ class Dom {
: [...this.bodyElements];

if (head.length > 0) {
result = result.replaceAll(/^([\s\t]*)(<\/\s*head>)/gim, `$1$1${head.join('\n$1$1')}\n$1$2`);
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(`${match || ''}</head>`, `${headContent}${nl}${this.indent.indent}</head>`);
}

if (body.length > 0) {
result = result.replaceAll(/^([\s\t]*)(<\/\s*body>)/gim, `$1$1${body.join('\n$1$1')}\n$1$2`);
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 Expand Up @@ -183,8 +188,8 @@ class Dom {
appendStyles(css, referenceNode) {
const styles = this.createStyleNode(css);
referenceNode.append(styles);
styles.before(this.document.createTextNode(this.headIndent.indent));
styles.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
styles.before(this.document.createTextNode(this.indent.indent));
styles.after(this.document.createTextNode(`\n${this.indent.indent}`));
}

addNoscript(link) {
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>
3 changes: 2 additions & 1 deletion test/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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 ? ' ' : '')).replaceAll(/>\s+</gm, '><');

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