diff --git a/README.md b/README.md index d233d18..53c3531 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # glory-svelte-preprocess -A svelte preprocess for safely minimizing CSS class footprint for unbeliveable performance gain. :rocket: :rocket: :rocket: +A svelte preprocess minimize CSS class footprint with statical analysis for unbeliveable performance gain. :rocket: :rocket: :rocket: + +## Disclaimers + +Although this preprocess has been tested extensively, this is not stable yet and expect a bug or two given the complexity of the whole process. Do open an issue and let me know if something go south, and I will try to fix as fast as I can. ## tldr; diff --git a/src/helper.js b/src/helper.js index ae60cdf..254089f 100644 --- a/src/helper.js +++ b/src/helper.js @@ -210,27 +210,32 @@ export const matchWithSelector = (element, selector) => { case "AttributeSelector": { const attr = getAttribute(element, selector.name.name); const attrValue = attr.value[0]; + const unquoted = selector.value.value.replace(/(^["']|["']$)/g, ""); switch (selector.matcher) { case "=": { - return ( - attrValue.raw === selector.value.value.replace(/(^["']|["']$)/g, "") - ); + return attrValue.raw === unquoted; } - // TODO: Unhandled + // TODO: Fix as it will match hyphen as well case "~=": { + return new RegExp(`\\b${unquoted}\\b`).test(attrValue.raw) } + // TODO: Fix as it will match hyphen as well case "|=": { + return new RegExp(`\\b${unquoted}\\b`).test(attrValue.raw) } case "^=": { + return attrValue.raw.startsWith(unquoted); } case "$=": { + return attrValue.raw.endsWith(unquoted); } case "*=": { + return attrValue.raw.includes(unquoted); } default: { diff --git a/src/transformer.js b/src/transformer.js index c8c46f6..8168c96 100644 --- a/src/transformer.js +++ b/src/transformer.js @@ -41,7 +41,6 @@ const isTargetElement = (selectorNode, node, linker) => { } } else { const isMatch = matchWithSelector(curNode, selector); - console.log(combinator, isMatch, curNode, selector); if (isMatch) { selector = r.prev(); matchCount++; diff --git a/test/issue2.spec.js b/test/issue2.spec.js index 9c3505f..c739e87 100644 --- a/test/issue2.spec.js +++ b/test/issue2.spec.js @@ -249,33 +249,6 @@ describe("when given a rule with pseudo element", function () { }); }); -describe("when given a rule with attribute selector", function () { - it("should add class to the correct tag", function () { - const code = ` -
`; - - const filename = "/src/routes/index.svelte"; - - const result = wrappedPreprocessor(code, filename).code; - - expect(result.replace(/\s/g, "")).toBe( - ` - -`.replace(/\s/g, "") - ); - }); -}); - describe("when given a rule with :not pseudo selector", function () { it("should transform the html correctly", function () { const code = ` diff --git a/test/issue3.spec.js b/test/issue3.spec.js new file mode 100644 index 0000000..5f407d7 --- /dev/null +++ b/test/issue3.spec.js @@ -0,0 +1,176 @@ +import wrappedPreprocessor from "./wrapper.js"; + +describe("when given a rule with attribute selector", function () { + describe("when given a matcher of =", function () { + it("should add class to the correct tag", function () { + const code = ` +`; + + const filename = "/src/routes/index.svelte"; + + const result = wrappedPreprocessor(code, filename).code; + + expect(result.replace(/\s/g, "")).toBe( + ` + +`.replace(/\s/g, "") + ); + }); + }); +}); + +describe("when given a rule with attribute selector", function () { + describe("when given a matcher of ^=", () => { + it("should add class to the correct tag", function () { + const code = ` +