|
| 1 | +import wrappedPreprocessor from "./wrapper.js"; |
| 2 | + |
| 3 | +describe("when given a rule with attribute selector", function () { |
| 4 | + describe("when given a matcher of =", function () { |
| 5 | + it("should add class to the correct tag", function () { |
| 6 | + const code = ` |
| 7 | +<style> |
| 8 | +[href="https://example.org"]{ |
| 9 | + color: #ff3e00; |
| 10 | +} |
| 11 | +</style><div><a href="https://example.org"></a></div>`; |
| 12 | + |
| 13 | + const filename = "/src/routes/index.svelte"; |
| 14 | + |
| 15 | + const result = wrappedPreprocessor(code, filename).code; |
| 16 | + |
| 17 | + expect(result.replace(/\s/g, "")).toBe( |
| 18 | + `<style> |
| 19 | + :global(.a){ |
| 20 | + color: #ff3e00; |
| 21 | + } |
| 22 | + </style> |
| 23 | + <div> |
| 24 | + <a href="https://example.org" class="a"></a> |
| 25 | + </div> |
| 26 | +`.replace(/\s/g, "") |
| 27 | + ); |
| 28 | + }); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("when given a rule with attribute selector", function () { |
| 33 | + describe("when given a matcher of ^=", () => { |
| 34 | + it("should add class to the correct tag", function () { |
| 35 | + const code = ` |
| 36 | +<style> |
| 37 | +[title^="hello"]{ |
| 38 | + color: #ff3e00; |
| 39 | +} |
| 40 | +</style><div><div><img title="hello world" /></div></div>`; |
| 41 | + |
| 42 | + const filename = "/src/routes/index.svelte"; |
| 43 | + |
| 44 | + const result = wrappedPreprocessor(code, filename).code; |
| 45 | + |
| 46 | + expect(result.replace(/\s/g, "")).toBe( |
| 47 | + `<style> |
| 48 | + :global(.a){ |
| 49 | + color: #ff3e00; |
| 50 | + } |
| 51 | + </style> |
| 52 | + <div> |
| 53 | + <div><img title="hello world" class="a" /></div> |
| 54 | + </div> |
| 55 | +`.replace(/\s/g, "") |
| 56 | + ); |
| 57 | + }); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("when given a rule with attribute selector", function () { |
| 62 | + describe("when given a matcher of $=", () => { |
| 63 | + it("should add class to the correct tag", function () { |
| 64 | + const code = ` |
| 65 | +<style> |
| 66 | +[href$="org"]{ |
| 67 | + color: #ff3e00; |
| 68 | +} |
| 69 | +</style><div><a href="https://example.org"></a><a href="http://hello.org"></a></div>`; |
| 70 | + |
| 71 | + const filename = "/src/routes/index.svelte"; |
| 72 | + |
| 73 | + const result = wrappedPreprocessor(code, filename).code; |
| 74 | + |
| 75 | + expect(result.replace(/\s/g, "")).toBe( |
| 76 | + `<style> |
| 77 | + :global(.a){ |
| 78 | + color: #ff3e00; |
| 79 | + } |
| 80 | + </style> |
| 81 | + <div> |
| 82 | + <a href="https://example.org" class="a"></a> |
| 83 | + <a href="http://hello.org" class="a"></a> |
| 84 | + </div> |
| 85 | +`.replace(/\s/g, "") |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("when given a rule with attribute selector", function () { |
| 92 | + describe("when given a matcher of *=", () => { |
| 93 | + it("should add class to the correct tag", function () { |
| 94 | + const code = ` |
| 95 | +<style> |
| 96 | +[title*="bar"]{ |
| 97 | + color: #ff3e00; |
| 98 | +} |
| 99 | +</style><div><img title="hellobarworld" /></div>`; |
| 100 | + |
| 101 | + const filename = "/src/routes/index.svelte"; |
| 102 | + |
| 103 | + const result = wrappedPreprocessor(code, filename).code; |
| 104 | + |
| 105 | + expect(result.replace(/\s/g, "")).toBe( |
| 106 | + `<style> |
| 107 | + :global(.a){ |
| 108 | + color: #ff3e00; |
| 109 | + } |
| 110 | + </style> |
| 111 | + <div> |
| 112 | + <img title="hellobarworld" class="a"/> |
| 113 | + </div> |
| 114 | +`.replace(/\s/g, "") |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe("when given a rule with attribute selector", function () { |
| 121 | + describe("when given a matcher of ~=", () => { |
| 122 | + it("should add class to the correct tag", function () { |
| 123 | + const code = ` |
| 124 | +<style> |
| 125 | +[title~="bar"]{ |
| 126 | + color: #ff3e00; |
| 127 | +} |
| 128 | +</style><div><img title="hello bar world" /></div>`; |
| 129 | + |
| 130 | + const filename = "/src/routes/index.svelte"; |
| 131 | + |
| 132 | + const result = wrappedPreprocessor(code, filename).code; |
| 133 | + |
| 134 | + expect(result.replace(/\s/g, "")).toBe( |
| 135 | + `<style> |
| 136 | + :global(.a){ |
| 137 | + color: #ff3e00; |
| 138 | + } |
| 139 | + </style> |
| 140 | + <div> |
| 141 | + <img title="hello bar world" class="a"/> |
| 142 | + </div> |
| 143 | +`.replace(/\s/g, "") |
| 144 | + ); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe("when given a rule with attribute selector", function () { |
| 150 | + describe("when given a matcher of |=", () => { |
| 151 | + it("should add class to the correct tag", function () { |
| 152 | + const code = ` |
| 153 | +<style> |
| 154 | +[title|="friend"]{ |
| 155 | + color: #ff3e00; |
| 156 | +} |
| 157 | +</style><div><img title="foo-friend-bar" /></div>`; |
| 158 | + |
| 159 | + const filename = "/src/routes/index.svelte"; |
| 160 | + |
| 161 | + const result = wrappedPreprocessor(code, filename).code; |
| 162 | + |
| 163 | + expect(result.replace(/\s/g, "")).toBe( |
| 164 | + `<style> |
| 165 | + :global(.a){ |
| 166 | + color: #ff3e00; |
| 167 | + } |
| 168 | + </style> |
| 169 | + <div> |
| 170 | + <img title="foo-friend-bar" class="a"/> |
| 171 | + </div> |
| 172 | +`.replace(/\s/g, "") |
| 173 | + ); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments