@tiavision/sinter-mail 0.0.0-stage → 0.2.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 +113 -2
- package/docs/mail.md +298 -0
- package/docs/proposals/progressive-rendering.md +180 -0
- package/docs/proposals/sinter-mail.md +520 -0
- package/llms.txt +21 -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 +348 -0
- package/sinter/css/media.ts +254 -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 +669 -0
- package/sinter/engine.ts +6 -0
- package/sinter/index.ts +5 -0
- package/sinter/parse.ts +359 -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 +144 -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
|
+
}
|