Skip to content

Commit

Permalink
Merge pull request #307 from bezoerb/fix/298
Browse files Browse the repository at this point in the history
skip print & speech stylesheets
  • Loading branch information
bezoerb committed Oct 7, 2023
2 parents c4848f4 + 2396deb commit ce0037f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
13 changes: 7 additions & 6 deletions index.js
Expand Up @@ -107,8 +107,7 @@ export function inline(html, styles, options) {
);

// Add link tags before old links
// eslint-disable-next-line array-callback-return
o.replaceStylesheets.map((href) => {
o.replaceStylesheets.forEach((href) => {
const link = document.createElement('link');

link.setAttribute('rel', 'stylesheet');
Expand Down Expand Up @@ -143,8 +142,7 @@ export function inline(html, styles, options) {
});

// Remove old links
// eslint-disable-next-line array-callback-return
removable.map((link) => {
removable.forEach((link) => {
if (link.parentElement.tagName === 'NOSCRIPT') {
document.remove(link.parentElement);
} else {
Expand All @@ -153,13 +151,16 @@ export function inline(html, styles, options) {
});
} else {
// Modify links and add clones to noscript block
// eslint-disable-next-line array-callback-return
links.map((link) => {
links.forEach((link) => {
const href = link.getAttribute('href');
const media = link.getAttribute('media');
const type = link.getAttribute('type');
const integrity = link.getAttribute('integrity');

if (['print', 'speech'].includes(media)) {
return;
}

if (o.extract) {
const file = resolve(join(o.basePath || process.cwd, href));

Expand Down
20 changes: 20 additions & 0 deletions test/expected/print.html
@@ -0,0 +1,20 @@
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<style>.blue{color:#00f}.green{color:green}</style>
<link rel="stylesheet" href="css/main.css" media="print" onload="this.media='test'">
<link rel="stylesheet" href="css/simple.css" media="print">
<style>
.red {
color: red;
}
</style>
</head>
<body>
<div class="container">
Test
</div>
<noscript><link rel="stylesheet" href="css/main.css" media="test"></noscript>
</body>
</html>
18 changes: 18 additions & 0 deletions test/fixtures/print.html
@@ -0,0 +1,18 @@
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css" media="test">
<link rel="stylesheet" href="css/simple.css" media="print">
<style>
.red {
color: red;
}
</style>
</head>
<body>
<div class="container">
Test
</div>
</body>
</html>
10 changes: 10 additions & 0 deletions test/index.test.js
Expand Up @@ -820,3 +820,13 @@ test('Issue 300', async () => {

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

test("don't add onload attribute to print stylesheet", async () => {
const html = await read('fixtures/print.html');
const css = await read('fixtures/css/simple.css');

const expected = await read('expected/print.html');
const out = inline(html, css, {strategy: 'media'});

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

0 comments on commit ce0037f

Please sign in to comment.