@astl/editor 0.0.0 → 0.1.1
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/CHANGELOG.md +107 -0
- package/LICENSE +202 -0
- package/README.md +43 -0
- package/dist/authoring.d.ts +284 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +566 -0
- package/dist/authoring.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +35 -0
- package/dist/define.js.map +1 -0
- package/dist/edit.d.ts +45 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +150 -0
- package/dist/edit.js.map +1 -0
- package/dist/elements/base.d.ts +31 -0
- package/dist/elements/base.d.ts.map +1 -0
- package/dist/elements/base.js +75 -0
- package/dist/elements/base.js.map +1 -0
- package/dist/elements/color-control.d.ts +25 -0
- package/dist/elements/color-control.d.ts.map +1 -0
- package/dist/elements/color-control.js +139 -0
- package/dist/elements/color-control.js.map +1 -0
- package/dist/elements/help-form.d.ts +31 -0
- package/dist/elements/help-form.d.ts.map +1 -0
- package/dist/elements/help-form.js +371 -0
- package/dist/elements/help-form.js.map +1 -0
- package/dist/elements/insert-menu.d.ts +79 -0
- package/dist/elements/insert-menu.d.ts.map +1 -0
- package/dist/elements/insert-menu.js +362 -0
- package/dist/elements/insert-menu.js.map +1 -0
- package/dist/elements/inspector.d.ts +24 -0
- package/dist/elements/inspector.d.ts.map +1 -0
- package/dist/elements/inspector.js +846 -0
- package/dist/elements/inspector.js.map +1 -0
- package/dist/elements/issue-list.d.ts +18 -0
- package/dist/elements/issue-list.d.ts.map +1 -0
- package/dist/elements/issue-list.js +85 -0
- package/dist/elements/issue-list.js.map +1 -0
- package/dist/elements/issues.d.ts +24 -0
- package/dist/elements/issues.d.ts.map +1 -0
- package/dist/elements/issues.js +81 -0
- package/dist/elements/issues.js.map +1 -0
- package/dist/elements/picture-dialog.d.ts +40 -0
- package/dist/elements/picture-dialog.d.ts.map +1 -0
- package/dist/elements/picture-dialog.js +433 -0
- package/dist/elements/picture-dialog.js.map +1 -0
- package/dist/elements/picture-field.d.ts +33 -0
- package/dist/elements/picture-field.d.ts.map +1 -0
- package/dist/elements/picture-field.js +692 -0
- package/dist/elements/picture-field.js.map +1 -0
- package/dist/elements/preview.d.ts +34 -0
- package/dist/elements/preview.d.ts.map +1 -0
- package/dist/elements/preview.js +165 -0
- package/dist/elements/preview.js.map +1 -0
- package/dist/elements/style-form.d.ts +30 -0
- package/dist/elements/style-form.d.ts.map +1 -0
- package/dist/elements/style-form.js +640 -0
- package/dist/elements/style-form.js.map +1 -0
- package/dist/elements/style-load.d.ts +26 -0
- package/dist/elements/style-load.d.ts.map +1 -0
- package/dist/elements/style-load.js +50 -0
- package/dist/elements/style-load.js.map +1 -0
- package/dist/elements/style-popups.d.ts +39 -0
- package/dist/elements/style-popups.d.ts.map +1 -0
- package/dist/elements/style-popups.js +173 -0
- package/dist/elements/style-popups.js.map +1 -0
- package/dist/elements/style-rules.d.ts +21 -0
- package/dist/elements/style-rules.d.ts.map +1 -0
- package/dist/elements/style-rules.js +274 -0
- package/dist/elements/style-rules.js.map +1 -0
- package/dist/elements/styles.d.ts +9 -0
- package/dist/elements/styles.d.ts.map +1 -0
- package/dist/elements/styles.js +161 -0
- package/dist/elements/styles.js.map +1 -0
- package/dist/elements/toolbar.d.ts +65 -0
- package/dist/elements/toolbar.d.ts.map +1 -0
- package/dist/elements/toolbar.js +1270 -0
- package/dist/elements/toolbar.js.map +1 -0
- package/dist/help.d.ts +33 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +111 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/inspect.d.ts +251 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +428 -0
- package/dist/inspect.js.map +1 -0
- package/dist/messages/en.json +252 -0
- package/dist/messages/help/en.json +18 -0
- package/dist/messages/help/pl.json +18 -0
- package/dist/messages/pl.json +252 -0
- package/dist/messages/style/en.json +67 -0
- package/dist/messages/style/pl.json +67 -0
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/pages.d.ts +28 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +147 -0
- package/dist/pages.js.map +1 -0
- package/dist/scan.d.ts +82 -0
- package/dist/scan.d.ts.map +1 -0
- package/dist/scan.js +329 -0
- package/dist/scan.js.map +1 -0
- package/dist/style-messages.d.ts +7 -0
- package/dist/style-messages.d.ts.map +1 -0
- package/dist/style-messages.js +12 -0
- package/dist/style-messages.js.map +1 -0
- package/dist/style-model.d.ts +50 -0
- package/dist/style-model.d.ts.map +1 -0
- package/dist/style-model.js +76 -0
- package/dist/style-model.js.map +1 -0
- package/dist/style-placing.d.ts +38 -0
- package/dist/style-placing.d.ts.map +1 -0
- package/dist/style-placing.js +98 -0
- package/dist/style-placing.js.map +1 -0
- package/dist/textarea.d.ts +17 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +175 -0
- package/dist/textarea.js.map +1 -0
- package/package.json +54 -4
- package/src/authoring.ts +896 -0
- package/src/define.ts +34 -0
- package/src/edit.ts +195 -0
- package/src/elements/base.ts +93 -0
- package/src/elements/color-control.ts +159 -0
- package/src/elements/help-form.ts +404 -0
- package/src/elements/insert-menu.ts +481 -0
- package/src/elements/inspector.ts +941 -0
- package/src/elements/issue-list.ts +103 -0
- package/src/elements/issues.ts +89 -0
- package/src/elements/picture-dialog.ts +461 -0
- package/src/elements/picture-field.ts +770 -0
- package/src/elements/preview.ts +186 -0
- package/src/elements/style-form.ts +751 -0
- package/src/elements/style-load.ts +64 -0
- package/src/elements/style-popups.ts +257 -0
- package/src/elements/style-rules.ts +294 -0
- package/src/elements/styles.ts +162 -0
- package/src/elements/toolbar.ts +1386 -0
- package/src/help.ts +156 -0
- package/src/index.ts +81 -0
- package/src/inspect.ts +774 -0
- package/src/messages/en.json +252 -0
- package/src/messages/help/en.json +18 -0
- package/src/messages/help/pl.json +18 -0
- package/src/messages/pl.json +252 -0
- package/src/messages/style/en.json +67 -0
- package/src/messages/style/pl.json +67 -0
- package/src/messages.ts +11 -0
- package/src/pages.ts +158 -0
- package/src/scan.ts +382 -0
- package/src/style-messages.ts +13 -0
- package/src/style-model.ts +125 -0
- package/src/style-placing.ts +117 -0
- package/src/textarea.ts +182 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"style.section.text": "Tekst",
|
|
3
|
+
"style.section.box": "Tło i obramowanie",
|
|
4
|
+
"style.section.spacing": "Odstępy",
|
|
5
|
+
"style.section.size": "Rozmiar i położenie",
|
|
6
|
+
"style.section.named": "Styl nazwany",
|
|
7
|
+
"style.section.own": "Własny CSS",
|
|
8
|
+
"style.field.font-family": "Czcionka",
|
|
9
|
+
"style.field.font-size": "Rozmiar czcionki",
|
|
10
|
+
"style.field.font-weight": "Grubość",
|
|
11
|
+
"style.field.font-style": "Pochylenie",
|
|
12
|
+
"style.field.color": "Kolor tekstu",
|
|
13
|
+
"style.field.text-align": "Wyrównanie",
|
|
14
|
+
"style.field.line-height": "Interlinia",
|
|
15
|
+
"style.field.background-color": "Tło",
|
|
16
|
+
"style.field.border-width": "Grubość obramowania",
|
|
17
|
+
"style.field.border-style": "Rodzaj obramowania",
|
|
18
|
+
"style.field.border-color": "Kolor obramowania",
|
|
19
|
+
"style.field.border-radius": "Narożniki",
|
|
20
|
+
"style.field.padding": "Wypełnienie",
|
|
21
|
+
"style.field.margin-block-start": "Odstęp przed",
|
|
22
|
+
"style.field.margin-block-end": "Odstęp po",
|
|
23
|
+
"style.field.width": "Szerokość",
|
|
24
|
+
"style.field.position": "Położenie",
|
|
25
|
+
"style.field.gap": "Odstęp między kolumnami",
|
|
26
|
+
"style.field.align-items": "Wyrównanie kolumn",
|
|
27
|
+
"style.field.flex-grow": "Udział w szerokości",
|
|
28
|
+
"style.field.named": "Styl",
|
|
29
|
+
"style.field.own": "Deklaracje CSS",
|
|
30
|
+
"style.value.normal": "Normalna",
|
|
31
|
+
"style.value.bold": "Pogrubiona",
|
|
32
|
+
"style.value.italic": "Pochylona",
|
|
33
|
+
"style.value.start": "Do lewej",
|
|
34
|
+
"style.value.center": "Do środka",
|
|
35
|
+
"style.value.end": "Do prawej",
|
|
36
|
+
"style.value.justify": "Wyjustowane",
|
|
37
|
+
"style.value.none": "Brak",
|
|
38
|
+
"style.value.solid": "Ciągłe",
|
|
39
|
+
"style.value.dashed": "Kreskowane",
|
|
40
|
+
"style.value.dotted": "Kropkowane",
|
|
41
|
+
"style.value.double": "Podwójne",
|
|
42
|
+
"style.value.stretch": "Równa wysokość",
|
|
43
|
+
"style.named.none": "Bez stylu",
|
|
44
|
+
"style.save.label": "Nazwa nowego stylu",
|
|
45
|
+
"style.save": "Zapisz jako styl",
|
|
46
|
+
"style.refused.property": "„{name}” jest niedozwolone: styl ćwiczenia nie może przenosić go poza jego miejsce, zasłaniać innych treści ani niczego wczytywać.",
|
|
47
|
+
"style.refused.value": "Ta wartość „{name}” jest niedozwolona: sprawdź jednostkę i dopuszczalny zakres.",
|
|
48
|
+
"style.refused.text": "„{name}” to nie jest deklaracja: wpisz ją jako właściwość: wartość.",
|
|
49
|
+
"style.refused.length": "Ten styl jest za długi: najwyżej {max} znaków.",
|
|
50
|
+
"style.rules.tags": "Style tekstu",
|
|
51
|
+
"style.rules.empty": "Nie masz jeszcze własnych stylów. Dodaj styl tutaj albo zapisz styl elementu.",
|
|
52
|
+
"style.rules.edit": "Edytuj styl: {label}",
|
|
53
|
+
"style.rules.rename": "Zmień nazwę stylu: {label}",
|
|
54
|
+
"style.rules.renameShort": "Zmień nazwę",
|
|
55
|
+
"style.rules.renameField": "Nowa nazwa stylu: {label}",
|
|
56
|
+
"style.rules.delete": "Usuń styl: {label}",
|
|
57
|
+
"style.rules.deleteShort": "Usuń",
|
|
58
|
+
"style.rules.newLabel": "Nazwa nowego stylu",
|
|
59
|
+
"style.rules.add": "Dodaj styl",
|
|
60
|
+
"style.tag.paragraph": "Akapit",
|
|
61
|
+
"style.tag.heading": "Nagłówek",
|
|
62
|
+
"style.tag.subheading": "Podtytuł",
|
|
63
|
+
"style.tag.quote": "Cytat",
|
|
64
|
+
"style.tag.list": "Lista",
|
|
65
|
+
"style.tag.caption": "Podpis",
|
|
66
|
+
"style.tag.picture": "Obrazek"
|
|
67
|
+
}
|
package/src/messages.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MessageCatalog } from '@astl/core';
|
|
2
|
+
import en from './messages/en.json' with { type: 'json' };
|
|
3
|
+
import pl from './messages/pl.json' with { type: 'json' };
|
|
4
|
+
|
|
5
|
+
/** i18n namespace of the authoring kit's own UI (insert menu, inspector, issues, preview). */
|
|
6
|
+
export const EDITOR_NAMESPACE = 'astl.editor';
|
|
7
|
+
|
|
8
|
+
export const editorMessages: readonly MessageCatalog[] = [
|
|
9
|
+
{ namespace: EDITOR_NAMESPACE, locale: 'en', messages: en },
|
|
10
|
+
{ namespace: EDITOR_NAMESPACE, locale: 'pl', messages: pl },
|
|
11
|
+
];
|
package/src/pages.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page breaks, new pages and joins as edits of the source text, for text-based adapters (the textarea adapter's
|
|
3
|
+
* `splitPage`, `addPage` and `mergePage`): the rules of `EditorAdapter`, on the element ranges of `scan.ts`, so
|
|
4
|
+
* everything around an edit stays as written.
|
|
5
|
+
*/
|
|
6
|
+
import { CLASS_ATTRIBUTE, CONTENT_ATTRIBUTES, DOC_TAG, PAGE_TAG, STYLE_TAG } from '@astl/core';
|
|
7
|
+
import { attributeValue, type ElementRange, elementTree, tokenize, usedIds } from './scan.js';
|
|
8
|
+
|
|
9
|
+
/** A source after a page edit, and the offset of the caret in it. */
|
|
10
|
+
export interface PageEdit {
|
|
11
|
+
readonly source: string;
|
|
12
|
+
readonly caret: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A page edit that added a page: its id. */
|
|
16
|
+
export interface AddedPage extends PageEdit {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Blocks that break at the caret: the others break after themselves. */
|
|
21
|
+
const TEXT_BLOCKS: ReadonlySet<string> = new Set(['p', 'h2', 'h3', 'h4']);
|
|
22
|
+
|
|
23
|
+
/** Where an element's content ends: at its end tag, or where it was closed. */
|
|
24
|
+
const contentEnd = (range: ElementRange): number => range.endTag?.start ?? range.end;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The content of `<astl-doc>` (the whole source without one), its blocks and its pages. It starts after the named styles
|
|
28
|
+
* at its head: they stay there, out of any page.
|
|
29
|
+
*/
|
|
30
|
+
function documentOf(source: string) {
|
|
31
|
+
const roots = elementTree(source);
|
|
32
|
+
const doc = roots.find((range) => range.name === DOC_TAG);
|
|
33
|
+
const children = doc?.children ?? roots;
|
|
34
|
+
const content = children.findIndex((range) => range.name !== STYLE_TAG);
|
|
35
|
+
const blocks = content < 0 ? [] : children.slice(content);
|
|
36
|
+
return {
|
|
37
|
+
start: (content < 0 ? children.at(-1) : children[content - 1])?.end ?? doc?.startTag.end ?? 0,
|
|
38
|
+
end: doc === undefined ? source.length : contentEnd(doc),
|
|
39
|
+
blocks,
|
|
40
|
+
pages: blocks.filter((range) => range.name === PAGE_TAG),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `page-<n>` with the smallest n no id of the source uses; `used` takes it. */
|
|
45
|
+
function freshPage(used: Set<string>): string {
|
|
46
|
+
let n = 1;
|
|
47
|
+
while (used.has(`page-${n}`)) n++;
|
|
48
|
+
used.add(`page-${n}`);
|
|
49
|
+
return `page-${n}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Nothing a student would see: whitespace and formatting tags, no text, line break, picture or component. */
|
|
53
|
+
function blank(fragment: string): boolean {
|
|
54
|
+
let at = 0;
|
|
55
|
+
for (const tag of tokenize(fragment)) {
|
|
56
|
+
if (fragment.slice(at, tag.start).trim() !== '') return false;
|
|
57
|
+
if (tag.type === 'start' && (tag.name === 'br' || tag.name === 'img' || tag.name.includes('-')))
|
|
58
|
+
return false;
|
|
59
|
+
at = tag.end;
|
|
60
|
+
}
|
|
61
|
+
return fragment.slice(at).trim() === '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The elements from `block` down to the innermost one whose content holds `caret` (its formatting), or null when the
|
|
66
|
+
* caret is in a tag or in a component.
|
|
67
|
+
*/
|
|
68
|
+
function inlinePath(block: ElementRange, caret: number): ElementRange[] | null {
|
|
69
|
+
const path = [block];
|
|
70
|
+
for (let el = block; ; ) {
|
|
71
|
+
if (caret < el.startTag.end || caret > contentEnd(el)) return null;
|
|
72
|
+
const child = el.children.find((c) => c.start < caret && caret < c.end);
|
|
73
|
+
if (child === undefined) return path;
|
|
74
|
+
if (child.name.includes('-')) return null;
|
|
75
|
+
path.push(child);
|
|
76
|
+
el = child;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A page break at `caret` (`EditorAdapter.splitPage`): in a paragraph or a heading on a page, at the caret (the
|
|
82
|
+
* formatting around it closed before the break and opened again after it); at its start before it, at its end after
|
|
83
|
+
* it; in any other block (a layout, whatever holds the caret in it), or in a tag, after that block. Null when the caret
|
|
84
|
+
* is not in a page's content (or in the document's, without pages).
|
|
85
|
+
*/
|
|
86
|
+
export function splitPageSource(source: string, caret: number): AddedPage | null {
|
|
87
|
+
const doc = documentOf(source);
|
|
88
|
+
const page = doc.pages.find((p) => p.startTag.end <= caret && caret <= contentEnd(p));
|
|
89
|
+
if (doc.pages.length > 0 ? page === undefined : caret < doc.start || caret > doc.end) return null;
|
|
90
|
+
const [from, to] = page === undefined ? [doc.start, doc.end] : [page.startTag.end, contentEnd(page)];
|
|
91
|
+
const blocks = page?.children ?? doc.blocks;
|
|
92
|
+
let at = caret;
|
|
93
|
+
// In the middle of a paragraph: its formatting, closed before the break and opened again after it.
|
|
94
|
+
let close = '';
|
|
95
|
+
let open = '';
|
|
96
|
+
const block = blocks.find((b) => b.start < caret && caret < b.end);
|
|
97
|
+
if (block !== undefined) {
|
|
98
|
+
const path = TEXT_BLOCKS.has(block.name) ? inlinePath(block, caret) : null;
|
|
99
|
+
if (path === null) at = block.end;
|
|
100
|
+
else if (blank(source.slice(block.startTag.end, caret))) at = block.start;
|
|
101
|
+
else if (blank(source.slice(caret, contentEnd(block)))) at = block.end;
|
|
102
|
+
else {
|
|
103
|
+
close = path
|
|
104
|
+
.map((el) => `</${el.name}>`)
|
|
105
|
+
.reverse()
|
|
106
|
+
.join('');
|
|
107
|
+
open = path.map((el) => source.slice(el.startTag.start, el.startTag.end)).join('');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const used = usedIds(source);
|
|
111
|
+
const first = page === undefined ? `<astl-page id="${freshPage(used)}">` : '';
|
|
112
|
+
const id = freshPage(used);
|
|
113
|
+
// The new page keeps the content language and the look of what moves to it, as written.
|
|
114
|
+
const language = (page?.startTag.attributes ?? [])
|
|
115
|
+
.filter((a) => CONTENT_ATTRIBUTES.has(a.name) || a.name === CLASS_ATTRIBUTE)
|
|
116
|
+
.map((a) => ` ${source.slice(a.start, a.end)}`)
|
|
117
|
+
.join('');
|
|
118
|
+
// A side left without blocks gets an empty paragraph, as an editor needs one to put the cursor in.
|
|
119
|
+
const before = open !== '' || blocks.some((b) => b.end <= at) ? '' : '<p></p>';
|
|
120
|
+
const empty = open === '' && !blocks.some((b) => b.start >= at);
|
|
121
|
+
const left =
|
|
122
|
+
`${source.slice(0, from)}${first}${source.slice(from, at)}${close}${before}</astl-page>` +
|
|
123
|
+
`<astl-page id="${id}"${language}>${empty ? '<p>' : open}`;
|
|
124
|
+
const right = `${empty ? '</p>' : ''}${source.slice(at, to)}${first === '' ? '' : '</astl-page>'}${source.slice(to)}`;
|
|
125
|
+
return { source: left + right, caret: left.length, id };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* An empty page (one empty paragraph) after the page `after`, else after the last page (`EditorAdapter.addPage`); in a
|
|
130
|
+
* document without pages, after its content made the first page. The caret goes into the paragraph.
|
|
131
|
+
*/
|
|
132
|
+
export function addPageSource(source: string, after?: string): AddedPage {
|
|
133
|
+
const doc = documentOf(source);
|
|
134
|
+
const used = usedIds(source);
|
|
135
|
+
const page = doc.pages.find((p) => attributeValue(p.startTag, 'id') === after) ?? doc.pages.at(-1);
|
|
136
|
+
const head =
|
|
137
|
+
page === undefined
|
|
138
|
+
? `${source.slice(0, doc.start)}<astl-page id="${freshPage(used)}">${source.slice(doc.start, doc.end)}</astl-page>`
|
|
139
|
+
: source.slice(0, page.end);
|
|
140
|
+
const id = freshPage(used);
|
|
141
|
+
const left = `${head}<astl-page id="${id}"><p>`;
|
|
142
|
+
return { source: `${left}</p></astl-page>${source.slice(page?.end ?? doc.end)}`, caret: left.length, id };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The page `id` joined to the page before it (`EditorAdapter.mergePage`): the end tag of that page and the start tag
|
|
147
|
+
* of this one go, so nothing between them is lost. The caret goes where its content starts. Null for the first page
|
|
148
|
+
* or an id that is not a page's.
|
|
149
|
+
*/
|
|
150
|
+
export function mergePageSource(source: string, id: string): PageEdit | null {
|
|
151
|
+
const { pages } = documentOf(source);
|
|
152
|
+
const index = pages.findIndex((p) => attributeValue(p.startTag, 'id') === id);
|
|
153
|
+
const end = pages[index - 1]?.endTag;
|
|
154
|
+
const start = pages[index]?.startTag;
|
|
155
|
+
if (index < 1 || end == null || start === undefined) return null;
|
|
156
|
+
const left = source.slice(0, end.start) + source.slice(end.end, start.start);
|
|
157
|
+
return { source: left + source.slice(start.end), caret: left.length };
|
|
158
|
+
}
|
package/src/scan.ts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, tolerant HTML tokenizer for text-based editing. It only locates tags (with exact source offsets), so edits
|
|
3
|
+
* can splice the source and leave everything else byte-identical. Reading values the way a browser would (entities,
|
|
4
|
+
* tree construction) is the engine's `parseHtml`.
|
|
5
|
+
*/
|
|
6
|
+
import { COLUMN_TAG, COLUMNS_TAG, HELP_TAGS, PAGE_TAG, STYLE_TAG } from '@astl/core';
|
|
7
|
+
import { parseHtml } from '@astl/engine';
|
|
8
|
+
|
|
9
|
+
export interface AttributeToken {
|
|
10
|
+
/** Lowercase name. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** Value as written (entities not decoded); null for a bare attribute. */
|
|
13
|
+
readonly raw: string | null;
|
|
14
|
+
/** Offset of the name. */
|
|
15
|
+
readonly start: number;
|
|
16
|
+
/** Offset after the value (after the closing quote), or after the name of a bare attribute. */
|
|
17
|
+
readonly end: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TagToken {
|
|
21
|
+
readonly type: 'start' | 'end';
|
|
22
|
+
/** Lowercase name. */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
/** Offset of `<`. */
|
|
25
|
+
readonly start: number;
|
|
26
|
+
/** Offset after `>`, or the end of the source for an unterminated tag. */
|
|
27
|
+
readonly end: number;
|
|
28
|
+
readonly attributes: readonly AttributeToken[];
|
|
29
|
+
/** Written as `<name ... />`. */
|
|
30
|
+
readonly selfClosing: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** An element located in the source. `end` is after its end tag, or where it was implicitly closed. */
|
|
34
|
+
export interface ElementRange {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly start: number;
|
|
37
|
+
readonly end: number;
|
|
38
|
+
readonly startTag: TagToken;
|
|
39
|
+
readonly endTag: TagToken | null;
|
|
40
|
+
readonly children: readonly ElementRange[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A page or a component found by `scanElements`. Offsets index into the scanned string. */
|
|
44
|
+
export interface ScannedElement {
|
|
45
|
+
readonly tag: string;
|
|
46
|
+
/** The `id` attribute (entities decoded), or null when absent. */
|
|
47
|
+
readonly id: string | null;
|
|
48
|
+
readonly start: number;
|
|
49
|
+
readonly end: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Elements whose content the HTML parser reads as raw text: tags inside them are not tags. */
|
|
53
|
+
const RAW_TEXT: ReadonlySet<string> = new Set([
|
|
54
|
+
'script',
|
|
55
|
+
'style',
|
|
56
|
+
'textarea',
|
|
57
|
+
'title',
|
|
58
|
+
'xmp',
|
|
59
|
+
'iframe',
|
|
60
|
+
'noembed',
|
|
61
|
+
'noframes',
|
|
62
|
+
'noscript',
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
const VOID: ReadonlySet<string> = new Set([
|
|
66
|
+
'area',
|
|
67
|
+
'base',
|
|
68
|
+
'br',
|
|
69
|
+
'col',
|
|
70
|
+
'embed',
|
|
71
|
+
'hr',
|
|
72
|
+
'img',
|
|
73
|
+
'input',
|
|
74
|
+
'link',
|
|
75
|
+
'meta',
|
|
76
|
+
'source',
|
|
77
|
+
'track',
|
|
78
|
+
'wbr',
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
const DOC_TAG = 'astl-doc';
|
|
82
|
+
|
|
83
|
+
function isSpace(code: number): boolean {
|
|
84
|
+
return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isAsciiLetter(code: number): boolean {
|
|
88
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Offset after a comment that starts at `at` (`<!--`). */
|
|
92
|
+
function commentEnd(source: string, at: number): number {
|
|
93
|
+
if (source.startsWith('<!-->', at)) return at + 5;
|
|
94
|
+
if (source.startsWith('<!--->', at)) return at + 6;
|
|
95
|
+
const ends = [source.indexOf('-->', at + 4), source.indexOf('--!>', at + 4)].filter((i) => i >= 0);
|
|
96
|
+
if (ends.length === 0) return source.length;
|
|
97
|
+
const end = Math.min(...ends);
|
|
98
|
+
return end + (source.startsWith('-->', end) ? 3 : 4);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Offset after the next `>` (bogus comments, doctypes), or the end. */
|
|
102
|
+
function afterNextGt(source: string, from: number): number {
|
|
103
|
+
const gt = source.indexOf('>', from);
|
|
104
|
+
return gt < 0 ? source.length : gt + 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Offset of the end tag that closes a raw text element, or the end of the source. */
|
|
108
|
+
function rawTextEnd(source: string, from: number, name: string): number {
|
|
109
|
+
const re = new RegExp(`</${name}(?=[\\t\\n\\f\\r />])`, 'gi');
|
|
110
|
+
re.lastIndex = from;
|
|
111
|
+
return re.exec(source)?.index ?? source.length;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function readTag(source: string, start: number, type: 'start' | 'end'): TagToken {
|
|
115
|
+
const n = source.length;
|
|
116
|
+
let i = start + (type === 'end' ? 2 : 1);
|
|
117
|
+
const nameStart = i;
|
|
118
|
+
while (i < n) {
|
|
119
|
+
const c = source.charCodeAt(i);
|
|
120
|
+
if (isSpace(c) || c === 47 /* / */ || c === 62 /* > */) break;
|
|
121
|
+
i++;
|
|
122
|
+
}
|
|
123
|
+
const name = source.slice(nameStart, i).toLowerCase();
|
|
124
|
+
const attributes: AttributeToken[] = [];
|
|
125
|
+
const done = (end: number, selfClosing: boolean): TagToken => ({
|
|
126
|
+
type,
|
|
127
|
+
name,
|
|
128
|
+
start,
|
|
129
|
+
end,
|
|
130
|
+
attributes,
|
|
131
|
+
selfClosing,
|
|
132
|
+
});
|
|
133
|
+
while (i < n) {
|
|
134
|
+
const c = source.charCodeAt(i);
|
|
135
|
+
if (isSpace(c)) {
|
|
136
|
+
i++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (c === 62) return done(i + 1, false);
|
|
140
|
+
if (c === 47) {
|
|
141
|
+
if (source.charCodeAt(i + 1) === 62) return done(i + 2, true);
|
|
142
|
+
i++;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// Attribute name: up to space, `/`, `>` or `=` (a leading `=` belongs to the name, as in the HTML tokenizer).
|
|
146
|
+
const attrStart = i;
|
|
147
|
+
i++;
|
|
148
|
+
while (i < n) {
|
|
149
|
+
const d = source.charCodeAt(i);
|
|
150
|
+
if (isSpace(d) || d === 47 || d === 62 || d === 61 /* = */) break;
|
|
151
|
+
i++;
|
|
152
|
+
}
|
|
153
|
+
const attrName = source.slice(attrStart, i).toLowerCase();
|
|
154
|
+
let j = i;
|
|
155
|
+
while (j < n && isSpace(source.charCodeAt(j))) j++;
|
|
156
|
+
if (source.charCodeAt(j) !== 61) {
|
|
157
|
+
attributes.push({ name: attrName, raw: null, start: attrStart, end: i });
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
j++;
|
|
161
|
+
while (j < n && isSpace(source.charCodeAt(j))) j++;
|
|
162
|
+
const quote = source[j];
|
|
163
|
+
if (quote === '"' || quote === "'") {
|
|
164
|
+
const close = source.indexOf(quote, j + 1);
|
|
165
|
+
const end = close < 0 ? n : close + 1;
|
|
166
|
+
attributes.push({
|
|
167
|
+
name: attrName,
|
|
168
|
+
raw: source.slice(j + 1, close < 0 ? n : close),
|
|
169
|
+
start: attrStart,
|
|
170
|
+
end,
|
|
171
|
+
});
|
|
172
|
+
i = end;
|
|
173
|
+
} else {
|
|
174
|
+
let k = j;
|
|
175
|
+
while (k < n && !isSpace(source.charCodeAt(k)) && source.charCodeAt(k) !== 62) k++;
|
|
176
|
+
attributes.push({ name: attrName, raw: source.slice(j, k), start: attrStart, end: k });
|
|
177
|
+
i = k;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return done(n, false);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Start and end tags in source order. Comments, doctypes and bogus comments are skipped, and so is the content of raw
|
|
185
|
+
* text elements (`script`, `style`, `textarea`, ...). A `<` that does not start a tag is text.
|
|
186
|
+
*/
|
|
187
|
+
export function tokenize(source: string): TagToken[] {
|
|
188
|
+
const out: TagToken[] = [];
|
|
189
|
+
const n = source.length;
|
|
190
|
+
let i = 0;
|
|
191
|
+
while (i < n) {
|
|
192
|
+
const lt = source.indexOf('<', i);
|
|
193
|
+
if (lt < 0) break;
|
|
194
|
+
i = lt;
|
|
195
|
+
const next = source.charCodeAt(i + 1);
|
|
196
|
+
if (source.startsWith('<!--', i)) {
|
|
197
|
+
i = commentEnd(source, i);
|
|
198
|
+
} else if (next === 33 /* ! */ || next === 63 /* ? */) {
|
|
199
|
+
i = afterNextGt(source, i + 2);
|
|
200
|
+
} else if (next === 47 /* / */) {
|
|
201
|
+
const c = source.charCodeAt(i + 2);
|
|
202
|
+
if (isAsciiLetter(c)) {
|
|
203
|
+
const tag = readTag(source, i, 'end');
|
|
204
|
+
out.push(tag);
|
|
205
|
+
i = tag.end;
|
|
206
|
+
} else {
|
|
207
|
+
i = c === 62 ? i + 3 : afterNextGt(source, i + 2);
|
|
208
|
+
}
|
|
209
|
+
} else if (isAsciiLetter(next)) {
|
|
210
|
+
const tag = readTag(source, i, 'start');
|
|
211
|
+
out.push(tag);
|
|
212
|
+
i = tag.end;
|
|
213
|
+
if (RAW_TEXT.has(tag.name)) i = rawTextEnd(source, i, tag.name);
|
|
214
|
+
else if (tag.name === 'plaintext') i = n;
|
|
215
|
+
} else {
|
|
216
|
+
i++;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface OpenElement {
|
|
223
|
+
readonly name: string;
|
|
224
|
+
readonly start: number;
|
|
225
|
+
readonly startTag: TagToken;
|
|
226
|
+
readonly children: ElementRange[];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function isCustom(name: string): boolean {
|
|
230
|
+
return name.includes('-');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The element tree of `source` with offsets. Tolerant: an end tag closes the nearest open element of its name (and
|
|
235
|
+
* everything opened inside it); stray end tags are ignored; elements still open at the end close there. Custom
|
|
236
|
+
* elements written `<x-y ... />` count as closed (the author's intent; HTML would leave them open).
|
|
237
|
+
*/
|
|
238
|
+
export function elementTree(source: string): ElementRange[] {
|
|
239
|
+
const roots: ElementRange[] = [];
|
|
240
|
+
const stack: OpenElement[] = [];
|
|
241
|
+
const add = (range: ElementRange) => (stack[stack.length - 1]?.children ?? roots).push(range);
|
|
242
|
+
const close = (open: OpenElement, end: number, endTag: TagToken | null) =>
|
|
243
|
+
add({
|
|
244
|
+
name: open.name,
|
|
245
|
+
start: open.start,
|
|
246
|
+
end,
|
|
247
|
+
startTag: open.startTag,
|
|
248
|
+
endTag,
|
|
249
|
+
children: open.children,
|
|
250
|
+
});
|
|
251
|
+
for (const tag of tokenize(source)) {
|
|
252
|
+
if (tag.type === 'start') {
|
|
253
|
+
if (VOID.has(tag.name) || (tag.selfClosing && isCustom(tag.name))) {
|
|
254
|
+
add({ name: tag.name, start: tag.start, end: tag.end, startTag: tag, endTag: null, children: [] });
|
|
255
|
+
} else {
|
|
256
|
+
stack.push({ name: tag.name, start: tag.start, startTag: tag, children: [] });
|
|
257
|
+
}
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
let index = stack.length - 1;
|
|
261
|
+
while (index >= 0 && stack[index]?.name !== tag.name) index--;
|
|
262
|
+
if (index < 0) continue;
|
|
263
|
+
while (stack.length - 1 > index) close(stack.pop() as OpenElement, tag.start, null);
|
|
264
|
+
close(stack.pop() as OpenElement, tag.end, tag);
|
|
265
|
+
}
|
|
266
|
+
while (stack.length > 0) close(stack.pop() as OpenElement, source.length, null);
|
|
267
|
+
return roots;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Decodes entities in an attribute value, exactly as the HTML parser does. */
|
|
271
|
+
export function decodeAttribute(raw: string): string {
|
|
272
|
+
if (!raw.includes('&')) return raw;
|
|
273
|
+
const [node] = parseHtml(`<b title="${raw.replaceAll('"', '"')}"></b>`);
|
|
274
|
+
return node?.type === 'element' ? (node.attrs.title ?? raw) : raw;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Decoded value of the first attribute called `name`; '' for a bare attribute, null when absent. */
|
|
278
|
+
export function attributeValue(tag: TagToken, name: string): string | null {
|
|
279
|
+
const attribute = tag.attributes.find((a) => a.name === name);
|
|
280
|
+
if (attribute === undefined) return null;
|
|
281
|
+
return attribute.raw === null ? '' : decodeAttribute(attribute.raw);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface ScannedElementRange extends ScannedElement {
|
|
285
|
+
readonly range: ElementRange;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Pages and components with their ranges (internal form of `scanElements`). */
|
|
289
|
+
export function scanRanges(source: string): ScannedElementRange[] {
|
|
290
|
+
const out: ScannedElementRange[] = [];
|
|
291
|
+
const walk = (ranges: readonly ElementRange[]) => {
|
|
292
|
+
for (const range of ranges) {
|
|
293
|
+
const { name } = range;
|
|
294
|
+
// Neither a named style nor a layout is a component; what a layout holds is on its page, and so is what follows
|
|
295
|
+
// a self-closed named style, which HTML reads as its content.
|
|
296
|
+
// Nor is help for students on a page: what it holds (a component, wrongly) is on the page too.
|
|
297
|
+
if (name === STYLE_TAG || name === COLUMNS_TAG || name === COLUMN_TAG || HELP_TAGS.has(name)) {
|
|
298
|
+
walk(range.children);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (name === PAGE_TAG || (isCustom(name) && name !== DOC_TAG)) {
|
|
302
|
+
const id = attributeValue(range.startTag, 'id');
|
|
303
|
+
out.push({ tag: name, id, start: range.start, end: range.end, range });
|
|
304
|
+
// Components do not nest: custom elements inside one are its children (options, alternatives).
|
|
305
|
+
if (name !== PAGE_TAG) continue;
|
|
306
|
+
}
|
|
307
|
+
walk(range.children);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
walk(elementTree(source));
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Pages (`astl-page`) and components in document order, with offsets into `source`, for text-based adapters. An
|
|
316
|
+
* component is any custom element outside another component (so a misspelt tag is found too), but for the tags of the
|
|
317
|
+
* format: a layout's are looked through, named styles are skipped; custom elements inside a component are its children.
|
|
318
|
+
* Tolerant of messy input: comments, quoted `>` in attributes, any case, unclosed or self-closed elements.
|
|
319
|
+
*/
|
|
320
|
+
export function scanElements(source: string): ScannedElement[] {
|
|
321
|
+
return scanRanges(source).map(({ tag, id, start, end }) => ({ tag, id, start, end }));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Every `id` attribute value in the source (pages, components and local child ids). */
|
|
325
|
+
export function usedIds(source: string): Set<string> {
|
|
326
|
+
const ids = new Set<string>();
|
|
327
|
+
for (const tag of tokenize(source)) {
|
|
328
|
+
if (tag.type !== 'start') continue;
|
|
329
|
+
const id = attributeValue(tag, 'id');
|
|
330
|
+
if (id !== null) ids.add(id);
|
|
331
|
+
}
|
|
332
|
+
return ids;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const NBSP = String.fromCodePoint(0xa0);
|
|
336
|
+
const TEXT_SPECIALS = new RegExp(`[&<>${NBSP}]`, 'g');
|
|
337
|
+
const ATTRIBUTE_SPECIALS = new RegExp(`[&"<>${NBSP}]`, 'g');
|
|
338
|
+
const HTML_SPECIALS = new RegExp(`[&<>"'${NBSP}]`, 'g');
|
|
339
|
+
const ENTITIES: Readonly<Record<string, string>> = {
|
|
340
|
+
'&': '&',
|
|
341
|
+
'<': '<',
|
|
342
|
+
'>': '>',
|
|
343
|
+
'"': '"',
|
|
344
|
+
"'": ''',
|
|
345
|
+
[NBSP]: ' ',
|
|
346
|
+
};
|
|
347
|
+
const entity = (ch: string) => ENTITIES[ch] ?? ch;
|
|
348
|
+
|
|
349
|
+
export function escapeText(value: string): string {
|
|
350
|
+
return value.replace(TEXT_SPECIALS, entity);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function escapeAttribute(value: string): string {
|
|
354
|
+
return value.replace(ATTRIBUTE_SPECIALS, entity);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Safe in text and in quoted attribute values. */
|
|
358
|
+
export function escapeHtml(value: string): string {
|
|
359
|
+
return value.replace(HTML_SPECIALS, entity);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export interface Edit {
|
|
363
|
+
readonly start: number;
|
|
364
|
+
readonly end: number;
|
|
365
|
+
readonly text: string;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Applies non-overlapping edits. */
|
|
369
|
+
export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
370
|
+
let out = source;
|
|
371
|
+
for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
|
|
372
|
+
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
373
|
+
}
|
|
374
|
+
return out;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Start of the whitespace run that ends at `at` (or `at` itself). */
|
|
378
|
+
export function spaceBefore(source: string, at: number): number {
|
|
379
|
+
let i = at;
|
|
380
|
+
while (i > 0 && isSpace(source.charCodeAt(i - 1))) i--;
|
|
381
|
+
return i;
|
|
382
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MessageCatalog } from '@astl/core';
|
|
2
|
+
import en from './messages/style/en.json' with { type: 'json' };
|
|
3
|
+
import pl from './messages/style/pl.json' with { type: 'json' };
|
|
4
|
+
import { EDITOR_NAMESPACE } from './messages.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What only `<astl-style-form>` and `<astl-style-rules>` say. They bring it with them, outside the editor's first load,
|
|
8
|
+
* and add it to the authoring they are given.
|
|
9
|
+
*/
|
|
10
|
+
export const styleMessages: readonly MessageCatalog[] = [
|
|
11
|
+
{ namespace: EDITOR_NAMESPACE, locale: 'en', messages: en },
|
|
12
|
+
{ namespace: EDITOR_NAMESPACE, locale: 'pl', messages: pl },
|
|
13
|
+
];
|