@tiavision/sinter-mail 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +137 -2
- package/docs/mail.md +267 -0
- package/docs/proposals/progressive-rendering.md +180 -0
- package/docs/proposals/sinter-mail.md +520 -0
- package/llms.txt +20 -0
- package/package.json +65 -4
- package/sinter/autolink.ts +98 -0
- package/sinter/components/internal/mail-a.html +5 -0
- package/sinter/components/internal/mail-abbr.html +5 -0
- package/sinter/components/internal/mail-address.html +5 -0
- package/sinter/components/internal/mail-article.html +5 -0
- package/sinter/components/internal/mail-aside.html +5 -0
- package/sinter/components/internal/mail-b.html +5 -0
- package/sinter/components/internal/mail-bdi.html +5 -0
- package/sinter/components/internal/mail-bdo.html +5 -0
- package/sinter/components/internal/mail-blockquote.html +5 -0
- package/sinter/components/internal/mail-br.html +5 -0
- package/sinter/components/internal/mail-center.html +5 -0
- package/sinter/components/internal/mail-cite.html +5 -0
- package/sinter/components/internal/mail-code.html +5 -0
- package/sinter/components/internal/mail-dd.html +5 -0
- package/sinter/components/internal/mail-del.html +5 -0
- package/sinter/components/internal/mail-details.html +5 -0
- package/sinter/components/internal/mail-dfn.html +5 -0
- package/sinter/components/internal/mail-div.html +5 -0
- package/sinter/components/internal/mail-dl.html +5 -0
- package/sinter/components/internal/mail-dt.html +5 -0
- package/sinter/components/internal/mail-em.html +5 -0
- package/sinter/components/internal/mail-figcaption.html +5 -0
- package/sinter/components/internal/mail-figure.html +5 -0
- package/sinter/components/internal/mail-font.html +5 -0
- package/sinter/components/internal/mail-footer.html +5 -0
- package/sinter/components/internal/mail-h1.html +5 -0
- package/sinter/components/internal/mail-h2.html +5 -0
- package/sinter/components/internal/mail-h3.html +5 -0
- package/sinter/components/internal/mail-h4.html +5 -0
- package/sinter/components/internal/mail-h5.html +5 -0
- package/sinter/components/internal/mail-h6.html +5 -0
- package/sinter/components/internal/mail-header.html +5 -0
- package/sinter/components/internal/mail-hr.html +5 -0
- package/sinter/components/internal/mail-i.html +5 -0
- package/sinter/components/internal/mail-img.html +5 -0
- package/sinter/components/internal/mail-ins.html +5 -0
- package/sinter/components/internal/mail-kbd.html +5 -0
- package/sinter/components/internal/mail-li.html +5 -0
- package/sinter/components/internal/mail-main.html +5 -0
- package/sinter/components/internal/mail-mark.html +5 -0
- package/sinter/components/internal/mail-nav.html +5 -0
- package/sinter/components/internal/mail-node.html +5 -0
- package/sinter/components/internal/mail-ol.html +5 -0
- package/sinter/components/internal/mail-orphan.html +5 -0
- package/sinter/components/internal/mail-p.html +5 -0
- package/sinter/components/internal/mail-pre.html +5 -0
- package/sinter/components/internal/mail-q.html +5 -0
- package/sinter/components/internal/mail-s.html +5 -0
- package/sinter/components/internal/mail-samp.html +5 -0
- package/sinter/components/internal/mail-section.html +5 -0
- package/sinter/components/internal/mail-small.html +5 -0
- package/sinter/components/internal/mail-span.html +5 -0
- package/sinter/components/internal/mail-steps.html +5 -0
- package/sinter/components/internal/mail-steps.ts +46 -0
- package/sinter/components/internal/mail-strike.html +5 -0
- package/sinter/components/internal/mail-strong.html +5 -0
- package/sinter/components/internal/mail-sub.html +5 -0
- package/sinter/components/internal/mail-summary.html +5 -0
- package/sinter/components/internal/mail-sup.html +5 -0
- package/sinter/components/internal/mail-table.html +5 -0
- package/sinter/components/internal/mail-time.html +5 -0
- package/sinter/components/internal/mail-tt.html +5 -0
- package/sinter/components/internal/mail-u.html +5 -0
- package/sinter/components/internal/mail-ul.html +5 -0
- package/sinter/components/internal/mail-var.html +5 -0
- package/sinter/components/internal/mail-wbr.html +5 -0
- package/sinter/components/mail-body.html +21 -0
- package/sinter/css/hints.ts +259 -0
- package/sinter/css/index.ts +270 -0
- package/sinter/css/media.ts +251 -0
- package/sinter/css/parse.ts +124 -0
- package/sinter/css/selectors.ts +530 -0
- package/sinter/css/tokenize.ts +279 -0
- package/sinter/css/values.ts +647 -0
- package/sinter/engine.ts +6 -0
- package/sinter/index.ts +5 -0
- package/sinter/parse.ts +355 -0
- package/sinter/parser/entities.ts +22 -0
- package/sinter/parser/index.ts +54 -0
- package/sinter/parser/tokenizer.ts +275 -0
- package/sinter/parser/tree.ts +1265 -0
- package/sinter/plain.ts +125 -0
- package/sinter/raw.ts +28 -0
- package/sinter/sinter.helpers.ts +11 -0
- package/sinter/sinter.types.ts +135 -0
- package/sinter/tags.ts +169 -0
- package/sinter/tree.ts +168 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Generated by scripts/build-components.mjs from sinter/tags.ts: do not edit.
|
|
3
|
+
<tt>.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Element }"><tt data-sn-attr="{ dir: node.attrs.dir, lang: node.attrs.lang, title: node.attrs.title, id: node.attrs.id ? 'mail-' + node.attrs.id : null }" data-sn-style="{ color: node.style.color, 'font-family': node.style['font-family'], 'font-size': node.style['font-size'], 'font-style': node.style['font-style'], 'font-weight': node.style['font-weight'], 'font-variant': node.style['font-variant'], 'line-height': node.style['line-height'], 'letter-spacing': node.style['letter-spacing'], 'word-spacing': node.style['word-spacing'], 'text-align': node.style['text-align'], 'text-decoration': node.style['text-decoration'], 'text-transform': node.style['text-transform'], 'text-indent': node.style['text-indent'], 'white-space': node.style['white-space'], 'word-break': node.style['word-break'], 'overflow-wrap': node.style['overflow-wrap'], 'background-color': node.style['background-color'], display: node.style.display, 'vertical-align': node.style['vertical-align'], width: node.style.width, height: node.style.height, 'min-width': node.style['min-width'], 'max-width': node.style['max-width'], 'min-height': node.style['min-height'], 'max-height': node.style['max-height'], 'margin-top': node.style['margin-top'], 'margin-right': node.style['margin-right'], 'margin-bottom': node.style['margin-bottom'], 'margin-left': node.style['margin-left'], 'padding-top': node.style['padding-top'], 'padding-right': node.style['padding-right'], 'padding-bottom': node.style['padding-bottom'], 'padding-left': node.style['padding-left'], 'border-top-width': node.style['border-top-width'], 'border-right-width': node.style['border-right-width'], 'border-bottom-width': node.style['border-bottom-width'], 'border-left-width': node.style['border-left-width'], 'border-top-style': node.style['border-top-style'], 'border-right-style': node.style['border-right-style'], 'border-bottom-style': node.style['border-bottom-style'], 'border-left-style': node.style['border-left-style'], 'border-top-color': node.style['border-top-color'], 'border-right-color': node.style['border-right-color'], 'border-bottom-color': node.style['border-bottom-color'], 'border-left-color': node.style['border-left-color'], 'border-top-left-radius': node.style['border-top-left-radius'], 'border-top-right-radius': node.style['border-top-right-radius'], 'border-bottom-right-radius': node.style['border-bottom-right-radius'], 'border-bottom-left-radius': node.style['border-bottom-left-radius'], overflow: node.style.overflow, opacity: node.style.opacity, 'box-sizing': node.style['box-sizing'], float: node.style.float, clear: node.style.clear }"><template data-sn-for="(c, ci) in node.children" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template></tt></template>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Generated by scripts/build-components.mjs from sinter/tags.ts: do not edit.
|
|
3
|
+
<u>.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Element }"><u data-sn-attr="{ dir: node.attrs.dir, lang: node.attrs.lang, title: node.attrs.title, id: node.attrs.id ? 'mail-' + node.attrs.id : null }" data-sn-style="{ color: node.style.color, 'font-family': node.style['font-family'], 'font-size': node.style['font-size'], 'font-style': node.style['font-style'], 'font-weight': node.style['font-weight'], 'font-variant': node.style['font-variant'], 'line-height': node.style['line-height'], 'letter-spacing': node.style['letter-spacing'], 'word-spacing': node.style['word-spacing'], 'text-align': node.style['text-align'], 'text-decoration': node.style['text-decoration'], 'text-transform': node.style['text-transform'], 'text-indent': node.style['text-indent'], 'white-space': node.style['white-space'], 'word-break': node.style['word-break'], 'overflow-wrap': node.style['overflow-wrap'], 'background-color': node.style['background-color'], display: node.style.display, 'vertical-align': node.style['vertical-align'], width: node.style.width, height: node.style.height, 'min-width': node.style['min-width'], 'max-width': node.style['max-width'], 'min-height': node.style['min-height'], 'max-height': node.style['max-height'], 'margin-top': node.style['margin-top'], 'margin-right': node.style['margin-right'], 'margin-bottom': node.style['margin-bottom'], 'margin-left': node.style['margin-left'], 'padding-top': node.style['padding-top'], 'padding-right': node.style['padding-right'], 'padding-bottom': node.style['padding-bottom'], 'padding-left': node.style['padding-left'], 'border-top-width': node.style['border-top-width'], 'border-right-width': node.style['border-right-width'], 'border-bottom-width': node.style['border-bottom-width'], 'border-left-width': node.style['border-left-width'], 'border-top-style': node.style['border-top-style'], 'border-right-style': node.style['border-right-style'], 'border-bottom-style': node.style['border-bottom-style'], 'border-left-style': node.style['border-left-style'], 'border-top-color': node.style['border-top-color'], 'border-right-color': node.style['border-right-color'], 'border-bottom-color': node.style['border-bottom-color'], 'border-left-color': node.style['border-left-color'], 'border-top-left-radius': node.style['border-top-left-radius'], 'border-top-right-radius': node.style['border-top-right-radius'], 'border-bottom-right-radius': node.style['border-bottom-right-radius'], 'border-bottom-left-radius': node.style['border-bottom-left-radius'], overflow: node.style.overflow, opacity: node.style.opacity, 'box-sizing': node.style['box-sizing'], float: node.style.float, clear: node.style.clear }"><template data-sn-for="(c, ci) in node.children" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template></u></template>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Generated by scripts/build-components.mjs from sinter/tags.ts: do not edit.
|
|
3
|
+
<ul>.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Element }"><ul data-sn-attr="{ dir: node.attrs.dir, lang: node.attrs.lang, title: node.attrs.title, id: node.attrs.id ? 'mail-' + node.attrs.id : null, type: node.attrs.type }" data-sn-style="{ color: node.style.color, 'font-family': node.style['font-family'], 'font-size': node.style['font-size'], 'font-style': node.style['font-style'], 'font-weight': node.style['font-weight'], 'font-variant': node.style['font-variant'], 'line-height': node.style['line-height'], 'letter-spacing': node.style['letter-spacing'], 'word-spacing': node.style['word-spacing'], 'text-align': node.style['text-align'], 'text-decoration': node.style['text-decoration'], 'text-transform': node.style['text-transform'], 'text-indent': node.style['text-indent'], 'white-space': node.style['white-space'], 'word-break': node.style['word-break'], 'overflow-wrap': node.style['overflow-wrap'], 'background-color': node.style['background-color'], display: node.style.display, 'vertical-align': node.style['vertical-align'], width: node.style.width, height: node.style.height, 'min-width': node.style['min-width'], 'max-width': node.style['max-width'], 'min-height': node.style['min-height'], 'max-height': node.style['max-height'], 'margin-top': node.style['margin-top'], 'margin-right': node.style['margin-right'], 'margin-bottom': node.style['margin-bottom'], 'margin-left': node.style['margin-left'], 'padding-top': node.style['padding-top'], 'padding-right': node.style['padding-right'], 'padding-bottom': node.style['padding-bottom'], 'padding-left': node.style['padding-left'], 'border-top-width': node.style['border-top-width'], 'border-right-width': node.style['border-right-width'], 'border-bottom-width': node.style['border-bottom-width'], 'border-left-width': node.style['border-left-width'], 'border-top-style': node.style['border-top-style'], 'border-right-style': node.style['border-right-style'], 'border-bottom-style': node.style['border-bottom-style'], 'border-left-style': node.style['border-left-style'], 'border-top-color': node.style['border-top-color'], 'border-right-color': node.style['border-right-color'], 'border-bottom-color': node.style['border-bottom-color'], 'border-left-color': node.style['border-left-color'], 'border-top-left-radius': node.style['border-top-left-radius'], 'border-top-right-radius': node.style['border-top-right-radius'], 'border-bottom-right-radius': node.style['border-bottom-right-radius'], 'border-bottom-left-radius': node.style['border-bottom-left-radius'], overflow: node.style.overflow, opacity: node.style.opacity, 'box-sizing': node.style['box-sizing'], float: node.style.float, clear: node.style.clear, 'list-style-type': node.style['list-style-type'], 'list-style-position': node.style['list-style-position'] }"><template data-sn-for="(c, ci) in node.children" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template></ul></template>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Generated by scripts/build-components.mjs from sinter/tags.ts: do not edit.
|
|
3
|
+
<var>.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Element }"><var data-sn-attr="{ dir: node.attrs.dir, lang: node.attrs.lang, title: node.attrs.title, id: node.attrs.id ? 'mail-' + node.attrs.id : null }" data-sn-style="{ color: node.style.color, 'font-family': node.style['font-family'], 'font-size': node.style['font-size'], 'font-style': node.style['font-style'], 'font-weight': node.style['font-weight'], 'font-variant': node.style['font-variant'], 'line-height': node.style['line-height'], 'letter-spacing': node.style['letter-spacing'], 'word-spacing': node.style['word-spacing'], 'text-align': node.style['text-align'], 'text-decoration': node.style['text-decoration'], 'text-transform': node.style['text-transform'], 'text-indent': node.style['text-indent'], 'white-space': node.style['white-space'], 'word-break': node.style['word-break'], 'overflow-wrap': node.style['overflow-wrap'], 'background-color': node.style['background-color'], display: node.style.display, 'vertical-align': node.style['vertical-align'], width: node.style.width, height: node.style.height, 'min-width': node.style['min-width'], 'max-width': node.style['max-width'], 'min-height': node.style['min-height'], 'max-height': node.style['max-height'], 'margin-top': node.style['margin-top'], 'margin-right': node.style['margin-right'], 'margin-bottom': node.style['margin-bottom'], 'margin-left': node.style['margin-left'], 'padding-top': node.style['padding-top'], 'padding-right': node.style['padding-right'], 'padding-bottom': node.style['padding-bottom'], 'padding-left': node.style['padding-left'], 'border-top-width': node.style['border-top-width'], 'border-right-width': node.style['border-right-width'], 'border-bottom-width': node.style['border-bottom-width'], 'border-left-width': node.style['border-left-width'], 'border-top-style': node.style['border-top-style'], 'border-right-style': node.style['border-right-style'], 'border-bottom-style': node.style['border-bottom-style'], 'border-left-style': node.style['border-left-style'], 'border-top-color': node.style['border-top-color'], 'border-right-color': node.style['border-right-color'], 'border-bottom-color': node.style['border-bottom-color'], 'border-left-color': node.style['border-left-color'], 'border-top-left-radius': node.style['border-top-left-radius'], 'border-top-right-radius': node.style['border-top-right-radius'], 'border-bottom-right-radius': node.style['border-bottom-right-radius'], 'border-bottom-left-radius': node.style['border-bottom-left-radius'], overflow: node.style.overflow, opacity: node.style.opacity, 'box-sizing': node.style['box-sizing'], float: node.style.float, clear: node.style.clear }"><template data-sn-for="(c, ci) in node.children" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template></var></template>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Generated by scripts/build-components.mjs from sinter/tags.ts: do not edit.
|
|
3
|
+
<wbr>.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Element }"><wbr data-sn-attr="{ dir: node.attrs.dir, lang: node.attrs.lang, title: node.attrs.title, id: node.attrs.id ? 'mail-' + node.attrs.id : null }"></template>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
mail-body: one email body, HTML or plain text. Public.
|
|
3
|
+
<mail-body data-sn-prop="{ body }"></mail-body>, `body` from mail.parse or mail.plain (or an API that
|
|
4
|
+
runs the parse on the server and sends the tree as JSON).
|
|
5
|
+
Its root is a plain <div class="mail">, so the tag may carry an id or a class. It carries the body's own
|
|
6
|
+
style (its text properties and background-color) and `display: flow-root`, so the email's floats and
|
|
7
|
+
margins stay inside its box. `display` depends on the body on purpose: the build writes a constant style
|
|
8
|
+
into the HTML as a style attribute, which `style-src 'self'` blocks, while one that depends on data is set
|
|
9
|
+
in the browser with style.setProperty, which the policy allows. With no body (a lazy parser that has not
|
|
10
|
+
arrived) it is an empty <div class="mail">.
|
|
11
|
+
A body that mail.progressive numbered (`body.head`) renders progressively: its first part in the task that
|
|
12
|
+
receives it, the rest in steps after the browser has painted that, a task each, sized to about 12 ms (mail-steps emits each step's
|
|
13
|
+
tree; only the elements a step cuts are copies, and rows are keyed by the nodes' numbers, so a step renders that
|
|
14
|
+
path and its new rows). Any other body renders whole in one task, and a static page's (an email from a file)
|
|
15
|
+
folds at build time with no JavaScript: the progressive branch, which has the script, is decided away.
|
|
16
|
+
The slots below are the library's whole contract: `link` replaces every <a> of the email, `image` every
|
|
17
|
+
<img>, each fill reading the element as $slot.node (its attrs, style and, for a link, children through
|
|
18
|
+
<slot></slot>). Without a fill a link opens in a new tab (rel="noopener noreferrer"); an image is a plain
|
|
19
|
+
<img> that keeps its box when the parse left its src out.
|
|
20
|
+
-->
|
|
21
|
+
<template data-sn-component="{ body?: Body | null; link: Slot<{ node: Element }>; image: Slot<{ node: Element }> }"><div class="mail" data-sn-state="{ shown: null }" data-sn-style="{ display: body ? 'flow-root' : null, color: body?.style.color, 'font-family': body?.style['font-family'], 'font-size': body?.style['font-size'], 'font-style': body?.style['font-style'], 'font-weight': body?.style['font-weight'], 'font-variant': body?.style['font-variant'], 'line-height': body?.style['line-height'], 'letter-spacing': body?.style['letter-spacing'], 'word-spacing': body?.style['word-spacing'], 'text-align': body?.style['text-align'], 'text-decoration': body?.style['text-decoration'], 'text-transform': body?.style['text-transform'], 'text-indent': body?.style['text-indent'], 'white-space': body?.style['white-space'], 'word-break': body?.style['word-break'], 'overflow-wrap': body?.style['overflow-wrap'], 'background-color': body?.style['background-color'] }"><template data-sn-if="body && body.head"><template data-sn-for="(c, ci) in shown ? shown.children : []" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template><mail-steps data-sn-prop="{ body }" data-sn-on-step="shown = $event.detail"></mail-steps></template><template data-sn-else><template data-sn-for="(c, ci) in body ? body.children : []" data-sn-key="c.n ?? ci"><template data-sn-if="c.type === 'text'"><template data-sn-text="c.value"></template></template><template data-sn-else><mail-node data-sn-prop="{ node: c }"></mail-node></template></template></template></div></template>
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Presentational hints: the legacy attributes email still uses (bgcolor, align, cellpadding, <font>…)
|
|
2
|
+
// as the declarations a browser maps them to (HTML, "Rendering"), at the lowest precedence of the
|
|
3
|
+
// author's styles. Where the standard leaves room, this follows Chromium, measured
|
|
4
|
+
// (test/css.browser.test.ts): `align` on a div, a paragraph or a table part is `-webkit-center` (which
|
|
5
|
+
// also centres the blocks inside, as a centred email's table needs), table cells take the table's
|
|
6
|
+
// `border` and `cellpadding`, rows and sections pass their `valign` to their cells.
|
|
7
|
+
import { attr, type RawElement } from '../raw.js';
|
|
8
|
+
import type { Info } from './selectors.js';
|
|
9
|
+
import { expand, isNamedColor, num, type Assignment, type Longhand } from './values.js';
|
|
10
|
+
|
|
11
|
+
const SIDES = ['top', 'right', 'bottom', 'left'] as const;
|
|
12
|
+
const sides = (what: 'width' | 'style' | 'color', v: string): Assignment[] => SIDES.map((s) => [`border-${s}-${what}` as Longhand, v]);
|
|
13
|
+
const isWs = (c: string) => c === ' ' || c === '\t' || c === '\n' || c === '\f' || c === '\r';
|
|
14
|
+
|
|
15
|
+
/** HTML's rules for parsing a legacy colour value: `#rrggbb`, a name, or null */
|
|
16
|
+
export function legacyColor(input: string): string | null {
|
|
17
|
+
let s = input;
|
|
18
|
+
// strip leading and trailing ASCII whitespace
|
|
19
|
+
let a = 0, b = s.length;
|
|
20
|
+
while (a < b && isWs(s[a])) a++;
|
|
21
|
+
while (b > a && isWs(s[b - 1])) b--;
|
|
22
|
+
s = s.slice(a, b);
|
|
23
|
+
if (s === '') return null;
|
|
24
|
+
const lower = s.toLowerCase();
|
|
25
|
+
if (lower === 'transparent') return null;
|
|
26
|
+
if (isNamedColor(lower)) return lower;
|
|
27
|
+
if (s.length === 4 && s[0] === '#' && /^[0-9a-f]{3}$/i.test(s.slice(1))) {
|
|
28
|
+
const [r, g, bl] = s.slice(1).toLowerCase();
|
|
29
|
+
return `#${r}${r}${g}${g}${bl}${bl}`;
|
|
30
|
+
}
|
|
31
|
+
// characters beyond the BMP count as "00"; then at most 128 code units
|
|
32
|
+
s = Array.from(s, (ch) => (ch.length > 1 ? '00' : ch)).join('').slice(0, 128);
|
|
33
|
+
if (s[0] === '#') s = s.slice(1);
|
|
34
|
+
s = s.replace(/[^0-9a-f]/gi, '0');
|
|
35
|
+
while (s.length === 0 || s.length % 3 !== 0) s += '0';
|
|
36
|
+
let len = s.length / 3;
|
|
37
|
+
let parts = [s.slice(0, len), s.slice(len, 2 * len), s.slice(2 * len)];
|
|
38
|
+
if (len > 8) { parts = parts.map((p) => p.slice(len - 8)); len = 8; }
|
|
39
|
+
while (len > 2 && parts.every((p) => p[0] === '0')) { parts = parts.map((p) => p.slice(1)); len--; }
|
|
40
|
+
if (len > 2) parts = parts.map((p) => p.slice(0, 2));
|
|
41
|
+
return '#' + parts.map((p) => p.padStart(2, '0')).join('').toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** HTML's rules for parsing a dimension value: px or % as a CSS value, or null; `nonzero` drops 0 */
|
|
45
|
+
export function dimension(input: string | null, nonzero = false): string | null {
|
|
46
|
+
if (input === null) return null;
|
|
47
|
+
const m = /^[ \t\n\f\r]*(\d+(?:\.\d+)?|\.\d+)(%?)/.exec(input);
|
|
48
|
+
if (!m) return null;
|
|
49
|
+
const n = Number(m[1]);
|
|
50
|
+
if (!Number.isFinite(n) || (nonzero && n === 0)) return null;
|
|
51
|
+
return num(n) + (m[2] ? '%' : n === 0 ? '' : 'px');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** HTML's rules for parsing a non-negative integer, or null */
|
|
55
|
+
export function integer(input: string | null): number | null {
|
|
56
|
+
if (input === null) return null;
|
|
57
|
+
const m = /^[ \t\n\f\r]*\+?(\d+)/.exec(input);
|
|
58
|
+
if (!m) return null;
|
|
59
|
+
const n = Number(m[1]);
|
|
60
|
+
return Number.isSafeInteger(n) ? n : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// <font size>: 1 to 7 (or ±n from 3), as the standard's keywords x-small … xxx-large, written in px
|
|
64
|
+
const FONT_SIZES = ['10px', '13px', '16px', '18px', '24px', '32px', '48px'];
|
|
65
|
+
|
|
66
|
+
/** HTML's rules for parsing a legacy font size */
|
|
67
|
+
export function legacyFontSize(input: string): string | null {
|
|
68
|
+
const m = /^[ \t\n\f\r]*([+-]?)(\d+)/.exec(input);
|
|
69
|
+
if (!m) return null;
|
|
70
|
+
let v = Number(m[2].slice(0, 9));
|
|
71
|
+
if (m[1] === '+') v = 3 + v;
|
|
72
|
+
else if (m[1] === '-') v = 3 - v;
|
|
73
|
+
return FONT_SIZES[Math.min(7, Math.max(1, v)) - 1];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const lower = (el: RawElement, name: string) => attr(el, name)?.trim().toLowerCase() ?? null;
|
|
77
|
+
|
|
78
|
+
/** `align` on a block or a table part, as Chromium maps it */
|
|
79
|
+
function blockAlign(v: string | null, webkit: boolean): string | null {
|
|
80
|
+
switch (v) {
|
|
81
|
+
case 'center': case 'middle': return webkit ? '-webkit-center' : 'center';
|
|
82
|
+
case 'left': return webkit ? '-webkit-left' : 'left';
|
|
83
|
+
case 'right': return webkit ? '-webkit-right' : 'right';
|
|
84
|
+
case 'justify': return 'justify';
|
|
85
|
+
case 'absmiddle': return webkit ? 'center' : null;
|
|
86
|
+
default: return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function valign(v: string | null): string | null {
|
|
91
|
+
return v === 'top' || v === 'middle' || v === 'bottom' || v === 'baseline' ? v : null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const SECTIONS = new Set(['thead', 'tbody', 'tfoot']);
|
|
95
|
+
|
|
96
|
+
/** a cell's row, its row's section and its table, as the parser builds them */
|
|
97
|
+
function tableOf(cell: Info): { row: Info | null; section: Info | null; table: Info | null } {
|
|
98
|
+
const row = cell.parent?.el.tag === 'tr' ? cell.parent : null;
|
|
99
|
+
const up = row?.parent ?? null;
|
|
100
|
+
const section = up && SECTIONS.has(up.el.tag) ? up : null;
|
|
101
|
+
const t = section ? section.parent : up;
|
|
102
|
+
return { row, section, table: t?.el.tag === 'table' ? t : null };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** what the body passes down: its `link` colour, for links */
|
|
106
|
+
export interface HintContext {
|
|
107
|
+
link: string | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** the presentational hints of an element, in the order they apply */
|
|
111
|
+
export function hints(e: Info, cx: HintContext): Assignment[] {
|
|
112
|
+
const el = e.el;
|
|
113
|
+
const out: Assignment[] = [];
|
|
114
|
+
const push = (p: Longhand, v: string | null) => { if (v !== null) out.push([p, v]); };
|
|
115
|
+
const bg = () => { const v = attr(el, 'bgcolor'); if (v !== null) push('background-color', legacyColor(v)); };
|
|
116
|
+
const wh = (nonzero: boolean) => {
|
|
117
|
+
push('width', dimension(attr(el, 'width'), nonzero));
|
|
118
|
+
push('height', dimension(attr(el, 'height'), nonzero));
|
|
119
|
+
};
|
|
120
|
+
switch (el.tag) {
|
|
121
|
+
case 'body': {
|
|
122
|
+
bg();
|
|
123
|
+
const text = attr(el, 'text');
|
|
124
|
+
if (text !== null) push('color', legacyColor(text));
|
|
125
|
+
const mw = dimension(attr(el, 'marginwidth'));
|
|
126
|
+
const mh = dimension(attr(el, 'marginheight'));
|
|
127
|
+
push('margin-left', dimension(attr(el, 'leftmargin')) ?? mw);
|
|
128
|
+
push('margin-right', dimension(attr(el, 'rightmargin')) ?? mw);
|
|
129
|
+
push('margin-top', dimension(attr(el, 'topmargin')) ?? mh);
|
|
130
|
+
push('margin-bottom', dimension(attr(el, 'bottommargin')) ?? mh);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'a':
|
|
134
|
+
if (cx.link && attr(el, 'href') !== null) push('color', cx.link);
|
|
135
|
+
break;
|
|
136
|
+
case 'p': case 'div':
|
|
137
|
+
push('text-align', blockAlign(lower(el, 'align'), true));
|
|
138
|
+
break;
|
|
139
|
+
case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
|
|
140
|
+
push('text-align', blockAlign(lower(el, 'align'), false));
|
|
141
|
+
break;
|
|
142
|
+
case 'center':
|
|
143
|
+
push('text-align', '-webkit-center');
|
|
144
|
+
break;
|
|
145
|
+
case 'caption': {
|
|
146
|
+
const a = lower(el, 'align');
|
|
147
|
+
if (a === 'top' || a === 'bottom') push('caption-side', a);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'br': {
|
|
151
|
+
const c = lower(el, 'clear');
|
|
152
|
+
push('clear', c === 'all' || c === 'both' ? 'both' : c === 'left' || c === 'right' || c === 'none' ? c : null);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case 'table': {
|
|
156
|
+
bg();
|
|
157
|
+
wh(true);
|
|
158
|
+
const a = lower(el, 'align');
|
|
159
|
+
if (a === 'center') { push('margin-left', 'auto'); push('margin-right', 'auto'); }
|
|
160
|
+
else if (a === 'left' || a === 'right') push('float', a);
|
|
161
|
+
const border = tableBorder(el);
|
|
162
|
+
const bc = attr(el, 'bordercolor');
|
|
163
|
+
const bcolor = bc !== null ? legacyColor(bc) : null;
|
|
164
|
+
if (border !== null) {
|
|
165
|
+
out.push(...sides('width', border + 'px'));
|
|
166
|
+
if (border > 0) out.push(...sides('style', bcolor ? 'solid' : 'outset'));
|
|
167
|
+
}
|
|
168
|
+
if (bcolor) out.push(...sides('color', bcolor));
|
|
169
|
+
const sp = integer(attr(el, 'cellspacing'));
|
|
170
|
+
if (sp !== null) push('border-spacing', num(sp) + 'px');
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
case 'thead': case 'tbody': case 'tfoot': case 'tr':
|
|
174
|
+
bg();
|
|
175
|
+
push('text-align', blockAlign(lower(el, 'align'), true));
|
|
176
|
+
push('vertical-align', valign(lower(el, 'valign')));
|
|
177
|
+
push('height', dimension(attr(el, 'height'), true));
|
|
178
|
+
break;
|
|
179
|
+
case 'col': case 'colgroup':
|
|
180
|
+
bg();
|
|
181
|
+
push('width', dimension(attr(el, 'width'), true));
|
|
182
|
+
break;
|
|
183
|
+
case 'td': case 'th': {
|
|
184
|
+
const { row, section, table } = tableOf(e);
|
|
185
|
+
// what the cell takes from its table, below its own attributes
|
|
186
|
+
if (table) {
|
|
187
|
+
const border = tableBorder(table.el);
|
|
188
|
+
if (border !== null && border > 0) {
|
|
189
|
+
const solid = attr(table.el, 'bordercolor') !== null && legacyColor(attr(table.el, 'bordercolor')!) !== null;
|
|
190
|
+
out.push(...sides('width', '1px'), ...sides('style', solid ? 'solid' : 'inset'), ...sides('color', 'inherit'));
|
|
191
|
+
}
|
|
192
|
+
// present and not a number is 0, as Chromium reads it
|
|
193
|
+
const cp = attr(table.el, 'cellpadding');
|
|
194
|
+
const pad = cp === null ? null : integer(cp) ?? 0;
|
|
195
|
+
if (pad !== null) for (const s of SIDES) push(`padding-${s}`, num(pad) + 'px');
|
|
196
|
+
}
|
|
197
|
+
bg();
|
|
198
|
+
wh(true);
|
|
199
|
+
push('text-align', blockAlign(lower(el, 'align'), true));
|
|
200
|
+
push('vertical-align', valign(lower(el, 'valign')) ?? (row && valign(lower(row.el, 'valign'))) ?? (section && valign(lower(section.el, 'valign'))));
|
|
201
|
+
if (attr(el, 'nowrap') !== null) push('white-space', 'nowrap');
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case 'img': {
|
|
205
|
+
wh(false);
|
|
206
|
+
const a = lower(el, 'align');
|
|
207
|
+
if (a === 'left' || a === 'right') { push('float', a); push('vertical-align', 'top'); }
|
|
208
|
+
else {
|
|
209
|
+
const VA: Record<string, string> = { top: 'top', middle: 'middle', absmiddle: 'middle', center: 'middle', bottom: 'baseline', absbottom: 'bottom', baseline: 'baseline', texttop: 'text-top' };
|
|
210
|
+
if (a !== null && a in VA) push('vertical-align', VA[a]);
|
|
211
|
+
}
|
|
212
|
+
const b = dimension(attr(el, 'border'));
|
|
213
|
+
if (b !== null) { out.push(...sides('width', b === '0' ? '0' : b), ...sides('style', 'solid')); }
|
|
214
|
+
const hs = dimension(attr(el, 'hspace'));
|
|
215
|
+
const vs = dimension(attr(el, 'vspace'));
|
|
216
|
+
if (hs !== null) { push('margin-left', hs); push('margin-right', hs); }
|
|
217
|
+
if (vs !== null) { push('margin-top', vs); push('margin-bottom', vs); }
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case 'hr': {
|
|
221
|
+
const a = lower(el, 'align');
|
|
222
|
+
if (a === 'left') { push('margin-left', '0'); push('margin-right', 'auto'); }
|
|
223
|
+
else if (a === 'right') { push('margin-left', 'auto'); push('margin-right', '0'); }
|
|
224
|
+
else if (a === 'center') { push('margin-left', 'auto'); push('margin-right', 'auto'); }
|
|
225
|
+
push('width', dimension(attr(el, 'width'), true));
|
|
226
|
+
const c = attr(el, 'color');
|
|
227
|
+
const cv = c !== null ? legacyColor(c) : null;
|
|
228
|
+
if (cv) out.push(...sides('style', 'solid'), ...sides('color', cv), ['background-color', cv]);
|
|
229
|
+
else if (attr(el, 'noshade') !== null) out.push(...sides('style', 'solid'), ...sides('color', 'gray'), ['background-color', 'gray']);
|
|
230
|
+
const size = attr(el, 'size');
|
|
231
|
+
if (size !== null) {
|
|
232
|
+
const n = integer(size) ?? 0;
|
|
233
|
+
if (n <= 1) push('border-bottom-width', '0');
|
|
234
|
+
else push('height', num(n - 2) + 'px');
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
case 'font': {
|
|
239
|
+
const c = attr(el, 'color');
|
|
240
|
+
if (c !== null) push('color', legacyColor(c));
|
|
241
|
+
const face = attr(el, 'face');
|
|
242
|
+
if (face !== null) {
|
|
243
|
+
const f = expand('font-family', face);
|
|
244
|
+
if (f && !['inherit', 'initial', 'unset'].includes(f[0][1])) out.push(...f);
|
|
245
|
+
}
|
|
246
|
+
const size = attr(el, 'size');
|
|
247
|
+
if (size !== null) push('font-size', legacyFontSize(size));
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** a table's `border` attribute in px: present and not a number is 1 */
|
|
255
|
+
function tableBorder(table: RawElement): number | null {
|
|
256
|
+
const v = attr(table, 'border');
|
|
257
|
+
if (v === null) return null;
|
|
258
|
+
return integer(v) ?? 1;
|
|
259
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// The cascade, resolved once at parse time: every element's declarations from its presentational hints,
|
|
2
|
+
// the email's <style> rules (by specificity, then order, `!important` after normal ones) and its style
|
|
3
|
+
// attribute (above the author's rules, `!important` in it above the author's `!important`), as CSS
|
|
4
|
+
// Cascading Level 4 orders them within the author origin. What wins for each kept property is the
|
|
5
|
+
// element's style, rendered inline by data-sn-style: there is no stylesheet to evaluate later, so
|
|
6
|
+
// `@media` is decided here too (the `media` option), and selectors are matched against the raw tree.
|
|
7
|
+
//
|
|
8
|
+
// Inheritance is not computed: the tree keeps the document's structure, so an inline style inherits in
|
|
9
|
+
// the app as it did in the email.
|
|
10
|
+
//
|
|
11
|
+
// Pure: no DOM, no Node APIs, only the language's globals, so sinter folds it at build time.
|
|
12
|
+
import { attr, type RawElement } from '../raw.js';
|
|
13
|
+
import { PROPERTIES } from '../tags.js';
|
|
14
|
+
import type { Property, Style } from '../sinter.types.js';
|
|
15
|
+
import { hints, legacyColor } from './hints.js';
|
|
16
|
+
import { matchMedia, type Media } from './media.js';
|
|
17
|
+
import { parseDeclarations, parseStylesheet, rules as parseRules, type Declaration, type Rule } from './parse.js';
|
|
18
|
+
import { bucketKey, exhausted, indexTree, matches, parseSelectorList, type Budget, type Complex } from './selectors.js';
|
|
19
|
+
import { expand, joinOverflow, joinTextDecoration, type Assignment, type Longhand } from './values.js';
|
|
20
|
+
|
|
21
|
+
export { matchMedia } from './media.js';
|
|
22
|
+
export { parseStylesheet, parseDeclarations } from './parse.js';
|
|
23
|
+
export { expand } from './values.js';
|
|
24
|
+
export { tokenize } from './tokenize.js';
|
|
25
|
+
|
|
26
|
+
export interface ResolveOptions {
|
|
27
|
+
/** the width `@media` is evaluated at: 'wide' (800 px) or 'narrow' (375 px) */
|
|
28
|
+
media: Media;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// every tracked longhand by number: an element's values are an array, not a map, since a hostile
|
|
32
|
+
// stylesheet can match thousands of rules on every element
|
|
33
|
+
const LONGHANDS: Longhand[] = [
|
|
34
|
+
...(PROPERTIES.filter((p) => p !== 'text-decoration' && p !== 'overflow') as Longhand[]),
|
|
35
|
+
'text-decoration-line', 'text-decoration-style', 'text-decoration-color', 'overflow-x', 'overflow-y',
|
|
36
|
+
];
|
|
37
|
+
const ID = new Map(LONGHANDS.map((l, i) => [l, i]));
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Assignments as parallel arrays of longhand numbers and values, the last one per longhand only, and
|
|
41
|
+
* which longhands they set as a bit mask in three 30-bit words (small integers), so a rule whose
|
|
42
|
+
* longhands a higher one already set is skipped with a few integer operations.
|
|
43
|
+
*/
|
|
44
|
+
interface Compiled {
|
|
45
|
+
props: number[];
|
|
46
|
+
values: string[];
|
|
47
|
+
m0: number;
|
|
48
|
+
m1: number;
|
|
49
|
+
m2: number;
|
|
50
|
+
}
|
|
51
|
+
if (LONGHANDS.length > 90) throw new Error('sinter/css: more longhands than the masks hold');
|
|
52
|
+
|
|
53
|
+
interface StyleRule {
|
|
54
|
+
normal: Compiled;
|
|
55
|
+
important: Compiled;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Entry {
|
|
59
|
+
/** unique per entry: a list of matched entries is keyed by these */
|
|
60
|
+
id: number;
|
|
61
|
+
sel: Complex;
|
|
62
|
+
specificity: number;
|
|
63
|
+
order: number;
|
|
64
|
+
normal: Compiled;
|
|
65
|
+
/** null when the rule has no !important declaration */
|
|
66
|
+
important: Compiled | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// the matching work allowed for one resolve (selectors.ts, budget): a 1,800-element newsletter with
|
|
70
|
+
// 300 rules takes 0.5 million steps; a hostile stylesheet stops matching here, in a few hundred
|
|
71
|
+
// milliseconds on a CI runner (test/css.test.ts, safety)
|
|
72
|
+
const MAX_STEPS = 2_000_000;
|
|
73
|
+
|
|
74
|
+
// the author rules kept for one resolve, every <style> together (one per selector of a list): a real
|
|
75
|
+
// email has hundreds; past this a hostile stylesheet's rules are dropped, so compiling them, and going
|
|
76
|
+
// through an element's matches, stays bounded
|
|
77
|
+
const MAX_RULES = 10_000;
|
|
78
|
+
|
|
79
|
+
// an element's style is shared with the elements like it when it matches at most this many rules (a real
|
|
80
|
+
// email's elements match a handful)
|
|
81
|
+
const CACHED = 64;
|
|
82
|
+
|
|
83
|
+
// `@media` nested deeper than this is dropped rather than walked
|
|
84
|
+
const MAX_MEDIA_DEPTH = 16;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Every element's resolved style under `options`: the kept properties only, each value re-serialized.
|
|
88
|
+
* Elements whose style is empty have no entry.
|
|
89
|
+
*/
|
|
90
|
+
export function resolveStyles(root: RawElement, options: ResolveOptions): Map<RawElement, Style> {
|
|
91
|
+
const media = options.media;
|
|
92
|
+
const all = indexTree(root);
|
|
93
|
+
|
|
94
|
+
// the author rules, from every <style> in document order
|
|
95
|
+
const buckets = new Map<string, Entry[]>();
|
|
96
|
+
let order = 0, ids = 0;
|
|
97
|
+
const addRules = (list: Rule[], depth: number) => {
|
|
98
|
+
for (const r of list) {
|
|
99
|
+
if (r.t === 'at') {
|
|
100
|
+
if (r.name === 'media' && r.block && depth < MAX_MEDIA_DEPTH && matchMedia(r.prelude, media)) addRules(parseRules(r.block.items, false), depth + 1);
|
|
101
|
+
// @import, @font-face, @keyframes, @supports, @layer, @page, @charset and the rest: dropped
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const sels = parseSelectorList(r.prelude);
|
|
105
|
+
if (!sels || sels.length === 0) continue;
|
|
106
|
+
const rule = compile(r.declarations);
|
|
107
|
+
// a rule that sets nothing kept is not matched at all
|
|
108
|
+
if (rule.normal.props.length === 0 && rule.important.props.length === 0) continue;
|
|
109
|
+
if (ids >= MAX_RULES) return;
|
|
110
|
+
const o = order++;
|
|
111
|
+
for (const sel of sels) {
|
|
112
|
+
const key = bucketKey(sel);
|
|
113
|
+
let b = buckets.get(key);
|
|
114
|
+
if (!b) buckets.set(key, (b = []));
|
|
115
|
+
b.push({ id: ids++, sel, specificity: sel.specificity, order: o, normal: rule.normal, important: rule.important.props.length ? rule.important : null });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
for (const e of all) {
|
|
120
|
+
if (e.el.tag !== 'style' || !isCss(e.el)) continue;
|
|
121
|
+
const m = attr(e.el, 'media');
|
|
122
|
+
if (m !== null && !matchMedia(m, media)) continue;
|
|
123
|
+
let text = '';
|
|
124
|
+
for (const c of e.el.children) if (c.type === 'text') text += c.value;
|
|
125
|
+
if (ids >= MAX_RULES) break;
|
|
126
|
+
addRules(parseStylesheet(text), 0);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const body = all.find((e) => e.el.tag === 'body' && e.parent?.el === root) ?? null;
|
|
130
|
+
const link = body ? attr(body.el, 'link') : null;
|
|
131
|
+
const cx = { link: link !== null ? legacyColor(link) : null };
|
|
132
|
+
|
|
133
|
+
// each bucket in cascade order, so an element's matches are merged, not sorted
|
|
134
|
+
const byCascade = (a: Entry, b: Entry) => a.specificity - b.specificity || a.order - b.order;
|
|
135
|
+
for (const b of buckets.values()) b.sort(byCascade);
|
|
136
|
+
|
|
137
|
+
const out = new Map<RawElement, Style>();
|
|
138
|
+
const universal = buckets.get('*');
|
|
139
|
+
const values: (string | undefined)[] = new Array(LONGHANDS.length);
|
|
140
|
+
const work: Budget = { left: MAX_STEPS };
|
|
141
|
+
// Emails repeat themselves: builders write the same inline style and legacy attributes on every cell of
|
|
142
|
+
// a kind, and those cells match the same rules. So within one resolve: an inline style is parsed once
|
|
143
|
+
// per distinct text, the hints compiled once per distinct list, and an element's style is computed once
|
|
144
|
+
// per distinct (matched rules, inline style, hints) and shared, frozen with the tree, by every element
|
|
145
|
+
// that has the same. Bounded by the input: one entry per distinct string.
|
|
146
|
+
const inlines = new Map<string, StyleRule>();
|
|
147
|
+
const hinted = new Map<string, Compiled>();
|
|
148
|
+
const styles = new Map<string, Style | null>();
|
|
149
|
+
{
|
|
150
|
+
for (const e of all) {
|
|
151
|
+
let candidates: Entry[] = [];
|
|
152
|
+
const take = (list: Entry[] | undefined) => {
|
|
153
|
+
if (!list || exhausted(work)) return;
|
|
154
|
+
const m: Entry[] = [];
|
|
155
|
+
for (const x of list) if (matches(x.sel, e, work)) m.push(x);
|
|
156
|
+
if (m.length) candidates = candidates.length ? merge(candidates, m, byCascade) : m;
|
|
157
|
+
};
|
|
158
|
+
if (e.id !== null) take(buckets.get('#' + e.id));
|
|
159
|
+
const seen = new Set<string>();
|
|
160
|
+
for (const c of e.classes) if (!seen.has(c)) { seen.add(c); take(buckets.get('.' + c)); }
|
|
161
|
+
take(buckets.get(e.el.tag));
|
|
162
|
+
take(universal);
|
|
163
|
+
|
|
164
|
+
const inline = attr(e.el, 'style');
|
|
165
|
+
const hs = hints(e, cx);
|
|
166
|
+
let hkey = '';
|
|
167
|
+
for (const [p, v] of hs) hkey += p + ':' + v + ';';
|
|
168
|
+
// the hints (no `|` in them), the matched entries' ids, then the inline text: unambiguous. Not for an
|
|
169
|
+
// element that matches more than CACHED rules (a hostile stylesheet's thousands): its key would cost
|
|
170
|
+
// as much as its style, so it is computed as it is
|
|
171
|
+
let key: string | null = null;
|
|
172
|
+
if (candidates.length <= CACHED) {
|
|
173
|
+
key = hkey + '|';
|
|
174
|
+
for (const c of candidates) key += c.id + ',';
|
|
175
|
+
key += '|' + (inline ?? '');
|
|
176
|
+
const known = styles.get(key);
|
|
177
|
+
if (known !== undefined) {
|
|
178
|
+
if (known) out.set(e.el, known);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// from the highest precedence down, each longhand taken from the first that sets it: inline
|
|
184
|
+
// !important, author !important (the last in cascade order first), inline, author, hints
|
|
185
|
+
values.fill(undefined);
|
|
186
|
+
let s0 = 0, s1 = 0, s2 = 0;
|
|
187
|
+
const apply = (c: Compiled) => {
|
|
188
|
+
if (!((c.m0 & ~s0) | (c.m1 & ~s1) | (c.m2 & ~s2))) return;
|
|
189
|
+
for (let i = 0; i < c.props.length; i++) if (values[c.props[i]] === undefined) values[c.props[i]] = c.values[i];
|
|
190
|
+
s0 |= c.m0; s1 |= c.m1; s2 |= c.m2;
|
|
191
|
+
};
|
|
192
|
+
let own: StyleRule | null = null;
|
|
193
|
+
if (inline !== null) {
|
|
194
|
+
own = inlines.get(inline) ?? null;
|
|
195
|
+
if (!own) inlines.set(inline, (own = compile(parseDeclarations(inline))));
|
|
196
|
+
}
|
|
197
|
+
if (own) apply(own.important);
|
|
198
|
+
for (let i = candidates.length - 1; i >= 0; i--) { const imp = candidates[i].important; if (imp) apply(imp); }
|
|
199
|
+
if (own) apply(own.normal);
|
|
200
|
+
for (let i = candidates.length - 1; i >= 0; i--) apply(candidates[i].normal);
|
|
201
|
+
let h = hinted.get(hkey);
|
|
202
|
+
if (!h) hinted.set(hkey, (h = compiled(hs)));
|
|
203
|
+
apply(h);
|
|
204
|
+
const style = finish(values);
|
|
205
|
+
if (key !== null) styles.set(key, style);
|
|
206
|
+
if (style) out.set(e.el, style);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** two lists in cascade order as one */
|
|
213
|
+
function merge(a: Entry[], b: Entry[], cmp: (x: Entry, y: Entry) => number): Entry[] {
|
|
214
|
+
const out: Entry[] = [];
|
|
215
|
+
let i = 0, j = 0;
|
|
216
|
+
while (i < a.length && j < b.length) out.push(cmp(a[i], b[j]) <= 0 ? a[i++] : b[j++]);
|
|
217
|
+
while (i < a.length) out.push(a[i++]);
|
|
218
|
+
while (j < b.length) out.push(b[j++]);
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const isCss = (el: RawElement) => {
|
|
223
|
+
const t = attr(el, 'type');
|
|
224
|
+
if (t === null) return true;
|
|
225
|
+
const v = t.trim().toLowerCase();
|
|
226
|
+
return v === '' || v === 'text/css';
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/** a rule's declarations as longhand assignments, normal and important apart, in declaration order */
|
|
230
|
+
function compile(decls: Declaration[]): StyleRule {
|
|
231
|
+
const normal: Assignment[] = [];
|
|
232
|
+
const important: Assignment[] = [];
|
|
233
|
+
for (const d of decls) {
|
|
234
|
+
const a = expand(d.name, d.value);
|
|
235
|
+
if (a) (d.important ? important : normal).push(...a);
|
|
236
|
+
}
|
|
237
|
+
return { normal: compiled(normal), important: compiled(important) };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function compiled(list: Assignment[]): Compiled {
|
|
241
|
+
const last = new Map<number, string>();
|
|
242
|
+
for (const [p, v] of list) {
|
|
243
|
+
const id = ID.get(p)!;
|
|
244
|
+
// re-inserted, so the map's order stays the order of the winning assignments
|
|
245
|
+
last.delete(id);
|
|
246
|
+
last.set(id, v);
|
|
247
|
+
}
|
|
248
|
+
const mask = [0, 0, 0];
|
|
249
|
+
for (const id of last.keys()) mask[Math.floor(id / 30)] |= 1 << id % 30;
|
|
250
|
+
return { props: [...last.keys()], values: [...last.values()], m0: mask[0], m1: mask[1], m2: mask[2] };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const at = (values: (string | undefined)[], l: Longhand) => values[ID.get(l)!];
|
|
254
|
+
|
|
255
|
+
/** the tracked longhands as the element's style: the kept properties, in PROPERTIES order */
|
|
256
|
+
function finish(values: (string | undefined)[]): Style | null {
|
|
257
|
+
const style: Style = {};
|
|
258
|
+
let any = false;
|
|
259
|
+
for (const p of PROPERTIES as readonly Property[]) {
|
|
260
|
+
let v: string | null | undefined;
|
|
261
|
+
if (p === 'text-decoration') v = joinTextDecoration(at(values, 'text-decoration-line'), at(values, 'text-decoration-style'), at(values, 'text-decoration-color'));
|
|
262
|
+
else if (p === 'overflow') v = joinOverflow(at(values, 'overflow-x'), at(values, 'overflow-y'));
|
|
263
|
+
else v = at(values, p);
|
|
264
|
+
if (v != null && safe(v)) { style[p] = v; any = true; }
|
|
265
|
+
}
|
|
266
|
+
return any ? style : null;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** the runtime's own check, as a last gate: what it would refuse is never written */
|
|
270
|
+
const safe = (v: string) => !/\\|url\(|image\(|image-set\(/i.test(v);
|