agent-sanitizer 2.64.0 → 2.64.2
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-THIRD-PARTY +31 -3
- package/THREAT-MODEL.md +7 -3
- package/package.json +4 -1
- package/src/css-syntax-data.mjs +274 -0
- package/src/gfm.mjs +3 -2
- package/src/html-tree-adapter.mjs +150 -1
- package/src/html.mjs +873 -354
- package/src/url-attributes.mjs +76 -0
- package/src/vendor/gfm-autolink-literal.mjs +53 -178
- package/types/css-syntax-data.d.mts +6 -0
- package/types/html-tree-adapter.d.mts +46 -0
- package/types/html.d.mts +43 -7
- package/types/url-attributes.d.mts +11 -0
package/LICENSE-THIRD-PARTY
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
Third-party code redistributed in this package.
|
|
2
2
|
|
|
3
|
-
src/vendor/gfm-autolink-literal.mjs
|
|
4
|
-
|
|
5
|
-
function changed (see
|
|
3
|
+
src/vendor/gfm-autolink-literal.mjs (generated, not tracked)
|
|
4
|
+
Generated at install and pack time from micromark-extension-gfm-autolink-literal@2.1.0
|
|
5
|
+
with one function changed by scripts/patches/ (see scripts/gen-gfm-autolink.mjs).
|
|
6
|
+
Original licence follows.
|
|
6
7
|
|
|
7
8
|
(The MIT License)
|
|
8
9
|
|
|
@@ -26,3 +27,30 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
26
27
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
27
28
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
28
29
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
30
|
+
|
|
31
|
+
src/css-syntax-data.mjs
|
|
32
|
+
CSS value grammars extracted by scripts/gen-css-syntax.mjs from
|
|
33
|
+
css-tree@3.2.1's definition-syntax-data (mdn-data@2.27.1, CC0 1.0, plus
|
|
34
|
+
css-tree's own patch). css-tree's licence follows.
|
|
35
|
+
|
|
36
|
+
(The MIT License)
|
|
37
|
+
|
|
38
|
+
Copyright (C) 2016-2026 by Roman Dvornov
|
|
39
|
+
|
|
40
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
41
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
42
|
+
in the Software without restriction, including without limitation the rights
|
|
43
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
44
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
45
|
+
furnished to do so, subject to the following conditions:
|
|
46
|
+
|
|
47
|
+
The above copyright notice and this permission notice shall be included in
|
|
48
|
+
all copies or substantial portions of the Software.
|
|
49
|
+
|
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
51
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
52
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
53
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
54
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
55
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
56
|
+
THE SOFTWARE.
|
package/THREAT-MODEL.md
CHANGED
|
@@ -142,9 +142,13 @@ exfil-shaped URLs.
|
|
|
142
142
|
|
|
143
143
|
## Layer 3—exfil URLs (detection only)
|
|
144
144
|
|
|
145
|
-
Report—never rewrite—URLs in markdown links/images/definitions
|
|
146
|
-
attributes (`src
|
|
147
|
-
`
|
|
145
|
+
Report—never rewrite—URLs in markdown links/images/definitions, every HTML/SVG
|
|
146
|
+
URL attribute in `src/url-attributes.mjs` (`src`, `href`, `xlink:href`,
|
|
147
|
+
`srcset`/`imagesrcset`, `ping`, `poster`, `object data`, form
|
|
148
|
+
`action`/`formaction`, `meta refresh`, …), CSS `url()` in `style` attributes and
|
|
149
|
+
`<style>` elements, and the same again inside HTML comments, that are shaped to
|
|
150
|
+
carry data off-origin. A URL's scheme is read by the WHATWG URL parser, and
|
|
151
|
+
`srcset` and `meta refresh` by ports of the HTML Standard's own parsing steps:
|
|
148
152
|
|
|
149
153
|
- suspicious query/fragment parameters (long base64/hex blobs, credential-shaped
|
|
150
154
|
tokens), tuned to skip request-signing / pagination / analytics parameters
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-sanitizer",
|
|
3
|
-
"version": "2.64.
|
|
3
|
+
"version": "2.64.2",
|
|
4
4
|
"description": "Defend an agent against hidden-content injection: strip payload-capable invisible Unicode and ANSI, splice out human-invisible HTML, and flag data-exfil URLs in untrusted text before any model sees it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -58,12 +58,14 @@
|
|
|
58
58
|
"agent-sanitizer": "link:.",
|
|
59
59
|
"bun": "1.3.11",
|
|
60
60
|
"c8": "11.0.0",
|
|
61
|
+
"diff": "9.0.0",
|
|
61
62
|
"esbuild": "0.28.1",
|
|
62
63
|
"eslint": "10.4.0",
|
|
63
64
|
"eslint-plugin-redos": "4.5.0",
|
|
64
65
|
"fast-check": "4.8.0",
|
|
65
66
|
"globals": "17.6.0",
|
|
66
67
|
"lint-staged": "^17.0.5",
|
|
68
|
+
"micromark-extension-gfm-autolink-literal": "2.1.0",
|
|
67
69
|
"prettier": "^3.0.0",
|
|
68
70
|
"rehype-parse": "9.0.1",
|
|
69
71
|
"remark-gfm": "4.0.1",
|
|
@@ -249,6 +251,7 @@
|
|
|
249
251
|
"build:types": "tsc -p tsconfig.build.json && tsc -p tsconfig.build-hooks.json",
|
|
250
252
|
"gen:joining-type": "node scripts/gen-joining-type.mjs",
|
|
251
253
|
"gen:unicode": "node scripts/gen-unicode.mjs",
|
|
254
|
+
"gen:gfm-autolink": "node scripts/gen-gfm-autolink.mjs",
|
|
252
255
|
"gen:unicode-properties": "node scripts/gen-unicode-properties.mjs",
|
|
253
256
|
"gen:fail-open-lib": "node scripts/gen-fail-open-lib.mjs",
|
|
254
257
|
"gen:node-floor-lib": "node scripts/gen-node-floor-lib.mjs",
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/** GENERATED by scripts/gen-css-syntax.mjs — DO NOT EDIT (mdn-data, CC0). */
|
|
2
|
+
// Stryker disable all — generated; its correctness is the round-trip test.
|
|
3
|
+
/** @type {{ types: Record<string, string>, properties: Record<string, string> }} */
|
|
4
|
+
export const CSS_SYNTAX = {
|
|
5
|
+
types: {
|
|
6
|
+
"-legacy-gradient": "<-webkit-gradient()> | <-legacy-linear-gradient> | <-legacy-repeating-linear-gradient> | <-legacy-radial-gradient> | <-legacy-repeating-radial-gradient>",
|
|
7
|
+
"-legacy-linear-gradient": "-moz-linear-gradient( <-legacy-linear-gradient-arguments> ) | -webkit-linear-gradient( <-legacy-linear-gradient-arguments> ) | -o-linear-gradient( <-legacy-linear-gradient-arguments> )",
|
|
8
|
+
"-legacy-linear-gradient-arguments": "[ <angle> | <side-or-corner> ]? , <color-stop-list>",
|
|
9
|
+
"-legacy-radial-gradient": "-moz-radial-gradient( <-legacy-radial-gradient-arguments> ) | -webkit-radial-gradient( <-legacy-radial-gradient-arguments> ) | -o-radial-gradient( <-legacy-radial-gradient-arguments> )",
|
|
10
|
+
"-legacy-radial-gradient-arguments": "[ <position> , ]? [ [ [ <-legacy-radial-gradient-shape> || <-legacy-radial-gradient-size> ] | [ <length> | <percentage> ]{2} ] , ]? <color-stop-list>",
|
|
11
|
+
"-legacy-radial-gradient-shape": "circle | ellipse",
|
|
12
|
+
"-legacy-radial-gradient-size": "closest-side | closest-corner | farthest-side | farthest-corner | contain | cover",
|
|
13
|
+
"-legacy-repeating-linear-gradient": "-moz-repeating-linear-gradient( <-legacy-linear-gradient-arguments> ) | -webkit-repeating-linear-gradient( <-legacy-linear-gradient-arguments> ) | -o-repeating-linear-gradient( <-legacy-linear-gradient-arguments> )",
|
|
14
|
+
"-legacy-repeating-radial-gradient": "-moz-repeating-radial-gradient( <-legacy-radial-gradient-arguments> ) | -webkit-repeating-radial-gradient( <-legacy-radial-gradient-arguments> ) | -o-repeating-radial-gradient( <-legacy-radial-gradient-arguments> )",
|
|
15
|
+
"-ms-filter-function": "<-ms-filter-function-progid> | <-ms-filter-function-legacy>",
|
|
16
|
+
"-ms-filter-function-legacy": "<ident-token> | <function-token> <any-value>? )",
|
|
17
|
+
"-ms-filter-function-list": "<-ms-filter-function>+",
|
|
18
|
+
"-ms-filter-function-progid": "'progid:' [ <ident-token> '.' ]* [ <ident-token> | <function-token> <any-value>? ) ]",
|
|
19
|
+
"-non-standard-color": "-moz-ButtonDefault | -moz-ButtonHoverFace | -moz-ButtonHoverText | -moz-CellHighlight | -moz-CellHighlightText | -moz-Combobox | -moz-ComboboxText | -moz-Dialog | -moz-DialogText | -moz-dragtargetzone | -moz-EvenTreeRow | -moz-Field | -moz-FieldText | -moz-html-CellHighlight | -moz-html-CellHighlightText | -moz-mac-accentdarkestshadow | -moz-mac-accentdarkshadow | -moz-mac-accentface | -moz-mac-accentlightesthighlight | -moz-mac-accentlightshadow | -moz-mac-accentregularhighlight | -moz-mac-accentregularshadow | -moz-mac-chrome-active | -moz-mac-chrome-inactive | -moz-mac-focusring | -moz-mac-menuselect | -moz-mac-menushadow | -moz-mac-menutextselect | -moz-MenuHover | -moz-MenuHoverText | -moz-MenuBarText | -moz-MenuBarHoverText | -moz-nativehyperlinktext | -moz-OddTreeRow | -moz-win-communicationstext | -moz-win-mediatext | -moz-activehyperlinktext | -moz-default-background-color | -moz-default-color | -moz-hyperlinktext | -moz-visitedhyperlinktext | -webkit-activelink | -webkit-focus-ring-color | -webkit-link | -webkit-text",
|
|
20
|
+
"-non-standard-display": "-ms-inline-flexbox | -ms-grid | -ms-inline-grid | -webkit-flex | -webkit-inline-flex | -webkit-box | -webkit-inline-box | -moz-inline-stack | -moz-box | -moz-inline-box",
|
|
21
|
+
"-non-standard-font": "-apple-system-body | -apple-system-headline | -apple-system-subheadline | -apple-system-caption1 | -apple-system-caption2 | -apple-system-footnote | -apple-system-short-body | -apple-system-short-headline | -apple-system-short-subheadline | -apple-system-short-caption1 | -apple-system-short-footnote | -apple-system-tall-body",
|
|
22
|
+
"-non-standard-generic-family": "-apple-system | BlinkMacSystemFont",
|
|
23
|
+
"-non-standard-overflow": "overlay | -moz-scrollbars-none | -moz-scrollbars-horizontal | -moz-scrollbars-vertical | -moz-hidden-unscrollable",
|
|
24
|
+
"-non-standard-size": "intrinsic | min-intrinsic | -webkit-fill-available | -webkit-fit-content | -webkit-min-content | -webkit-max-content | -moz-available | -moz-fit-content | -moz-min-content | -moz-max-content",
|
|
25
|
+
"-webkit-gradient()": "-webkit-gradient( <-webkit-gradient-type>, <-webkit-gradient-point> [, <-webkit-gradient-point> | , <-webkit-gradient-radius>, <-webkit-gradient-point> ] [, <-webkit-gradient-radius>]? [, <-webkit-gradient-color-stop>]* )",
|
|
26
|
+
"-webkit-gradient-color-stop": "from( <color> ) | color-stop( [ <number-zero-one> | <percentage> ] , <color> ) | to( <color> )",
|
|
27
|
+
"-webkit-gradient-point": "[ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]",
|
|
28
|
+
"-webkit-gradient-radius": "<length> | <percentage>",
|
|
29
|
+
"-webkit-gradient-type": "linear | radial",
|
|
30
|
+
"absolute-size": "xx-small | x-small | small | medium | large | x-large | xx-large | xxx-large",
|
|
31
|
+
"alpha-value": "<number> | <percentage>",
|
|
32
|
+
"anchor()": "anchor( <anchor-name>? && <anchor-side>, <length-percentage>? )",
|
|
33
|
+
"anchor-name": "<dashed-ident>",
|
|
34
|
+
"anchor-side": "inside | outside | top | left | right | bottom | start | end | self-start | self-end | <percentage> | center",
|
|
35
|
+
"anchor-size()": "anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )",
|
|
36
|
+
"anchor-size": "width | height | block | inline | self-block | self-inline",
|
|
37
|
+
"angle-percentage": "<angle> | <percentage>",
|
|
38
|
+
"angular-color-hint": "<angle-percentage> | <zero>",
|
|
39
|
+
"angular-color-stop": "<color> <color-stop-angle>?",
|
|
40
|
+
"angular-color-stop-list": "<angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#?",
|
|
41
|
+
"attachment": "scroll | fixed | local",
|
|
42
|
+
"basic-shape": "<inset()> | <xywh()> | <rect()> | <circle()> | <ellipse()> | <polygon()> | <path()>",
|
|
43
|
+
"bg-clip": "<visual-box> | border-area | text",
|
|
44
|
+
"bg-image": "<image> | none",
|
|
45
|
+
"bg-layer": "<bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <visual-box> || <visual-box>",
|
|
46
|
+
"bg-position": "[ [ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ] ]",
|
|
47
|
+
"bg-size": "[ <length-percentage [0,∞]> | auto ]{1,2} | cover | contain",
|
|
48
|
+
"blur()": "blur( <length>? )",
|
|
49
|
+
"bottom": "<length> | auto",
|
|
50
|
+
"brightness()": "brightness( [ <number> | <percentage> ]? )",
|
|
51
|
+
"calc-constant": "e | pi | infinity | -infinity | NaN",
|
|
52
|
+
"calc-product": "<calc-value> [ '*' <calc-value> | '/' <number> ]*",
|
|
53
|
+
"calc-size()": "calc-size( <calc-size-basis>, <calc-sum> )",
|
|
54
|
+
"calc-size-basis": "<intrinsic-size-keyword> | <calc-size()> | any | <calc-sum>",
|
|
55
|
+
"calc-sum": "<calc-product> [ [ '+' | '-' ] <calc-product> ]*",
|
|
56
|
+
"calc-value": "<number> | <dimension> | <percentage> | <calc-constant> | ( <calc-sum> )",
|
|
57
|
+
"cf-final-image": "<image> | <color>",
|
|
58
|
+
"cf-mixing-image": "<percentage>? && <image>",
|
|
59
|
+
"circle()": "circle( <radial-size>? [ at <position> ]? )",
|
|
60
|
+
"clip-source": "<url>",
|
|
61
|
+
"cmyk-component": "<number> | <percentage> | none",
|
|
62
|
+
"color()": "color( <colorspace-params> [ / [ <alpha-value> | none ] ]? )",
|
|
63
|
+
"color": "<color-base> | currentColor | <system-color> | <device-cmyk()> | <light-dark()> | <-non-standard-color>",
|
|
64
|
+
"color-base": "<hex-color> | <color-function> | <named-color> | <color-mix()> | transparent",
|
|
65
|
+
"color-function": "<rgb()> | <rgba()> | <hsl()> | <hsla()> | <hwb()> | <lab()> | <lch()> | <oklab()> | <oklch()> | <color()>",
|
|
66
|
+
"color-interpolation-method": "in [ <rectangular-color-space> | <polar-color-space> <hue-interpolation-method>? | <custom-color-space> ]",
|
|
67
|
+
"color-mix()": "color-mix( <color-interpolation-method> , [ <color> && <percentage [0,100]>? ]#{2} )",
|
|
68
|
+
"color-stop-angle": "[ <angle-percentage> | <zero> ]{1,2}",
|
|
69
|
+
"color-stop-length": "<length-percentage>{1,2}",
|
|
70
|
+
"color-stop-list": "<linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#?",
|
|
71
|
+
"colorspace-params": "[ <predefined-rgb-params> | <xyz-params>]",
|
|
72
|
+
"conic-gradient()": "conic-gradient( [ <conic-gradient-syntax> ] )",
|
|
73
|
+
"conic-gradient-syntax": "[ [ [ from [ <angle> | <zero> ] ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <angular-color-stop-list>",
|
|
74
|
+
"contrast()": "contrast( [ <number> | <percentage> ]? )",
|
|
75
|
+
"cross-fade()": "cross-fade( <cf-mixing-image> , <cf-final-image>? )",
|
|
76
|
+
"custom-color-space": "<dashed-ident>",
|
|
77
|
+
"device-cmyk()": "<legacy-device-cmyk-syntax> | <modern-device-cmyk-syntax>",
|
|
78
|
+
"display-box": "contents | none",
|
|
79
|
+
"display-inside": "flow | flow-root | table | flex | grid | ruby",
|
|
80
|
+
"display-internal": "table-row-group | table-header-group | table-footer-group | table-row | table-cell | table-column-group | table-column | table-caption | ruby-base | ruby-text | ruby-base-container | ruby-text-container",
|
|
81
|
+
"display-legacy": "inline-block | inline-list-item | inline-table | inline-flex | inline-grid",
|
|
82
|
+
"display-listitem": "<display-outside>? && [ flow | flow-root ]? && list-item",
|
|
83
|
+
"display-outside": "block | inline | run-in",
|
|
84
|
+
"drop-shadow()": "drop-shadow( [ <color>? && <length>{2,3} ] )",
|
|
85
|
+
"element()": "element( <custom-ident> , [ first | start | last | first-except ]? ) | element( <id-selector> )",
|
|
86
|
+
"ellipse()": "ellipse( <radial-size>? [ at <position> ]? )",
|
|
87
|
+
"family-name": "<string> | <custom-ident>+",
|
|
88
|
+
"filter-function": "<blur()> | <brightness()> | <contrast()> | <drop-shadow()> | <grayscale()> | <hue-rotate()> | <invert()> | <opacity()> | <saturate()> | <sepia()>",
|
|
89
|
+
"filter-value-list": "[ <filter-function> | <url> ]+",
|
|
90
|
+
"final-bg-layer": "<bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <visual-box> || <visual-box> || <'background-color'>",
|
|
91
|
+
"font-variant-css2": "normal | small-caps",
|
|
92
|
+
"font-weight-absolute": "normal | bold | <number [1,1000]>",
|
|
93
|
+
"font-width-css3": "normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded",
|
|
94
|
+
"generic-complete": "serif | sans-serif | system-ui | cursive | fantasy | math | monospace",
|
|
95
|
+
"generic-family": "<generic-script-specific>| <generic-complete> | <generic-incomplete> | <-non-standard-generic-family>",
|
|
96
|
+
"generic-incomplete": "ui-serif | ui-sans-serif | ui-monospace | ui-rounded",
|
|
97
|
+
"generic-script-specific": "generic(kai) | generic(fangsong) | generic(nastaliq)",
|
|
98
|
+
"geometry-box": "<shape-box> | fill-box | stroke-box | view-box",
|
|
99
|
+
"gradient": "<linear-gradient()> | <repeating-linear-gradient()> | <radial-gradient()> | <repeating-radial-gradient()> | <conic-gradient()> | <repeating-conic-gradient()> | <-legacy-gradient>",
|
|
100
|
+
"grayscale()": "grayscale( [ <number> | <percentage> ]? )",
|
|
101
|
+
"hsl()": "hsl( <hue>, <percentage>, <percentage>, <alpha-value>? ) | hsl( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )",
|
|
102
|
+
"hsla()": "hsla( <hue>, <percentage>, <percentage>, <alpha-value>? ) | hsla( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )",
|
|
103
|
+
"hue": "<number> | <angle>",
|
|
104
|
+
"hue-interpolation-method": "[ shorter | longer | increasing | decreasing ] hue",
|
|
105
|
+
"hue-rotate()": "hue-rotate( [ <angle> | <zero> ]? )",
|
|
106
|
+
"hwb()": "hwb( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )",
|
|
107
|
+
"id-selector": "<hash-token>",
|
|
108
|
+
"image()": "image( <image-tags>? [ <image-src>? , <color>? ]! )",
|
|
109
|
+
"image": "<url> | <image()> | <image-set()> | <element()> | <paint()> | <cross-fade()> | <gradient>",
|
|
110
|
+
"image-set()": "image-set( <image-set-option># )",
|
|
111
|
+
"image-set-option": "[ <image> | <string> ] [ <resolution> || type(<string>) ]",
|
|
112
|
+
"image-src": "<url> | <string>",
|
|
113
|
+
"image-tags": "ltr | rtl",
|
|
114
|
+
"inset()": "inset( <length-percentage>{1,4} [ round <'border-radius'> ]? )",
|
|
115
|
+
"intrinsic-size-keyword": "min-content | max-content | fit-content",
|
|
116
|
+
"invert()": "invert( [ <number> | <percentage> ]? )",
|
|
117
|
+
"lab()": "lab( [<percentage> | <number> | none] [ <percentage> | <number> | none] [ <percentage> | <number> | none] [ / [<alpha-value> | none] ]? )",
|
|
118
|
+
"lch()": "lch( [<percentage> | <number> | none] [ <percentage> | <number> | none] [ <hue> | none] [ / [<alpha-value> | none] ]? )",
|
|
119
|
+
"left": "<length> | auto",
|
|
120
|
+
"legacy-device-cmyk-syntax": "device-cmyk( <number>#{4} )",
|
|
121
|
+
"length-percentage": "<length> | <percentage>",
|
|
122
|
+
"light-dark()": "light-dark( <color>, <color> )",
|
|
123
|
+
"line-style": "none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset",
|
|
124
|
+
"line-width": "<length> | thin | medium | thick",
|
|
125
|
+
"linear-color-hint": "<length-percentage>",
|
|
126
|
+
"linear-color-stop": "<color> <color-stop-length>?",
|
|
127
|
+
"linear-gradient()": "linear-gradient( [ <linear-gradient-syntax> ] )",
|
|
128
|
+
"linear-gradient-syntax": "[ [ <angle> | <zero> | to <side-or-corner> ] || <color-interpolation-method> ]? , <color-stop-list>",
|
|
129
|
+
"matrix()": "matrix( <number>#{6} )",
|
|
130
|
+
"matrix3d()": "matrix3d( <number>#{16} )",
|
|
131
|
+
"modern-device-cmyk-syntax": "device-cmyk( <cmyk-component>{4} [ / [ <alpha-value> | none ] ]? )",
|
|
132
|
+
"named-color": "aliceblue | antiquewhite | aqua | aquamarine | azure | beige | bisque | black | blanchedalmond | blue | blueviolet | brown | burlywood | cadetblue | chartreuse | chocolate | coral | cornflowerblue | cornsilk | crimson | cyan | darkblue | darkcyan | darkgoldenrod | darkgray | darkgreen | darkgrey | darkkhaki | darkmagenta | darkolivegreen | darkorange | darkorchid | darkred | darksalmon | darkseagreen | darkslateblue | darkslategray | darkslategrey | darkturquoise | darkviolet | deeppink | deepskyblue | dimgray | dimgrey | dodgerblue | firebrick | floralwhite | forestgreen | fuchsia | gainsboro | ghostwhite | gold | goldenrod | gray | green | greenyellow | grey | honeydew | hotpink | indianred | indigo | ivory | khaki | lavender | lavenderblush | lawngreen | lemonchiffon | lightblue | lightcoral | lightcyan | lightgoldenrodyellow | lightgray | lightgreen | lightgrey | lightpink | lightsalmon | lightseagreen | lightskyblue | lightslategray | lightslategrey | lightsteelblue | lightyellow | lime | limegreen | linen | magenta | maroon | mediumaquamarine | mediumblue | mediumorchid | mediumpurple | mediumseagreen | mediumslateblue | mediumspringgreen | mediumturquoise | mediumvioletred | midnightblue | mintcream | mistyrose | moccasin | navajowhite | navy | oldlace | olive | olivedrab | orange | orangered | orchid | palegoldenrod | palegreen | paleturquoise | palevioletred | papayawhip | peachpuff | peru | pink | plum | powderblue | purple | rebeccapurple | red | rosybrown | royalblue | saddlebrown | salmon | sandybrown | seagreen | seashell | sienna | silver | skyblue | slateblue | slategray | slategrey | snow | springgreen | steelblue | tan | teal | thistle | tomato | turquoise | violet | wheat | white | whitesmoke | yellow | yellowgreen",
|
|
133
|
+
"number-zero-one": "<number [0,1]>",
|
|
134
|
+
"oklab()": "oklab( [ <percentage> | <number> | none] [ <percentage> | <number> | none] [ <percentage> | <number> | none] [ / [<alpha-value> | none] ]? )",
|
|
135
|
+
"oklch()": "oklch( [ <percentage> | <number> | none] [ <percentage> | <number> | none] [ <hue> | none] [ / [<alpha-value> | none] ]? )",
|
|
136
|
+
"opacity()": "opacity( [ <number> | <percentage> ]? )",
|
|
137
|
+
"opacity-value": "<number> | <percentage>",
|
|
138
|
+
"paint()": "paint( <ident>, <declaration-value>? )",
|
|
139
|
+
"path()": "path( <'fill-rule'>? , <string> )",
|
|
140
|
+
"perspective()": "perspective( [ <length [0,∞]> | none ] )",
|
|
141
|
+
"polar-color-space": "hsl | hwb | lch | oklch",
|
|
142
|
+
"polygon()": "polygon( <'fill-rule'>? , [ <length-percentage> <length-percentage> ]# )",
|
|
143
|
+
"position": "[ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]",
|
|
144
|
+
"predefined-rgb": "srgb | srgb-linear | display-p3 | display-p3-linear | a98-rgb | prophoto-rgb | rec2020",
|
|
145
|
+
"predefined-rgb-params": "<predefined-rgb> [ <number> | <percentage> | none ]{3}",
|
|
146
|
+
"radial-extent": "closest-corner | closest-side | farthest-corner | farthest-side",
|
|
147
|
+
"radial-gradient()": "radial-gradient( [ <radial-gradient-syntax> ] )",
|
|
148
|
+
"radial-gradient-syntax": "[ [ [ <radial-shape> || <radial-size> ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <color-stop-list>",
|
|
149
|
+
"radial-shape": "circle | ellipse",
|
|
150
|
+
"radial-size": "<radial-extent> | <length [0,∞]> | <length-percentage [0,∞]>{2}",
|
|
151
|
+
"rect()": "rect( [ <length-percentage> | auto ]{4} [ round <'border-radius'> ]? )",
|
|
152
|
+
"rectangular-color-space": "srgb | srgb-linear | display-p3 | display-p3-linear | a98-rgb | prophoto-rgb | rec2020 | lab | oklab | xyz | xyz-d50 | xyz-d65",
|
|
153
|
+
"relative-size": "larger | smaller",
|
|
154
|
+
"repeat-style": "repeat-x | repeat-y | [ repeat | space | round | no-repeat ]{1,2}",
|
|
155
|
+
"repeating-conic-gradient()": "repeating-conic-gradient( [ <conic-gradient-syntax> ] )",
|
|
156
|
+
"repeating-linear-gradient()": "repeating-linear-gradient( [ <linear-gradient-syntax> ] )",
|
|
157
|
+
"repeating-radial-gradient()": "repeating-radial-gradient( [ <radial-gradient-syntax> ] )",
|
|
158
|
+
"rgb()": "rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? ) | rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? )",
|
|
159
|
+
"rgba()": "rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? ) | rgba( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? )",
|
|
160
|
+
"right": "<length> | auto",
|
|
161
|
+
"rotate()": "rotate( [ <angle> | <zero> ] )",
|
|
162
|
+
"rotate3d()": "rotate3d( <number> , <number> , <number> , [ <angle> | <zero> ] )",
|
|
163
|
+
"rotateX()": "rotateX( [ <angle> | <zero> ] )",
|
|
164
|
+
"rotateY()": "rotateY( [ <angle> | <zero> ] )",
|
|
165
|
+
"rotateZ()": "rotateZ( [ <angle> | <zero> ] )",
|
|
166
|
+
"saturate()": "saturate( [ <number> | <percentage> ]? )",
|
|
167
|
+
"scale()": "scale( [ <number> | <percentage> ]#{1,2} )",
|
|
168
|
+
"scale3d()": "scale3d( [ <number> | <percentage> ]#{3} )",
|
|
169
|
+
"scaleX()": "scaleX( [ <number> | <percentage> ] )",
|
|
170
|
+
"scaleY()": "scaleY( [ <number> | <percentage> ] )",
|
|
171
|
+
"scaleZ()": "scaleZ( [ <number> | <percentage> ] )",
|
|
172
|
+
"sepia()": "sepia( [ <number> | <percentage> ]? )",
|
|
173
|
+
"shape": "rect( <top>, <right>, <bottom>, <left> ) | rect( <top> <right> <bottom> <left> )",
|
|
174
|
+
"shape-box": "<visual-box> | margin-box",
|
|
175
|
+
"side-or-corner": "[ left | right ] || [ top | bottom ]",
|
|
176
|
+
"skew()": "skew( [ <angle> | <zero> ] , [ <angle> | <zero> ]? )",
|
|
177
|
+
"skewX()": "skewX( [ <angle> | <zero> ] )",
|
|
178
|
+
"skewY()": "skewY( [ <angle> | <zero> ] )",
|
|
179
|
+
"system-color": "AccentColor | AccentColorText | ActiveText | ButtonBorder | ButtonFace | ButtonText | Canvas | CanvasText | Field | FieldText | GrayText | Highlight | HighlightText | LinkText | Mark | MarkText | SelectedItem | SelectedItemText | VisitedText",
|
|
180
|
+
"system-family-name": "caption | icon | menu | message-box | small-caption | status-bar",
|
|
181
|
+
"top": "<length> | auto",
|
|
182
|
+
"transform-function": "<matrix()> | <translate()> | <translateX()> | <translateY()> | <scale()> | <scaleX()> | <scaleY()> | <rotate()> | <skew()> | <skewX()> | <skewY()> | <matrix3d()> | <translate3d()> | <translateZ()> | <scale3d()> | <scaleZ()> | <rotate3d()> | <rotateX()> | <rotateY()> | <rotateZ()> | <perspective()>",
|
|
183
|
+
"transform-list": "<transform-function>+",
|
|
184
|
+
"translate()": "translate( <length-percentage> , <length-percentage>? )",
|
|
185
|
+
"translate3d()": "translate3d( <length-percentage> , <length-percentage> , <length> )",
|
|
186
|
+
"translateX()": "translateX( <length-percentage> )",
|
|
187
|
+
"translateY()": "translateY( <length-percentage> )",
|
|
188
|
+
"translateZ()": "translateZ( <length> )",
|
|
189
|
+
"url": "url( <string> <url-modifier>* ) | <url-token>",
|
|
190
|
+
"url-modifier": "<ident> | <function-token> <any-value> )",
|
|
191
|
+
"visual-box": "content-box | padding-box | border-box",
|
|
192
|
+
"xywh()": "xywh( <length-percentage>{2} <length-percentage [0,∞]>{2} [ round <'border-radius'> ]? )",
|
|
193
|
+
"xyz-params": "<xyz-space> [ <number> | <percentage> | none ]{3}",
|
|
194
|
+
"xyz-space": "xyz | xyz-d50 | xyz-d65",
|
|
195
|
+
},
|
|
196
|
+
properties: {
|
|
197
|
+
"-webkit-background-clip": "[ <visual-box> | border | padding | content | text ]#",
|
|
198
|
+
"-webkit-text-fill-color": "<color>",
|
|
199
|
+
"-webkit-text-stroke": "<length> || <color>",
|
|
200
|
+
"-webkit-text-stroke-color": "<color>",
|
|
201
|
+
"all": "initial | inherit | unset | revert | revert-layer",
|
|
202
|
+
"background": "<bg-layer>#? , <final-bg-layer>",
|
|
203
|
+
"background-clip": "<bg-clip>#",
|
|
204
|
+
"background-color": "<color>",
|
|
205
|
+
"background-image": "<bg-image>#",
|
|
206
|
+
"border": "<line-width> || <line-style> || <color>",
|
|
207
|
+
"border-block": "<'border-block-start'>",
|
|
208
|
+
"border-block-end": "<'border-top-width'> || <'border-top-style'> || <color>",
|
|
209
|
+
"border-block-end-width": "<'border-top-width'>",
|
|
210
|
+
"border-block-start": "<'border-top-width'> || <'border-top-style'> || <color>",
|
|
211
|
+
"border-block-start-width": "<'border-top-width'>",
|
|
212
|
+
"border-block-width": "<'border-top-width'>{1,2}",
|
|
213
|
+
"border-bottom": "<line-width> || <line-style> || <color>",
|
|
214
|
+
"border-bottom-width": "<line-width>",
|
|
215
|
+
"border-inline": "<'border-block-start'>",
|
|
216
|
+
"border-inline-end": "<'border-top-width'> || <'border-top-style'> || <color>",
|
|
217
|
+
"border-inline-end-width": "<'border-top-width'>",
|
|
218
|
+
"border-inline-start": "<'border-top-width'> || <'border-top-style'> || <color>",
|
|
219
|
+
"border-inline-start-width": "<'border-top-width'>",
|
|
220
|
+
"border-inline-width": "<'border-top-width'>{1,2}",
|
|
221
|
+
"border-left": "<line-width> || <line-style> || <color>",
|
|
222
|
+
"border-left-width": "<line-width>",
|
|
223
|
+
"border-radius": "<length-percentage [0,∞]>{1,4} [ / <length-percentage [0,∞]>{1,4} ]?",
|
|
224
|
+
"border-right": "<line-width> || <line-style> || <color>",
|
|
225
|
+
"border-right-width": "<line-width>",
|
|
226
|
+
"border-top": "<line-width> || <line-style> || <color>",
|
|
227
|
+
"border-top-style": "<line-style>",
|
|
228
|
+
"border-top-width": "<line-width>",
|
|
229
|
+
"border-width": "<line-width>{1,4}",
|
|
230
|
+
"bottom": "auto | <length-percentage> | <anchor()> | <anchor-size()>",
|
|
231
|
+
"clip": "<shape> | auto",
|
|
232
|
+
"clip-path": "<clip-source> | [ <basic-shape> || <geometry-box> ] | none",
|
|
233
|
+
"color": "<color>",
|
|
234
|
+
"content-visibility": "visible | auto | hidden",
|
|
235
|
+
"display": "[ <display-outside> || <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy> | <-non-standard-display>",
|
|
236
|
+
"fill-rule": "nonzero | evenodd",
|
|
237
|
+
"filter": "none | <filter-value-list> | <-ms-filter-function-list>",
|
|
238
|
+
"float": "left | right | none | inline-start | inline-end",
|
|
239
|
+
"font": "[ [ <'font-style'> || <font-variant-css2> || <'font-weight'> || <font-width-css3> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'># ] | <system-family-name> | <-non-standard-font>",
|
|
240
|
+
"font-family": "[ <family-name> | <generic-family> ]#",
|
|
241
|
+
"font-size": "<absolute-size> | <relative-size> | <length-percentage [0,∞]> | math",
|
|
242
|
+
"font-style": "normal | italic | oblique <angle>?",
|
|
243
|
+
"font-weight": "<font-weight-absolute> | bolder | lighter",
|
|
244
|
+
"height": "auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()> | stretch | <-non-standard-size>",
|
|
245
|
+
"inset": "<'top'>{1,4}",
|
|
246
|
+
"left": "auto | <length-percentage> | <anchor()> | <anchor-size()>",
|
|
247
|
+
"line-height": "normal | <number> | <length> | <percentage>",
|
|
248
|
+
"max-height": "none | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()> | stretch | <-non-standard-size>",
|
|
249
|
+
"max-width": "none | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()> | stretch | <-non-standard-size>",
|
|
250
|
+
"opacity": "<opacity-value>",
|
|
251
|
+
"overflow": "[ visible | hidden | clip | scroll | auto ]{1,2} | <-non-standard-overflow>",
|
|
252
|
+
"padding": "<'padding-top'>{1,4}",
|
|
253
|
+
"padding-block": "<'padding-top'>{1,2}",
|
|
254
|
+
"padding-block-end": "<'padding-top'>",
|
|
255
|
+
"padding-block-start": "<'padding-top'>",
|
|
256
|
+
"padding-bottom": "<length-percentage [0,∞]>",
|
|
257
|
+
"padding-inline": "<'padding-top'>{1,2}",
|
|
258
|
+
"padding-inline-end": "<'padding-top'>",
|
|
259
|
+
"padding-inline-start": "<'padding-top'>",
|
|
260
|
+
"padding-left": "<length-percentage [0,∞]>",
|
|
261
|
+
"padding-right": "<length-percentage [0,∞]>",
|
|
262
|
+
"padding-top": "<length-percentage [0,∞]>",
|
|
263
|
+
"position": "static | relative | absolute | sticky | fixed | -webkit-sticky",
|
|
264
|
+
"right": "auto | <length-percentage> | <anchor()> | <anchor-size()>",
|
|
265
|
+
"text-indent": "<length-percentage> && hanging? && each-line?",
|
|
266
|
+
"top": "auto | <length-percentage> | <anchor()> | <anchor-size()>",
|
|
267
|
+
"transform": "none | <transform-list>",
|
|
268
|
+
"translate": "none | <length-percentage> [ <length-percentage> <length>? ]?",
|
|
269
|
+
"visibility": "visible | hidden | collapse",
|
|
270
|
+
"width": "auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()> | stretch | <-non-standard-size>",
|
|
271
|
+
"x": "<length> | <percentage>",
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
// Stryker restore all
|
package/src/gfm.mjs
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* This is `remark-gfm@4.0.1`'s own plugin body and
|
|
5
5
|
* `micromark-extension-gfm@3.0.0`'s own `gfm()` composition, inlined so that
|
|
6
6
|
* `gfmAutolinkLiteral` can come from `./vendor/gfm-autolink-literal.mjs`
|
|
7
|
-
* instead of the published package
|
|
8
|
-
*
|
|
7
|
+
* instead of the published package. That module is generated, not committed:
|
|
8
|
+
* `scripts/gen-gfm-autolink.mjs` patches the pinned upstream at install time,
|
|
9
|
+
* and its header says what differs and when to delete both. Every other extension, and the mdast layer, are the upstream
|
|
9
10
|
* ones at the versions `remark-gfm` pins.
|
|
10
11
|
*
|
|
11
12
|
* Assembling the list here rather than adding an extension beside `remark-gfm`
|
|
@@ -139,5 +139,154 @@ export function parseHtmlFragment(html) {
|
|
|
139
139
|
treeAdapter: adapter,
|
|
140
140
|
});
|
|
141
141
|
settle();
|
|
142
|
-
return
|
|
142
|
+
return fromParse5Bounded(fragment, new VFile(html));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* How many levels of the tree one `fromParse5` call converts.
|
|
147
|
+
*
|
|
148
|
+
* WHY IT EXISTS — `hast-util-from-parse5` converts by recursion, three frames
|
|
149
|
+
* (`one` → `all` → `element`) per level, so how deep a tree it survives is
|
|
150
|
+
* whatever stack is left, and that depends on the JIT tier: a cold call's
|
|
151
|
+
* interpreted frames are bigger than an optimized one's. ~2500 nested elements
|
|
152
|
+
* overflowed on the first call in a process and converted fine on a later one,
|
|
153
|
+
* so the same document came back withheld or sanitized depending on what had
|
|
154
|
+
* run before it. 128 levels still convert cold on a 100 KB stack, a tenth of
|
|
155
|
+
* Node's default.
|
|
156
|
+
*/
|
|
157
|
+
export const CONVERT_DEPTH = 128;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The deepest a node may sit below the fragment root; a deeper parse throws, so
|
|
161
|
+
* the caller fails closed on it.
|
|
162
|
+
*
|
|
163
|
+
* Conversion no longer limits depth (see {@link CONVERT_DEPTH}), so this is the
|
|
164
|
+
* one place that does, and it answers the same way at every JIT tier. No real
|
|
165
|
+
* document comes near it — Blink's parser flattens anything nested past 512 —
|
|
166
|
+
* and what lies past it is built to cost: parse5's scope checks walk the
|
|
167
|
+
* open-element stack per tag, so parsing is quadratic in depth.
|
|
168
|
+
*/
|
|
169
|
+
export const MAX_HTML_DEPTH = 4096;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Which elements `hast-util-from-parse5` converts as a template: hastscript reads
|
|
173
|
+
* the tag name as a selector (`template.x` is a `template` with class `x`), so
|
|
174
|
+
* the name up to its first `#` or `.`, lowercased.
|
|
175
|
+
*
|
|
176
|
+
* WHY IT MATTERS — the converter then recurses into the parse5 node's `content`,
|
|
177
|
+
* which only an HTML-namespace `<template>` has. A `template` inside SVG or
|
|
178
|
+
* MathML, or an HTML `template.x` tag, has none, the converter throws,
|
|
179
|
+
* and the whole document would be withheld over a benign element.
|
|
180
|
+
*/
|
|
181
|
+
const CONVERTS_AS_TEMPLATE = /^template(?:[#.]|$)/i;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Prefixed to such an element's name for the conversion, and stripped after.
|
|
185
|
+
*
|
|
186
|
+
* A space, because no parsed element's name can hold one (the tokenizer ends a
|
|
187
|
+
* tag name at whitespace), so a masked name collides with nothing, and the
|
|
188
|
+
* converter neither drops it (its selector parse splits on `#` and `.` only) nor
|
|
189
|
+
* changes it (lowercasing, the SVG case map). The element then converts the way
|
|
190
|
+
* any element without template content does: its children as `children`, where
|
|
191
|
+
* every scan of the hast tree reads them, which is also where the DOM has them.
|
|
192
|
+
*/
|
|
193
|
+
const TEMPLATE_MASK = " ";
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* `fromParse5(tree, { file })`, recursing at most `chunkDepth` levels at a time.
|
|
197
|
+
*
|
|
198
|
+
* Every node a multiple of `chunkDepth` levels down is converted on its own,
|
|
199
|
+
* deepest first, and then stands in its parent's `childNodes` as a placeholder
|
|
200
|
+
* text node carrying the converted subtree's position — the one thing about a
|
|
201
|
+
* child its parent's conversion reads (an unclosed element ends where its last
|
|
202
|
+
* child does). One walk then swaps each placeholder for its subtree. The result
|
|
203
|
+
* is the tree a single `fromParse5` call returns, which
|
|
204
|
+
* `html-tree-adapter.test` holds it to — except that an element the converter
|
|
205
|
+
* would take for a template with no content to take (see
|
|
206
|
+
* {@link CONVERTS_AS_TEMPLATE}) converts as an ordinary element instead of
|
|
207
|
+
* throwing.
|
|
208
|
+
*
|
|
209
|
+
* Mutates `tree`: {@link parseHtmlFragment} owns the parse and drops it after.
|
|
210
|
+
* @param {any} tree
|
|
211
|
+
* @param {VFile} file
|
|
212
|
+
* @param {number} [chunkDepth]
|
|
213
|
+
* @returns {any}
|
|
214
|
+
*/
|
|
215
|
+
export function fromParse5Bounded(tree, file, chunkDepth = CONVERT_DEPTH) {
|
|
216
|
+
/** @type {Array<{ siblings: any[], index: number }>} */
|
|
217
|
+
const cuts = [];
|
|
218
|
+
const stack = [{ node: tree, depth: 0 }];
|
|
219
|
+
let masked = false;
|
|
220
|
+
while (stack.length > 0) {
|
|
221
|
+
const { node, depth } = /** @type {{ node: any, depth: number }} */ (
|
|
222
|
+
stack.pop()
|
|
223
|
+
);
|
|
224
|
+
if (depth > MAX_HTML_DEPTH)
|
|
225
|
+
throw new RangeError(`HTML nested deeper than ${MAX_HTML_DEPTH} levels`);
|
|
226
|
+
// A template's content is a root its template's conversion recurses into.
|
|
227
|
+
if (node.content) stack.push({ node: node.content, depth: depth + 1 });
|
|
228
|
+
else if (
|
|
229
|
+
node.tagName !== undefined &&
|
|
230
|
+
CONVERTS_AS_TEMPLATE.test(node.tagName)
|
|
231
|
+
) {
|
|
232
|
+
node.tagName = TEMPLATE_MASK + node.tagName;
|
|
233
|
+
masked = true;
|
|
234
|
+
}
|
|
235
|
+
const siblings = node.childNodes ?? [];
|
|
236
|
+
const childDepth = depth + 1;
|
|
237
|
+
for (let index = 0; index < siblings.length; index++) {
|
|
238
|
+
if (childDepth % chunkDepth === 0) cuts.push({ siblings, index });
|
|
239
|
+
stack.push({ node: siblings[index], depth: childDepth });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
// The walk reaches a cut before any cut below it, so going backwards converts
|
|
243
|
+
// a subtree only once everything cut out of it already stands as a
|
|
244
|
+
// placeholder.
|
|
245
|
+
const grafts = new Map();
|
|
246
|
+
for (let i = cuts.length - 1; i >= 0; i--) {
|
|
247
|
+
const { siblings, index } = cuts[i];
|
|
248
|
+
const converted = fromParse5(siblings[index], { file });
|
|
249
|
+
const token = {};
|
|
250
|
+
grafts.set(token, converted);
|
|
251
|
+
siblings[index] = {
|
|
252
|
+
nodeName: "#text",
|
|
253
|
+
value: token,
|
|
254
|
+
sourceCodeLocation: parse5Location(converted.position),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
const root = fromParse5(tree, { file });
|
|
258
|
+
/** @type {any[]} */
|
|
259
|
+
const pending = [root];
|
|
260
|
+
while ((grafts.size > 0 || masked) && pending.length > 0) {
|
|
261
|
+
const node = pending.pop();
|
|
262
|
+
if (masked && node.tagName?.startsWith(TEMPLATE_MASK))
|
|
263
|
+
node.tagName = node.tagName.slice(TEMPLATE_MASK.length);
|
|
264
|
+
if (node.content) pending.push(node.content);
|
|
265
|
+
const children = node.children ?? [];
|
|
266
|
+
for (let i = 0; i < children.length; i++) {
|
|
267
|
+
children[i] = grafts.get(children[i].value) ?? children[i];
|
|
268
|
+
pending.push(children[i]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return root;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The parse5 location `hast-util-from-parse5` turns back into `position`.
|
|
276
|
+
*
|
|
277
|
+
* A position it built always has both points: it drops one only when parse5
|
|
278
|
+
* gave no line, and parse5 gives a location whole or not at all.
|
|
279
|
+
* @param {any} position
|
|
280
|
+
*/
|
|
281
|
+
function parse5Location(position) {
|
|
282
|
+
if (!position) return undefined;
|
|
283
|
+
const { start, end } = position;
|
|
284
|
+
return {
|
|
285
|
+
startLine: start.line,
|
|
286
|
+
startCol: start.column,
|
|
287
|
+
startOffset: start.offset,
|
|
288
|
+
endLine: end.line,
|
|
289
|
+
endCol: end.column,
|
|
290
|
+
endOffset: end.offset,
|
|
291
|
+
};
|
|
143
292
|
}
|