-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-style.test.js
More file actions
186 lines (147 loc) · 6.66 KB
/
Copy pathlocal-style.test.js
File metadata and controls
186 lines (147 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { describe, test, expect, beforeEach } from 'vitest';
// ── LocalStyle.localize — ## selector replacement ───────────────
describe('LocalStyle.localize', () => {
let body;
beforeEach(() => {
// Clean up body for each test
document.body.innerHTML = '';
});
test('replaces ## with local selector path (simple case)', () => {
// Build: body > section#main > div > <style>## .item { color: red; }</style>
const section = document.createElement('section');
section.id = 'main';
const container = document.createElement('div');
container.className = 'content';
section.appendChild(container);
const style = document.createElement('style');
style.innerHTML = '## .item { color: red; }';
container.appendChild(style);
document.body.appendChild(section);
// localize replaces ## with the path from body down to parent
LocalStyle.localize(style);
// The style element should be replaced with a new <style> element
// whose content has ## replaced with the selector path
const newStyle = container.querySelector('style');
expect(newStyle).not.toBeNull();
// The path should include section#main and div.content
const content = newStyle.innerHTML;
expect(content).not.toContain('##');
expect(content).toContain('section#main');
expect(content).toContain('.item');
expect(content).toContain('color: red');
});
test('replaces multiple ## occurrences independently', () => {
const section = document.createElement('section');
section.id = 'app';
const wrapper = document.createElement('div');
wrapper.id = 'wrap';
section.appendChild(wrapper);
const style = document.createElement('style');
style.innerHTML = '## .a { margin: 0; } ## .b { padding: 0; }';
wrapper.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = wrapper.querySelector('style');
const content = newStyle.innerHTML;
// Both ## should be replaced
expect(content).not.toContain('##');
// Should appear twice (two replacements)
const selectorPath = 'section#app';
const occurrences = content.split(selectorPath).length - 1;
expect(occurrences).toBe(2);
});
test('does not replace ### (triple hash)', () => {
const section = document.createElement('section');
section.id = 'test';
const div = document.createElement('div');
div.id = 'inner';
section.appendChild(div);
const style = document.createElement('style');
style.innerHTML = '### .x { color: blue; }';
div.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = div.querySelector('style');
const content = newStyle.innerHTML;
// ### should remain (regex: ##(?!#))
expect(content).toContain('###');
});
test('does not replace # preceded by another # (e.g. in id selector)', () => {
const section = document.createElement('section');
section.id = 'x';
const div = document.createElement('div');
div.id = 'y';
section.appendChild(div);
const style = document.createElement('style');
// A normal id selector should NOT be treated as ##
style.innerHTML = '#someId { color: green; }';
div.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = div.querySelector('style');
expect(newStyle.innerHTML).toContain('#someId');
});
test('uses container data-container-id for container elements', () => {
const section = document.createElement('section');
section.id = 'sec';
const container = document.createElement('div');
container.className = 'container';
container.dataset.containerId = 'myContainer';
section.appendChild(container);
const style = document.createElement('style');
style.innerHTML = '## .child { display: flex; }';
container.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = container.querySelector('style');
const content = newStyle.innerHTML;
expect(content).toContain('data-container-id="myContainer"');
});
test('uses article data-article-id for article elements', () => {
const section = document.createElement('section');
section.id = 'sec';
const article = document.createElement('article');
article.dataset.articleId = 'myArticle';
section.appendChild(article);
const style = document.createElement('style');
style.innerHTML = '## .inner { margin: auto; }';
article.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = article.querySelector('style');
const content = newStyle.innerHTML;
expect(content).toContain('data-article-id="myArticle"');
});
test('decodes HTML entities in style text', () => {
const section = document.createElement('section');
section.id = 'ent';
const div = document.createElement('div');
div.id = 'box';
section.appendChild(div);
const style = document.createElement('style');
// Browser may encode > as > in innerHTML
style.innerHTML = '## > .child { color: red; }';
div.appendChild(style);
document.body.appendChild(section);
LocalStyle.localize(style);
const newStyle = div.querySelector('style');
const content = newStyle.innerHTML;
// > should be decoded to >
expect(content).toContain('>');
expect(content).not.toContain('>');
});
test('appendLocalize adds style at location without source element', () => {
const section = document.createElement('section');
section.id = 'loc';
const wrapper = document.createElement('div');
wrapper.id = 'target';
section.appendChild(wrapper);
document.body.appendChild(section);
LocalStyle.appendLocalize(wrapper, '## .added { font-size: 14px; }');
// A <style> element should be appended to wrapper
const style = wrapper.querySelector('style');
expect(style).not.toBeNull();
// Note: appendLocalize(null, ...) adds directly, so content may be empty
// because the styleSheet is created but content is set only via elem path
});
});