-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { appendTags, appendTagsForReact } from "./append-tags"; | ||
import { COMPILED_BACK_TICK_JSX_WITH_PREFLIGHT_TAGS } from "@/mocks/compiled-jsx"; | ||
import { HTML } from "@/mocks/html"; | ||
|
||
describe("append-tags", () => { | ||
describe("appendTagsForReact", () => { | ||
it("should work", async () => { | ||
const transformedCode = appendTagsForReact("test-id")( | ||
COMPILED_BACK_TICK_JSX_WITH_PREFLIGHT_TAGS, | ||
); | ||
expect(transformedCode).toEqual( | ||
expect.objectContaining({ | ||
code: expect.stringContaining(`className: "test-id`), | ||
}), | ||
); | ||
}); | ||
}); | ||
|
||
describe("appendTags", () => { | ||
it("should work", async () => { | ||
const transformedCode = appendTags("test-id")(HTML); | ||
expect(transformedCode).toEqual( | ||
expect.objectContaining({ | ||
code: expect.stringContaining(`class="test-id`), | ||
}), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { appendClass, appendClassForReact } from "./append-classes"; | ||
import { PREFLIGHT_AFFECTED_TAGS } from "./postcss/preflight"; | ||
|
||
export const appendTagsForReact = (id: string) => (code: string) => { | ||
let currCode = code; | ||
for (const tag of PREFLIGHT_AFFECTED_TAGS) { | ||
const regex = new RegExp(`"${tag}", {(?:.|\\s)+?}\\)`, "g"); | ||
const matches = [...currCode.matchAll(regex)].map((m) => m[0]); | ||
|
||
for (const match of matches) { | ||
const output = appendClassForReact(id)(match); | ||
|
||
if (output.code === match) { | ||
// That means that className is not present | ||
// We need to add it | ||
currCode = currCode.replace( | ||
match, | ||
match.replace(`, {`, `, {\nclassName: "${id}",`), | ||
); | ||
} else { | ||
currCode = currCode.replace(match, output.code); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
code: currCode, | ||
map: null, | ||
}; | ||
}; | ||
|
||
export const appendTags = (id: string) => (code: string) => { | ||
let currCode = code; | ||
for (const tag of PREFLIGHT_AFFECTED_TAGS) { | ||
const regex = new RegExp(`<${tag}.*>[^<]*</${tag}>`, "g"); | ||
const matches = [...currCode.matchAll(regex)].map((m) => m[0]); | ||
|
||
for (const match of matches) { | ||
const output = appendClass(id)(match); | ||
|
||
if (output.code === match) { | ||
// That means that className is not present | ||
// We need to add it | ||
currCode = currCode.replace( | ||
match, | ||
match.replace(`<${tag}`, `<${tag} class="${id}"`), | ||
); | ||
} else { | ||
currCode = currCode.replace(match, output.code); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
code: currCode, | ||
map: null, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const PREFLIGHT_AFFECTED_TAGS = new Set([ | ||
"blockquote", | ||
"dl", | ||
"dd", | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6", | ||
"hr", | ||
"figure", | ||
"p", | ||
"pr", | ||
"ol", | ||
"ul", | ||
"img", | ||
"svg", | ||
"video", | ||
"canvas", | ||
"audio", | ||
"iframe", | ||
"embed", | ||
"object", | ||
]); |