|
| 1 | +/* generate from test/specs/nesting.spec.ts */ |
| 2 | +import {expect as f} from '../../node_modules/@esm-bundle/chai/esm/chai.js'; |
| 3 | +import {parse, render} from '../../dist/web/index.js'; |
| 4 | + |
| 5 | +describe('flatten nested css rules', function () { |
| 6 | + it('flatten #1', function () { |
| 7 | + const nesting1 = ` |
| 8 | +
|
| 9 | +.foo { |
| 10 | + color: blue; |
| 11 | + &Bar { color: red; } |
| 12 | +} |
| 13 | +`; |
| 14 | + return parse(nesting1, { |
| 15 | + minify: true, nestingRules: true |
| 16 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.foo { |
| 17 | + color: blue |
| 18 | +} |
| 19 | +Bar.foo { |
| 20 | + color: red |
| 21 | +}`)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('flatten #2', function () { |
| 25 | + const nesting1 = ` |
| 26 | +.header { |
| 27 | + background-color: blue; |
| 28 | + & p { |
| 29 | + font-size: 16px; |
| 30 | + & span { |
| 31 | + &:hover { |
| 32 | + color: green |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +`; |
| 38 | + return parse(nesting1, { |
| 39 | + minify: true, nestingRules: true |
| 40 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.header { |
| 41 | + background-color: blue |
| 42 | +} |
| 43 | +.header p { |
| 44 | + font-size: 16px |
| 45 | +} |
| 46 | +.header p span:hover { |
| 47 | + color: green |
| 48 | +}`)); |
| 49 | + }); |
| 50 | + |
| 51 | + it('flatten with at-rule #3', function () { |
| 52 | + const nesting1 = ` |
| 53 | +.foo { |
| 54 | + display: grid; |
| 55 | + |
| 56 | + @media (orientation: landscape) { |
| 57 | + & { |
| 58 | + grid-auto-flow: column; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +`; |
| 63 | + return parse(nesting1, { |
| 64 | + minify: true, nestingRules: true |
| 65 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.foo { |
| 66 | + display: grid |
| 67 | +} |
| 68 | +@media (orientation:landscape) { |
| 69 | + .foo { |
| 70 | + grid-auto-flow: column |
| 71 | + } |
| 72 | +}`)); |
| 73 | + }); |
| 74 | + |
| 75 | + it('flatten with at-rule #4', function () { |
| 76 | + const nesting1 = ` |
| 77 | +.foo { |
| 78 | + display: grid; |
| 79 | +
|
| 80 | + @media (orientation: landscape) { |
| 81 | + grid-auto-flow: column; |
| 82 | + } |
| 83 | +} |
| 84 | +`; |
| 85 | + return parse(nesting1, { |
| 86 | + minify: true, nestingRules: true |
| 87 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.foo { |
| 88 | + display: grid |
| 89 | +} |
| 90 | +@media (orientation:landscape) { |
| 91 | + .foo { |
| 92 | + grid-auto-flow: column |
| 93 | + } |
| 94 | +}`)); |
| 95 | + }); |
| 96 | + |
| 97 | + it('flatten with at-rule #5', function () { |
| 98 | + const nesting1 = ` |
| 99 | +.foo { |
| 100 | + display: grid; |
| 101 | +
|
| 102 | + @media (orientation: landscape) { |
| 103 | + grid-auto-flow: column; |
| 104 | +
|
| 105 | + @media (min-width > 1024px) { |
| 106 | + max-inline-size: 1024px; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +`; |
| 111 | + return parse(nesting1, { |
| 112 | + minify: true, nestingRules: true |
| 113 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.foo { |
| 114 | + display: grid |
| 115 | +} |
| 116 | +@media (orientation:landscape) { |
| 117 | + .foo { |
| 118 | + grid-auto-flow: column |
| 119 | + } |
| 120 | +} |
| 121 | +@media (orientation:landscape) and (min-width >1024px) { |
| 122 | + .foo { |
| 123 | + max-inline-size: 1024px |
| 124 | + } |
| 125 | +}`)); |
| 126 | + }); |
| 127 | + |
| 128 | + it('flatten with at-rule #6', function () { |
| 129 | + const nesting1 = ` |
| 130 | +html { |
| 131 | + @layer base { |
| 132 | + block-size: 100%; |
| 133 | +
|
| 134 | + @layer support { |
| 135 | + & body { |
| 136 | + min-block-size: 100%; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +`; |
| 142 | + return parse(nesting1, { |
| 143 | + minify: true, nestingRules: true |
| 144 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`@layer base { |
| 145 | + html { |
| 146 | + block-size: 100% |
| 147 | + } |
| 148 | +} |
| 149 | +@layer base.support { |
| 150 | + html body { |
| 151 | + min-block-size: 100% |
| 152 | + } |
| 153 | +}`)); |
| 154 | + }); |
| 155 | + |
| 156 | + it('flatten with at-rule #7', function () { |
| 157 | + const nesting1 = ` |
| 158 | +html { |
| 159 | + @layer base { |
| 160 | + block-size: 100%; |
| 161 | +
|
| 162 | + @layer support { |
| 163 | + body { |
| 164 | + min-block-size: 100%; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +`; |
| 170 | + return parse(nesting1, { |
| 171 | + minify: true, nestingRules: true |
| 172 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`@layer base { |
| 173 | + html { |
| 174 | + block-size: 100% |
| 175 | + } |
| 176 | +} |
| 177 | +@layer base.support { |
| 178 | + html body { |
| 179 | + min-block-size: 100% |
| 180 | + } |
| 181 | +}`)); |
| 182 | + }); |
| 183 | + |
| 184 | + it('flatten with at-rule #8', function () { |
| 185 | + const nesting1 = ` |
| 186 | +.card { |
| 187 | + inline-size: 40ch; |
| 188 | + aspect-ratio: 3/4; |
| 189 | +
|
| 190 | + @scope (&) { |
| 191 | + :scope { |
| 192 | + border: 1px solid white; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | +`; |
| 197 | + return parse(nesting1, { |
| 198 | + minify: true, nestingRules: true |
| 199 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.card { |
| 200 | + inline-size: 40ch; |
| 201 | + aspect-ratio: 3/4 |
| 202 | +} |
| 203 | +@scope (.card) { |
| 204 | + :scope { |
| 205 | + border: 1px solid #fff |
| 206 | + } |
| 207 | +}`)); |
| 208 | + }); |
| 209 | + |
| 210 | + it('flatten with at-rule #9', function () { |
| 211 | + const nesting1 = ` |
| 212 | +.foo { |
| 213 | + color: blue; |
| 214 | + && { padding: 2ch; } |
| 215 | +} |
| 216 | +`; |
| 217 | + return parse(nesting1, { |
| 218 | + minify: true, nestingRules: true |
| 219 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`.foo { |
| 220 | + color: blue |
| 221 | +} |
| 222 | +.foo.foo { |
| 223 | + padding: 2ch |
| 224 | +}`)); |
| 225 | + }); |
| 226 | + |
| 227 | + it('flatten with at-rule #10', function () { |
| 228 | + const nesting1 = ` |
| 229 | +
|
| 230 | +@scope (.card) to (> header) { |
| 231 | + :scope { |
| 232 | + inline-size: 40ch; |
| 233 | + aspect-ratio: 3/4; |
| 234 | +
|
| 235 | + > header { |
| 236 | + border-block-end: 1px solid white; |
| 237 | + } |
| 238 | + } |
| 239 | +} |
| 240 | +`; |
| 241 | + return parse(nesting1, { |
| 242 | + minify: true, nestingRules: true |
| 243 | + }).then((result) => f(render(result.ast, {minify: false, expandNestingRules: true}).code).equals(`@scope (.card) to (>header) { |
| 244 | + :scope { |
| 245 | + inline-size: 40ch; |
| 246 | + aspect-ratio: 3/4 |
| 247 | + } |
| 248 | + :scope>header { |
| 249 | + border-block-end: 1px solid #fff |
| 250 | + } |
| 251 | +}`)); |
| 252 | + }); |
| 253 | +}); |
0 commit comments