-
Notifications
You must be signed in to change notification settings - Fork 831
feat(export): inline computed iframe styles for WeChat paste #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TuYv
wants to merge
8
commits into
nexu-io:main
Choose a base branch
from
TuYv:feat/wechat-computed-style-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+713
−11
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
37813b2
feat(export): inline computed iframe styles for WeChat paste
f5d11b3
fix(export): preserve ::before/::after content in WeChat computed-sty…
TuYv 1392515
chore: add yaduo docker deployment
TuYv 8b7cd7a
fix(export): keep WeChat pseudo-element styles aligned
ce362f1
chore: remove unrelated deployment changes
4732ad8
fix(export): preserve all deck slides for WeChat
3179f3c
fix(export): render complete WeChat document offscreen
9f2f982
fix(export): stabilize rendered WeChat clipboard output
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,213 @@ | ||
| import { describe, it, expect, afterEach } from "vitest"; | ||
| import { toWechatHtmlFromDocument } from "../wechat"; | ||
|
|
||
| function parseFragment(html: string): HTMLBodyElement { | ||
| return new DOMParser().parseFromString(`<body>${html}</body>`, "text/html") | ||
| .body as HTMLBodyElement; | ||
| } | ||
|
|
||
| describe("toWechatHtmlFromDocument", () => { | ||
| afterEach(() => { | ||
| document.head.innerHTML = ""; | ||
| document.body.innerHTML = ""; | ||
| }); | ||
|
|
||
| it("inlines computed styles from the rendered preview DOM", () => { | ||
| document.head.innerHTML = ` | ||
| <style> | ||
| .card { | ||
| background: rgb(12, 34, 56); | ||
| border-radius: 18px; | ||
| padding: 24px; | ||
| } | ||
| .title { | ||
| color: rgb(210, 55, 44); | ||
| font-size: 32px; | ||
| font-weight: 800; | ||
| line-height: 1.25; | ||
| } | ||
| </style> | ||
| `; | ||
| document.body.innerHTML = ` | ||
| <article class="card"> | ||
| <h1 class="title">Styled headline</h1> | ||
| </article> | ||
| `; | ||
|
|
||
| const body = parseFragment(toWechatHtmlFromDocument(document)); | ||
| const section = body.querySelector("section"); | ||
| const card = body.querySelector("article"); | ||
| const title = body.querySelector("h1"); | ||
|
|
||
| expect(section?.getAttribute("data-tool")).toBe("html-anything"); | ||
| expect(card?.getAttribute("data-tool")).toBe("html-anything"); | ||
| expect(card?.getAttribute("style")).toContain("background-color: rgb(12, 34, 56)"); | ||
| expect(card?.getAttribute("style")).toContain("border-radius: 18px"); | ||
| expect(card?.getAttribute("style")).toContain("padding: 24px"); | ||
| expect(title?.getAttribute("style")).toContain("color: rgb(210, 55, 44)"); | ||
| expect(title?.getAttribute("style")).toContain("font-size: 32px"); | ||
| expect(title?.getAttribute("style")).toContain("font-weight: 800"); | ||
| }); | ||
|
|
||
| it("drops fragile page-layout styles so WeChat paste stays in article flow", () => { | ||
| document.head.innerHTML = ` | ||
| <style> | ||
| .layout { | ||
| position: absolute; | ||
| display: grid; | ||
| grid-template-columns: 320px 1fr; | ||
| width: 1280px; | ||
| height: 720px; | ||
| gap: 48px; | ||
| background: rgb(250, 248, 240); | ||
| padding: 40px; | ||
| } | ||
| .panel { | ||
| display: flex; | ||
| min-height: 360px; | ||
| color: rgb(25, 28, 32); | ||
| border: 2px solid rgb(80, 90, 100); | ||
| } | ||
| </style> | ||
| `; | ||
| document.body.innerHTML = ` | ||
| <section class="layout"> | ||
| <p class="panel">First paragraph</p> | ||
| <p class="panel">Second paragraph</p> | ||
| </section> | ||
| `; | ||
|
|
||
| const html = toWechatHtmlFromDocument(document); | ||
| const body = parseFragment(html); | ||
| const layoutStyle = body.querySelector(".layout")?.getAttribute("style") ?? ""; | ||
| const panelStyle = body.querySelector(".panel")?.getAttribute("style") ?? ""; | ||
|
|
||
| expect(layoutStyle).toContain("background-color: rgb(250, 248, 240)"); | ||
| expect(layoutStyle).toContain("padding: 40px"); | ||
| expect(panelStyle).toContain("color: rgb(25, 28, 32)"); | ||
| expect(panelStyle).toContain("border-top: 2px solid rgb(80, 90, 100)"); | ||
| expect(`${layoutStyle}; ${panelStyle}`).not.toMatch( | ||
| /(?:^|;\s*)(position|display|grid-template-columns|width|height|min-height|gap|flex-direction|flex-wrap|flex):/, | ||
| ); | ||
| }); | ||
|
|
||
| it("materializes ::before and ::after content into real DOM nodes", () => { | ||
| document.body.innerHTML = ` | ||
| <ul> | ||
| <li class="check">Item one</li> | ||
| </ul> | ||
| <p class="tier">Pro plan</p> | ||
| `; | ||
|
|
||
| const original = window.getComputedStyle.bind(window); | ||
| const stub = ((el: Element, pseudo?: string | null) => { | ||
| const base = original(el); | ||
| if (!pseudo) return base; | ||
| const overrides: Record<string, string> = {}; | ||
| if (pseudo === "::before" && (el as HTMLElement).matches?.(".check")) { | ||
| overrides.content = '"✓"'; | ||
| overrides.color = "rgb(255, 0, 0)"; | ||
| } else if (pseudo === "::before" && (el as HTMLElement).matches?.(".tier")) { | ||
| overrides.content = '"Recommended"'; | ||
| overrides.color = "rgb(255, 255, 255)"; | ||
| } else if (pseudo === "::after" && (el as HTMLElement).matches?.(".tier")) { | ||
| overrides.content = '"★"'; | ||
| } | ||
| return new Proxy(base, { | ||
| get(target, prop) { | ||
| if (prop === "getPropertyValue") { | ||
| return (name: string) => overrides[name] ?? target.getPropertyValue(name); | ||
| } | ||
| const value = (target as unknown as Record<string | symbol, unknown>)[prop]; | ||
| return typeof value === "function" ? (value as () => unknown).bind(target) : value; | ||
| }, | ||
| }); | ||
| }) as typeof window.getComputedStyle; | ||
| window.getComputedStyle = stub; | ||
|
|
||
| try { | ||
| const body = parseFragment(toWechatHtmlFromDocument(document)); | ||
| const check = body.querySelector("li.check"); | ||
| const tier = body.querySelector("p.tier"); | ||
|
|
||
| const checkBefore = check?.firstElementChild; | ||
| expect(checkBefore?.getAttribute("data-pseudo")).toBe("::before"); | ||
| expect(checkBefore?.textContent).toBe("✓"); | ||
| expect(checkBefore?.getAttribute("style") ?? "").toContain("color: rgb(255, 0, 0)"); | ||
|
|
||
| const tierBefore = tier?.firstElementChild; | ||
| expect(tierBefore?.getAttribute("data-pseudo")).toBe("::before"); | ||
| expect(tierBefore?.textContent).toBe("Recommended"); | ||
|
|
||
| const tierAfter = tier?.lastElementChild; | ||
| expect(tierAfter?.getAttribute("data-pseudo")).toBe("::after"); | ||
| expect(tierAfter?.textContent).toBe("★"); | ||
| } finally { | ||
| window.getComputedStyle = original as typeof window.getComputedStyle; | ||
| } | ||
| }); | ||
|
|
||
| it("keeps descendant styles aligned when an ancestor has generated content", () => { | ||
| document.body.innerHTML = ` | ||
| <div class="card"><span class="label">Hello</span></div> | ||
| `; | ||
|
|
||
| const original = window.getComputedStyle.bind(window); | ||
| const stub = ((el: Element, pseudo?: string | null) => { | ||
| const base = original(el); | ||
| const overrides: Record<string, string> = {}; | ||
| if (pseudo === "::before" && (el as HTMLElement).matches?.(".card")) { | ||
| overrides.content = '"Badge"'; | ||
| } else if (!pseudo && (el as HTMLElement).matches?.(".label")) { | ||
| overrides.color = "rgb(255, 0, 0)"; | ||
| } | ||
| return new Proxy(base, { | ||
| get(target, prop) { | ||
| if (prop === "getPropertyValue") { | ||
| return (name: string) => overrides[name] ?? target.getPropertyValue(name); | ||
| } | ||
| const value = (target as unknown as Record<string | symbol, unknown>)[prop]; | ||
| return typeof value === "function" ? (value as () => unknown).bind(target) : value; | ||
| }, | ||
| }); | ||
| }) as typeof window.getComputedStyle; | ||
| window.getComputedStyle = stub; | ||
|
|
||
| try { | ||
| const body = parseFragment(toWechatHtmlFromDocument(document)); | ||
| const card = body.querySelector(".card"); | ||
| const pseudo = card?.querySelector("[data-pseudo='::before']"); | ||
| const label = card?.querySelector(".label"); | ||
|
|
||
| expect(pseudo?.textContent).toBe("Badge"); | ||
| expect(pseudo?.getAttribute("style") ?? "").not.toContain("color: rgb(255, 0, 0)"); | ||
| expect(label?.getAttribute("style") ?? "").toContain("color: rgb(255, 0, 0)"); | ||
| } finally { | ||
| window.getComputedStyle = original as typeof window.getComputedStyle; | ||
| } | ||
| }); | ||
|
|
||
| it("clamps oversized spacing and drops negative margins", () => { | ||
| document.head.innerHTML = ` | ||
| <style> | ||
| .loose { | ||
| margin-top: -24px; | ||
| margin-bottom: 180px; | ||
| padding: 96px; | ||
| color: rgb(40, 40, 40); | ||
| } | ||
| </style> | ||
| `; | ||
| document.body.innerHTML = `<p class="loose">Too much spacing</p>`; | ||
|
|
||
| const body = parseFragment(toWechatHtmlFromDocument(document)); | ||
| const style = body.querySelector("p")?.getAttribute("style") ?? ""; | ||
|
|
||
| expect(style).toContain("margin-bottom: 48px"); | ||
| expect(style).toContain("padding: 48px"); | ||
| expect(style).toContain("color: rgb(40, 40, 40)"); | ||
| expect(style).not.toContain("-24px"); | ||
| expect(style).not.toContain("180px"); | ||
| expect(style).not.toContain("96px"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.