@ongov/ontario-design-system-component-html-generator 8.1.0 → 9.0.0-alpha.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ongov/ontario-design-system-component-html-generator",
3
- "version": "8.1.0",
3
+ "version": "9.0.0-alpha.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ongov/ontario-design-system",
@@ -16,9 +16,10 @@
16
16
  },
17
17
  "scripts": {
18
18
  "build": "tsc && pnpm run docs:copy:readme",
19
- "docs:copy:readme": "tsx ../../scripts/documentation-helper.ts -f README.md -p \"---\ntitle: HTML Sample Generator\n---\n\" -df ../docusaurus/docs/07-readme-html-sample-generator.md",
19
+ "docs:copy:readme": "tsx ../../scripts/documentation-helper.ts -f README.md -p \"---\ntitle: HTML Sample Generator\n---\n\" --df ../docusaurus/docs/07-readme-html-sample-generator.md",
20
20
  "start": "node dist/cli.js",
21
- "test": "jest"
21
+ "test": "vitest run",
22
+ "test:watch": "vitest"
22
23
  },
23
24
  "keywords": [
24
25
  "ontario",
@@ -39,20 +40,16 @@
39
40
  }
40
41
  },
41
42
  "dependencies": {
42
- "@ongov/ontario-design-system-component-library": "8.1.0",
43
+ "@ongov/ontario-design-system-component-library": "9.0.0-alpha.1",
43
44
  "commander": "^11.0.0"
44
45
  },
45
46
  "devDependencies": {
46
- "@jest/globals": "^29.7.0",
47
47
  "@stencil/core": "^4.38.3",
48
- "@types/jest": "^29.5.3",
49
48
  "@types/node": "^24.10.1",
50
- "jest": "^29.7.0",
51
- "jest-environment-node": "^29.7.0",
52
- "puppeteer": "^24.30.0",
53
- "ts-jest": "^29.4.9",
49
+ "puppeteer": "^25.0.0",
54
50
  "tsx": "^4.20.5",
55
- "typescript": "^5.9.3"
51
+ "typescript": "^6.0.0",
52
+ "vitest": "^4.1.8"
56
53
  },
57
- "gitHead": "e67bf7a76a67a05713fe83ef221376de1791a000"
54
+ "gitHead": "d283d8e978ec46cf3fdb2d007f48405dd2c36be9"
58
55
  }
package/src/utils.test.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import {
2
3
  buildDocument,
3
4
  extractOntarioComponents,
package/tsconfig.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "module": "NodeNext",
5
5
  "moduleResolution": "NodeNext",
6
6
  "lib": ["es2019", "dom"],
7
- "types": ["node", "jest"],
7
+ "types": ["node"],
8
8
  "typeRoots": ["./node_modules/@types", "./"],
9
9
  "esModuleInterop": true,
10
10
  "allowSyntheticDefaultImports": true,
@@ -20,5 +20,5 @@
20
20
  "declaration": true
21
21
  },
22
22
  "include": ["src", "stencil.internal.d.ts"],
23
- "exclude": ["node_modules"]
23
+ "exclude": ["node_modules", "dist", "src/**/*.test.ts"]
24
24
  }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['src/**/*.test.ts'],
7
+ },
8
+ });
@@ -1 +0,0 @@
1
- export {};
@@ -1,193 +0,0 @@
1
- import { buildDocument, extractOntarioComponents, flattenShadowRoot, formatHtml, stripCssComments, stripHydrationArtifacts, transformHostSelectors, } from './utils.js';
2
- // ─── stripHydrationArtifacts ────────────────────────────────────────────────
3
- describe('stripHydrationArtifacts', () => {
4
- it('removes data-* attributes', () => {
5
- const input = '<div data-foo="bar" data-stencil-id="1">text</div>';
6
- expect(stripHydrationArtifacts(input)).not.toContain('data-');
7
- });
8
- it('removes s-id and c-id attributes', () => {
9
- const input = '<ontario-button s-id="1" c-id="2">click</ontario-button>';
10
- const result = stripHydrationArtifacts(input);
11
- expect(result).not.toContain('s-id');
12
- expect(result).not.toContain('c-id');
13
- });
14
- it('removes s-* and c-* scope attributes', () => {
15
- const input = '<span s-cr c-x="foo">text</span>';
16
- const result = stripHydrationArtifacts(input);
17
- expect(result).not.toMatch(/\bs-cr\b/);
18
- expect(result).not.toContain('c-x');
19
- });
20
- it('removes "hydrated" class name but preserves other classes', () => {
21
- const input = '<ontario-button class="hydrated my-class sc-ontario-button">click</ontario-button>';
22
- const result = stripHydrationArtifacts(input);
23
- expect(result).toContain('class="my-class"');
24
- expect(result).not.toContain('hydrated');
25
- expect(result).not.toContain('sc-ontario-button');
26
- });
27
- it('removes class attribute entirely when only artifact classes remain', () => {
28
- const input = '<div class="hydrated sc-foo"></div>';
29
- const result = stripHydrationArtifacts(input);
30
- expect(result).not.toContain('class=');
31
- });
32
- it('leaves markup without artifacts unchanged (aside from whitespace normalisation)', () => {
33
- const input = '<p>Hello world</p>';
34
- expect(stripHydrationArtifacts(input)).toBe('<p>Hello world</p>');
35
- });
36
- });
37
- // ─── flattenShadowRoot ───────────────────────────────────────────────────────
38
- describe('flattenShadowRoot', () => {
39
- it('removes <mock:shadow-root> open and close tags', () => {
40
- const input = '<ontario-button><mock:shadow-root><button>click</button></mock:shadow-root></ontario-button>';
41
- const result = flattenShadowRoot(input);
42
- expect(result).not.toContain('mock:shadow-root');
43
- expect(result).toContain('<button>click</button>');
44
- });
45
- it('removes <template shadowroot> and </template>', () => {
46
- const input = '<div><template shadowroot="open"><p>inner</p></template></div>';
47
- const result = flattenShadowRoot(input);
48
- expect(result).not.toContain('<template');
49
- expect(result).not.toContain('</template>');
50
- expect(result).toContain('<p>inner</p>');
51
- });
52
- it('is a no-op on plain markup', () => {
53
- const input = '<p>No shadow DOM here</p>';
54
- expect(flattenShadowRoot(input)).toBe(input);
55
- });
56
- });
57
- // ─── transformHostSelectors ─────────────────────────────────────────────────
58
- describe('transformHostSelectors', () => {
59
- it('replaces :host with the component tag name', () => {
60
- const css = ':host { display: block; }';
61
- expect(transformHostSelectors(css, 'ontario-button')).toBe('ontario-button { display: block; }');
62
- });
63
- it('replaces multiple :host occurrences', () => {
64
- const css = ':host { color: red; } :host(:hover) { color: blue; }';
65
- const result = transformHostSelectors(css, 'ontario-input');
66
- expect(result).toBe('ontario-input { color: red; } ontario-input(:hover) { color: blue; }');
67
- });
68
- it('does not alter rules without :host', () => {
69
- const css = '.button { font-size: 16px; }';
70
- expect(transformHostSelectors(css, 'ontario-button')).toBe(css);
71
- });
72
- });
73
- // ─── stripCssComments ───────────────────────────────────────────────────────
74
- describe('stripCssComments', () => {
75
- it('removes single block comments', () => {
76
- const css = '/* This is a comment */ .foo { color: red; }';
77
- expect(stripCssComments(css)).toBe('.foo { color: red; }');
78
- });
79
- it('removes multi-line block comments', () => {
80
- const css = '/*\n * Multi-line\n * comment\n */\n.bar { display: flex; }';
81
- expect(stripCssComments(css)).toBe('.bar { display: flex; }');
82
- });
83
- it('normalises excess blank lines', () => {
84
- // The empty-line strip pass removes ALL blank lines; excess runs are then collapsed.
85
- // Four consecutive newlines become two after normalisation.
86
- const css = '.a {}\n\n\n\n\n.b {}';
87
- const result = stripCssComments(css);
88
- expect(result).not.toMatch(/\n{3,}/);
89
- expect(result).toContain('.a {}');
90
- expect(result).toContain('.b {}');
91
- });
92
- it('trims leading and trailing whitespace', () => {
93
- const css = ' .x { margin: 0; } ';
94
- expect(stripCssComments(css)).toBe('.x { margin: 0; }');
95
- });
96
- it('returns empty string for comment-only input', () => {
97
- expect(stripCssComments('/* nothing else */')).toBe('');
98
- });
99
- });
100
- // ─── extractOntarioComponents ───────────────────────────────────────────────
101
- describe('extractOntarioComponents', () => {
102
- it('extracts a single Ontario component', () => {
103
- const markup = '<ontario-button type="primary">Click</ontario-button>';
104
- expect(extractOntarioComponents(markup)).toEqual(['ontario-button']);
105
- });
106
- it('extracts multiple distinct components', () => {
107
- const markup = '<ontario-button>A</ontario-button><ontario-input name="x"></ontario-input>';
108
- const result = extractOntarioComponents(markup);
109
- expect(result).toContain('ontario-button');
110
- expect(result).toContain('ontario-input');
111
- expect(result).toHaveLength(2);
112
- });
113
- it('deduplicates repeated components', () => {
114
- const markup = '<ontario-button>A</ontario-button><ontario-button>B</ontario-button>';
115
- expect(extractOntarioComponents(markup)).toEqual(['ontario-button']);
116
- });
117
- it('normalises ontario-icon-* variants to ontario-icon', () => {
118
- const markup = '<ontario-icon-warning></ontario-icon-warning><ontario-icon-info></ontario-icon-info>';
119
- expect(extractOntarioComponents(markup)).toEqual(['ontario-icon']);
120
- });
121
- it('returns an empty array when no Ontario components are present', () => {
122
- expect(extractOntarioComponents('<div><p>Hello</p></div>')).toEqual([]);
123
- });
124
- });
125
- // ─── buildDocument ───────────────────────────────────────────────────────────
126
- describe('buildDocument', () => {
127
- it('wraps markup in a style block when styles are provided', () => {
128
- const result = buildDocument('<p>hello</p>', 'p { color: red; }');
129
- expect(result).toBe('<style>\np { color: red; }\n</style>\n\n<p>hello</p>');
130
- });
131
- it('returns bare markup when styles are empty', () => {
132
- expect(buildDocument('<p>hello</p>', '')).toBe('<p>hello</p>');
133
- });
134
- it('returns a complete HTML document when fullDocument is true', () => {
135
- const result = buildDocument('<main><p>hello</p></main>', 'p { color: red; }', true);
136
- expect(result).toContain('<!DOCTYPE html>');
137
- expect(result).toContain('<html lang="en">');
138
- expect(result).toContain('<head>');
139
- expect(result).toContain('<body>');
140
- expect(result).toContain('<style>');
141
- expect(result).toContain('<main><p>hello</p></main>');
142
- });
143
- });
144
- // ─── formatHtml ──────────────────────────────────────────────────────────────
145
- describe('formatHtml', () => {
146
- it('removes DOCTYPE, html, head, and body wrappers', () => {
147
- const input = '<!doctype html><html><head><title>x</title></head><body><p>content</p></body></html>';
148
- const result = formatHtml(input);
149
- expect(result).not.toMatch(/<!doctype/i);
150
- expect(result).not.toMatch(/<html/i);
151
- expect(result).not.toMatch(/<head/i);
152
- expect(result).not.toMatch(/<body/i);
153
- expect(result).toContain('<p>');
154
- });
155
- it('removes HTML comments', () => {
156
- const input = '<div><!-- comment --><p>text</p></div>';
157
- expect(formatHtml(input)).not.toContain('<!--');
158
- });
159
- it('removes <style> tags by default', () => {
160
- const input = '<style>body { color: red; }</style><p>text</p>';
161
- expect(formatHtml(input)).not.toContain('<style>');
162
- });
163
- it('preserves <style> tags when removeStyles is false', () => {
164
- const input = '<style>body { color: red; }</style><p>text</p>';
165
- expect(formatHtml(input, false, false)).toContain('<style>');
166
- });
167
- it('indents nested tags with tabs', () => {
168
- const input = '<div><p>text</p></div>';
169
- const result = formatHtml(input);
170
- expect(result).toContain('\t<p>');
171
- });
172
- it('strips outer component tag when stripOuterComponent is true', () => {
173
- const input = '<ontario-button><button class="btn">Click</button></ontario-button>';
174
- const result = formatHtml(input, true);
175
- expect(result).not.toMatch(/^<ontario-button/);
176
- expect(result).toContain('<button');
177
- });
178
- it('preserves outer component tag when stripOuterComponent is false', () => {
179
- const input = '<ontario-button><button>Click</button></ontario-button>';
180
- const result = formatHtml(input, false);
181
- expect(result).toMatch(/^<ontario-button/);
182
- });
183
- it('handles self-closing tags without increasing indent', () => {
184
- const input = '<div><img src="a.png" /><p>text</p></div>';
185
- const result = formatHtml(input);
186
- const lines = result.split('\n');
187
- const imgLine = lines.find((l) => l.includes('<img'));
188
- const pLine = lines.find((l) => l.includes('<p>'));
189
- // Both img and p should be at the same indent level (one tab inside div)
190
- expect(imgLine === null || imgLine === void 0 ? void 0 : imgLine.startsWith('\t')).toBe(true);
191
- expect(pLine === null || pLine === void 0 ? void 0 : pLine.startsWith('\t')).toBe(true);
192
- });
193
- });
package/jest.config.mjs DELETED
@@ -1,21 +0,0 @@
1
- /** @type {import('jest').Config} */
2
- export default {
3
- preset: 'ts-jest',
4
- testEnvironment: 'node',
5
- // Map .js extension imports in source to their .ts counterparts
6
- moduleNameMapper: {
7
- '^(\\.{1,2}/.*)\\.js$': '$1',
8
- },
9
- transform: {
10
- '^.+\\.tsx?$': [
11
- 'ts-jest',
12
- {
13
- tsconfig: {
14
- module: 'CommonJS',
15
- moduleResolution: 'node',
16
- },
17
- },
18
- ],
19
- },
20
- testMatch: ['**/src/**/*.test.ts'],
21
- };