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

skip print & speech stylesheets #307

Merged
merged 1 commit into from Oct 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions index.js
Expand Up @@ -108,8 +108,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 @@ -144,8 +143,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 @@ -154,13 +152,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);
});