Repository navigation
fix(canvas): make a drawn frame's white a default a design's own background beats #492
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
Closed
Closed
Changes from 1 commit
Commits
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
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,48 @@ | ||
| /* The paper a drawn frame starts on. The Frame tool makes an empty document | ||
| white, and that white has to give way the moment a design brings a | ||
| background of its own: written inline on the body it won instead, so a dark | ||
| design put into a drawn frame sat on white, and the white showed as a light | ||
| line along the frame's edge wherever the design's edge pixel was blended | ||
| (a dark design on a dark canvas). As a `:where()` rule it has no | ||
| specificity, so any `body { background }` in the design beats it. */ | ||
|
|
||
| export const FRAME_PAPER_ATTR = 'data-doop-paper' | ||
| export const FRAME_PAPER_CSS = ':where(body){background:#ffffff}' | ||
| export const FRAME_PAPER_STYLE = `<style ${FRAME_PAPER_ATTR}>${FRAME_PAPER_CSS}</style>` | ||
|
|
||
| const BODY_TAG = /<body\b[^>]*>/i | ||
| const STYLE_ATTR = /\bstyle="([^"]*)"/i | ||
| /* the white as the Frame tool wrote it before, in the forms a style | ||
| attribute serializes it to */ | ||
| const INLINE_WHITE = /(^|;)\s*background(?:-color)?:\s*(?:rgb\(255,\s*255,\s*255\)|#fff(?:fff)?)\s*(?:;|$)/i | ||
| const STYLE_ELEMENT = /<style\b([^>]*)>([\s\S]*?)<\/style>/gi | ||
| const CSS_COMMENT = /\/\*[\s\S]*?\*\//g | ||
| /* a rule whose subject is the body (`body`, `html, body`, `body.dark`), not | ||
| one that only starts there (`body > main`), giving it a background */ | ||
| const BODY_BACKGROUND = | ||
| /(?:^|[{}\s,])body(?:[.:#[][^\s{},>+~]*)?\s*(?:,[^{}]*)?\{[^{}]*\bbackground(?:-color|-image)?\s*:/i | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| /** Whether one of the document's own stylesheets gives the body a background. */ | ||
| export function stylesBodyBackground(html: string): boolean { | ||
| for (const [, attrs = '', css = ''] of html.matchAll(STYLE_ELEMENT)) { | ||
| if (attrs.includes(FRAME_PAPER_ATTR)) continue | ||
| if (BODY_BACKGROUND.test(css.replace(CSS_COMMENT, ''))) return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| /** A frame document without the inline white an earlier Frame tool wrote on | ||
| * its body, where the design's own stylesheet gives the body a background | ||
| * that the inline white was overriding. Every other document comes back as | ||
| * it is, and only the body's style attribute is ever touched. */ | ||
| export function withoutPaperOverride(html: string): string { | ||
| const body = BODY_TAG.exec(html) | ||
| if (!body) return html | ||
| const style = STYLE_ATTR.exec(body[0]) | ||
| const declarations = style?.[1] | ||
| if (!style || declarations === undefined || !INLINE_WHITE.test(declarations)) return html | ||
| if (!stylesBodyBackground(html)) return html | ||
| const kept = declarations.replace(INLINE_WHITE, (_match, lead: string) => lead).trim() | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| const tag = body[0].replace(style[0], `style="${kept}"`) | ||
| return html.slice(0, body.index) + tag + html.slice(body.index + body[0].length) | ||
| } | ||
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
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
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,71 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { FRAME_PAPER_STYLE, stylesBodyBackground, withoutPaperOverride } from '../shared/framePaper' | ||
|
|
||
| const SHELL = 'width: 100%; height: 100%; margin: 0px;' | ||
| const WHITE = 'background: rgb(255, 255, 255);' | ||
| const legacy = (content: string, bodyStyle = `${SHELL} ${WHITE}`) => | ||
| `<!doctype html>\n<html style="${SHELL}"><head></head><body style="${bodyStyle}">${content}</body></html>` | ||
| const DARK_DESIGN = '<meta charset="utf-8"><style>*{margin:0}body{background:#101111;color:#fff}</style><main></main>' | ||
|
|
||
| describe('stylesBodyBackground', () => { | ||
| const styled = (css: string) => `<html><head><style>${css}</style></head><body></body></html>` | ||
|
|
||
| it('finds a rule whose subject is the body', () => { | ||
| for (const css of [ | ||
| 'body{background:#101111}', | ||
| 'html, body { background-color: black }', | ||
| '*{margin:0}body.dark{background-image:linear-gradient(#000,#111)}', | ||
| '@media (min-width:1px){ body { background: red } }', | ||
| ]) | ||
| expect(stylesBodyBackground(styled(css)), css).toBe(true) | ||
| }) | ||
|
|
||
| it('does not count the Frame tool paper itself', () => { | ||
| expect(stylesBodyBackground(`<html><head>${FRAME_PAPER_STYLE}</head><body></body></html>`)).toBe(false) | ||
| }) | ||
|
|
||
| it('passes over rules that are not the body getting a background', () => { | ||
| for (const css of [ | ||
| 'body{color:red}', | ||
| 'body > main{background:#000}', | ||
| 'tbody{background:#000}', | ||
| '.body{background:#000}', | ||
| '/* body{background:#000} */ main{background:#000}', | ||
| ]) | ||
| expect(stylesBodyBackground(styled(css)), css).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('withoutPaperOverride', () => { | ||
| it('drops the inline white where the design styles the body itself', () => { | ||
| const html = withoutPaperOverride(legacy(DARK_DESIGN)) | ||
| expect(html).toBe(legacy(DARK_DESIGN, SHELL)) | ||
| }) | ||
|
|
||
| it('reads the white in the forms a style attribute holds it', () => { | ||
| for (const white of ['background: #ffffff;', 'background:#fff', 'background-color: rgb(255,255,255);']) | ||
| expect(withoutPaperOverride(legacy(DARK_DESIGN, `${SHELL} ${white}`))).toBe(legacy(DARK_DESIGN, SHELL)) | ||
| }) | ||
|
|
||
| it('keeps the white of a drawn frame nothing has styled yet', () => { | ||
| const empty = legacy('') | ||
| expect(withoutPaperOverride(empty)).toBe(empty) | ||
| const content = legacy('<style>main{background:#101111}</style><main></main>') | ||
| expect(withoutPaperOverride(content)).toBe(content) | ||
| }) | ||
|
|
||
| it('keeps a body background that is not the Frame tool white', () => { | ||
| const tinted = legacy(DARK_DESIGN, `${SHELL} background: rgb(250, 240, 230);`) | ||
| expect(withoutPaperOverride(tinted)).toBe(tinted) | ||
| }) | ||
|
|
||
| it('leaves documents without a styled body, or without a body, alone', () => { | ||
| for (const html of ['<main>fragment</main>', '<html><body><p>plain</p></body></html>', DARK_DESIGN]) | ||
| expect(withoutPaperOverride(html)).toBe(html) | ||
| }) | ||
|
|
||
| it('touches nothing but the body tag', () => { | ||
| const html = legacy(`${DARK_DESIGN}<div style="${WHITE}">card</div>`) | ||
| expect(withoutPaperOverride(html)).toBe(legacy(`${DARK_DESIGN}<div style="${WHITE}">card</div>`, SHELL)) | ||
| }) | ||
| }) |
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.