@projectwallace/css-code-coverage 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,52 +19,33 @@ npm install @projectwallace/css-code-coverage
19
19
 
20
20
  ### Prerequisites
21
21
 
22
- 1. You have collected browser coverage data of your CSS. There are several ways to do this:
23
-
24
- 1. in the browser devtools in [Edge](https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/coverage/)/[Chrome](https://developer.chrome.com/docs/devtools/coverage/)/chromium
25
- 1. Via the `coverage.startCSSCoverage()` API that headless browsers like [Playwright](https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage) or [Puppeteer](https://pptr.dev/api/puppeteer.coverage.startcsscoverage/) provide.
26
-
27
- Either way you end up with one or more JSON files that contain coverage data.
28
-
29
- ```ts
30
- // Read a single JSON or a folder full of JSON files with coverage data
31
- // Coverage data looks like this:
32
- // {
33
- // url: 'https://www.projectwallace.com/style.css',
34
- // text: 'a { color: blue; text-decoration: underline; }', etc.
35
- // ranges: [
36
- // { start: 0, end: 46 }
37
- // ]
38
- // }
39
- import { parse_coverage } from '@projectwallace/css-code-coverage'
40
-
41
- let files = await fs.glob('./css-coverage/**/*.json')
42
- let coverage_data = []
43
-
44
- for (let file of files) {
45
- let json_content = await fs.readFile(file, 'urf-8')
46
- coverage_data.push(...parse_coverage(json_content))
47
- }
48
- ```
49
-
50
- 1. You provide a HTML parser that we use to 'scrape' the HTML in case the browser gives us not just plain CSS contents. Depending on where you run this analysis you can use:
51
-
52
- 1. Browser:
53
- ```ts
54
- function parse_html(html) {
55
- return new DOMParser().parseFromString(html, 'text/html')
56
- }
57
- ```
58
- 1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example):
59
-
60
- ```ts
61
- // $ npm install linkedom
62
- import { DOMParser } from 'linkedom'
63
-
64
- function parse_html(html: string) {
65
- return new DOMParser().parseFromString(html, 'text/html')
66
- }
67
- ```
22
+ You have collected browser coverage data of your CSS. There are several ways to do this:
23
+
24
+ 1. in the browser devtools in [Edge](https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/coverage/)/[Chrome](https://developer.chrome.com/docs/devtools/coverage/)/chromium
25
+ 1. Via the `coverage.startCSSCoverage()` API that headless browsers like [Playwright](https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage) or [Puppeteer](https://pptr.dev/api/puppeteer.coverage.startcsscoverage/) provide.
26
+
27
+ Either way you end up with one or more JSON files that contain coverage data.
28
+
29
+ ```ts
30
+ // Read a single JSON or a folder full of JSON files with coverage data
31
+ // Coverage data looks like this:
32
+ // {
33
+ // url: 'https://www.projectwallace.com/style.css',
34
+ // text: 'a { color: blue; text-decoration: underline; }', etc.
35
+ // ranges: [
36
+ // { start: 0, end: 46 }
37
+ // ]
38
+ // }
39
+ import { parse_coverage } from '@projectwallace/css-code-coverage'
40
+
41
+ let files = await fs.glob('./css-coverage/**/*.json')
42
+ let coverage_data = []
43
+
44
+ for (let file of files) {
45
+ let json_content = await fs.readFile(file, 'urf-8')
46
+ coverage_data.push(...parse_coverage(json_content))
47
+ }
48
+ ```
68
49
 
69
50
  ### Bringing it together
70
51
 
@@ -73,3 +54,26 @@ import { calculate_coverage } from '@projectwallace/css-code-coverage'
73
54
 
74
55
  let report = calculcate_coverage(coverage_data, parse_html)
75
56
  ```
57
+
58
+ See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main/src/index.ts) for the data that's returned.
59
+
60
+ ### Optional: coverage from `<style>` blocks
61
+
62
+ Covergae generators also create coverage ranges for `<style>` blocks in HTML. If this applies to your code you should provide a HTML parser that we use to 'scrape' the HTML in case the browser gives us not just plain CSS contents. Depending on where you run this analysis you can use:
63
+
64
+ 1. Browser:
65
+ ```ts
66
+ function parse_html(html) {
67
+ return new DOMParser().parseFromString(html, 'text/html')
68
+ }
69
+ ```
70
+ 1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example):
71
+
72
+ ```ts
73
+ // $ npm install linkedom
74
+ import { DOMParser } from 'linkedom'
75
+
76
+ function parse_html(html: string) {
77
+ return new DOMParser().parseFromString(html, 'text/html')
78
+ }
79
+ ```
@@ -134,6 +134,8 @@ function G(t, e) {
134
134
  continue;
135
135
  }
136
136
  if (D(n.text)) {
137
+ if (!e)
138
+ continue;
137
139
  let { css: u, ranges: o } = R(e, n.text, n.ranges);
138
140
  l.push({
139
141
  url: n.url,
@@ -1,3 +1,3 @@
1
1
  import { Coverage } from './parse-coverage.ts';
2
2
  import { Parser } from './types.ts';
3
- export declare function filter_coverage(coverage: Coverage[], parse_html: Parser): Coverage[];
3
+ export declare function filter_coverage(coverage: Coverage[], parse_html?: Parser): Coverage[];
@@ -32,7 +32,7 @@ export type CoverageResult = CoverageData & {
32
32
  * 4. Calculate used/unused CSS bytes (fastest path, no inspection of the actual CSS needed)
33
33
  * 5. Calculate line-coverage, byte-coverage per stylesheet
34
34
  */
35
- export declare function calculate_coverage(coverage: Coverage[], parse_html: Parser): CoverageResult;
35
+ export declare function calculate_coverage(coverage: Coverage[], parse_html?: Parser): CoverageResult;
36
36
  export type { Coverage, Range } from './parse-coverage.ts';
37
37
  export { parse_coverage } from './parse-coverage.ts';
38
38
  export type { Parser } from './types.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/css-code-coverage",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "author": "Bart Veneman <bart@projectwallace.com>",
6
6
  "repository": {