@open-artifact/dc 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 +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +53 -4
- package/src/check.d.ts +30 -0
- package/src/check.js +337 -0
- package/src/document.d.ts +63 -0
- package/src/document.js +126 -0
- package/src/edit.d.ts +72 -0
- package/src/edit.js +249 -0
- package/src/holes.d.ts +46 -0
- package/src/holes.js +91 -0
- package/src/html.d.ts +113 -0
- package/src/html.js +335 -0
- package/src/index.d.ts +16 -0
- package/src/index.js +15 -0
- package/src/props.d.ts +51 -0
- package/src/props.js +57 -0
- package/src/protocol.d.ts +266 -0
- package/src/protocol.js +16 -0
- package/src/render.d.ts +34 -0
- package/src/render.js +256 -0
- package/src/rich-text.d.ts +106 -0
- package/src/rich-text.js +515 -0
- package/src/runtime/board.d.ts +6 -0
- package/src/runtime/board.js +509 -0
- package/src/runtime/focus-guard.d.ts +18 -0
- package/src/runtime/focus-guard.js +40 -0
- package/src/runtime/index.d.ts +16 -0
- package/src/runtime/index.js +15 -0
- package/src/runtime/logic.d.ts +31 -0
- package/src/runtime/logic.js +47 -0
- package/src/runtime/text-edit.d.ts +121 -0
- package/src/runtime/text-edit.js +1251 -0
- package/src/style.d.ts +30 -0
- package/src/style.js +108 -0
- package/src/template.d.ts +80 -0
- package/src/template.js +186 -0
package/src/style.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline `style` attributes: parse into ordered declarations and write them
|
|
3
|
+
* back, keeping the order and spelling of declarations an edit did not touch.
|
|
4
|
+
*/
|
|
5
|
+
/** One `property: value` declaration. */
|
|
6
|
+
export interface StyleDeclaration {
|
|
7
|
+
/** Property name, lower-case (custom properties keep their case). */
|
|
8
|
+
property: string;
|
|
9
|
+
value: string;
|
|
10
|
+
important: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Parses an inline style. Malformed pieces are dropped. */
|
|
13
|
+
export declare function parseStyle(text: string): StyleDeclaration[];
|
|
14
|
+
/** Writes declarations as `a: b; c: d`. */
|
|
15
|
+
export declare function serializeStyle(declarations: readonly StyleDeclaration[]): string;
|
|
16
|
+
/** Converts `fontSize` to `font-size`; kebab and custom names pass through. */
|
|
17
|
+
export declare function kebabCase(property: string): string;
|
|
18
|
+
/** Converts `font-size` to `fontSize` (React style keys); custom properties pass through. */
|
|
19
|
+
export declare function camelCase(property: string): string;
|
|
20
|
+
/** A style patch: a value sets, `null` removes. Keys may be kebab or camel case. */
|
|
21
|
+
export type StylePatch = Readonly<Record<string, string | null>>;
|
|
22
|
+
/**
|
|
23
|
+
* Applies a patch: changed properties keep their position, new ones are
|
|
24
|
+
* appended, removed ones disappear.
|
|
25
|
+
*/
|
|
26
|
+
export declare function patchStyle(text: string, patch: StylePatch): string;
|
|
27
|
+
/** Reads a style into a plain map (kebab keys). Later declarations win. */
|
|
28
|
+
export declare function styleMap(text: string): Record<string, string>;
|
|
29
|
+
/** Converts a style string to a React style object. */
|
|
30
|
+
export declare function toReactStyle(text: string): Record<string, string>;
|
package/src/style.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline `style` attributes: parse into ordered declarations and write them
|
|
3
|
+
* back, keeping the order and spelling of declarations an edit did not touch.
|
|
4
|
+
*/
|
|
5
|
+
/** Splits text on a separator outside parentheses and quotes. */
|
|
6
|
+
function splitTopLevel(text, separator) {
|
|
7
|
+
const parts = [];
|
|
8
|
+
let depth = 0;
|
|
9
|
+
let quote = null;
|
|
10
|
+
let start = 0;
|
|
11
|
+
for (let i = 0; i < text.length; i++) {
|
|
12
|
+
const ch = text[i];
|
|
13
|
+
if (quote) {
|
|
14
|
+
if (ch === '\\')
|
|
15
|
+
i++;
|
|
16
|
+
else if (ch === quote)
|
|
17
|
+
quote = null;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (ch === '"' || ch === "'")
|
|
21
|
+
quote = ch;
|
|
22
|
+
else if (ch === '(')
|
|
23
|
+
depth++;
|
|
24
|
+
else if (ch === ')')
|
|
25
|
+
depth = Math.max(0, depth - 1);
|
|
26
|
+
else if (ch === separator && depth === 0) {
|
|
27
|
+
parts.push(text.slice(start, i));
|
|
28
|
+
start = i + 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
parts.push(text.slice(start));
|
|
32
|
+
return parts;
|
|
33
|
+
}
|
|
34
|
+
/** Parses an inline style. Malformed pieces are dropped. */
|
|
35
|
+
export function parseStyle(text) {
|
|
36
|
+
const declarations = [];
|
|
37
|
+
for (const piece of splitTopLevel(text, ';')) {
|
|
38
|
+
const colon = piece.indexOf(':');
|
|
39
|
+
if (colon <= 0)
|
|
40
|
+
continue;
|
|
41
|
+
const rawProperty = piece.slice(0, colon).trim();
|
|
42
|
+
if (!rawProperty)
|
|
43
|
+
continue;
|
|
44
|
+
let value = piece.slice(colon + 1).trim();
|
|
45
|
+
const important = /!\s*important\s*$/i.test(value);
|
|
46
|
+
if (important)
|
|
47
|
+
value = value.replace(/!\s*important\s*$/i, '').trim();
|
|
48
|
+
const property = rawProperty.startsWith('--') ? rawProperty : rawProperty.toLowerCase();
|
|
49
|
+
declarations.push({ property, value, important });
|
|
50
|
+
}
|
|
51
|
+
return declarations;
|
|
52
|
+
}
|
|
53
|
+
/** Writes declarations as `a: b; c: d`. */
|
|
54
|
+
export function serializeStyle(declarations) {
|
|
55
|
+
return declarations
|
|
56
|
+
.map((declaration) => `${declaration.property}: ${declaration.value}${declaration.important ? ' !important' : ''}`)
|
|
57
|
+
.join('; ');
|
|
58
|
+
}
|
|
59
|
+
/** Converts `fontSize` to `font-size`; kebab and custom names pass through. */
|
|
60
|
+
export function kebabCase(property) {
|
|
61
|
+
if (property.startsWith('--') || property.includes('-'))
|
|
62
|
+
return property;
|
|
63
|
+
return property.replace(/[A-Z]/g, (ch) => `-${ch.toLowerCase()}`);
|
|
64
|
+
}
|
|
65
|
+
/** Converts `font-size` to `fontSize` (React style keys); custom properties pass through. */
|
|
66
|
+
export function camelCase(property) {
|
|
67
|
+
if (property.startsWith('--'))
|
|
68
|
+
return property;
|
|
69
|
+
const name = property.startsWith('-ms-') ? property.slice(1) : property;
|
|
70
|
+
return name.replace(/-([a-z])/g, (_, ch) => ch.toUpperCase());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Applies a patch: changed properties keep their position, new ones are
|
|
74
|
+
* appended, removed ones disappear.
|
|
75
|
+
*/
|
|
76
|
+
export function patchStyle(text, patch) {
|
|
77
|
+
const declarations = parseStyle(text);
|
|
78
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
79
|
+
const property = kebabCase(key);
|
|
80
|
+
const index = declarations.findIndex((declaration) => declaration.property === property);
|
|
81
|
+
if (value === null || value === '') {
|
|
82
|
+
if (index >= 0)
|
|
83
|
+
declarations.splice(index, 1);
|
|
84
|
+
}
|
|
85
|
+
else if (index >= 0) {
|
|
86
|
+
declarations[index] = { property, value, important: declarations[index].important };
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
declarations.push({ property, value, important: false });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return serializeStyle(declarations);
|
|
93
|
+
}
|
|
94
|
+
/** Reads a style into a plain map (kebab keys). Later declarations win. */
|
|
95
|
+
export function styleMap(text) {
|
|
96
|
+
const map = {};
|
|
97
|
+
for (const declaration of parseStyle(text))
|
|
98
|
+
map[declaration.property] = declaration.value;
|
|
99
|
+
return map;
|
|
100
|
+
}
|
|
101
|
+
/** Converts a style string to a React style object. */
|
|
102
|
+
export function toReactStyle(text) {
|
|
103
|
+
const style = {};
|
|
104
|
+
for (const declaration of parseStyle(text)) {
|
|
105
|
+
style[camelCase(declaration.property)] = declaration.important ? `${declaration.value} !important` : declaration.value;
|
|
106
|
+
}
|
|
107
|
+
return style;
|
|
108
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The template fragment (`<x-dc>` … last `</x-dc>`) as a tree, and the two
|
|
3
|
+
* ways to address one of its elements:
|
|
4
|
+
*
|
|
5
|
+
* - **tid**: every element numbered depth-first in source order from 0. All
|
|
6
|
+
* tags count (`<helmet>` and its `<style>`, `<sc-for>`, `<sc-if>`,
|
|
7
|
+
* `<dc-import>`); text and comments do not.
|
|
8
|
+
* - **path**: the index among the fragment's top-level elements, then the
|
|
9
|
+
* index among each element's element children, down to the element.
|
|
10
|
+
*
|
|
11
|
+
* Together they form a view-context address: `File.dc.html#tid:path`, the
|
|
12
|
+
* file stem percent-encoded with `encodeURIComponent`. An agent resolves an
|
|
13
|
+
* address against its own copy of the file by the same rules; when tid and
|
|
14
|
+
* path disagree, the copies differ.
|
|
15
|
+
*/
|
|
16
|
+
import { type HtmlElement, type HtmlNode } from './html.ts';
|
|
17
|
+
/** A parsed template with its elements indexed by tid. */
|
|
18
|
+
export interface TemplateModel {
|
|
19
|
+
/** The template text this model was parsed from. */
|
|
20
|
+
text: string;
|
|
21
|
+
/** Top-level nodes. Offsets index into {@link text}. */
|
|
22
|
+
nodes: HtmlNode[];
|
|
23
|
+
/** Every element in tid order: `elements[tid]`. */
|
|
24
|
+
elements: HtmlElement[];
|
|
25
|
+
/** The tid of an element of this model. */
|
|
26
|
+
tidOf: Map<HtmlElement, number>;
|
|
27
|
+
}
|
|
28
|
+
/** Parses template text (the part between `<x-dc>` and the last `</x-dc>`). */
|
|
29
|
+
export declare function parseTemplate(text: string): TemplateModel;
|
|
30
|
+
/** The child-index path of an element, top-level index first. */
|
|
31
|
+
export declare function elementPath(model: TemplateModel, element: HtmlElement): number[];
|
|
32
|
+
/** Finds an element by its path, or `null`. */
|
|
33
|
+
export declare function elementAtPath(model: TemplateModel, path: readonly number[]): HtmlElement | null;
|
|
34
|
+
/** Grammar of an encoded artboard file name. */
|
|
35
|
+
export declare const BOARD_NAME_PATTERN: RegExp;
|
|
36
|
+
/** Grammar of an element address. */
|
|
37
|
+
export declare const ADDRESS_PATTERN: RegExp;
|
|
38
|
+
/** Longest encoded file name the view context writes. */
|
|
39
|
+
export declare const MAX_ENCODED_NAME = 220;
|
|
40
|
+
/**
|
|
41
|
+
* Encodes an artboard path for the view context: the base name with the part
|
|
42
|
+
* before `.dc.html` percent-encoded. Returns `null` when the result would not
|
|
43
|
+
* match {@link BOARD_NAME_PATTERN} (such entries are left out).
|
|
44
|
+
*/
|
|
45
|
+
export declare function encodeBoardName(path: string): string | null;
|
|
46
|
+
/** A decoded element address. */
|
|
47
|
+
export interface ElementAddress {
|
|
48
|
+
/** The encoded file name, as written in the address. */
|
|
49
|
+
file: string;
|
|
50
|
+
tid: number;
|
|
51
|
+
path: number[];
|
|
52
|
+
/** Instance index inside a loop; never written today. */
|
|
53
|
+
instance?: number;
|
|
54
|
+
}
|
|
55
|
+
/** Builds `File.dc.html#tid:path`, or `null` when the grammar cannot express it. */
|
|
56
|
+
export declare function formatAddress(boardPath: string, tid: number, path: readonly number[]): string | null;
|
|
57
|
+
/** Parses an address; `null` when it does not match the grammar. */
|
|
58
|
+
export declare function parseAddress(address: string): ElementAddress | null;
|
|
59
|
+
/**
|
|
60
|
+
* Resolves a tid/path pair against a model. Returns the element only when
|
|
61
|
+
* both name the same node; otherwise the caller's copy differs from the
|
|
62
|
+
* viewer's.
|
|
63
|
+
*/
|
|
64
|
+
export declare function resolveElement(model: TemplateModel, tid: number, path: readonly number[]): HtmlElement | null;
|
|
65
|
+
/** What a selected element is, for the view context. */
|
|
66
|
+
export type ElementKind = 'text' | 'image' | 'shape' | 'line' | 'other';
|
|
67
|
+
/** Template-level structure tags: they render no box of their own. */
|
|
68
|
+
export declare const STRUCTURE_TAGS: ReadonlySet<string>;
|
|
69
|
+
/** True when the element holds words directly (text children, no element children). */
|
|
70
|
+
export declare function holdsText(element: HtmlElement): boolean;
|
|
71
|
+
/** Classifies an element. */
|
|
72
|
+
export declare function elementKind(element: HtmlElement): ElementKind;
|
|
73
|
+
/** Collapses whitespace and cuts to about `max` characters. */
|
|
74
|
+
export declare function clip(text: string, max?: number): string;
|
|
75
|
+
/**
|
|
76
|
+
* The label of an element: its template text cut to about 60 characters
|
|
77
|
+
* (holes arrive as written), an image's `alt`, or `undefined` when it has no
|
|
78
|
+
* words.
|
|
79
|
+
*/
|
|
80
|
+
export declare function elementLabel(element: HtmlElement): string | undefined;
|
package/src/template.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The template fragment (`<x-dc>` … last `</x-dc>`) as a tree, and the two
|
|
3
|
+
* ways to address one of its elements:
|
|
4
|
+
*
|
|
5
|
+
* - **tid**: every element numbered depth-first in source order from 0. All
|
|
6
|
+
* tags count (`<helmet>` and its `<style>`, `<sc-for>`, `<sc-if>`,
|
|
7
|
+
* `<dc-import>`); text and comments do not.
|
|
8
|
+
* - **path**: the index among the fragment's top-level elements, then the
|
|
9
|
+
* index among each element's element children, down to the element.
|
|
10
|
+
*
|
|
11
|
+
* Together they form a view-context address: `File.dc.html#tid:path`, the
|
|
12
|
+
* file stem percent-encoded with `encodeURIComponent`. An agent resolves an
|
|
13
|
+
* address against its own copy of the file by the same rules; when tid and
|
|
14
|
+
* path disagree, the copies differ.
|
|
15
|
+
*/
|
|
16
|
+
import { attributeValue, elementChildren, parseHtml, textContent, } from "./html.js";
|
|
17
|
+
/** Parses template text (the part between `<x-dc>` and the last `</x-dc>`). */
|
|
18
|
+
export function parseTemplate(text) {
|
|
19
|
+
const nodes = parseHtml(text);
|
|
20
|
+
const elements = [];
|
|
21
|
+
const visit = (list) => {
|
|
22
|
+
for (const node of list) {
|
|
23
|
+
if (node.type !== 'element')
|
|
24
|
+
continue;
|
|
25
|
+
elements.push(node);
|
|
26
|
+
visit(node.children);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
visit(nodes);
|
|
30
|
+
return { text, nodes, elements, tidOf: new Map(elements.map((element, tid) => [element, tid])) };
|
|
31
|
+
}
|
|
32
|
+
/** The child-index path of an element, top-level index first. */
|
|
33
|
+
export function elementPath(model, element) {
|
|
34
|
+
const path = [];
|
|
35
|
+
for (let node = element; node; node = node.parent) {
|
|
36
|
+
const siblings = node.parent ? elementChildren(node.parent) : elementChildren(model.nodes);
|
|
37
|
+
path.unshift(siblings.indexOf(node));
|
|
38
|
+
}
|
|
39
|
+
return path;
|
|
40
|
+
}
|
|
41
|
+
/** Finds an element by its path, or `null`. */
|
|
42
|
+
export function elementAtPath(model, path) {
|
|
43
|
+
let siblings = elementChildren(model.nodes);
|
|
44
|
+
let element = null;
|
|
45
|
+
for (const index of path) {
|
|
46
|
+
element = siblings[index] ?? null;
|
|
47
|
+
if (!element)
|
|
48
|
+
return null;
|
|
49
|
+
siblings = elementChildren(element);
|
|
50
|
+
}
|
|
51
|
+
return element;
|
|
52
|
+
}
|
|
53
|
+
/* -------------------------------------------------------------------------- */
|
|
54
|
+
/* Addresses */
|
|
55
|
+
/* -------------------------------------------------------------------------- */
|
|
56
|
+
/** Grammar of an encoded artboard file name. */
|
|
57
|
+
export const BOARD_NAME_PATTERN = /^(?:[A-Za-z0-9_.!~*'()-]|%[0-9A-F]{2}){1,200}\.dc\.html$/;
|
|
58
|
+
/** Grammar of an element address. */
|
|
59
|
+
export const ADDRESS_PATTERN = /^(?:[A-Za-z0-9_.!~*'()-]|%[0-9A-F]{2}){1,200}\.dc\.html#\d{1,4}:\d{1,2}(\/\d{1,2}){0,8}(@\d{1,3})?$/;
|
|
60
|
+
/** Longest encoded file name the view context writes. */
|
|
61
|
+
export const MAX_ENCODED_NAME = 220;
|
|
62
|
+
/**
|
|
63
|
+
* Encodes an artboard path for the view context: the base name with the part
|
|
64
|
+
* before `.dc.html` percent-encoded. Returns `null` when the result would not
|
|
65
|
+
* match {@link BOARD_NAME_PATTERN} (such entries are left out).
|
|
66
|
+
*/
|
|
67
|
+
export function encodeBoardName(path) {
|
|
68
|
+
const base = path.slice(path.lastIndexOf('/') + 1);
|
|
69
|
+
const match = /^(.*)\.dc\.html$/i.exec(base);
|
|
70
|
+
if (!match || !match[1])
|
|
71
|
+
return null;
|
|
72
|
+
const encoded = `${encodeURIComponent(match[1])}.dc.html`;
|
|
73
|
+
return encoded.length <= MAX_ENCODED_NAME && BOARD_NAME_PATTERN.test(encoded) ? encoded : null;
|
|
74
|
+
}
|
|
75
|
+
/** Builds `File.dc.html#tid:path`, or `null` when the grammar cannot express it. */
|
|
76
|
+
export function formatAddress(boardPath, tid, path) {
|
|
77
|
+
const file = encodeBoardName(boardPath);
|
|
78
|
+
if (!file || path.length === 0 || path.length > 9 || tid > 9999)
|
|
79
|
+
return null;
|
|
80
|
+
if (path.some((index) => index < 0 || index > 99))
|
|
81
|
+
return null;
|
|
82
|
+
const address = `${file}#${tid}:${path.join('/')}`;
|
|
83
|
+
return ADDRESS_PATTERN.test(address) ? address : null;
|
|
84
|
+
}
|
|
85
|
+
/** Parses an address; `null` when it does not match the grammar. */
|
|
86
|
+
export function parseAddress(address) {
|
|
87
|
+
if (!ADDRESS_PATTERN.test(address))
|
|
88
|
+
return null;
|
|
89
|
+
const hash = address.indexOf('#');
|
|
90
|
+
const file = address.slice(0, hash);
|
|
91
|
+
const rest = address.slice(hash + 1);
|
|
92
|
+
const [ref, instance] = rest.split('@');
|
|
93
|
+
const colon = ref.indexOf(':');
|
|
94
|
+
const tid = Number(ref.slice(0, colon));
|
|
95
|
+
const path = ref.slice(colon + 1).split('/').map(Number);
|
|
96
|
+
return instance === undefined ? { file, tid, path } : { file, tid, path, instance: Number(instance) };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Resolves a tid/path pair against a model. Returns the element only when
|
|
100
|
+
* both name the same node; otherwise the caller's copy differs from the
|
|
101
|
+
* viewer's.
|
|
102
|
+
*/
|
|
103
|
+
export function resolveElement(model, tid, path) {
|
|
104
|
+
const byTid = model.elements[tid];
|
|
105
|
+
const byPath = elementAtPath(model, path);
|
|
106
|
+
return byTid && byTid === byPath ? byTid : null;
|
|
107
|
+
}
|
|
108
|
+
const TEXT_TAGS = new Set([
|
|
109
|
+
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'span', 'a', 'button', 'label', 'li', 'td', 'th', 'strong', 'em',
|
|
110
|
+
'b', 'i', 'small', 'blockquote', 'figcaption', 'caption', 'code', 'pre', 'dt', 'dd', 'summary', 'legend',
|
|
111
|
+
]);
|
|
112
|
+
const SHAPE_TAGS = new Set(['div', 'section', 'article', 'aside', 'header', 'footer', 'main', 'nav', 'figure']);
|
|
113
|
+
const SVG_SHAPES = new Set(['rect', 'circle', 'ellipse', 'polygon', 'path']);
|
|
114
|
+
const SVG_LINES = new Set(['line', 'polyline']);
|
|
115
|
+
/** Template-level structure tags: they render no box of their own. */
|
|
116
|
+
export const STRUCTURE_TAGS = new Set(['helmet', 'sc-for', 'sc-if']);
|
|
117
|
+
/** True when the element holds words directly (text children, no element children). */
|
|
118
|
+
export function holdsText(element) {
|
|
119
|
+
const kids = elementChildren(element);
|
|
120
|
+
return kids.length === 0 && textContent(element).trim().length > 0;
|
|
121
|
+
}
|
|
122
|
+
/** Classifies an element. */
|
|
123
|
+
export function elementKind(element) {
|
|
124
|
+
const tag = element.tag;
|
|
125
|
+
if (tag === 'img' || tag === 'picture' || tag === 'image' || tag === 'video')
|
|
126
|
+
return 'image';
|
|
127
|
+
if (SVG_LINES.has(tag))
|
|
128
|
+
return 'line';
|
|
129
|
+
if (tag === 'svg') {
|
|
130
|
+
// An svg whose only drawing is lines (an arrow or rule) counts as a line.
|
|
131
|
+
const drawn = [];
|
|
132
|
+
const collect = (list) => {
|
|
133
|
+
for (const child of list) {
|
|
134
|
+
if (child.type !== 'element')
|
|
135
|
+
continue;
|
|
136
|
+
if (SVG_LINES.has(child.tag) || SVG_SHAPES.has(child.tag))
|
|
137
|
+
drawn.push(child);
|
|
138
|
+
collect(child.children);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
collect(element.children);
|
|
142
|
+
const isStroke = (child) => SVG_LINES.has(child.tag) || (child.tag === 'path' && attributeValue(child, 'fill')?.trim() === 'none');
|
|
143
|
+
if (drawn.length > 0 && drawn.every(isStroke))
|
|
144
|
+
return 'line';
|
|
145
|
+
return 'shape';
|
|
146
|
+
}
|
|
147
|
+
if (SVG_SHAPES.has(tag))
|
|
148
|
+
return 'shape';
|
|
149
|
+
if (TEXT_TAGS.has(tag) || holdsText(element))
|
|
150
|
+
return 'text';
|
|
151
|
+
if (SHAPE_TAGS.has(tag))
|
|
152
|
+
return 'shape';
|
|
153
|
+
return 'other';
|
|
154
|
+
}
|
|
155
|
+
/** Collapses whitespace and cuts to about `max` characters. */
|
|
156
|
+
export function clip(text, max = 60) {
|
|
157
|
+
const flat = text.replace(/\s+/g, ' ').trim();
|
|
158
|
+
return flat.length > max ? `${flat.slice(0, max - 1).trimEnd()}…` : flat;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The label of an element: its template text cut to about 60 characters
|
|
162
|
+
* (holes arrive as written), an image's `alt`, or `undefined` when it has no
|
|
163
|
+
* words.
|
|
164
|
+
*/
|
|
165
|
+
export function elementLabel(element) {
|
|
166
|
+
if (element.tag === 'img') {
|
|
167
|
+
const alt = attributeValue(element, 'alt')?.trim();
|
|
168
|
+
return alt ? clip(alt) : undefined;
|
|
169
|
+
}
|
|
170
|
+
if (element.tag === 'style' || element.tag === 'script' || element.tag === 'helmet')
|
|
171
|
+
return undefined;
|
|
172
|
+
const words = [];
|
|
173
|
+
const collect = (list) => {
|
|
174
|
+
for (const node of list) {
|
|
175
|
+
if (node.type === 'text')
|
|
176
|
+
words.push(node.value);
|
|
177
|
+
else if (node.type === 'element' && node.tag !== 'style' && node.tag !== 'script')
|
|
178
|
+
collect(node.children);
|
|
179
|
+
if (words.join(' ').length > 200)
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
collect(element.children);
|
|
184
|
+
const text = clip(words.join(' '));
|
|
185
|
+
return text || undefined;
|
|
186
|
+
}
|