@format.dev/utils 0.0.0 → 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/README.md CHANGED
@@ -1,32 +1,25 @@
1
1
  # @format.dev/utils
2
2
 
3
- **This is a bootstrap placeholder. It reserves the package name and does nothing else.**
3
+ Shared constants and helpers across the Format platform. This package exists so `@format.dev/zip`, `@format.dev/compile`, Format Studio, and the Format API can depend on one readable source for the values they share. It is not a general-purpose utility library.
4
4
 
5
- Format is not released yet. Version `0.0.0` is not a usable version of this
6
- package: it contains no implementation, and installing it gets you an empty
7
- module.
5
+ ## Exports
8
6
 
9
- ## Why it exists
7
+ The MIME module (`src/mime.ts`):
10
8
 
11
- npm binds a package’s trusted publisher, and its staged-publishing flow, to a
12
- package that already exists. Creating these names ahead of the first release is
13
- what lets every real release go out through a reviewed, credential-free
14
- workflow rather than through a token somebody holds.
9
+ - `MIME_TYPE` maps file extensions to MIME types for every asset Format serves. `USER_MIME_TYPE` is the subset that user uploads may carry.
10
+ - `EXT_TO_MIME` and `MIME_TO_EXT` translate between dotted extensions and MIME types, in both directions.
11
+ - `getPermittedContentType`, `isFormatAssetExtension`, and `isUserAssetExtension` guard those maps.
15
12
 
16
- This version is published deliberately and deprecated immediately.
13
+ The DOM policy lists (`src/dom.ts`):
17
14
 
18
- ## Why a bare install resolves it
15
+ - `STUDIO_FORBID_TAGS` lists the tags removed from every document, including the Format Studio preview.
16
+ - `FORBID_TAGS` is the render-time removal list: `STUDIO_FORBID_TAGS` plus the form elements.
17
+ - `UNSUPPORTED_TAGS` lists the tags the Format Studio preview keeps visible but the rendered PDF drops.
19
18
 
20
- npm creates a `latest` tag on a package’s first publish and will not remove it,
21
- so `latest` points at this placeholder until Format reaches general
22
- availability. That is why the deprecation warning is there.
19
+ ## Module format
23
20
 
24
- - Beta releases go out under the `beta` tag.
25
- - General availability is when `latest` moves to a real release.
21
+ The package ships ES modules only. Loading it with `require()` needs Node 22.13 or later, which is the floor `@format.dev/zip` declares.
26
22
 
27
- ## Format
23
+ ## What belongs here
28
24
 
29
- - https://format.dev
30
- - https://github.com/format-hq/format-js
31
-
32
- Licensed under MIT. The full text ships beside this file.
25
+ Code lands here when at least two packages need it, or when a Format user could reasonably import it. Package-specific helpers stay in their package. Type-only declarations belong in `@format.dev/types`.
package/dist/dom.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ //#region src/dom.d.ts
2
+ declare const STUDIO_FORBID_TAGS: string[];
3
+ declare const FORM_TAGS: string[];
4
+ declare const FORBID_TAGS: string[];
5
+ declare const UNSUPPORTED_TAGS: string[];
6
+ //#endregion
7
+ export { FORBID_TAGS, FORM_TAGS, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS };
8
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.d.ts","names":[],"sources":["../src/dom.ts"],"sourcesContent":[],"mappings":";cACa;AAAA,cAuCA,SALZ,EAAA,MAAA,EAAA;AAKY,cAGA,WAH+F,EAAA,MAAA,EAAA;AAG/F,cAOA,gBAPmD,EAAA,MAAA,EAAA"}
package/dist/dom.js ADDED
@@ -0,0 +1,69 @@
1
+ //#region src/dom.ts
2
+ const STUDIO_FORBID_TAGS = [
3
+ "html",
4
+ "head",
5
+ "body",
6
+ "title",
7
+ "base",
8
+ "meta",
9
+ "script",
10
+ "noscript",
11
+ "iframe",
12
+ "embed",
13
+ "object",
14
+ "param",
15
+ "picture",
16
+ "source",
17
+ "track",
18
+ "audio",
19
+ "video",
20
+ "canvas",
21
+ "dialog",
22
+ "meter",
23
+ "fencedframe",
24
+ "keygen",
25
+ "geolocation",
26
+ "applet",
27
+ "frame",
28
+ "frameset",
29
+ "bgsound",
30
+ "noframes",
31
+ "noembed",
32
+ "content",
33
+ "shadow",
34
+ "isindex",
35
+ "marquee"
36
+ ];
37
+ const FORM_TAGS = [
38
+ "form",
39
+ "input",
40
+ "button",
41
+ "textarea",
42
+ "select",
43
+ "option",
44
+ "optgroup",
45
+ "datalist"
46
+ ];
47
+ const FORBID_TAGS = [...STUDIO_FORBID_TAGS, ...FORM_TAGS];
48
+ const UNSUPPORTED_TAGS = [
49
+ "form",
50
+ "input",
51
+ "button",
52
+ "textarea",
53
+ "select",
54
+ "option",
55
+ "optgroup",
56
+ "datalist",
57
+ "selectedcontent",
58
+ "menuitem",
59
+ "map",
60
+ "area",
61
+ "slot",
62
+ "multicol",
63
+ "blink",
64
+ "spacer"
65
+ ];
66
+
67
+ //#endregion
68
+ export { FORBID_TAGS, FORM_TAGS, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS };
69
+ //# sourceMappingURL=dom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.js","names":[],"sources":["../src/dom.ts"],"sourcesContent":["// Tags we always remove\nexport const STUDIO_FORBID_TAGS = [\n\t'html',\n\t'head',\n\t'body',\n\t'title',\n\t'base',\n\t'meta',\n\t'script',\n\t'noscript',\n\t'iframe',\n\t'embed',\n\t'object',\n\t'param',\n\t'picture',\n\t'source',\n\t'track',\n\t'audio',\n\t'video',\n\t'canvas',\n\t'dialog',\n\t'meter',\n\t'fencedframe',\n\t'keygen',\n\t'geolocation',\n\t'applet',\n\t'frame',\n\t'frameset',\n\t'bgsound',\n\t'noframes',\n\t'noembed',\n\t'content',\n\t'shadow',\n\t'isindex',\n\t'marquee'\n]\n\n// Form elements are currently unsupported, but we are keeping them as a named\n// group so a future engine version that supports PDF forms can omit this group from\n// its sanitizer policy without the render path needing to know which elements that means.\nexport const FORM_TAGS = ['form', 'input', 'button', 'textarea', 'select', 'option', 'optgroup', 'datalist']\n\n// Tags we remove at render time only\nexport const FORBID_TAGS = [...STUDIO_FORBID_TAGS, ...FORM_TAGS]\n\n// Tags the studio preview keeps visible but that won't render in the final PDF:\n// form controls (dropped at render), plus deprecated and web-component tags the\n// engine ignores. STUDIO_FORBID_TAGS are excluded — those are already removed\n// from the preview and reported as \"removed\" elements. Kept in sync with the\n// supported HTML elements reference (apps/web html-elements-table).\nexport const UNSUPPORTED_TAGS = [\n\t'form',\n\t'input',\n\t'button',\n\t'textarea',\n\t'select',\n\t'option',\n\t'optgroup',\n\t'datalist',\n\t'selectedcontent',\n\t'menuitem',\n\t'map',\n\t'area',\n\t'slot',\n\t'multicol',\n\t'blink',\n\t'spacer'\n]\n"],"mappings":";AACA,MAAa,qBAAqB;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AAKD,MAAa,YAAY;CAAC;CAAQ;CAAS;CAAU;CAAY;CAAU;CAAU;CAAY;CAAW;AAG5G,MAAa,cAAc,CAAC,GAAG,oBAAoB,GAAG,UAAU;AAOhE,MAAa,mBAAmB;CAC/B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA"}
package/dist/html.d.ts ADDED
@@ -0,0 +1,124 @@
1
+ //#region src/html.d.ts
2
+ /**
3
+ * Reads one HTML opening tag: where it ends, and which attributes it sets.
4
+ *
5
+ * This is not an HTML parser. It does not understand documents, nesting, text
6
+ * content, or closing tags. You give it a string and the position of a `<`
7
+ * inside it, and it reads the single tag that starts at that position.
8
+ *
9
+ * ## Why not use a regular expression
10
+ *
11
+ * The two regular expressions normally written for this job both give wrong
12
+ * answers on valid HTML.
13
+ *
14
+ * The first is used to find where a tag ends, usually written like
15
+ * `/<div\b[^>]*>/`. The `[^>]*` part stops at the first `>` in the string, even
16
+ * when that `>` is inside a quoted attribute value. Given this tag:
17
+ *
18
+ * <div title="Q1 > Q2" id="chart">
19
+ *
20
+ * the match ends just after `Q1 >`. The tag therefore looks like
21
+ * `<div title="Q1 >`, the `id` attribute is invisible, and code that inserts
22
+ * something directly after the tag inserts it into the middle of the title
23
+ * instead.
24
+ *
25
+ * The second is used to read one attribute, usually written like
26
+ * `/\btitle\s*=\s*(['"])(.*?)\1/`. It searches the whole tag, including the
27
+ * inside of other attributes' values. Given this tag:
28
+ *
29
+ * <div id="example" data-note='write title="x" to set a title'>
30
+ *
31
+ * it reports that the tag sets `title` to `x`. The tag sets no title at all;
32
+ * the text is part of the note.
33
+ *
34
+ * Both mistakes matter most when a tag's attributes decide what a program does,
35
+ * rather than only how something looks. This function avoids both by walking
36
+ * through the tag one character at a time and keeping track of whether it is
37
+ * currently inside a quoted value.
38
+ *
39
+ * ## What it accepts
40
+ *
41
+ * Attribute values may be double-quoted, single-quoted, unquoted, or absent.
42
+ * Attribute names are matched case-insensitively, because HTML treats them that
43
+ * way. A tag that never closes is rejected rather than guessed at.
44
+ *
45
+ * ## Example
46
+ *
47
+ * const tag = scanOpeningTag('<div title="Q1 > Q2" id="chart">text', 0)
48
+ *
49
+ * tag.source // '<div title="Q1 > Q2" id="chart">'
50
+ * tag.end // 32
51
+ * tag.attributes.get('title') // 'Q1 > Q2'
52
+ * tag.attributes.get('id') // 'chart'
53
+ */
54
+ /**
55
+ * Where one attribute sits inside the html, for callers that rewrite a tag.
56
+ *
57
+ * Rewriting by replacing these bytes keeps every other attribute exactly as it
58
+ * was written, including which quotes it used. Rebuilding a tag from its parsed
59
+ * values instead has to choose a quote character, and choosing wrongly produces
60
+ * invalid HTML: a value written `data-note='say "hi"'` cannot be re-emitted
61
+ * with double quotes without escaping.
62
+ */
63
+ interface AttributeSpan {
64
+ /** Offset of the attribute's first character, which is the start of its name. */
65
+ start: number;
66
+ /** One past its last character: the closing quote, or the end of a bare value. */
67
+ end: number;
68
+ }
69
+ interface OpeningTag {
70
+ /** The tag exactly as written, from the `<` through the `>` that closes it. */
71
+ source: string;
72
+ /** The position of the `<`, the same value that was passed in. */
73
+ start: number;
74
+ /**
75
+ * The position just after the closing `>`.
76
+ *
77
+ * `html.slice(tag.end)` is everything following the tag, so this is where to
78
+ * insert content that should come immediately after it.
79
+ */
80
+ end: number;
81
+ /**
82
+ * The attributes the tag sets, keyed by lowercased attribute name.
83
+ *
84
+ * An attribute written without a value, such as `hidden`, maps to the empty
85
+ * string. HTML defines it that way, so `attributes.has('hidden')` answers
86
+ * "was it written?" and `attributes.get('hidden')` answers "what does it
87
+ * say?" without the two disagreeing.
88
+ *
89
+ * If the same attribute name appears twice, the first one is kept. Browsers
90
+ * do the same.
91
+ */
92
+ attributes: ReadonlyMap<string, string>;
93
+ /**
94
+ * Where each attribute is written, keyed the same way as `attributes`.
95
+ *
96
+ * Offsets are into the html that was scanned, not into `source`, so a caller
97
+ * can splice directly. Present for every attribute, including one written
98
+ * without a value.
99
+ */
100
+ spans: ReadonlyMap<string, AttributeSpan>;
101
+ }
102
+ /**
103
+ * Reads the opening tag that starts at `start`.
104
+ *
105
+ * Returns `null` when there is no opening tag at that position, or when the tag
106
+ * is not complete. There are two such cases.
107
+ *
108
+ * The first is that `start` does not point at an opening tag. An opening tag is
109
+ * a `<` followed immediately by a letter. Without that check, `</div>`,
110
+ * `<!doctype html>`, `<!-- note -->` and a stray `<` in ordinary text are all
111
+ * read as tags, and their words are reported as attribute names — `</div>`
112
+ * would come back as a tag setting an attribute called `div`. The tag's name is
113
+ * not checked any further than the first letter: deciding which elements are
114
+ * interesting is the caller's job.
115
+ *
116
+ * The second is that the tag never closes, either because the string ends
117
+ * before the `>` or because a quoted value is never closed. Returning a partial
118
+ * tag would be worse than returning nothing, because the caller has no way to
119
+ * tell a partial answer from a complete one.
120
+ */
121
+ declare function scanOpeningTag(html: string, start: number): OpeningTag | null;
122
+ //#endregion
123
+ export { AttributeSpan, OpeningTag, scanOpeningTag };
124
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","names":[],"sources":["../src/html.ts"],"sourcesContent":[],"mappings":";;AA8DA;AAOA;;;;;AA4DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAnEiB,aAAA;;;;;;UAOA,UAAA;;;;;;;;;;;;;;;;;;;;;;;cAuBJ;;;;;;;;SAQL,oBAAoB;;;;;;;;;;;;;;;;;;;;;iBA6BZ,cAAA,+BAA6C"}
package/dist/html.js ADDED
@@ -0,0 +1,88 @@
1
+ //#region src/html.ts
2
+ const TAG_NAME_START = /[A-Za-z]/;
3
+ const WHITESPACE = /\s/;
4
+ /** An attribute name ends at whitespace, `=`, `/`, or the end of the tag. */
5
+ const NAME_END = /[\s/>=]/;
6
+ /** An unquoted attribute value ends at whitespace or the end of the tag. */
7
+ const UNQUOTED_END = /[\s>]/;
8
+ /**
9
+ * Reads the opening tag that starts at `start`.
10
+ *
11
+ * Returns `null` when there is no opening tag at that position, or when the tag
12
+ * is not complete. There are two such cases.
13
+ *
14
+ * The first is that `start` does not point at an opening tag. An opening tag is
15
+ * a `<` followed immediately by a letter. Without that check, `</div>`,
16
+ * `<!doctype html>`, `<!-- note -->` and a stray `<` in ordinary text are all
17
+ * read as tags, and their words are reported as attribute names — `</div>`
18
+ * would come back as a tag setting an attribute called `div`. The tag's name is
19
+ * not checked any further than the first letter: deciding which elements are
20
+ * interesting is the caller's job.
21
+ *
22
+ * The second is that the tag never closes, either because the string ends
23
+ * before the `>` or because a quoted value is never closed. Returning a partial
24
+ * tag would be worse than returning nothing, because the caller has no way to
25
+ * tell a partial answer from a complete one.
26
+ */
27
+ function scanOpeningTag(html, start) {
28
+ if (html[start] !== "<" || !TAG_NAME_START.test(html[start + 1] ?? "")) return null;
29
+ const attributes = /* @__PURE__ */ new Map();
30
+ const spans = /* @__PURE__ */ new Map();
31
+ let i = start + 1;
32
+ while (i < html.length && !NAME_END.test(html[i])) i++;
33
+ while (i < html.length) {
34
+ while (i < html.length && (WHITESPACE.test(html[i]) || html[i] === "/")) i++;
35
+ if (i >= html.length) break;
36
+ if (html[i] === ">") {
37
+ const end = i + 1;
38
+ return {
39
+ source: html.slice(start, end),
40
+ start,
41
+ end,
42
+ attributes,
43
+ spans
44
+ };
45
+ }
46
+ const nameStart = i;
47
+ while (i < html.length && !NAME_END.test(html[i])) i++;
48
+ const name = html.slice(nameStart, i).toLowerCase();
49
+ while (i < html.length && WHITESPACE.test(html[i])) i++;
50
+ if (html[i] !== "=") {
51
+ if (name && !attributes.has(name)) {
52
+ attributes.set(name, "");
53
+ spans.set(name, {
54
+ start: nameStart,
55
+ end: nameStart + name.length
56
+ });
57
+ }
58
+ continue;
59
+ }
60
+ i++;
61
+ while (i < html.length && WHITESPACE.test(html[i])) i++;
62
+ let value;
63
+ const quote = html[i];
64
+ if (quote === "\"" || quote === "'") {
65
+ const valueStart = ++i;
66
+ while (i < html.length && html[i] !== quote) i++;
67
+ if (i >= html.length) break;
68
+ value = html.slice(valueStart, i);
69
+ i++;
70
+ } else {
71
+ const valueStart = i;
72
+ while (i < html.length && !UNQUOTED_END.test(html[i])) i++;
73
+ value = html.slice(valueStart, i);
74
+ }
75
+ if (name && !attributes.has(name)) {
76
+ attributes.set(name, value);
77
+ spans.set(name, {
78
+ start: nameStart,
79
+ end: i
80
+ });
81
+ }
82
+ }
83
+ return null;
84
+ }
85
+
86
+ //#endregion
87
+ export { scanOpeningTag };
88
+ //# sourceMappingURL=html.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.js","names":["value: string"],"sources":["../src/html.ts"],"sourcesContent":["/**\n * Reads one HTML opening tag: where it ends, and which attributes it sets.\n *\n * This is not an HTML parser. It does not understand documents, nesting, text\n * content, or closing tags. You give it a string and the position of a `<`\n * inside it, and it reads the single tag that starts at that position.\n *\n * ## Why not use a regular expression\n *\n * The two regular expressions normally written for this job both give wrong\n * answers on valid HTML.\n *\n * The first is used to find where a tag ends, usually written like\n * `/<div\\b[^>]*>/`. The `[^>]*` part stops at the first `>` in the string, even\n * when that `>` is inside a quoted attribute value. Given this tag:\n *\n * <div title=\"Q1 > Q2\" id=\"chart\">\n *\n * the match ends just after `Q1 >`. The tag therefore looks like\n * `<div title=\"Q1 >`, the `id` attribute is invisible, and code that inserts\n * something directly after the tag inserts it into the middle of the title\n * instead.\n *\n * The second is used to read one attribute, usually written like\n * `/\\btitle\\s*=\\s*(['\"])(.*?)\\1/`. It searches the whole tag, including the\n * inside of other attributes' values. Given this tag:\n *\n * <div id=\"example\" data-note='write title=\"x\" to set a title'>\n *\n * it reports that the tag sets `title` to `x`. The tag sets no title at all;\n * the text is part of the note.\n *\n * Both mistakes matter most when a tag's attributes decide what a program does,\n * rather than only how something looks. This function avoids both by walking\n * through the tag one character at a time and keeping track of whether it is\n * currently inside a quoted value.\n *\n * ## What it accepts\n *\n * Attribute values may be double-quoted, single-quoted, unquoted, or absent.\n * Attribute names are matched case-insensitively, because HTML treats them that\n * way. A tag that never closes is rejected rather than guessed at.\n *\n * ## Example\n *\n * const tag = scanOpeningTag('<div title=\"Q1 > Q2\" id=\"chart\">text', 0)\n *\n * tag.source // '<div title=\"Q1 > Q2\" id=\"chart\">'\n * tag.end // 32\n * tag.attributes.get('title') // 'Q1 > Q2'\n * tag.attributes.get('id') // 'chart'\n */\n\n/**\n * Where one attribute sits inside the html, for callers that rewrite a tag.\n *\n * Rewriting by replacing these bytes keeps every other attribute exactly as it\n * was written, including which quotes it used. Rebuilding a tag from its parsed\n * values instead has to choose a quote character, and choosing wrongly produces\n * invalid HTML: a value written `data-note='say \"hi\"'` cannot be re-emitted\n * with double quotes without escaping.\n */\nexport interface AttributeSpan {\n\t/** Offset of the attribute's first character, which is the start of its name. */\n\tstart: number\n\t/** One past its last character: the closing quote, or the end of a bare value. */\n\tend: number\n}\n\nexport interface OpeningTag {\n\t/** The tag exactly as written, from the `<` through the `>` that closes it. */\n\tsource: string\n\t/** The position of the `<`, the same value that was passed in. */\n\tstart: number\n\t/**\n\t * The position just after the closing `>`.\n\t *\n\t * `html.slice(tag.end)` is everything following the tag, so this is where to\n\t * insert content that should come immediately after it.\n\t */\n\tend: number\n\t/**\n\t * The attributes the tag sets, keyed by lowercased attribute name.\n\t *\n\t * An attribute written without a value, such as `hidden`, maps to the empty\n\t * string. HTML defines it that way, so `attributes.has('hidden')` answers\n\t * \"was it written?\" and `attributes.get('hidden')` answers \"what does it\n\t * say?\" without the two disagreeing.\n\t *\n\t * If the same attribute name appears twice, the first one is kept. Browsers\n\t * do the same.\n\t */\n\tattributes: ReadonlyMap<string, string>\n\t/**\n\t * Where each attribute is written, keyed the same way as `attributes`.\n\t *\n\t * Offsets are into the html that was scanned, not into `source`, so a caller\n\t * can splice directly. Present for every attribute, including one written\n\t * without a value.\n\t */\n\tspans: ReadonlyMap<string, AttributeSpan>\n}\n\nconst TAG_NAME_START = /[A-Za-z]/\nconst WHITESPACE = /\\s/\n/** An attribute name ends at whitespace, `=`, `/`, or the end of the tag. */\nconst NAME_END = /[\\s/>=]/\n/** An unquoted attribute value ends at whitespace or the end of the tag. */\nconst UNQUOTED_END = /[\\s>]/\n\n/**\n * Reads the opening tag that starts at `start`.\n *\n * Returns `null` when there is no opening tag at that position, or when the tag\n * is not complete. There are two such cases.\n *\n * The first is that `start` does not point at an opening tag. An opening tag is\n * a `<` followed immediately by a letter. Without that check, `</div>`,\n * `<!doctype html>`, `<!-- note -->` and a stray `<` in ordinary text are all\n * read as tags, and their words are reported as attribute names — `</div>`\n * would come back as a tag setting an attribute called `div`. The tag's name is\n * not checked any further than the first letter: deciding which elements are\n * interesting is the caller's job.\n *\n * The second is that the tag never closes, either because the string ends\n * before the `>` or because a quoted value is never closed. Returning a partial\n * tag would be worse than returning nothing, because the caller has no way to\n * tell a partial answer from a complete one.\n */\nexport function scanOpeningTag(html: string, start: number): OpeningTag | null {\n\tif (html[start] !== '<' || !TAG_NAME_START.test(html[start + 1] ?? '')) return null\n\n\tconst attributes = new Map<string, string>()\n\tconst spans = new Map<string, AttributeSpan>()\n\n\t// Step over the tag name, so that it is not mistaken for an attribute.\n\tlet i = start + 1\n\twhile (i < html.length && !NAME_END.test(html[i])) i++\n\n\t// Read one attribute per pass, until the tag closes or the string runs out.\n\twhile (i < html.length) {\n\t\t// Skip the space between attributes, and the `/` of a self-closing tag.\n\t\twhile (i < html.length && (WHITESPACE.test(html[i]) || html[i] === '/')) i++\n\n\t\tif (i >= html.length) break\n\n\t\tif (html[i] === '>') {\n\t\t\tconst end = i + 1\n\t\t\treturn { source: html.slice(start, end), start, end, attributes, spans }\n\t\t}\n\n\t\tconst nameStart = i\n\t\twhile (i < html.length && !NAME_END.test(html[i])) i++\n\t\tconst name = html.slice(nameStart, i).toLowerCase()\n\n\t\twhile (i < html.length && WHITESPACE.test(html[i])) i++\n\n\t\t// No `=` means an attribute written without a value, such as `hidden`.\n\t\tif (html[i] !== '=') {\n\t\t\tif (name && !attributes.has(name)) {\n\t\t\t\tattributes.set(name, '')\n\t\t\t\tspans.set(name, { start: nameStart, end: nameStart + name.length })\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\n\t\ti++\n\t\twhile (i < html.length && WHITESPACE.test(html[i])) i++\n\n\t\tlet value: string\n\t\tconst quote = html[i]\n\n\t\tif (quote === '\"' || quote === \"'\") {\n\t\t\t// Read to the matching quote. Any `>` before it is part of the value,\n\t\t\t// which is the case the plain regular expression gets wrong.\n\t\t\tconst valueStart = ++i\n\t\t\twhile (i < html.length && html[i] !== quote) i++\n\n\t\t\t// Running out of string means the quote was never closed, so the tag\n\t\t\t// never closed either. Break out and return null below.\n\t\t\tif (i >= html.length) break\n\n\t\t\tvalue = html.slice(valueStart, i)\n\t\t\ti++\n\t\t} else {\n\t\t\tconst valueStart = i\n\t\t\twhile (i < html.length && !UNQUOTED_END.test(html[i])) i++\n\t\t\tvalue = html.slice(valueStart, i)\n\t\t}\n\n\t\tif (name && !attributes.has(name)) {\n\t\t\tattributes.set(name, value)\n\t\t\tspans.set(name, { start: nameStart, end: i })\n\t\t}\n\t}\n\n\treturn null\n}\n"],"mappings":";AAuGA,MAAM,iBAAiB;AACvB,MAAM,aAAa;;AAEnB,MAAM,WAAW;;AAEjB,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;AAqBrB,SAAgB,eAAe,MAAc,OAAkC;AAC9E,KAAI,KAAK,WAAW,OAAO,CAAC,eAAe,KAAK,KAAK,QAAQ,MAAM,GAAG,CAAE,QAAO;CAE/E,MAAM,6BAAa,IAAI,KAAqB;CAC5C,MAAM,wBAAQ,IAAI,KAA4B;CAG9C,IAAI,IAAI,QAAQ;AAChB,QAAO,IAAI,KAAK,UAAU,CAAC,SAAS,KAAK,KAAK,GAAG,CAAE;AAGnD,QAAO,IAAI,KAAK,QAAQ;AAEvB,SAAO,IAAI,KAAK,WAAW,WAAW,KAAK,KAAK,GAAG,IAAI,KAAK,OAAO,KAAM;AAEzE,MAAI,KAAK,KAAK,OAAQ;AAEtB,MAAI,KAAK,OAAO,KAAK;GACpB,MAAM,MAAM,IAAI;AAChB,UAAO;IAAE,QAAQ,KAAK,MAAM,OAAO,IAAI;IAAE;IAAO;IAAK;IAAY;IAAO;;EAGzE,MAAM,YAAY;AAClB,SAAO,IAAI,KAAK,UAAU,CAAC,SAAS,KAAK,KAAK,GAAG,CAAE;EACnD,MAAM,OAAO,KAAK,MAAM,WAAW,EAAE,CAAC,aAAa;AAEnD,SAAO,IAAI,KAAK,UAAU,WAAW,KAAK,KAAK,GAAG,CAAE;AAGpD,MAAI,KAAK,OAAO,KAAK;AACpB,OAAI,QAAQ,CAAC,WAAW,IAAI,KAAK,EAAE;AAClC,eAAW,IAAI,MAAM,GAAG;AACxB,UAAM,IAAI,MAAM;KAAE,OAAO;KAAW,KAAK,YAAY,KAAK;KAAQ,CAAC;;AAEpE;;AAGD;AACA,SAAO,IAAI,KAAK,UAAU,WAAW,KAAK,KAAK,GAAG,CAAE;EAEpD,IAAIA;EACJ,MAAM,QAAQ,KAAK;AAEnB,MAAI,UAAU,QAAO,UAAU,KAAK;GAGnC,MAAM,aAAa,EAAE;AACrB,UAAO,IAAI,KAAK,UAAU,KAAK,OAAO,MAAO;AAI7C,OAAI,KAAK,KAAK,OAAQ;AAEtB,WAAQ,KAAK,MAAM,YAAY,EAAE;AACjC;SACM;GACN,MAAM,aAAa;AACnB,UAAO,IAAI,KAAK,UAAU,CAAC,aAAa,KAAK,KAAK,GAAG,CAAE;AACvD,WAAQ,KAAK,MAAM,YAAY,EAAE;;AAGlC,MAAI,QAAQ,CAAC,WAAW,IAAI,KAAK,EAAE;AAClC,cAAW,IAAI,MAAM,MAAM;AAC3B,SAAM,IAAI,MAAM;IAAE,OAAO;IAAW,KAAK;IAAG,CAAC;;;AAI/C,QAAO"}
@@ -0,0 +1,4 @@
1
+ import { FORBID_TAGS, FORM_TAGS, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS } from "./dom.js";
2
+ import { AttributeSpan, OpeningTag, scanOpeningTag } from "./html.js";
3
+ import { EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, ExcludedExtensions, FORMAT_MIME_TYPE, FormatAssetsMimeExtension, MIME_TO_EXT, MIME_TYPE, MimeType, USER_MIME_TYPE, UserAssetsMimeExtension, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension } from "./mime.js";
4
+ export { AttributeSpan, EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, ExcludedExtensions, FORBID_TAGS, FORMAT_MIME_TYPE, FORM_TAGS, FormatAssetsMimeExtension, MIME_TO_EXT, MIME_TYPE, MimeType, OpeningTag, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS, USER_MIME_TYPE, UserAssetsMimeExtension, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension, scanOpeningTag };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { FORBID_TAGS, FORM_TAGS, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS } from "./dom.js";
2
+ import { scanOpeningTag } from "./html.js";
3
+ import { EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, FORMAT_MIME_TYPE, MIME_TO_EXT, MIME_TYPE, USER_MIME_TYPE, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension } from "./mime.js";
4
+
5
+ export { EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, FORBID_TAGS, FORMAT_MIME_TYPE, FORM_TAGS, MIME_TO_EXT, MIME_TYPE, STUDIO_FORBID_TAGS, UNSUPPORTED_TAGS, USER_MIME_TYPE, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension, scanOpeningTag };
package/dist/mime.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ //#region src/mime.d.ts
2
+ declare const EXTENSION_TO_MIME_MAP: {
3
+ readonly html: "text/html";
4
+ readonly js: "text/javascript";
5
+ readonly mjs: "text/javascript";
6
+ readonly wasm: "application/wasm";
7
+ readonly webmanifest: "application/manifest+json";
8
+ readonly css: "text/css";
9
+ readonly json: "application/json";
10
+ readonly ttf: "font/ttf";
11
+ readonly otf: "font/otf";
12
+ readonly woff: "font/woff";
13
+ readonly woff2: "font/woff2";
14
+ readonly eot: "application/vnd.ms-fontobject";
15
+ readonly png: "image/png";
16
+ readonly jpg: "image/jpg";
17
+ readonly jpeg: "image/jpeg";
18
+ readonly gif: "image/gif";
19
+ readonly svg: "image/svg+xml";
20
+ readonly webp: "image/webp";
21
+ readonly bmp: "image/bmp";
22
+ readonly tiff: "image/tiff";
23
+ readonly avif: "image/avif";
24
+ readonly tif: "image/tiff";
25
+ readonly heic: "image/heic";
26
+ readonly heif: "image/heif";
27
+ };
28
+ declare const EXCLUDED_EXTENSIONS: readonly ["js", "mjs", "wasm", "webmanifest"];
29
+ type FormatAssetsMimeExtension = keyof typeof EXTENSION_TO_MIME_MAP;
30
+ type ExcludedExtensions = (typeof EXCLUDED_EXTENSIONS)[number];
31
+ type UserAssetsMimeExtension = Exclude<FormatAssetsMimeExtension, ExcludedExtensions>;
32
+ declare const MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>>;
33
+ type MimeType = (typeof MIME_TYPE)[FormatAssetsMimeExtension];
34
+ declare const USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>>;
35
+ declare const FORMAT_MIME_TYPE: Readonly<Record<"js" | "mjs" | "wasm" | "webmanifest" | "html" | "css" | "json" | "ttf" | "otf" | "woff" | "woff2" | "eot" | "png" | "jpg" | "jpeg" | "gif" | "svg" | "webp" | "bmp" | "tiff" | "avif" | "tif" | "heic" | "heif", string>>;
36
+ declare const EXT_TO_MIME: Readonly<Record<string, string>>;
37
+ declare const MIME_TO_EXT: Readonly<Record<string, string>>;
38
+ declare function getPermittedContentType(ext: string): string | undefined;
39
+ declare function isFormatAssetExtension(ext: string): ext is FormatAssetsMimeExtension;
40
+ declare function isUserAssetExtension(ext: string): ext is UserAssetsMimeExtension;
41
+ //#endregion
42
+ export { EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, ExcludedExtensions, FORMAT_MIME_TYPE, FormatAssetsMimeExtension, MIME_TO_EXT, MIME_TYPE, MimeType, USER_MIME_TYPE, UserAssetsMimeExtension, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension };
43
+ //# sourceMappingURL=mime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mime.d.ts","names":[],"sources":["../src/mime.ts"],"sourcesContent":[],"mappings":";cAAa;EAAA,SAAA,IAAA,EAAA,WA4BH;EAEG,SAAA,EAAA,EAAA,iBAAmE;EAEpE,SAAA,GAAA,EAAA,iBAAyB;EAEzB,SAAA,IAAA,EAAA,kBAA6B;EAE7B,SAAA,WAAA,EAAA,2BAAuB;EAAW,SAAA,GAAA,EAAA,UAAA;EAA2B,SAAA,IAAA,EAAA,kBAAA;EAAnC,SAAA,GAAA,EAAA,UAAA;EAAO,SAAA,GAAA,EAAA,UAAA;EAEhC,SAAA,IAAsF,EAAA,WAAA;EAA3D,SAAA,KAAA,EAAA,YAAA;EAAP,SAAA,GAAA,EAAA,+BAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,GAAA,EAAA,WAAA;EAEpB,SAAA,IAAQ,EAAA,YAAW;EAElB,SAAA,GAAA,EAAA,WAEyC;EAFT,SAAA,GAAA,EAAA,eAAA;EAAP,SAAA,IAAA,EAAA,YAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,IAAA,EAAA,YAAA;EAIxB,SAAA,IAAA,EAAA,YAA4B;EAE5B,SAAA,GAAA,EAEZ,YAFkC;EAItB,SAAA,IAAA,EAEwB,YAFF;EAInB,SAAA,IAAA,EAAA,YAAuB;AAKvC,CAAA;AAIgB,cAnCH,mBAmC6C,EAAA,SAAA,CAAA,IAAA,EAAA,KAAuB,EAAA,MAAA,EAAA,aAAA,CAAA;KAjCrE,yBAAA,gBAAyC;KAEzC,kBAAA,WAA6B;KAE7B,uBAAA,GAA0B,QAAQ,2BAA2B;cAE5D,WAAW,SAAS,OAAO;KAE5B,QAAA,WAAmB,WAAW;cAE7B,gBAAgB,SAAS,OAAO;cAIhC,kBAAgB,SAAA;cAEhB,aAAa,SAAS;cAItB,aAAa,SAAS;iBAInB,uBAAA;iBAKA,sBAAA,sBAA4C;iBAI5C,oBAAA,sBAA0C"}
package/dist/mime.js ADDED
@@ -0,0 +1,51 @@
1
+ //#region src/mime.ts
2
+ const EXTENSION_TO_MIME_MAP = {
3
+ html: "text/html",
4
+ js: "text/javascript",
5
+ mjs: "text/javascript",
6
+ wasm: "application/wasm",
7
+ webmanifest: "application/manifest+json",
8
+ css: "text/css",
9
+ json: "application/json",
10
+ ttf: "font/ttf",
11
+ otf: "font/otf",
12
+ woff: "font/woff",
13
+ woff2: "font/woff2",
14
+ eot: "application/vnd.ms-fontobject",
15
+ png: "image/png",
16
+ jpg: "image/jpg",
17
+ jpeg: "image/jpeg",
18
+ gif: "image/gif",
19
+ svg: "image/svg+xml",
20
+ webp: "image/webp",
21
+ bmp: "image/bmp",
22
+ tiff: "image/tiff",
23
+ avif: "image/avif",
24
+ tif: "image/tiff",
25
+ heic: "image/heic",
26
+ heif: "image/heif"
27
+ };
28
+ const EXCLUDED_EXTENSIONS = [
29
+ "js",
30
+ "mjs",
31
+ "wasm",
32
+ "webmanifest"
33
+ ];
34
+ const MIME_TYPE = EXTENSION_TO_MIME_MAP;
35
+ const USER_MIME_TYPE = Object.fromEntries(Object.entries(EXTENSION_TO_MIME_MAP).filter(([ext]) => !EXCLUDED_EXTENSIONS.includes(ext)));
36
+ const FORMAT_MIME_TYPE = MIME_TYPE;
37
+ const EXT_TO_MIME = Object.fromEntries(Object.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [`.${ext}`, mime]));
38
+ const MIME_TO_EXT = Object.fromEntries(Object.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [mime, `.${ext}`]));
39
+ function getPermittedContentType(ext) {
40
+ return EXT_TO_MIME[ext.startsWith(".") ? ext : `.${ext}`];
41
+ }
42
+ function isFormatAssetExtension(ext) {
43
+ return ext in FORMAT_MIME_TYPE;
44
+ }
45
+ function isUserAssetExtension(ext) {
46
+ return ext in USER_MIME_TYPE;
47
+ }
48
+
49
+ //#endregion
50
+ export { EXCLUDED_EXTENSIONS, EXTENSION_TO_MIME_MAP, EXT_TO_MIME, FORMAT_MIME_TYPE, MIME_TO_EXT, MIME_TYPE, USER_MIME_TYPE, getPermittedContentType, isFormatAssetExtension, isUserAssetExtension };
51
+ //# sourceMappingURL=mime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mime.js","names":["MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>>","USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>>","EXT_TO_MIME: Readonly<Record<string, string>>","MIME_TO_EXT: Readonly<Record<string, string>>"],"sources":["../src/mime.ts"],"sourcesContent":["export const EXTENSION_TO_MIME_MAP = {\n\t// format-level\n\thtml: 'text/html',\n\tjs: 'text/javascript',\n\tmjs: 'text/javascript',\n\twasm: 'application/wasm',\n\twebmanifest: 'application/manifest+json',\n\n\t// user-level\n\tcss: 'text/css',\n\tjson: 'application/json',\n\tttf: 'font/ttf',\n\totf: 'font/otf',\n\twoff: 'font/woff',\n\twoff2: 'font/woff2',\n\teot: 'application/vnd.ms-fontobject',\n\tpng: 'image/png',\n\tjpg: 'image/jpg',\n\tjpeg: 'image/jpeg',\n\tgif: 'image/gif',\n\tsvg: 'image/svg+xml',\n\twebp: 'image/webp',\n\tbmp: 'image/bmp',\n\ttiff: 'image/tiff',\n\tavif: 'image/avif',\n\ttif: 'image/tiff',\n\theic: 'image/heic',\n\theif: 'image/heif'\n} as const\n\nexport const EXCLUDED_EXTENSIONS = ['js', 'mjs', 'wasm', 'webmanifest'] as const\n\nexport type FormatAssetsMimeExtension = keyof typeof EXTENSION_TO_MIME_MAP\n\nexport type ExcludedExtensions = (typeof EXCLUDED_EXTENSIONS)[number]\n\nexport type UserAssetsMimeExtension = Exclude<FormatAssetsMimeExtension, ExcludedExtensions>\n\nexport const MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>> = EXTENSION_TO_MIME_MAP\n\nexport type MimeType = (typeof MIME_TYPE)[FormatAssetsMimeExtension]\n\nexport const USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).filter(([ext]) => !EXCLUDED_EXTENSIONS.includes(ext as ExcludedExtensions))\n) as Readonly<Record<UserAssetsMimeExtension, string>>\n\nexport const FORMAT_MIME_TYPE = MIME_TYPE\n\nexport const EXT_TO_MIME: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [`.${ext}`, mime])\n)\n\nexport const MIME_TO_EXT: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [mime, `.${ext}`])\n) as Readonly<Record<string, string>>\n\nexport function getPermittedContentType(ext: string): string | undefined {\n\tconst normalized = ext.startsWith('.') ? ext : `.${ext}`\n\treturn EXT_TO_MIME[normalized]\n}\n\nexport function isFormatAssetExtension(ext: string): ext is FormatAssetsMimeExtension {\n\treturn ext in FORMAT_MIME_TYPE\n}\n\nexport function isUserAssetExtension(ext: string): ext is UserAssetsMimeExtension {\n\treturn ext in USER_MIME_TYPE\n}\n"],"mappings":";AAAA,MAAa,wBAAwB;CAEpC,MAAM;CACN,IAAI;CACJ,KAAK;CACL,MAAM;CACN,aAAa;CAGb,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,OAAO;CACP,KAAK;CACL,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,MAAM;CACN,MAAM;CACN,KAAK;CACL,MAAM;CACN,MAAM;CACN;AAED,MAAa,sBAAsB;CAAC;CAAM;CAAO;CAAQ;CAAc;AAQvE,MAAaA,YAAiE;AAI9E,MAAaC,iBAAoE,OAAO,YACvF,OAAO,QAAQ,sBAAsB,CAAC,QAAQ,CAAC,SAAS,CAAC,oBAAoB,SAAS,IAA0B,CAAC,CACjH;AAED,MAAa,mBAAmB;AAEhC,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,IAAI,OAAO,KAAK,CAAC,CAC7E;AAED,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,MAAM,IAAI,MAAM,CAAC,CAC7E;AAED,SAAgB,wBAAwB,KAAiC;AAExE,QAAO,YADY,IAAI,WAAW,IAAI,GAAG,MAAM,IAAI;;AAIpD,SAAgB,uBAAuB,KAA+C;AACrF,QAAO,OAAO;;AAGf,SAAgB,qBAAqB,KAA6C;AACjF,QAAO,OAAO"}
package/package.json CHANGED
@@ -1,32 +1,39 @@
1
1
  {
2
- "name": "@format.dev/utils",
3
- "version": "0.0.0",
4
- "description": "A bootstrap placeholder reserving this package name. Format is not released yet.",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/format-hq/format-js.git",
9
- "directory": "packages/utils"
10
- },
11
- "homepage": "https://format.dev",
12
- "files": [
13
- "LICENSE",
14
- "README.md",
15
- "index.d.ts",
16
- "index.js"
17
- ],
18
- "type": "module",
19
- "main": "./index.js",
20
- "types": "./index.d.ts",
21
- "exports": {
22
- ".": {
23
- "types": "./index.d.ts",
24
- "default": "./index.js"
25
- }
26
- },
27
- "publishConfig": {
28
- "access": "public",
29
- "tag": "bootstrap",
30
- "registry": "https://registry.npmjs.org/"
31
- }
32
- }
2
+ "name": "@format.dev/utils",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "Shared utilities across the Format platform.",
6
+ "author": "the format.dev developers",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/format-hq/format-js.git",
10
+ "directory": "packages/utils"
11
+ },
12
+ "type": "module",
13
+ "sideEffects": false,
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "LICENSE"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "rimraf": "^6.1.2",
29
+ "tsdown": "0.17.0-beta.6",
30
+ "typescript": "^5",
31
+ "vitest": "^4.1.8"
32
+ },
33
+ "scripts": {
34
+ "clean": "rimraf dist",
35
+ "build": "tsdown",
36
+ "dev": "tsdown --watch",
37
+ "typecheck": "tsc -p tsconfig.json --noEmit"
38
+ }
39
+ }
package/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- // This is a bootstrap placeholder. Format is not released yet.
2
- export {}
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- // This is a bootstrap placeholder. Format is not released yet, and this version
2
- // carries no implementation. See README.md.
3
- export {}