|
7 | 7 | inDom, |
8 | 8 | shadowHostInDom, |
9 | 9 | getShadowHost, |
| 10 | + getNestedRule, |
10 | 11 | } from '../src/utils'; |
11 | 12 |
|
12 | 13 | describe('Utilities for other modules', () => { |
@@ -143,4 +144,103 @@ describe('Utilities for other modules', () => { |
143 | 144 | expect(inDom(a.childNodes[0])).toBeTruthy(); |
144 | 145 | }); |
145 | 146 | }); |
| 147 | + |
| 148 | + describe('getNestedRule()', () => { |
| 149 | + let styleElement: HTMLStyleElement; |
| 150 | + let stylesheet: CSSStyleSheet; |
| 151 | + |
| 152 | + beforeEach(() => { |
| 153 | + // Create a style element with nested CSS rules for testing |
| 154 | + styleElement = document.createElement('style'); |
| 155 | + document.head.appendChild(styleElement); |
| 156 | + stylesheet = styleElement.sheet as CSSStyleSheet; |
| 157 | + }); |
| 158 | + |
| 159 | + afterEach(() => { |
| 160 | + document.head.removeChild(styleElement); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should return a top-level rule with single index [N]', () => { |
| 164 | + stylesheet.insertRule('.rule0 { color: red; }', 0); |
| 165 | + stylesheet.insertRule('.rule1 { color: blue; }', 1); |
| 166 | + stylesheet.insertRule('.rule2 { color: green; }', 2); |
| 167 | + |
| 168 | + const rule0 = getNestedRule(stylesheet.cssRules, [0]); |
| 169 | + expect((rule0 as CSSStyleRule).selectorText).toBe('.rule0'); |
| 170 | + |
| 171 | + const rule1 = getNestedRule(stylesheet.cssRules, [1]); |
| 172 | + expect((rule1 as CSSStyleRule).selectorText).toBe('.rule1'); |
| 173 | + |
| 174 | + const rule2 = getNestedRule(stylesheet.cssRules, [2]); |
| 175 | + expect((rule2 as CSSStyleRule).selectorText).toBe('.rule2'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should return a rule nested inside @media with index [0, N]', () => { |
| 179 | + // Insert @media rule with nested rules |
| 180 | + stylesheet.insertRule( |
| 181 | + '@media (min-width: 100px) { .rule0 { color: red; } .rule1 { color: blue; } .rule2 { color: green; } }', |
| 182 | + 0, |
| 183 | + ); |
| 184 | + |
| 185 | + const mediaRule = stylesheet.cssRules[0] as CSSMediaRule; |
| 186 | + expect(mediaRule.cssRules.length).toBe(3); |
| 187 | + |
| 188 | + // Access nested rules using [0, N] where 0 is the @media index |
| 189 | + const nestedRule0 = getNestedRule(stylesheet.cssRules, [0, 0]); |
| 190 | + expect((nestedRule0 as CSSStyleRule).selectorText).toBe('.rule0'); |
| 191 | + |
| 192 | + const nestedRule1 = getNestedRule(stylesheet.cssRules, [0, 1]); |
| 193 | + expect((nestedRule1 as CSSStyleRule).selectorText).toBe('.rule1'); |
| 194 | + |
| 195 | + const nestedRule2 = getNestedRule(stylesheet.cssRules, [0, 2]); |
| 196 | + expect((nestedRule2 as CSSStyleRule).selectorText).toBe('.rule2'); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should return a rule nested inside @supports with index [0, N]', () => { |
| 200 | + // Insert @supports rule with nested rules |
| 201 | + stylesheet.insertRule( |
| 202 | + '@supports (display: grid) { .grid-rule { display: grid; } }', |
| 203 | + 0, |
| 204 | + ); |
| 205 | + |
| 206 | + const supportsRule = stylesheet.cssRules[0] as CSSSupportsRule; |
| 207 | + expect(supportsRule.cssRules.length).toBe(1); |
| 208 | + |
| 209 | + const nestedRule = getNestedRule(stylesheet.cssRules, [0, 0]); |
| 210 | + expect((nestedRule as CSSStyleRule).selectorText).toBe('.grid-rule'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should return a doubly nested rule with index [0, 0, N]', () => { |
| 214 | + // Insert @media containing @supports containing a rule |
| 215 | + stylesheet.insertRule( |
| 216 | + '@media (min-width: 100px) { @supports (display: grid) { .nested-rule { color: red; } } }', |
| 217 | + 0, |
| 218 | + ); |
| 219 | + |
| 220 | + const mediaRule = stylesheet.cssRules[0] as CSSMediaRule; |
| 221 | + const supportsRule = mediaRule.cssRules[0] as CSSSupportsRule; |
| 222 | + expect(supportsRule.cssRules.length).toBe(1); |
| 223 | + |
| 224 | + // Access doubly nested rule using [0, 0, 0] |
| 225 | + const nestedRule = getNestedRule(stylesheet.cssRules, [0, 0, 0]); |
| 226 | + expect((nestedRule as CSSStyleRule).selectorText).toBe('.nested-rule'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('should handle @media at different indices in stylesheet', () => { |
| 230 | + // Insert some top-level rules first |
| 231 | + stylesheet.insertRule('.top-level { color: black; }', 0); |
| 232 | + stylesheet.insertRule( |
| 233 | + '@media (min-width: 100px) { .inside-media { color: red; } }', |
| 234 | + 1, |
| 235 | + ); |
| 236 | + |
| 237 | + // Top-level rule at index 0 |
| 238 | + const topLevel = getNestedRule(stylesheet.cssRules, [0]); |
| 239 | + expect((topLevel as CSSStyleRule).selectorText).toBe('.top-level'); |
| 240 | + |
| 241 | + // Nested rule: @media at index 1, rule at index 0 inside |
| 242 | + const insideMedia = getNestedRule(stylesheet.cssRules, [1, 0]); |
| 243 | + expect((insideMedia as CSSStyleRule).selectorText).toBe('.inside-media'); |
| 244 | + }); |
| 245 | + }); |
146 | 246 | }); |
0 commit comments