Skip to content

Commit 667f444

Browse files
authored
Add option to exclude inline styles (#51)
1 parent d0144b2 commit 667f444

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

readme.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ npm install extract-css-core
5656
yarn add extract-css-core
5757
```
5858

59-
## Problem, solution and shortcomings
59+
## Motivation, solution and shortcomings
6060

61-
### Problem
61+
### Motivation
6262

6363
Existing packages like
6464
[get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css)
@@ -95,6 +95,14 @@ Default: `exclude`
9595

9696
Possible values: `exclude`, `include`
9797

98+
#### inlineStyles
99+
100+
Type: `String`
101+
102+
Default: `include`
103+
104+
Possible values: `exclude`, `include`
105+
98106
#### waitUntil
99107

100108
Type: `String`

src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ InvalidUrlError.prototype = Error.prototype
1414
* @param {string} waitUntil https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options
1515
* @returns {string} All CSS that was found
1616
*/
17-
module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} = {}) => {
17+
module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude', inlineStyles = 'include'} = {}) => {
1818
// Setup a browser instance
1919
const browser = await puppeteer.launch()
2020

@@ -82,15 +82,18 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} =
8282
// CSSRule:
8383
// [x-extract-css-inline-style] { color: red; }
8484
//
85-
const inlineCssRules = await page.evaluate(() => {
86-
return [...document.querySelectorAll('[style]')]
87-
.map(element => element.getAttribute('style'))
88-
// Filter out empty style="" attributes
89-
.filter(Boolean)
90-
})
91-
const inlineCss = inlineCssRules
92-
.map(rule => `[x-extract-css-inline-style] { ${rule} }`)
93-
.map(css => ({type: 'inline', href: url, css}))
85+
let inlineCss = []
86+
if (inlineStyles !== 'exclude') {
87+
const inlineCssRules = await page.evaluate(() => {
88+
return [...document.querySelectorAll('[style]')]
89+
.map(element => element.getAttribute('style'))
90+
// Filter out empty style="" attributes
91+
.filter(Boolean)
92+
})
93+
inlineCss = inlineCssRules
94+
.map(rule => `[x-extract-css-inline-style] { ${rule} }`)
95+
.map(css => ({type: 'inline', href: url, css}))
96+
}
9497

9598
const links = coverage
9699
// Filter out the <style> tags that were found in the coverage
@@ -108,7 +111,7 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} =
108111

109112
const css = links
110113
.concat(styleSheetsApiCss)
111-
.concat(inlineCss)
114+
.concat(inlineStyles === 'exclude' ? [] : inlineCss)
112115

113116
// Return the complete structure ...
114117
if (origins === 'include') {

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ test('it finds inline styles - HTML', async t => {
8787
t.snapshot(actual)
8888
})
8989

90+
test('it ignores inline styles when `inlineStyles !== "include"`', async t => {
91+
const actual = await extractCss(server.url + '/inline-style-html.html', {
92+
inlineStyles: 'exclude'
93+
})
94+
95+
t.false(actual.includes('[x-extract-css-inline-style] { color: red; font-size: 12px; }'))
96+
t.false(actual.includes('[x-extract-css-inline-style] { color: blue }'))
97+
t.is(actual, '')
98+
})
99+
90100
test('it finds inline styles - JS', async t => {
91101
const actual = await extractCss(server.url + '/inline-style-js.html')
92102

0 commit comments

Comments
 (0)