From 274d44990e8973a069f8214dfa742d4b5e1420b0 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Mon, 9 Mar 2020 23:15:47 +0100 Subject: [PATCH 1/3] fix error when referencing a css file directly --- src/index.js | 8 ++++ test/index.js | 7 ++++ test/snapshots/index.js.md | 73 +---------------------------------- test/snapshots/index.js.snap | Bin 605 -> 117 bytes 4 files changed, 17 insertions(+), 71 deletions(-) diff --git a/src/index.js b/src/index.js index c17fc6a..546f9cd 100644 --- a/src/index.js +++ b/src/index.js @@ -36,6 +36,14 @@ module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => { ) } + // If the response is a CSS file, return that file + // instead of running our complicated setup + const headers = response.headers() + + if (headers['content-type'].includes('text/css')) { + return Promise.resolve(response.text()) + } + const coverage = await page.coverage.stopCSSCoverage() // Get all CSS generated with the CSSStyleSheet API diff --git a/test/index.js b/test/index.js index fc8681c..32c0e72 100644 --- a/test/index.js +++ b/test/index.js @@ -84,6 +84,13 @@ test('it finds inline styles - JS', async t => { t.snapshot(actual) }) +test.only('it returns a direct link to a CSS file', async t => { + const actual = await extractCss(server.url + '/import-in-css.css') + + t.true(actual.includes('.css-imported-with-css {}')) + t.snapshot(actual) +}) + test('it rejects if the url has an HTTP error status', async t => { server.get('/404-page', (req, res) => { res.status(404).send() diff --git a/test/snapshots/index.js.md b/test/snapshots/index.js.md index 69c6d1b..982e040 100644 --- a/test/snapshots/index.js.md +++ b/test/snapshots/index.js.md @@ -4,77 +4,8 @@ The actual snapshot is saved in `index.js.snap`. Generated by [AVA](https://ava.li). -## it finds CSS implemented in a mixed methods (inline, links, style tags) +## it returns a direct link to a CSS file > Snapshot 1 - `@import url("import-in-css.css");␊ - ␊ - .link-in-html { }␊ - .css-imported-with-css {}␊ - @import url("import-in-css.css");␊ - ␊ - .link-tag-created-with-js {}␊ - ␊ - .css-imported-with-css {}␊ - .style-tag-in-html { color: green; }␊ - .js-insertRule { color: red; }␊ - .style-tag-js { }␊ - [x-extract-css-inline-style] { background-image: url('background-image-inline-style-attribute-in-html'); }␊ - [x-extract-css-inline-style] { background-image: url("background-image-inline-style-js-cssText"); }␊ - [x-extract-css-inline-style] { background-image: url("background-image-inline-style-js-with-prop"); }` - -## it finds css in a tag - HTML - -> Snapshot 1 - - `@import url("import-in-css.css");␊ - ␊ - .link-in-html { }␊ - .css-imported-with-css {}` - -## it finds css in a tag - JS - -> Snapshot 1 - - `@import url("import-in-css.css");␊ - ␊ - .link-tag-created-with-js {}␊ - ␊ - .css-imported-with-css {}` - -## it finds css in a