@ongov/ontario-design-system-component-html-generator 8.1.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/LICENSE +201 -0
- package/README.md +401 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +138 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +130 -0
- package/dist/sample-config.d.ts +3 -0
- package/dist/sample-config.js +26 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +41 -0
- package/dist/utils.js +139 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +193 -0
- package/jest.config.mjs +21 -0
- package/package.json +53 -0
- package/src/cli.ts +179 -0
- package/src/components.d.ts +24 -0
- package/src/index.ts +199 -0
- package/src/sample-config.ts +36 -0
- package/src/types.ts +114 -0
- package/src/utils.test.ts +241 -0
- package/src/utils.ts +158 -0
- package/stencil.config.ts +12 -0
- package/stencil.internal.d.ts +3 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Default sample definitions used by the package CLI.
|
|
3
|
+
*
|
|
4
|
+
* This file intentionally contains data only. Consumers using the library API
|
|
5
|
+
* can provide their own `ComponentSample[]` instead of relying on this default set.
|
|
6
|
+
*/
|
|
7
|
+
import type { ComponentSample } from './types.js';
|
|
8
|
+
|
|
9
|
+
export type { ComponentSample } from './types.js';
|
|
10
|
+
|
|
11
|
+
export const samples: ComponentSample[] = [
|
|
12
|
+
{
|
|
13
|
+
component: 'ontario-button',
|
|
14
|
+
html: '<ontario-button type="primary" label="Click me"></ontario-button>',
|
|
15
|
+
outputFile: 'ontario-button.html',
|
|
16
|
+
description: 'Primary button from Ontario Design System',
|
|
17
|
+
includeStyles: true,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
component: 'ontario-accordion',
|
|
21
|
+
html: `<ontario-accordion
|
|
22
|
+
name="My Accordion"
|
|
23
|
+
expand-collapse-button='{
|
|
24
|
+
"expandAllSectionsLabel": "Expand All",
|
|
25
|
+
"collapseAllSectionsLabel": "Collapse All"
|
|
26
|
+
}'
|
|
27
|
+
accordion-data='[
|
|
28
|
+
{"label": "Section 1", "content": "Content for section 1"},
|
|
29
|
+
{"label": "Section 2", "content": "Content for section 2"}
|
|
30
|
+
]'
|
|
31
|
+
></ontario-accordion>`,
|
|
32
|
+
outputFile: 'ontario-accordion.html',
|
|
33
|
+
description: 'Accordion component from Ontario Design System',
|
|
34
|
+
includeStyles: true,
|
|
35
|
+
},
|
|
36
|
+
];
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Public type contracts for `@ongov/app-sample-generator`.
|
|
3
|
+
*
|
|
4
|
+
* These interfaces define the library-first API:
|
|
5
|
+
* callers provide a configuration object and receive a deterministic result object,
|
|
6
|
+
* while transport concerns (filesystem, HTTP, etc.) are handled by consumers.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* A single sample definition provided to the generator.
|
|
10
|
+
*
|
|
11
|
+
* This describes what should be rendered and how output should be named,
|
|
12
|
+
* without prescribing where or how the output is consumed.
|
|
13
|
+
*/
|
|
14
|
+
export interface ComponentSample {
|
|
15
|
+
/** Ontario component tag name (for example: `ontario-button`). */
|
|
16
|
+
component: string;
|
|
17
|
+
/** Input HTML snippet to render/hydrate. */
|
|
18
|
+
html: string;
|
|
19
|
+
/** Output filename consumers can use when persisting generated results. */
|
|
20
|
+
outputFile: string;
|
|
21
|
+
/** Optional human-readable description for tooling or docs. */
|
|
22
|
+
description?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Controls whether component styles are collected for this sample.
|
|
25
|
+
* Defaults to `true` when omitted.
|
|
26
|
+
*/
|
|
27
|
+
includeStyles?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Configuration contract for the library API.
|
|
32
|
+
*
|
|
33
|
+
* The library accepts this shape and always returns a `SampleGeneratorResult`.
|
|
34
|
+
* Callers decide where to write or display the returned data.
|
|
35
|
+
*/
|
|
36
|
+
export interface SampleGeneratorConfig {
|
|
37
|
+
/** List of samples to process in this invocation. */
|
|
38
|
+
samples: ComponentSample[];
|
|
39
|
+
/**
|
|
40
|
+
* Optional formatter behavior used when cleaning rendered HTML.
|
|
41
|
+
* Defaults preserve existing CLI behavior when omitted.
|
|
42
|
+
*/
|
|
43
|
+
formatterOptions?: SampleFormatterOptions;
|
|
44
|
+
/**
|
|
45
|
+
* Optional rendering strategy override.
|
|
46
|
+
* Use this to plug in a custom renderer instead of the default hydrate renderer.
|
|
47
|
+
*/
|
|
48
|
+
renderer?: (html: string) => Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Optional style loading strategy override.
|
|
51
|
+
* Use this to source CSS from custom locations or pipelines.
|
|
52
|
+
*/
|
|
53
|
+
styleLoader?: (tagName: string) => string | Promise<string>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Controls post-render HTML cleanup behavior.
|
|
58
|
+
*/
|
|
59
|
+
export interface SampleFormatterOptions {
|
|
60
|
+
/**
|
|
61
|
+
* When true, unwraps the outer Ontario component and returns only inner content.
|
|
62
|
+
* Defaults to `true`.
|
|
63
|
+
*/
|
|
64
|
+
stripOuterComponent?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* When true, removes `<style>` tags from rendered markup before formatting.
|
|
67
|
+
* Defaults to `true`.
|
|
68
|
+
*/
|
|
69
|
+
removeStyles?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* When true, wraps output in a full HTML document for browser preview:
|
|
72
|
+
* `<!DOCTYPE html>`, `<html>`, `<head>`, and `<body>`.
|
|
73
|
+
* Defaults to `false`.
|
|
74
|
+
*/
|
|
75
|
+
fullDocument?: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Per-sample generation outcome.
|
|
80
|
+
*
|
|
81
|
+
* On success, `markup`/`styles`/`renderedHtml` are populated.
|
|
82
|
+
* On failure, `error` is populated.
|
|
83
|
+
*/
|
|
84
|
+
export interface GeneratedSampleItem {
|
|
85
|
+
/** Original input sample associated with this result item. */
|
|
86
|
+
sample: ComponentSample;
|
|
87
|
+
/** Whether generation succeeded for this sample. */
|
|
88
|
+
success: boolean;
|
|
89
|
+
/** Clean rendered markup with framework artifacts removed. */
|
|
90
|
+
markup?: string;
|
|
91
|
+
/** Collected CSS for the sample and nested Ontario components. */
|
|
92
|
+
styles?: string;
|
|
93
|
+
/** Combined document string (style block + markup) for convenience/legacy consumers. */
|
|
94
|
+
renderedHtml?: string;
|
|
95
|
+
/** Error message when generation fails. */
|
|
96
|
+
error?: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Aggregate library response for a generation run.
|
|
101
|
+
*/
|
|
102
|
+
export interface SampleGeneratorResult {
|
|
103
|
+
/** Per-sample outcomes in the same processing order as input. */
|
|
104
|
+
items: GeneratedSampleItem[];
|
|
105
|
+
/** Quick run-level counts for reporting and CI checks. */
|
|
106
|
+
summary: {
|
|
107
|
+
/** Number of samples attempted. */
|
|
108
|
+
total: number;
|
|
109
|
+
/** Number of successful sample generations. */
|
|
110
|
+
succeeded: number;
|
|
111
|
+
/** Number of failed sample generations. */
|
|
112
|
+
failed: number;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildDocument,
|
|
3
|
+
extractOntarioComponents,
|
|
4
|
+
flattenShadowRoot,
|
|
5
|
+
formatHtml,
|
|
6
|
+
stripCssComments,
|
|
7
|
+
stripHydrationArtifacts,
|
|
8
|
+
transformHostSelectors,
|
|
9
|
+
} from './utils.js';
|
|
10
|
+
|
|
11
|
+
// ─── stripHydrationArtifacts ────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
describe('stripHydrationArtifacts', () => {
|
|
14
|
+
it('removes data-* attributes', () => {
|
|
15
|
+
const input = '<div data-foo="bar" data-stencil-id="1">text</div>';
|
|
16
|
+
expect(stripHydrationArtifacts(input)).not.toContain('data-');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('removes s-id and c-id attributes', () => {
|
|
20
|
+
const input = '<ontario-button s-id="1" c-id="2">click</ontario-button>';
|
|
21
|
+
const result = stripHydrationArtifacts(input);
|
|
22
|
+
expect(result).not.toContain('s-id');
|
|
23
|
+
expect(result).not.toContain('c-id');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('removes s-* and c-* scope attributes', () => {
|
|
27
|
+
const input = '<span s-cr c-x="foo">text</span>';
|
|
28
|
+
const result = stripHydrationArtifacts(input);
|
|
29
|
+
expect(result).not.toMatch(/\bs-cr\b/);
|
|
30
|
+
expect(result).not.toContain('c-x');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('removes "hydrated" class name but preserves other classes', () => {
|
|
34
|
+
const input = '<ontario-button class="hydrated my-class sc-ontario-button">click</ontario-button>';
|
|
35
|
+
const result = stripHydrationArtifacts(input);
|
|
36
|
+
expect(result).toContain('class="my-class"');
|
|
37
|
+
expect(result).not.toContain('hydrated');
|
|
38
|
+
expect(result).not.toContain('sc-ontario-button');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('removes class attribute entirely when only artifact classes remain', () => {
|
|
42
|
+
const input = '<div class="hydrated sc-foo"></div>';
|
|
43
|
+
const result = stripHydrationArtifacts(input);
|
|
44
|
+
expect(result).not.toContain('class=');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('leaves markup without artifacts unchanged (aside from whitespace normalisation)', () => {
|
|
48
|
+
const input = '<p>Hello world</p>';
|
|
49
|
+
expect(stripHydrationArtifacts(input)).toBe('<p>Hello world</p>');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// ─── flattenShadowRoot ───────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
describe('flattenShadowRoot', () => {
|
|
56
|
+
it('removes <mock:shadow-root> open and close tags', () => {
|
|
57
|
+
const input = '<ontario-button><mock:shadow-root><button>click</button></mock:shadow-root></ontario-button>';
|
|
58
|
+
const result = flattenShadowRoot(input);
|
|
59
|
+
expect(result).not.toContain('mock:shadow-root');
|
|
60
|
+
expect(result).toContain('<button>click</button>');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('removes <template shadowroot> and </template>', () => {
|
|
64
|
+
const input = '<div><template shadowroot="open"><p>inner</p></template></div>';
|
|
65
|
+
const result = flattenShadowRoot(input);
|
|
66
|
+
expect(result).not.toContain('<template');
|
|
67
|
+
expect(result).not.toContain('</template>');
|
|
68
|
+
expect(result).toContain('<p>inner</p>');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('is a no-op on plain markup', () => {
|
|
72
|
+
const input = '<p>No shadow DOM here</p>';
|
|
73
|
+
expect(flattenShadowRoot(input)).toBe(input);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// ─── transformHostSelectors ─────────────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
describe('transformHostSelectors', () => {
|
|
80
|
+
it('replaces :host with the component tag name', () => {
|
|
81
|
+
const css = ':host { display: block; }';
|
|
82
|
+
expect(transformHostSelectors(css, 'ontario-button')).toBe('ontario-button { display: block; }');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('replaces multiple :host occurrences', () => {
|
|
86
|
+
const css = ':host { color: red; } :host(:hover) { color: blue; }';
|
|
87
|
+
const result = transformHostSelectors(css, 'ontario-input');
|
|
88
|
+
expect(result).toBe('ontario-input { color: red; } ontario-input(:hover) { color: blue; }');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('does not alter rules without :host', () => {
|
|
92
|
+
const css = '.button { font-size: 16px; }';
|
|
93
|
+
expect(transformHostSelectors(css, 'ontario-button')).toBe(css);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ─── stripCssComments ───────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
describe('stripCssComments', () => {
|
|
100
|
+
it('removes single block comments', () => {
|
|
101
|
+
const css = '/* This is a comment */ .foo { color: red; }';
|
|
102
|
+
expect(stripCssComments(css)).toBe('.foo { color: red; }');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('removes multi-line block comments', () => {
|
|
106
|
+
const css = '/*\n * Multi-line\n * comment\n */\n.bar { display: flex; }';
|
|
107
|
+
expect(stripCssComments(css)).toBe('.bar { display: flex; }');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('normalises excess blank lines', () => {
|
|
111
|
+
// The empty-line strip pass removes ALL blank lines; excess runs are then collapsed.
|
|
112
|
+
// Four consecutive newlines become two after normalisation.
|
|
113
|
+
const css = '.a {}\n\n\n\n\n.b {}';
|
|
114
|
+
const result = stripCssComments(css);
|
|
115
|
+
expect(result).not.toMatch(/\n{3,}/);
|
|
116
|
+
expect(result).toContain('.a {}');
|
|
117
|
+
expect(result).toContain('.b {}');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('trims leading and trailing whitespace', () => {
|
|
121
|
+
const css = ' .x { margin: 0; } ';
|
|
122
|
+
expect(stripCssComments(css)).toBe('.x { margin: 0; }');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('returns empty string for comment-only input', () => {
|
|
126
|
+
expect(stripCssComments('/* nothing else */')).toBe('');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ─── extractOntarioComponents ───────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
describe('extractOntarioComponents', () => {
|
|
133
|
+
it('extracts a single Ontario component', () => {
|
|
134
|
+
const markup = '<ontario-button type="primary">Click</ontario-button>';
|
|
135
|
+
expect(extractOntarioComponents(markup)).toEqual(['ontario-button']);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('extracts multiple distinct components', () => {
|
|
139
|
+
const markup = '<ontario-button>A</ontario-button><ontario-input name="x"></ontario-input>';
|
|
140
|
+
const result = extractOntarioComponents(markup);
|
|
141
|
+
expect(result).toContain('ontario-button');
|
|
142
|
+
expect(result).toContain('ontario-input');
|
|
143
|
+
expect(result).toHaveLength(2);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('deduplicates repeated components', () => {
|
|
147
|
+
const markup = '<ontario-button>A</ontario-button><ontario-button>B</ontario-button>';
|
|
148
|
+
expect(extractOntarioComponents(markup)).toEqual(['ontario-button']);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('normalises ontario-icon-* variants to ontario-icon', () => {
|
|
152
|
+
const markup = '<ontario-icon-warning></ontario-icon-warning><ontario-icon-info></ontario-icon-info>';
|
|
153
|
+
expect(extractOntarioComponents(markup)).toEqual(['ontario-icon']);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('returns an empty array when no Ontario components are present', () => {
|
|
157
|
+
expect(extractOntarioComponents('<div><p>Hello</p></div>')).toEqual([]);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ─── buildDocument ───────────────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
describe('buildDocument', () => {
|
|
164
|
+
it('wraps markup in a style block when styles are provided', () => {
|
|
165
|
+
const result = buildDocument('<p>hello</p>', 'p { color: red; }');
|
|
166
|
+
expect(result).toBe('<style>\np { color: red; }\n</style>\n\n<p>hello</p>');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('returns bare markup when styles are empty', () => {
|
|
170
|
+
expect(buildDocument('<p>hello</p>', '')).toBe('<p>hello</p>');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('returns a complete HTML document when fullDocument is true', () => {
|
|
174
|
+
const result = buildDocument('<main><p>hello</p></main>', 'p { color: red; }', true);
|
|
175
|
+
expect(result).toContain('<!DOCTYPE html>');
|
|
176
|
+
expect(result).toContain('<html lang="en">');
|
|
177
|
+
expect(result).toContain('<head>');
|
|
178
|
+
expect(result).toContain('<body>');
|
|
179
|
+
expect(result).toContain('<style>');
|
|
180
|
+
expect(result).toContain('<main><p>hello</p></main>');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// ─── formatHtml ──────────────────────────────────────────────────────────────
|
|
185
|
+
|
|
186
|
+
describe('formatHtml', () => {
|
|
187
|
+
it('removes DOCTYPE, html, head, and body wrappers', () => {
|
|
188
|
+
const input = '<!doctype html><html><head><title>x</title></head><body><p>content</p></body></html>';
|
|
189
|
+
const result = formatHtml(input);
|
|
190
|
+
expect(result).not.toMatch(/<!doctype/i);
|
|
191
|
+
expect(result).not.toMatch(/<html/i);
|
|
192
|
+
expect(result).not.toMatch(/<head/i);
|
|
193
|
+
expect(result).not.toMatch(/<body/i);
|
|
194
|
+
expect(result).toContain('<p>');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('removes HTML comments', () => {
|
|
198
|
+
const input = '<div><!-- comment --><p>text</p></div>';
|
|
199
|
+
expect(formatHtml(input)).not.toContain('<!--');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('removes <style> tags by default', () => {
|
|
203
|
+
const input = '<style>body { color: red; }</style><p>text</p>';
|
|
204
|
+
expect(formatHtml(input)).not.toContain('<style>');
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('preserves <style> tags when removeStyles is false', () => {
|
|
208
|
+
const input = '<style>body { color: red; }</style><p>text</p>';
|
|
209
|
+
expect(formatHtml(input, false, false)).toContain('<style>');
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('indents nested tags with tabs', () => {
|
|
213
|
+
const input = '<div><p>text</p></div>';
|
|
214
|
+
const result = formatHtml(input);
|
|
215
|
+
expect(result).toContain('\t<p>');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('strips outer component tag when stripOuterComponent is true', () => {
|
|
219
|
+
const input = '<ontario-button><button class="btn">Click</button></ontario-button>';
|
|
220
|
+
const result = formatHtml(input, true);
|
|
221
|
+
expect(result).not.toMatch(/^<ontario-button/);
|
|
222
|
+
expect(result).toContain('<button');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('preserves outer component tag when stripOuterComponent is false', () => {
|
|
226
|
+
const input = '<ontario-button><button>Click</button></ontario-button>';
|
|
227
|
+
const result = formatHtml(input, false);
|
|
228
|
+
expect(result).toMatch(/^<ontario-button/);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('handles self-closing tags without increasing indent', () => {
|
|
232
|
+
const input = '<div><img src="a.png" /><p>text</p></div>';
|
|
233
|
+
const result = formatHtml(input);
|
|
234
|
+
const lines = result.split('\n');
|
|
235
|
+
const imgLine = lines.find((l) => l.includes('<img'));
|
|
236
|
+
const pLine = lines.find((l) => l.includes('<p>'));
|
|
237
|
+
// Both img and p should be at the same indent level (one tab inside div)
|
|
238
|
+
expect(imgLine?.startsWith('\t')).toBe(true);
|
|
239
|
+
expect(pLine?.startsWith('\t')).toBe(true);
|
|
240
|
+
});
|
|
241
|
+
});
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Pure utility functions for HTML and CSS processing.
|
|
3
|
+
*
|
|
4
|
+
* These are isolated from I/O and rendering so they can be unit-tested directly.
|
|
5
|
+
* All functions are stateless and deterministic.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Removes Stencil hydration artifacts from rendered markup:
|
|
9
|
+
* - `data-*` attributes
|
|
10
|
+
* - `s-id` / `c-id` attributes
|
|
11
|
+
* - `s-*` / `c-*` scope attributes
|
|
12
|
+
* - `hydrated` and `sc-*` class names
|
|
13
|
+
*/
|
|
14
|
+
export function stripHydrationArtifacts(markup: string): string {
|
|
15
|
+
let cleaned = markup;
|
|
16
|
+
|
|
17
|
+
// Remove all data-* attributes
|
|
18
|
+
cleaned = cleaned.replace(/\sdata-[^=\s]+="[^"]*"/g, '');
|
|
19
|
+
|
|
20
|
+
// Remove Stencil hydration attributes (s-id, c-id, etc.)
|
|
21
|
+
cleaned = cleaned.replace(/\s[sc]-id="[^"]*"/g, '');
|
|
22
|
+
|
|
23
|
+
// Remove Stencil scope attributes
|
|
24
|
+
cleaned = cleaned.replace(/\s[sc]-[a-z0-9-]+(?:="[^"]*")?/g, '');
|
|
25
|
+
|
|
26
|
+
// Clean up class attributes — remove `hydrated` and `sc-*` classes
|
|
27
|
+
cleaned = cleaned.replace(/\sclass="([^"]*)"/g, (_match, classValue: string) => {
|
|
28
|
+
const filtered = classValue.split(/\s+/).filter((name) => name && name !== 'hydrated' && !name.startsWith('sc-'));
|
|
29
|
+
return filtered.length ? ` class="${filtered.join(' ')}"` : '';
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Remove extra whitespace before closing angle brackets
|
|
33
|
+
return cleaned.replace(/\s+>/g, '>').replace(/\s+/g, ' ');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Removes mock shadow DOM wrappers that Stencil injects during testing,
|
|
38
|
+
* and strips declarative shadow DOM `<template shadowroot>` elements.
|
|
39
|
+
*/
|
|
40
|
+
export function flattenShadowRoot(markup: string): string {
|
|
41
|
+
return markup
|
|
42
|
+
.replace(/<mock:shadow-root\b[^>]*>/g, '')
|
|
43
|
+
.replace(/<\/mock:shadow-root>/g, '')
|
|
44
|
+
.replace(/<template\b[^>]*shadowroot[^>]*>/gi, '')
|
|
45
|
+
.replace(/<\/template>/gi, '');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Replaces `:host` CSS selectors with the actual component tag name so that
|
|
50
|
+
* scoped styles work correctly when inlined into a static HTML document.
|
|
51
|
+
*/
|
|
52
|
+
export function transformHostSelectors(css: string, tagName: string): string {
|
|
53
|
+
return css.replace(/:host\b/g, tagName);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Strips block CSS comments (`/* ... *\/`) and normalises excessive blank lines.
|
|
58
|
+
*/
|
|
59
|
+
export function stripCssComments(css: string): string {
|
|
60
|
+
let cleaned = css.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
61
|
+
cleaned = cleaned.replace(/^\s*[\r\n]/gm, '');
|
|
62
|
+
cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
|
|
63
|
+
return cleaned.trim();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extracts unique Ontario component tag names from an HTML string.
|
|
68
|
+
* All `ontario-icon-*` variants are normalised to `ontario-icon`.
|
|
69
|
+
*/
|
|
70
|
+
export function extractOntarioComponents(markup: string): string[] {
|
|
71
|
+
const componentRegex = /<(ontario-[a-z-]+)/gi;
|
|
72
|
+
const matches = markup.matchAll(componentRegex);
|
|
73
|
+
const components = new Set<string>();
|
|
74
|
+
|
|
75
|
+
for (const match of matches) {
|
|
76
|
+
let component = match[1].toLowerCase();
|
|
77
|
+
if (component.startsWith('ontario-icon-')) {
|
|
78
|
+
component = 'ontario-icon';
|
|
79
|
+
}
|
|
80
|
+
components.add(component);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return Array.from(components);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Wraps markup in a `<style>` block when styles are provided.
|
|
88
|
+
* Returns bare markup when there are no styles.
|
|
89
|
+
* Optionally emits a complete HTML document when `fullDocument` is true.
|
|
90
|
+
*/
|
|
91
|
+
export function buildDocument(markup: string, styles: string, fullDocument = false): string {
|
|
92
|
+
if (fullDocument) {
|
|
93
|
+
const styleBlock = styles ? `\n\t\t<style>\n${styles}\n\t\t</style>` : '';
|
|
94
|
+
|
|
95
|
+
return `<!DOCTYPE html>\n<html lang="en">\n\t<head>\n\t\t<meta charset="utf-8">\n\t\t<meta name="viewport" content="width=device-width, initial-scale=1">${styleBlock}\n\t</head>\n\t<body>\n${markup}\n\t</body>\n</html>`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!styles) {
|
|
99
|
+
return markup;
|
|
100
|
+
}
|
|
101
|
+
return `<style>\n${styles}\n</style>\n\n${markup}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Formats rendered HTML with consistent indentation and removes framework boilerplate:
|
|
106
|
+
* - Strips `<!doctype>`, `<html>`, `<head>`, `<body>` wrappers
|
|
107
|
+
* - Optionally removes `<style>` tags
|
|
108
|
+
* - Optionally unwraps the outermost Ontario component tag
|
|
109
|
+
* - Re-indents content with tabs
|
|
110
|
+
*/
|
|
111
|
+
export function formatHtml(html: string, stripOuterComponent = false, removeStyles = true): string {
|
|
112
|
+
let cleaned = html;
|
|
113
|
+
|
|
114
|
+
if (removeStyles) {
|
|
115
|
+
cleaned = cleaned.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
cleaned = cleaned.replace(/<!doctype[^>]*>/i, '');
|
|
119
|
+
cleaned = cleaned.replace(/<html[^>]*>/i, '');
|
|
120
|
+
cleaned = cleaned.replace(/<\/html>/i, '');
|
|
121
|
+
cleaned = cleaned.replace(/<head[^>]*>[\s\S]*?<\/head>/i, '');
|
|
122
|
+
cleaned = cleaned.replace(/<body[^>]*>/i, '');
|
|
123
|
+
cleaned = cleaned.replace(/<\/body>/i, '');
|
|
124
|
+
cleaned = cleaned.replace(/<!--[\s\S]*?-->/g, '');
|
|
125
|
+
|
|
126
|
+
if (stripOuterComponent) {
|
|
127
|
+
cleaned = cleaned.replace(/^[\s]*<[a-z]+-[a-z-]*[^>]*>[\s]*([\s\S]*?)[\s]*<\/[a-z]+-[a-z-]*>[\s]*$/i, '$1');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
cleaned = cleaned.trim();
|
|
131
|
+
|
|
132
|
+
const parts = cleaned.split(/(<[^>]+>)/);
|
|
133
|
+
const lines: string[] = [];
|
|
134
|
+
let indentLevel = 0;
|
|
135
|
+
|
|
136
|
+
for (const part of parts) {
|
|
137
|
+
if (!part.trim()) continue;
|
|
138
|
+
|
|
139
|
+
if (part.startsWith('</')) {
|
|
140
|
+
indentLevel = Math.max(0, indentLevel - 1);
|
|
141
|
+
lines.push('\t'.repeat(indentLevel) + part);
|
|
142
|
+
} else if (part.endsWith('/>') || /^<(br|hr|img|input|meta|link)[\s>]/i.test(part)) {
|
|
143
|
+
lines.push('\t'.repeat(indentLevel) + part);
|
|
144
|
+
} else if (part.startsWith('<')) {
|
|
145
|
+
lines.push('\t'.repeat(indentLevel) + part);
|
|
146
|
+
if (!part.match(/^<(br|hr|img|input|meta|link|area|base|embed|source|track|wbr)[\s>]/i)) {
|
|
147
|
+
indentLevel++;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
const trimmed = part.trim();
|
|
151
|
+
if (trimmed) {
|
|
152
|
+
lines.push('\t'.repeat(indentLevel) + trimmed);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return lines.join('\n');
|
|
158
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2019",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"lib": ["es2019", "dom"],
|
|
7
|
+
"types": ["node", "jest"],
|
|
8
|
+
"typeRoots": ["./node_modules/@types", "./"],
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"jsx": "react",
|
|
13
|
+
"jsxFactory": "h",
|
|
14
|
+
"jsxFragmentFactory": "Fragment",
|
|
15
|
+
"strict": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"forceConsistentCasingInFileNames": true,
|
|
18
|
+
"rootDir": "src",
|
|
19
|
+
"outDir": "dist",
|
|
20
|
+
"declaration": true
|
|
21
|
+
},
|
|
22
|
+
"include": ["src", "stencil.internal.d.ts"],
|
|
23
|
+
"exclude": ["node_modules"]
|
|
24
|
+
}
|