@open-artifact/dc 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +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/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/dc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Design Component (.dc.html) format: parser, template addressing, edits, checks, React renderer and the in-frame board runtime.
|
|
4
|
+
|
|
5
|
+
Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
|
|
6
|
+
and you view, edit and comment on in one local page. Source: [`packages/dc`](https://github.com/JimLiu/open-artifact/tree/main/packages/dc).
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/dc",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Design Component (.dc.html) format: parser, template addressing, edits, checks, React renderer and the in-frame board runtime.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./html": {
|
|
12
|
+
"types": "./src/html.d.ts",
|
|
13
|
+
"default": "./src/html.js"
|
|
14
|
+
},
|
|
15
|
+
"./render": {
|
|
16
|
+
"types": "./src/render.d.ts",
|
|
17
|
+
"default": "./src/render.js"
|
|
18
|
+
},
|
|
19
|
+
"./protocol": {
|
|
20
|
+
"types": "./src/protocol.d.ts",
|
|
21
|
+
"default": "./src/protocol.js"
|
|
22
|
+
},
|
|
23
|
+
"./runtime": {
|
|
24
|
+
"types": "./src/runtime/index.d.ts",
|
|
25
|
+
"default": "./src/runtime/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"NOTICE"
|
|
33
|
+
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^19.2.4",
|
|
36
|
+
"react-dom": "^19.2.4"
|
|
37
|
+
},
|
|
38
|
+
"license": "Apache-2.0",
|
|
39
|
+
"author": {
|
|
40
|
+
"name": "Jim Liu 宝玉",
|
|
41
|
+
"url": "https://github.com/JimLiu"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
47
|
+
"directory": "packages/dc"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/check.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static checks of one `.dc.html` file: the rules an author (usually an
|
|
3
|
+
* agent) must follow for the editor to load, edit and save it faithfully.
|
|
4
|
+
* Errors break the artboard; warnings degrade it.
|
|
5
|
+
*/
|
|
6
|
+
/** One finding. */
|
|
7
|
+
export interface Diagnostic {
|
|
8
|
+
level: 'error' | 'warning';
|
|
9
|
+
message: string;
|
|
10
|
+
/** 1-based line in the file, when known. */
|
|
11
|
+
line?: number;
|
|
12
|
+
}
|
|
13
|
+
/** Path rule for artboard files: segments start with a letter, digit or `_`. */
|
|
14
|
+
export declare const BOARD_PATH_PATTERN: RegExp;
|
|
15
|
+
/** True when `path` is a valid artboard path. */
|
|
16
|
+
export declare function isBoardPath(path: string): boolean;
|
|
17
|
+
/** Options of {@link checkDcFile}. */
|
|
18
|
+
export interface CheckOptions {
|
|
19
|
+
/** Every project file path, to verify `<dc-import>` targets and links. */
|
|
20
|
+
files?: ReadonlySet<string>;
|
|
21
|
+
/** The board's size in `canvas.json` (`w`, `h`), to compare with `$preview`. */
|
|
22
|
+
board?: {
|
|
23
|
+
w: number;
|
|
24
|
+
h: number;
|
|
25
|
+
};
|
|
26
|
+
/** Whether an upload exists (`/_blob/<id>`, 32 hex); without it uploads are not checked. */
|
|
27
|
+
blobExists?: (id: string) => boolean;
|
|
28
|
+
}
|
|
29
|
+
/** Checks a `.dc.html` file. */
|
|
30
|
+
export declare function checkDcFile(path: string, source: string, options?: CheckOptions): Diagnostic[];
|
package/src/check.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static checks of one `.dc.html` file: the rules an author (usually an
|
|
3
|
+
* agent) must follow for the editor to load, edit and save it faithfully.
|
|
4
|
+
* Errors break the artboard; warnings degrade it.
|
|
5
|
+
*/
|
|
6
|
+
import { SUPPORT_SCRIPT_TAG, parseDcDocument } from "./document.js";
|
|
7
|
+
import { VOID_ELEMENTS, attributeValue } from "./html.js";
|
|
8
|
+
import { splitHoles, wholeHole } from "./holes.js";
|
|
9
|
+
import { parseTemplate } from "./template.js";
|
|
10
|
+
/** Path rule for artboard files: segments start with a letter, digit or `_`. */
|
|
11
|
+
export const BOARD_PATH_PATTERN = /^(?:[A-Za-z0-9_][A-Za-z0-9_.-]*\/)*[A-Za-z0-9_][A-Za-z0-9_.-]*\.dc\.html$/;
|
|
12
|
+
/** True when `path` is a valid artboard path. */
|
|
13
|
+
export function isBoardPath(path) {
|
|
14
|
+
return BOARD_PATH_PATTERN.test(path) && !path.split('/').some((part) => part === '.' || part === '..');
|
|
15
|
+
}
|
|
16
|
+
/** Hosts a page may load from: Google Fonts stylesheets and font files. */
|
|
17
|
+
const ALLOWED_REMOTE = [/^https:\/\/fonts\.googleapis\.com\/css2\?/i, /^https:\/\/fonts\.gstatic\.com\//i];
|
|
18
|
+
/** Elements that embed other documents; never allowed. */
|
|
19
|
+
const EMBEDS = new Set(['iframe', 'object', 'embed', 'frame', 'frameset', 'portal']);
|
|
20
|
+
/** Attributes that load something. */
|
|
21
|
+
const URL_ATTRIBUTES = ['src', 'href', 'srcset', 'poster', 'data', 'action', 'formaction', 'xlink:href'];
|
|
22
|
+
/** An upload id. */
|
|
23
|
+
const BLOB_ID_PATTERN = /^[0-9a-f]{32}$/;
|
|
24
|
+
/** `/_blob/<id>`, and a design system's `../_blob/<id>`. */
|
|
25
|
+
const isBlobUrl = (url) => /^(?:\.{1,2}\/)*\/?_blob\//.test(url.trim());
|
|
26
|
+
/** An address on another host (`https://…`, `//…`). */
|
|
27
|
+
const isRemote = (url) => /^(?:[a-z][a-z0-9+.-]*:)?\/\//i.test(url.trim());
|
|
28
|
+
const isGoogleFonts = (url) => /^(?:https?:)?\/\/fonts\.googleapis\.com\//i.test(url.trim());
|
|
29
|
+
/** Checks a `.dc.html` file. */
|
|
30
|
+
export function checkDcFile(path, source, options = {}) {
|
|
31
|
+
const out = [];
|
|
32
|
+
const lineAt = (offset) => source.slice(0, offset).split('\n').length;
|
|
33
|
+
if (!isBoardPath(path)) {
|
|
34
|
+
out.push({ level: 'error', message: `"${path}" is not a valid artboard path: segments start with a letter, digit or _, then only letters, digits, _ . -, ending in .dc.html.` });
|
|
35
|
+
}
|
|
36
|
+
const doc = parseDcDocument(source);
|
|
37
|
+
if (!doc.hasSupportScript)
|
|
38
|
+
out.push({ level: 'error', message: `The <head> must contain exactly ${SUPPORT_SCRIPT_TAG}` });
|
|
39
|
+
if (!doc.template) {
|
|
40
|
+
out.push({ level: 'error', message: 'No <x-dc> … </x-dc> template.' });
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
if (!doc.title)
|
|
44
|
+
out.push({ level: 'warning', message: 'No <title>: give each artboard a short title.' });
|
|
45
|
+
if (!doc.lang)
|
|
46
|
+
out.push({ level: 'warning', message: 'No lang on <html>.' });
|
|
47
|
+
if (doc.propsError)
|
|
48
|
+
out.push({ level: 'error', message: doc.propsError, line: doc.script?.propsAttr ? lineAt(doc.script.propsAttr.start) : undefined });
|
|
49
|
+
// Exactly one logic block, sized like its board.
|
|
50
|
+
const logicBlocks = [...source.matchAll(/<script\b[^>]*\bdata-dc-script\b[^>]*>/gi)];
|
|
51
|
+
if (!doc.script) {
|
|
52
|
+
out.push({ level: 'error', message: 'Missing the <script type="text/x-dc" data-dc-script data-props=\'…\'> block (class Component extends DCLogic).' });
|
|
53
|
+
}
|
|
54
|
+
else if (logicBlocks.length > 1) {
|
|
55
|
+
out.push({ level: 'error', message: `Has ${logicBlocks.length} data-dc-script blocks; an artboard has exactly one.`, line: lineAt(logicBlocks[1].index) });
|
|
56
|
+
}
|
|
57
|
+
const preview = doc.props?.$preview;
|
|
58
|
+
if (doc.script && !doc.propsError && !preview) {
|
|
59
|
+
out.push({ level: 'warning', message: 'data-props has no "$preview": {"width": …, "height": …}; give it the board\'s w and h.', line: lineAt(doc.script.start) });
|
|
60
|
+
}
|
|
61
|
+
if (preview && options.board && (preview.width !== options.board.w || preview.height !== options.board.h)) {
|
|
62
|
+
out.push({
|
|
63
|
+
level: 'error',
|
|
64
|
+
message: `$preview is ${preview.width}×${preview.height} but canvas.json boards gives ${options.board.w}×${options.board.h}; make them equal.`,
|
|
65
|
+
line: doc.script?.propsAttr ? lineAt(doc.script.propsAttr.start) : undefined,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Every inline script (the logic block and any other): no UI built from script, no globals, no keys, no network.
|
|
69
|
+
for (const match of source.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi)) {
|
|
70
|
+
const attrs = match[1] ?? '';
|
|
71
|
+
const inTemplate = doc.template !== null && match.index > doc.template.start && match.index < doc.template.end;
|
|
72
|
+
if (inTemplate)
|
|
73
|
+
continue; // reported below: template scripts never run
|
|
74
|
+
const src = /\bsrc\s*=\s*["']([^"']*)["']/i.exec(attrs)?.[1];
|
|
75
|
+
if (src !== undefined) {
|
|
76
|
+
checkHeadRef(path, src, '<script src>', options, lineAt(match.index), out);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
checkScriptCode(match[2] ?? '', lineAt(match.index), out);
|
|
80
|
+
}
|
|
81
|
+
// Stylesheets and preloads outside the template.
|
|
82
|
+
for (const match of source.matchAll(/<link\b([^>]*)>/gi)) {
|
|
83
|
+
const inTemplate = doc.template !== null && match.index > doc.template.start && match.index < doc.template.end;
|
|
84
|
+
if (inTemplate)
|
|
85
|
+
continue;
|
|
86
|
+
const attrs = match[1] ?? '';
|
|
87
|
+
const rel = (/\brel\s*=\s*["']([^"']*)["']/i.exec(attrs)?.[1] ?? '').toLowerCase().split(/\s+/);
|
|
88
|
+
const href = /\bhref\s*=\s*["']([^"']*)["']/i.exec(attrs)?.[1];
|
|
89
|
+
if (href === undefined || !rel.some((r) => r === 'stylesheet' || r === 'preload' || r === 'modulepreload'))
|
|
90
|
+
continue;
|
|
91
|
+
checkHeadRef(path, href, '<link href>', options, lineAt(match.index), out);
|
|
92
|
+
}
|
|
93
|
+
// Uploads this artifact does not have.
|
|
94
|
+
if (options.blobExists) {
|
|
95
|
+
const reported = new Set();
|
|
96
|
+
for (const match of source.matchAll(/\/_blob\/([^\s"'()<>?#]*)/g)) {
|
|
97
|
+
const id = match[1];
|
|
98
|
+
if (reported.has(id))
|
|
99
|
+
continue;
|
|
100
|
+
reported.add(id);
|
|
101
|
+
if (!BLOB_ID_PATTERN.test(id)) {
|
|
102
|
+
out.push({ level: 'error', message: `/_blob/${id} is not a valid upload reference: use the /_blob/<id> that upload printed (32 lowercase hex digits).`, line: lineAt(match.index) });
|
|
103
|
+
}
|
|
104
|
+
else if (!options.blobExists(id)) {
|
|
105
|
+
out.push({ level: 'error', message: `/_blob/${id} is not an upload of this design: upload the file and use the /_blob/<id> it prints.`, line: lineAt(match.index) });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Logic script.
|
|
110
|
+
if (doc.script) {
|
|
111
|
+
const body = doc.script.body.text;
|
|
112
|
+
if (!/\bclass\s+Component\s+extends\s+DCLogic\b/.test(body)) {
|
|
113
|
+
out.push({ level: 'error', message: 'The logic script must define `class Component extends DCLogic`.', line: lineAt(doc.script.start) });
|
|
114
|
+
}
|
|
115
|
+
if (/^\s*(import|export)\b/m.test(body)) {
|
|
116
|
+
out.push({ level: 'error', message: 'The logic script is classic JavaScript: no import or export.', line: lineAt(doc.script.start) });
|
|
117
|
+
}
|
|
118
|
+
if (/\brender\s*\(\s*\)\s*\{/.test(body)) {
|
|
119
|
+
out.push({ level: 'warning', message: 'Do not define render(); return values from renderVals() instead.', line: lineAt(doc.script.start) });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const model = parseTemplate(doc.template.text);
|
|
123
|
+
const base = doc.template.start;
|
|
124
|
+
const at = (element) => lineAt(base + element.start);
|
|
125
|
+
for (const element of model.elements) {
|
|
126
|
+
const line = at(element);
|
|
127
|
+
if (!VOID_ELEMENTS.has(element.tag) && !element.selfClosing && element.closeStart < 0) {
|
|
128
|
+
out.push({ level: 'error', message: `<${element.rawTag}> is never closed.`, line });
|
|
129
|
+
}
|
|
130
|
+
if (EMBEDS.has(element.tag))
|
|
131
|
+
out.push({ level: 'error', message: `<${element.tag}> is not allowed in an artboard.`, line });
|
|
132
|
+
if (/[A-Z]/.test(element.rawTag[0] ?? '') && !element.rawTag.includes('-')) {
|
|
133
|
+
out.push({ level: 'error', message: `<${element.rawTag}>: capitalized component tags are not supported; use <dc-import name="${element.rawTag}"></dc-import>.`, line });
|
|
134
|
+
}
|
|
135
|
+
if (element.tag === 'script')
|
|
136
|
+
out.push({ level: 'warning', message: 'Scripts inside the template never run; put logic in the data-dc-script block.', line });
|
|
137
|
+
// Holes in attributes and text.
|
|
138
|
+
for (const attr of element.attrs) {
|
|
139
|
+
if (attr.value === null)
|
|
140
|
+
continue;
|
|
141
|
+
for (const part of splitHoles(attr.value)) {
|
|
142
|
+
if (part.kind === 'hole' && part.hole.kind === 'invalid') {
|
|
143
|
+
out.push({ level: 'error', message: `{{ ${part.hole.raw} }} is not a dotted path or literal; compute it in renderVals().`, line });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const lower = attr.name.toLowerCase();
|
|
147
|
+
if (/^on[a-z]/i.test(attr.name) && !wholeHole(attr.value)) {
|
|
148
|
+
out.push({ level: 'error', message: `${attr.name}="…" must be a whole hole bound to a function, like onClick="{{ pick }}".`, line });
|
|
149
|
+
}
|
|
150
|
+
if (URL_ATTRIBUTES.includes(lower))
|
|
151
|
+
checkUrl(attr.value, element, line, out);
|
|
152
|
+
if (lower === 'src' && (element.tag === 'img' || element.tag === 'source'))
|
|
153
|
+
checkAssetUrl(path, attr.value, `<${element.tag} src>`, options, line, out);
|
|
154
|
+
if (lower === 'style' && /url\(\s*['"]?https?:/i.test(attr.value)) {
|
|
155
|
+
out.push({ level: 'error', message: 'Inline styles may not load remote URLs; upload the image and use /_blob/<id>.', line });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
for (const child of element.children) {
|
|
159
|
+
if (child.type !== 'text' || element.tag === 'style' || element.tag === 'script')
|
|
160
|
+
continue;
|
|
161
|
+
for (const part of splitHoles(child.value)) {
|
|
162
|
+
if (part.kind === 'hole' && part.hole.kind === 'invalid') {
|
|
163
|
+
out.push({ level: 'error', message: `{{ ${part.hole.raw} }} is not a dotted path or literal; compute it in renderVals().`, line });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (element.tag === 'style') {
|
|
168
|
+
const css = element.children.map((child) => (child.type === 'text' ? child.value : '')).join('');
|
|
169
|
+
if (/@import\s+(url\()?\s*['"]?https?:/i.test(css) && !/fonts\.googleapis\.com\/css2/i.test(css)) {
|
|
170
|
+
out.push({ level: 'error', message: 'Only Google Fonts css2 stylesheets may be imported.', line });
|
|
171
|
+
}
|
|
172
|
+
for (const match of css.matchAll(/url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*))\s*\)/gi)) {
|
|
173
|
+
const url = match[1] ?? match[2] ?? match[3] ?? '';
|
|
174
|
+
if (isGoogleFonts(url))
|
|
175
|
+
continue;
|
|
176
|
+
checkAssetUrl(path, url, 'url(…)', options, line, out);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Structure tags.
|
|
180
|
+
if (element.tag === 'sc-for') {
|
|
181
|
+
const list = attributeValue(element, 'list');
|
|
182
|
+
if (!list || !wholeHole(list))
|
|
183
|
+
out.push({ level: 'error', message: '<sc-for> needs list="{{ items }}".', line });
|
|
184
|
+
if (!attributeValue(element, 'as'))
|
|
185
|
+
out.push({ level: 'warning', message: '<sc-for> has no as="…"; the loop variable defaults to "item".', line });
|
|
186
|
+
if (attributeValue(element, 'hint-placeholder-count') === undefined) {
|
|
187
|
+
out.push({ level: 'warning', message: '<sc-for> should set hint-placeholder-count.', line });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (element.tag === 'sc-if') {
|
|
191
|
+
if (attributeValue(element, 'value') === undefined)
|
|
192
|
+
out.push({ level: 'error', message: '<sc-if> needs value="{{ cond }}".', line });
|
|
193
|
+
if (attributeValue(element, 'hint-placeholder-val') === undefined) {
|
|
194
|
+
out.push({ level: 'warning', message: '<sc-if> should set hint-placeholder-val.', line });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (element.tag === 'dc-import') {
|
|
198
|
+
const name = attributeValue(element, 'name');
|
|
199
|
+
if (!name)
|
|
200
|
+
out.push({ level: 'error', message: '<dc-import> needs name="Sibling".', line });
|
|
201
|
+
if (element.selfClosing)
|
|
202
|
+
out.push({ level: 'error', message: '<dc-import> must not self-close; write <dc-import …></dc-import>.', line });
|
|
203
|
+
if (name && options.files) {
|
|
204
|
+
const target = `${path.slice(0, path.lastIndexOf('/') + 1)}${name}.dc.html`;
|
|
205
|
+
if (!options.files.has(target))
|
|
206
|
+
out.push({ level: 'error', message: `<dc-import name="${name}">: ${target} does not exist.`, line });
|
|
207
|
+
}
|
|
208
|
+
if (attributeValue(element, 'hint-size') === undefined)
|
|
209
|
+
out.push({ level: 'warning', message: '<dc-import> should set hint-size="W,H".', line });
|
|
210
|
+
}
|
|
211
|
+
if (element.tag === 'a' && options.files) {
|
|
212
|
+
const href = attributeValue(element, 'href') ?? '';
|
|
213
|
+
if (/\.dc\.html(#.*)?$/i.test(href) && !/^[a-z]+:/i.test(href)) {
|
|
214
|
+
const target = href.replace(/#.*$/, '');
|
|
215
|
+
const resolved = target.startsWith('/') ? target.slice(1) : resolveRelative(path, target);
|
|
216
|
+
if (!options.files.has(resolved))
|
|
217
|
+
out.push({ level: 'warning', message: `Link to ${href}: ${resolved} does not exist.`, line });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
/** Resolves a link relative to an artboard path. */
|
|
224
|
+
function resolveRelative(from, target) {
|
|
225
|
+
const parts = from.split('/').slice(0, -1);
|
|
226
|
+
for (const part of target.split('/')) {
|
|
227
|
+
if (part === '..')
|
|
228
|
+
parts.pop();
|
|
229
|
+
else if (part && part !== '.')
|
|
230
|
+
parts.push(part);
|
|
231
|
+
}
|
|
232
|
+
return parts.join('/');
|
|
233
|
+
}
|
|
234
|
+
/** Network rule: only Google Fonts and uploaded blobs load. */
|
|
235
|
+
function checkUrl(value, element, line, out) {
|
|
236
|
+
const url = value.trim();
|
|
237
|
+
if (!url || url.includes('{{'))
|
|
238
|
+
return;
|
|
239
|
+
if (element.tag === 'a')
|
|
240
|
+
return; // Links navigate; they do not load.
|
|
241
|
+
// Resource hints load nothing; the Google Fonts snippet starts with them.
|
|
242
|
+
if (element.tag === 'link' && /^(preconnect|dns-prefetch)$/i.test(attributeValue(element, 'rel')?.trim() ?? '')) {
|
|
243
|
+
if (/^https:\/\/fonts\.(googleapis|gstatic)\.com\/?$/i.test(url))
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (/^(data:|blob:|#)/i.test(url) || url.startsWith('/_blob/'))
|
|
247
|
+
return;
|
|
248
|
+
if (/^https?:\/\//i.test(url) || url.startsWith('//')) {
|
|
249
|
+
if (!ALLOWED_REMOTE.some((pattern) => pattern.test(url))) {
|
|
250
|
+
out.push({ level: 'error', message: `${url.slice(0, 80)}: only Google Fonts and /_blob/<id> uploads may load; upload the file first.`, line });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Resolves a reference against an artboard path; `null` when it leaves the project. */
|
|
255
|
+
function resolveInProject(from, ref) {
|
|
256
|
+
const clean = ref.split(/[?#]/)[0];
|
|
257
|
+
const parts = clean.startsWith('/') ? [] : from.split('/').slice(0, -1);
|
|
258
|
+
for (const part of clean.split('/')) {
|
|
259
|
+
if (part === '' || part === '.')
|
|
260
|
+
continue;
|
|
261
|
+
if (part === '..') {
|
|
262
|
+
if (parts.length === 0)
|
|
263
|
+
return null;
|
|
264
|
+
parts.pop();
|
|
265
|
+
}
|
|
266
|
+
else
|
|
267
|
+
parts.push(part);
|
|
268
|
+
}
|
|
269
|
+
return parts.join('/');
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* An image or `url(…)` address: an upload (`/_blob/<id>`), a `{{hole}}`, or a
|
|
273
|
+
* file of the project. A `data:` URI, a `blob:` URL and a bare file name the
|
|
274
|
+
* project does not have are reported.
|
|
275
|
+
*/
|
|
276
|
+
function checkAssetUrl(path, url, where, options, line, out) {
|
|
277
|
+
const value = url.trim();
|
|
278
|
+
if (value === '' || value.startsWith('#') || value.includes('{{') || isBlobUrl(value))
|
|
279
|
+
return;
|
|
280
|
+
if (/^data:/i.test(value)) {
|
|
281
|
+
out.push({ level: 'warning', message: `${where} uses a data: URI; upload the file and use its /_blob/<id>.`, line });
|
|
282
|
+
}
|
|
283
|
+
else if (/^blob:/i.test(value)) {
|
|
284
|
+
out.push({ level: 'error', message: `${where} uses a blob: URL, which only lives in one browser tab; upload the file and use its /_blob/<id>.`, line });
|
|
285
|
+
}
|
|
286
|
+
else if (isRemote(value) || /^[a-z][a-z0-9+.-]*:/i.test(value)) {
|
|
287
|
+
// Remote addresses are reported by the network rule (checkUrl) or allowed (Google Fonts files).
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
else if (options.files) {
|
|
291
|
+
const target = resolveInProject(path, value);
|
|
292
|
+
if (target === null || !options.files.has(target)) {
|
|
293
|
+
out.push({ level: 'error', message: `${where} names the file ${value.slice(0, 80)}, which is not in this design; upload it and use the /_blob/<id> it prints.`, line });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/** A `<script src>` or `<link href>` outside the template: an upload, Google Fonts, or a file of the project. */
|
|
298
|
+
function checkHeadRef(path, ref, where, options, line, out) {
|
|
299
|
+
const value = ref.trim();
|
|
300
|
+
if (!value || value === './support.js' || value.includes('{{') || isBlobUrl(value))
|
|
301
|
+
return;
|
|
302
|
+
if (isRemote(value)) {
|
|
303
|
+
if (!(where === '<link href>' && isGoogleFonts(value))) {
|
|
304
|
+
out.push({ level: 'error', message: `${where}="${value.slice(0, 80)}" loads from the network; only Google Fonts and /_blob/<id> uploads may load. Upload the file instead.`, line });
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(value)) {
|
|
309
|
+
out.push({ level: 'error', message: `${where}="${value.slice(0, 80)}": use a file of this design or an uploaded /_blob/<id>.`, line });
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (!options.files)
|
|
313
|
+
return;
|
|
314
|
+
const target = resolveInProject(path, value);
|
|
315
|
+
if (target === null || target === '' || !options.files.has(target)) {
|
|
316
|
+
out.push({ level: 'error', message: `${where}="${value.slice(0, 80)}" is not a file in this design; write it there or upload it.`, line });
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/** Script rules: UI is markup, state stays in the component, no global keys, no network. */
|
|
320
|
+
function checkScriptCode(code, line, out) {
|
|
321
|
+
if (/\.(?:inner|outer)HTML\s*=(?!=)|\binsertAdjacentHTML\s*\(|\.appendChild\s*\(|\bdocument\.write(?:ln)?\s*\(/.test(code)) {
|
|
322
|
+
out.push({ level: 'error', message: 'Builds UI from script (innerHTML / appendChild / document.write); all UI is <x-dc> markup people can select and edit.', line });
|
|
323
|
+
}
|
|
324
|
+
if (/\bwindow\.[A-Za-z_$][\w$]*\s*=(?!=)/.test(code)) {
|
|
325
|
+
out.push({ level: 'error', message: 'Sets a window.X global; keep state in the component (state / setState) and mount components with <dc-import> or a design system\'s x-import.', line });
|
|
326
|
+
}
|
|
327
|
+
if (/\b(?:window|document|globalThis)\s*\.\s*(?:addEventListener\s*\(\s*['"`]key(?:down|up|press)['"`]|onkey(?:down|up|press)\s*=)/.test(code)) {
|
|
328
|
+
out.push({ level: 'error', message: 'Installs a global keyboard handler; artboards must not handle keys on window or document (the editor owns them).', line });
|
|
329
|
+
}
|
|
330
|
+
for (const match of code.matchAll(/\bfetch\s*\(\s*['"`]([^'"`]+)['"`]/g)) {
|
|
331
|
+
if (isRemote(match[1]))
|
|
332
|
+
out.push({ level: 'error', message: `fetch("${match[1].slice(0, 80)}") goes to the network; upload the data and fetch its /_blob/<id>.`, line });
|
|
333
|
+
}
|
|
334
|
+
if (/\bnew\s+(?:WebSocket|EventSource)\s*\(|\bXMLHttpRequest\b/.test(code)) {
|
|
335
|
+
out.push({ level: 'error', message: 'Opens a network connection (WebSocket / EventSource / XMLHttpRequest); artboards stay offline.', line });
|
|
336
|
+
}
|
|
337
|
+
}
|