billboard.js 4.0.2 → 4.1.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -7
- package/dist/billboard.css +1 -1
- package/dist/billboard.js +1938 -557
- package/dist/billboard.min.css +1 -1
- package/dist/billboard.min.js +19 -25
- package/dist/billboard.pkgd.js +1934 -742
- package/dist/billboard.pkgd.min.js +24 -30
- package/dist/billboard.react.js +205 -0
- package/dist/billboard.react.min.js +9 -0
- package/dist/billboard.worker.js +10 -0
- package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
- package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-sparkline.js +3 -2
- package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
- package/dist/plugin/billboardjs-plugin-stanford.js +3 -2
- package/dist/plugin/billboardjs-plugin-stanford.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-tableview.js +57 -27
- package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
- package/dist/plugin/billboardjs-plugin-textoverlap.js +72 -54
- package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +3 -2
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +3 -2
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +57 -27
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +65 -3556
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +8 -8
- package/dist/theme/dark.css +1 -1
- package/dist/theme/dark.min.css +1 -1
- package/dist/theme/datalab.css +1 -1
- package/dist/theme/datalab.min.css +1 -1
- package/dist/theme/graph.css +1 -1
- package/dist/theme/graph.min.css +1 -1
- package/dist/theme/insight.css +1 -1
- package/dist/theme/insight.min.css +1 -1
- package/dist/theme/modern.css +1 -1
- package/dist/theme/modern.min.css +1 -1
- package/dist-esm/Chart/Chart.js +134 -152
- package/dist-esm/Chart/api/axis.js +217 -237
- package/dist-esm/Chart/api/category.js +54 -54
- package/dist-esm/Chart/api/chart.js +172 -215
- package/dist-esm/Chart/api/color.js +21 -21
- package/dist-esm/Chart/api/data.js +172 -173
- package/dist-esm/Chart/api/export.js +242 -262
- package/dist-esm/Chart/api/flow.js +198 -254
- package/dist-esm/Chart/api/focus.js +136 -147
- package/dist-esm/Chart/api/grid.js +191 -190
- package/dist-esm/Chart/api/group.js +31 -34
- package/dist-esm/Chart/api/legend.js +54 -63
- package/dist-esm/Chart/api/load.js +237 -257
- package/dist-esm/Chart/api/regions.js +148 -168
- package/dist-esm/Chart/api/selection.js +181 -139
- package/dist-esm/Chart/api/show.js +133 -147
- package/dist-esm/Chart/api/stubs.js +43 -42
- package/dist-esm/Chart/api/subchart.js +155 -172
- package/dist-esm/Chart/api/tooltip.js +132 -177
- package/dist-esm/Chart/api/x.js +67 -71
- package/dist-esm/Chart/api/zoom.js +183 -220
- package/dist-esm/ChartInternal/Axis/Axis.js +1059 -1266
- package/dist-esm/ChartInternal/Axis/AxisRenderer.js +408 -540
- package/dist-esm/ChartInternal/Axis/AxisRendererHelper.js +113 -149
- package/dist-esm/ChartInternal/ChartInternal.js +573 -749
- package/dist-esm/ChartInternal/data/convert.helper.js +133 -159
- package/dist-esm/ChartInternal/data/convert.js +200 -229
- package/dist-esm/ChartInternal/data/data.js +970 -1147
- package/dist-esm/ChartInternal/data/load.js +145 -197
- package/dist-esm/ChartInternal/interactions/drag.js +86 -122
- package/dist-esm/ChartInternal/interactions/eventrect.js +455 -621
- package/dist-esm/ChartInternal/interactions/flow.js +151 -215
- package/dist-esm/ChartInternal/interactions/interaction.js +214 -252
- package/dist-esm/ChartInternal/interactions/subchart.js +425 -300
- package/dist-esm/ChartInternal/interactions/zoom.js +321 -440
- package/dist-esm/ChartInternal/internals/category.js +18 -18
- package/dist-esm/ChartInternal/internals/class.js +89 -99
- package/dist-esm/ChartInternal/internals/clip.js +88 -119
- package/dist-esm/ChartInternal/internals/color.js +233 -288
- package/dist-esm/ChartInternal/internals/domain.js +452 -588
- package/dist-esm/ChartInternal/internals/format.js +64 -77
- package/dist-esm/ChartInternal/internals/grid.js +333 -457
- package/dist-esm/ChartInternal/internals/legend.js +675 -927
- package/dist-esm/ChartInternal/internals/redraw.js +200 -289
- package/dist-esm/ChartInternal/internals/region.js +106 -159
- package/dist-esm/ChartInternal/internals/scale.js +186 -215
- package/dist-esm/ChartInternal/internals/selection.js +132 -159
- package/dist-esm/ChartInternal/internals/size.axis.js +92 -139
- package/dist-esm/ChartInternal/internals/size.js +286 -394
- package/dist-esm/ChartInternal/internals/style.js +39 -41
- package/dist-esm/ChartInternal/internals/subchart.util.js +49 -0
- package/dist-esm/ChartInternal/internals/text.arc.js +155 -219
- package/dist-esm/ChartInternal/internals/text.js +368 -528
- package/dist-esm/ChartInternal/internals/text.util.js +179 -239
- package/dist-esm/ChartInternal/internals/title.js +96 -121
- package/dist-esm/ChartInternal/internals/tooltip.js +450 -586
- package/dist-esm/ChartInternal/internals/transform.js +79 -97
- package/dist-esm/ChartInternal/internals/type.js +286 -245
- package/dist-esm/ChartInternal/shape/arc.js +825 -1112
- package/dist-esm/ChartInternal/shape/area.js +73 -94
- package/dist-esm/ChartInternal/shape/bar.js +227 -272
- package/dist-esm/ChartInternal/shape/bubble.js +9 -8
- package/dist-esm/ChartInternal/shape/candlestick.js +110 -138
- package/dist-esm/ChartInternal/shape/core/barRadius.js +197 -133
- package/dist-esm/ChartInternal/shape/core/bubble.js +64 -73
- package/dist-esm/ChartInternal/shape/core/candlestick.js +93 -86
- package/dist-esm/ChartInternal/shape/core/dataRegion.js +68 -75
- package/dist-esm/ChartInternal/shape/core/geometry.js +112 -112
- package/dist-esm/ChartInternal/shape/core/path.js +142 -115
- package/dist-esm/ChartInternal/shape/core/point.js +105 -125
- package/dist-esm/ChartInternal/shape/core/treemap.js +83 -88
- package/dist-esm/ChartInternal/shape/funnel.js +310 -338
- package/dist-esm/ChartInternal/shape/gauge.js +71 -103
- package/dist-esm/ChartInternal/shape/line.js +208 -278
- package/dist-esm/ChartInternal/shape/point.js +310 -413
- package/dist-esm/ChartInternal/shape/polar.js +84 -109
- package/dist-esm/ChartInternal/shape/radar.js +228 -318
- package/dist-esm/ChartInternal/shape/shape.js +782 -828
- package/dist-esm/ChartInternal/shape/treemap.js +152 -190
- package/dist-esm/canvas/CanvasAxisRenderer.js +1180 -1328
- package/dist-esm/canvas/CanvasEngine.js +148 -166
- package/dist-esm/canvas/CanvasPainter.js +395 -411
- package/dist-esm/canvas/CanvasRenderer.js +1584 -1834
- package/dist-esm/canvas/CanvasTheme.js +899 -802
- package/dist-esm/canvas/HitDetector.js +394 -463
- package/dist-esm/canvas/axisTicks.js +672 -786
- package/dist-esm/canvas/classes.js +19 -17
- package/dist-esm/canvas/color.js +40 -59
- package/dist-esm/canvas/geometry.js +38 -38
- package/dist-esm/canvas/labels.js +318 -371
- package/dist-esm/canvas/pointPattern.js +578 -580
- package/dist-esm/canvas/util.js +242 -262
- package/dist-esm/config/Options/Options.js +29 -32
- package/dist-esm/config/Options/axis/axis.js +101 -101
- package/dist-esm/config/Options/axis/x.js +759 -755
- package/dist-esm/config/Options/axis/y.js +525 -522
- package/dist-esm/config/Options/axis/y2.js +501 -485
- package/dist-esm/config/Options/common/boost.js +93 -32
- package/dist-esm/config/Options/common/color.js +88 -87
- package/dist-esm/config/Options/common/grid.js +74 -73
- package/dist-esm/config/Options/common/legend.js +180 -179
- package/dist-esm/config/Options/common/main.js +474 -473
- package/dist-esm/config/Options/common/point.js +160 -159
- package/dist-esm/config/Options/common/title.js +47 -46
- package/dist-esm/config/Options/common/tooltip.js +253 -252
- package/dist-esm/config/Options/data/axis.js +173 -172
- package/dist-esm/config/Options/data/data.js +958 -957
- package/dist-esm/config/Options/data/selection.js +131 -130
- package/dist-esm/config/Options/interaction/interaction.js +53 -52
- package/dist-esm/config/Options/interaction/subchart.js +164 -73
- package/dist-esm/config/Options/interaction/zoom.js +86 -85
- package/dist-esm/config/Options/shape/arc.js +150 -149
- package/dist-esm/config/Options/shape/area.js +69 -68
- package/dist-esm/config/Options/shape/bar.js +147 -146
- package/dist-esm/config/Options/shape/bubble.js +35 -34
- package/dist-esm/config/Options/shape/candlestick.js +61 -60
- package/dist-esm/config/Options/shape/donut.js +181 -180
- package/dist-esm/config/Options/shape/funnel.js +49 -48
- package/dist-esm/config/Options/shape/gauge.js +237 -236
- package/dist-esm/config/Options/shape/line.js +58 -57
- package/dist-esm/config/Options/shape/pie.js +185 -184
- package/dist-esm/config/Options/shape/polar.js +167 -166
- package/dist-esm/config/Options/shape/radar.js +76 -75
- package/dist-esm/config/Options/shape/scatter.js +36 -36
- package/dist-esm/config/Options/shape/spline.js +46 -46
- package/dist-esm/config/Options/shape/treemap.js +80 -79
- package/dist-esm/config/Store/Element.js +64 -62
- package/dist-esm/config/Store/State.js +189 -200
- package/dist-esm/config/Store/Store.js +25 -26
- package/dist-esm/config/classes.js +157 -155
- package/dist-esm/config/config.js +26 -35
- package/dist-esm/config/const.js +120 -128
- package/dist-esm/config/resolver/axis.core.js +25 -25
- package/dist-esm/config/resolver/axis.js +26 -27
- package/dist-esm/config/resolver/canvas.js +2071 -2472
- package/dist-esm/config/resolver/canvasShape.js +201 -177
- package/dist-esm/config/resolver/category.js +26 -27
- package/dist-esm/config/resolver/export.js +24 -26
- package/dist-esm/config/resolver/flow.js +29 -31
- package/dist-esm/config/resolver/grid.js +34 -35
- package/dist-esm/config/resolver/interaction/selection.js +17 -17
- package/dist-esm/config/resolver/interaction/subchart.js +17 -17
- package/dist-esm/config/resolver/interaction/zoom.js +17 -17
- package/dist-esm/config/resolver/regions.js +30 -31
- package/dist-esm/config/resolver/shape/arc.helpers.js +19 -19
- package/dist-esm/config/resolver/shape/area.js +17 -17
- package/dist-esm/config/resolver/shape/axis.helpers.js +28 -25
- package/dist-esm/config/resolver/shape/bar.js +13 -13
- package/dist-esm/config/resolver/shape/bubble.js +18 -14
- package/dist-esm/config/resolver/shape/candlestick.js +13 -13
- package/dist-esm/config/resolver/shape/donut.js +11 -11
- package/dist-esm/config/resolver/shape/funnel.js +11 -11
- package/dist-esm/config/resolver/shape/gauge.js +12 -12
- package/dist-esm/config/resolver/shape/line.js +10 -10
- package/dist-esm/config/resolver/shape/pie.js +11 -11
- package/dist-esm/config/resolver/shape/polar.js +12 -12
- package/dist-esm/config/resolver/shape/radar.js +22 -14
- package/dist-esm/config/resolver/shape/scatter.js +13 -13
- package/dist-esm/config/resolver/shape/treemap.js +11 -11
- package/dist-esm/core.js +208 -211
- package/dist-esm/index.canvas.js +14 -13
- package/dist-esm/index.esm.js +25 -24
- package/dist-esm/module/Cache.js +117 -129
- package/dist-esm/module/browser.js +30 -35
- package/dist-esm/module/dsv.js +107 -127
- package/dist-esm/module/error.js +45 -52
- package/dist-esm/module/generator.js +75 -83
- package/dist-esm/module/sanitize.js +302 -399
- package/dist-esm/module/util/brush.js +27 -38
- package/dist-esm/module/util/dom.js +288 -348
- package/dist-esm/module/util/object.js +277 -341
- package/dist-esm/module/util/type-checks.js +21 -25
- package/dist-esm/module/worker.js +312 -130
- package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +185 -198
- package/dist-esm/plugin/billboardjs-plugin-sparkline.js +442 -450
- package/dist-esm/plugin/billboardjs-plugin-stanford.js +813 -950
- package/dist-esm/plugin/billboardjs-plugin-tableview.js +708 -788
- package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +344 -301
- package/dist-esm/react/index.js +157 -0
- package/package.json +28 -8
- package/src/Chart/api/focus.ts +8 -12
- package/src/Chart/api/selection.ts +130 -5
- package/src/ChartInternal/Axis/Axis.ts +164 -65
- package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
- package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
- package/src/ChartInternal/ChartInternal.ts +1 -1
- package/src/ChartInternal/data/convert.ts +54 -4
- package/src/ChartInternal/data/data.ts +19 -6
- package/src/ChartInternal/interactions/eventrect.ts +6 -0
- package/src/ChartInternal/interactions/subchart.ts +437 -66
- package/src/ChartInternal/internals/color.ts +7 -6
- package/src/ChartInternal/internals/domain.ts +15 -3
- package/src/ChartInternal/internals/grid.ts +21 -1
- package/src/ChartInternal/internals/scale.ts +9 -2
- package/src/ChartInternal/internals/selection.ts +1 -1
- package/src/ChartInternal/internals/subchart.util.ts +55 -0
- package/src/ChartInternal/internals/text.arc.ts +3 -1
- package/src/ChartInternal/internals/text.ts +19 -6
- package/src/ChartInternal/internals/transform.ts +8 -2
- package/src/ChartInternal/internals/type.ts +131 -33
- package/src/ChartInternal/shape/IShape.ts +6 -3
- package/src/ChartInternal/shape/bar.ts +87 -37
- package/src/ChartInternal/shape/core/barRadius.ts +129 -0
- package/src/ChartInternal/shape/core/candlestick.ts +1 -1
- package/src/ChartInternal/shape/core/path.ts +76 -9
- package/src/ChartInternal/shape/funnel.ts +2 -3
- package/src/ChartInternal/shape/line.ts +1 -1
- package/src/ChartInternal/shape/point.ts +27 -13
- package/src/ChartInternal/shape/shape.ts +172 -18
- package/src/Plugin/stanford/index.ts +0 -4
- package/src/Plugin/tableview/index.ts +85 -3
- package/src/Plugin/textoverlap/index.ts +9 -15
- package/src/canvas/CanvasAxisRenderer.ts +184 -36
- package/src/canvas/CanvasRenderer.ts +247 -160
- package/src/canvas/CanvasTheme.ts +212 -0
- package/src/canvas/axisTicks.ts +122 -40
- package/src/config/Options/axis/y2.ts +13 -0
- package/src/config/Options/common/boost.ts +64 -4
- package/src/config/Options/common/color.ts +2 -2
- package/src/config/Options/interaction/subchart.ts +98 -1
- package/src/config/Options/shape/gauge.ts +1 -1
- package/src/config/Options/shape/treemap.ts +2 -2
- package/src/config/Store/Element.ts +3 -1
- package/src/config/Store/State.ts +1 -0
- package/src/config/classes.ts +1 -0
- package/src/config/resolver/canvas.ts +92 -10
- package/src/module/voronoi.ts +119 -0
- package/src/module/worker.entry.ts +61 -0
- package/src/module/worker.ts +346 -63
- package/src/react/index.ts +193 -0
- package/types/options.d.ts +206 -3
- package/types/react/index.d.ts +27 -0
|
@@ -5,446 +5,349 @@
|
|
|
5
5
|
* billboard.js, JavaScript chart library
|
|
6
6
|
* https://naver.github.io/billboard.js/
|
|
7
7
|
*
|
|
8
|
-
* @version 4.0.
|
|
8
|
+
* @version 4.1.0-next.1
|
|
9
9
|
*/
|
|
10
|
+
//#region src/module/sanitize.ts
|
|
10
11
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
13
|
+
* billboard.js project is licensed under the MIT license
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
15
16
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
17
|
+
* HTML/SVG Sanitization module
|
|
18
|
+
* Pure whitelist approach - only explicitly allowed tags, attributes, and protocols pass through
|
|
19
|
+
*/
|
|
20
|
+
const ALLOWED_TAGS = /* @__PURE__ */ new Set([
|
|
21
|
+
"span",
|
|
22
|
+
"div",
|
|
23
|
+
"p",
|
|
24
|
+
"br",
|
|
25
|
+
"b",
|
|
26
|
+
"i",
|
|
27
|
+
"em",
|
|
28
|
+
"small",
|
|
29
|
+
"strong",
|
|
30
|
+
"mark",
|
|
31
|
+
"u",
|
|
32
|
+
"s",
|
|
33
|
+
"sub",
|
|
34
|
+
"sup",
|
|
35
|
+
"h1",
|
|
36
|
+
"h2",
|
|
37
|
+
"h3",
|
|
38
|
+
"h4",
|
|
39
|
+
"h5",
|
|
40
|
+
"h6",
|
|
41
|
+
"ul",
|
|
42
|
+
"ol",
|
|
43
|
+
"li",
|
|
44
|
+
"dl",
|
|
45
|
+
"dt",
|
|
46
|
+
"dd",
|
|
47
|
+
"table",
|
|
48
|
+
"thead",
|
|
49
|
+
"tbody",
|
|
50
|
+
"tfoot",
|
|
51
|
+
"tr",
|
|
52
|
+
"th",
|
|
53
|
+
"td",
|
|
54
|
+
"caption",
|
|
55
|
+
"colgroup",
|
|
56
|
+
"col",
|
|
57
|
+
"hr",
|
|
58
|
+
"pre",
|
|
59
|
+
"code",
|
|
60
|
+
"blockquote",
|
|
61
|
+
"abbr",
|
|
62
|
+
"ins",
|
|
63
|
+
"del",
|
|
64
|
+
"a",
|
|
65
|
+
"img",
|
|
66
|
+
"figure",
|
|
67
|
+
"figcaption",
|
|
68
|
+
"svg",
|
|
69
|
+
"g",
|
|
70
|
+
"path",
|
|
71
|
+
"circle",
|
|
72
|
+
"ellipse",
|
|
73
|
+
"rect",
|
|
74
|
+
"line",
|
|
75
|
+
"polyline",
|
|
76
|
+
"polygon",
|
|
77
|
+
"text",
|
|
78
|
+
"tspan",
|
|
79
|
+
"textPath",
|
|
80
|
+
"use",
|
|
81
|
+
"defs",
|
|
82
|
+
"symbol",
|
|
83
|
+
"clipPath",
|
|
84
|
+
"mask",
|
|
85
|
+
"linearGradient",
|
|
86
|
+
"radialGradient",
|
|
87
|
+
"stop",
|
|
88
|
+
"pattern",
|
|
89
|
+
"marker",
|
|
90
|
+
"title",
|
|
91
|
+
"desc"
|
|
92
|
+
]);
|
|
93
|
+
const ALLOWED_ATTRS = /* @__PURE__ */ new Set([
|
|
94
|
+
"class",
|
|
95
|
+
"id",
|
|
96
|
+
"style",
|
|
97
|
+
"title",
|
|
98
|
+
"lang",
|
|
99
|
+
"dir",
|
|
100
|
+
"href",
|
|
101
|
+
"src",
|
|
102
|
+
"alt",
|
|
103
|
+
"width",
|
|
104
|
+
"height",
|
|
105
|
+
"colspan",
|
|
106
|
+
"rowspan",
|
|
107
|
+
"scope",
|
|
108
|
+
"headers",
|
|
109
|
+
"d",
|
|
110
|
+
"points",
|
|
111
|
+
"x",
|
|
112
|
+
"y",
|
|
113
|
+
"x1",
|
|
114
|
+
"x2",
|
|
115
|
+
"y1",
|
|
116
|
+
"y2",
|
|
117
|
+
"cx",
|
|
118
|
+
"cy",
|
|
119
|
+
"r",
|
|
120
|
+
"rx",
|
|
121
|
+
"ry",
|
|
122
|
+
"dx",
|
|
123
|
+
"dy",
|
|
124
|
+
"viewBox",
|
|
125
|
+
"preserveAspectRatio",
|
|
126
|
+
"transform",
|
|
127
|
+
"fill",
|
|
128
|
+
"fill-opacity",
|
|
129
|
+
"fill-rule",
|
|
130
|
+
"stroke",
|
|
131
|
+
"stroke-width",
|
|
132
|
+
"stroke-opacity",
|
|
133
|
+
"stroke-linecap",
|
|
134
|
+
"stroke-linejoin",
|
|
135
|
+
"stroke-dasharray",
|
|
136
|
+
"stroke-dashoffset",
|
|
137
|
+
"opacity",
|
|
138
|
+
"clip-path",
|
|
139
|
+
"clip-rule",
|
|
140
|
+
"mask",
|
|
141
|
+
"font-family",
|
|
142
|
+
"font-size",
|
|
143
|
+
"font-weight",
|
|
144
|
+
"font-style",
|
|
145
|
+
"text-anchor",
|
|
146
|
+
"dominant-baseline",
|
|
147
|
+
"offset",
|
|
148
|
+
"stop-color",
|
|
149
|
+
"stop-opacity",
|
|
150
|
+
"gradientUnits",
|
|
151
|
+
"gradientTransform",
|
|
152
|
+
"spreadMethod",
|
|
153
|
+
"patternUnits",
|
|
154
|
+
"patternTransform",
|
|
155
|
+
"marker-start",
|
|
156
|
+
"marker-mid",
|
|
157
|
+
"marker-end",
|
|
158
|
+
"markerWidth",
|
|
159
|
+
"markerHeight",
|
|
160
|
+
"refX",
|
|
161
|
+
"refY",
|
|
162
|
+
"xlink:href"
|
|
94
163
|
]);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
"dir",
|
|
104
|
-
// HTML specific
|
|
105
|
-
"href",
|
|
106
|
-
"src",
|
|
107
|
-
"alt",
|
|
108
|
-
"width",
|
|
109
|
-
"height",
|
|
110
|
-
"colspan",
|
|
111
|
-
"rowspan",
|
|
112
|
-
"scope",
|
|
113
|
-
"headers",
|
|
114
|
-
// SVG presentation attributes
|
|
115
|
-
"d",
|
|
116
|
-
"points",
|
|
117
|
-
"x",
|
|
118
|
-
"y",
|
|
119
|
-
"x1",
|
|
120
|
-
"x2",
|
|
121
|
-
"y1",
|
|
122
|
-
"y2",
|
|
123
|
-
"cx",
|
|
124
|
-
"cy",
|
|
125
|
-
"r",
|
|
126
|
-
"rx",
|
|
127
|
-
"ry",
|
|
128
|
-
"dx",
|
|
129
|
-
"dy",
|
|
130
|
-
"viewBox",
|
|
131
|
-
"preserveAspectRatio",
|
|
132
|
-
"transform",
|
|
133
|
-
"fill",
|
|
134
|
-
"fill-opacity",
|
|
135
|
-
"fill-rule",
|
|
136
|
-
"stroke",
|
|
137
|
-
"stroke-width",
|
|
138
|
-
"stroke-opacity",
|
|
139
|
-
"stroke-linecap",
|
|
140
|
-
"stroke-linejoin",
|
|
141
|
-
"stroke-dasharray",
|
|
142
|
-
"stroke-dashoffset",
|
|
143
|
-
"opacity",
|
|
144
|
-
"clip-path",
|
|
145
|
-
"clip-rule",
|
|
146
|
-
"mask",
|
|
147
|
-
"font-family",
|
|
148
|
-
"font-size",
|
|
149
|
-
"font-weight",
|
|
150
|
-
"font-style",
|
|
151
|
-
"text-anchor",
|
|
152
|
-
"dominant-baseline",
|
|
153
|
-
"offset",
|
|
154
|
-
"stop-color",
|
|
155
|
-
"stop-opacity",
|
|
156
|
-
"gradientUnits",
|
|
157
|
-
"gradientTransform",
|
|
158
|
-
"spreadMethod",
|
|
159
|
-
"patternUnits",
|
|
160
|
-
"patternTransform",
|
|
161
|
-
"marker-start",
|
|
162
|
-
"marker-mid",
|
|
163
|
-
"marker-end",
|
|
164
|
-
"markerWidth",
|
|
165
|
-
"markerHeight",
|
|
166
|
-
"refX",
|
|
167
|
-
"refY",
|
|
168
|
-
"xlink:href"
|
|
164
|
+
const TAG_CASE_MAP = /* @__PURE__ */ new Map();
|
|
165
|
+
ALLOWED_TAGS.forEach((tag) => TAG_CASE_MAP.set(tag.toLowerCase(), tag));
|
|
166
|
+
const ATTR_CASE_MAP = /* @__PURE__ */ new Map();
|
|
167
|
+
ALLOWED_ATTRS.forEach((attr) => ATTR_CASE_MAP.set(attr.toLowerCase(), attr));
|
|
168
|
+
const ALLOWED_URI_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
169
|
+
"http:",
|
|
170
|
+
"https:",
|
|
171
|
+
"mailto:"
|
|
169
172
|
]);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
ALLOWED_ATTRS.forEach(attr => ATTR_CASE_MAP.set(attr.toLowerCase(), attr));
|
|
175
|
-
// Whitelist of allowed URI protocols
|
|
176
|
-
const ALLOWED_URI_PROTOCOLS = new Set([
|
|
177
|
-
"http:",
|
|
178
|
-
"https:",
|
|
179
|
-
"mailto:"
|
|
173
|
+
const URI_ATTRS = /* @__PURE__ */ new Set([
|
|
174
|
+
"href",
|
|
175
|
+
"src",
|
|
176
|
+
"xlink:href"
|
|
180
177
|
]);
|
|
181
|
-
// Attributes that contain URIs
|
|
182
|
-
const URI_ATTRS = new Set(["href", "src", "xlink:href"]);
|
|
183
|
-
// Pre-compiled regex patterns for performance
|
|
184
178
|
const TAG_NAME_REGEX = /^<\/?([a-zA-Z][a-zA-Z0-9]*)/;
|
|
185
179
|
const CLOSING_TAG_REGEX = /^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/;
|
|
186
180
|
const OPENING_TAG_REGEX = /^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/;
|
|
187
181
|
const ATTR_REGEX = /([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
188
182
|
const URL_IN_STYLE_REGEX = /url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi;
|
|
189
|
-
// Dangerous CSS patterns
|
|
190
183
|
const DANGEROUS_CSS_PATTERNS = [
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
184
|
+
"expression(",
|
|
185
|
+
"behavior:",
|
|
186
|
+
"binding:",
|
|
187
|
+
"@import",
|
|
188
|
+
"@charset",
|
|
189
|
+
"-moz-binding:"
|
|
197
190
|
];
|
|
198
191
|
/**
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
192
|
+
* Decode HTML entities in a string
|
|
193
|
+
* @param {string} str String with potential HTML entities
|
|
194
|
+
* @returns {string} Decoded string
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
204
197
|
function decodeHTMLEntities(str) {
|
|
205
|
-
|
|
206
|
-
// Named entities
|
|
207
|
-
.replace(/:/gi, ":")
|
|
208
|
-
.replace(/&newline;/gi, "\n")
|
|
209
|
-
.replace(/&tab;/gi, "\t")
|
|
210
|
-
.replace(/ /gi, " ")
|
|
211
|
-
.replace(/</gi, "<")
|
|
212
|
-
.replace(/>/gi, ">")
|
|
213
|
-
.replace(/&/gi, "&")
|
|
214
|
-
.replace(/"/gi, "\"")
|
|
215
|
-
.replace(/'/gi, "'")
|
|
216
|
-
// Numeric entities (decimal) - trailing semicolon is optional per HTML5 tokenizer
|
|
217
|
-
.replace(/&#(\d+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 10)))
|
|
218
|
-
// Numeric entities (hex) - trailing semicolon is optional per HTML5 tokenizer
|
|
219
|
-
.replace(/&#x([0-9a-f]+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 16)));
|
|
198
|
+
return str.replace(/:/gi, ":").replace(/&newline;/gi, "\n").replace(/&tab;/gi, " ").replace(/ /gi, " ").replace(/</gi, "<").replace(/>/gi, ">").replace(/&/gi, "&").replace(/"/gi, "\"").replace(/'/gi, "'").replace(/&#(\d+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 10))).replace(/&#x([0-9a-f]+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 16)));
|
|
220
199
|
}
|
|
221
200
|
/**
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
201
|
+
* Check if a URI is safe (whitelist approach)
|
|
202
|
+
* @param {string} uri URI to check
|
|
203
|
+
* @returns {boolean} Whether the URI is safe
|
|
204
|
+
* @private
|
|
205
|
+
*/
|
|
227
206
|
function isSafeURI(uri) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
// Empty or fragment-only URIs are safe
|
|
234
|
-
if (!normalized || normalized.startsWith("#")) {
|
|
235
|
-
return true;
|
|
236
|
-
}
|
|
237
|
-
// A ':' appearing before the first '/', '?' or '#' denotes a URI scheme.
|
|
238
|
-
// Reject the value unless that scheme is whitelisted - do NOT infer safety
|
|
239
|
-
// merely from the absence of a literal colon (which could be reconstructed
|
|
240
|
-
// from an alternate encoding once assigned to innerHTML).
|
|
241
|
-
const schemeMatch = normalized.match(/^[^/?#]*:/);
|
|
242
|
-
if (schemeMatch) {
|
|
243
|
-
return ALLOWED_URI_PROTOCOLS.has(schemeMatch[0]);
|
|
244
|
-
}
|
|
245
|
-
// No scheme → relative path / query / fragment only → safe
|
|
246
|
-
return true;
|
|
207
|
+
const normalized = decodeHTMLEntities(uri).trim().replace(/[\s\u0000-\u001f]/g, "").toLowerCase();
|
|
208
|
+
if (!normalized || normalized.startsWith("#")) return true;
|
|
209
|
+
const schemeMatch = normalized.match(/^[^/?#]*:/);
|
|
210
|
+
if (schemeMatch) return ALLOWED_URI_PROTOCOLS.has(schemeMatch[0]);
|
|
211
|
+
return true;
|
|
247
212
|
}
|
|
248
213
|
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
214
|
+
* Check if a style value is safe (whitelist approach)
|
|
215
|
+
* @param {string} style Style attribute value
|
|
216
|
+
* @returns {string|null} Sanitized style or null if unsafe
|
|
217
|
+
* @private
|
|
218
|
+
*/
|
|
254
219
|
function sanitizeStyleValue(style) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
let match;
|
|
263
|
-
while ((match = URL_IN_STYLE_REGEX.exec(cleaned)) !== null) {
|
|
264
|
-
if (!isSafeURI(match[1])) {
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
// Check for dangerous CSS patterns (expression, behavior, etc.)
|
|
269
|
-
const normalizedLower = cleaned.toLowerCase().replace(/\s/g, "");
|
|
270
|
-
for (const pattern of DANGEROUS_CSS_PATTERNS) {
|
|
271
|
-
if (normalizedLower.includes(pattern)) {
|
|
272
|
-
return null;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
return style;
|
|
220
|
+
const cleaned = decodeHTMLEntities(style).replace(/[\u0000-\u001f]/g, "");
|
|
221
|
+
URL_IN_STYLE_REGEX.lastIndex = 0;
|
|
222
|
+
let match;
|
|
223
|
+
while ((match = URL_IN_STYLE_REGEX.exec(cleaned)) !== null) if (!isSafeURI(match[1])) return null;
|
|
224
|
+
const normalizedLower = cleaned.toLowerCase().replace(/\s/g, "");
|
|
225
|
+
for (const pattern of DANGEROUS_CSS_PATTERNS) if (normalizedLower.includes(pattern)) return null;
|
|
226
|
+
return style;
|
|
276
227
|
}
|
|
277
|
-
// Lookup table for encoding dangerous characters in attribute values
|
|
278
228
|
const ATTR_ENCODE_MAP = {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
229
|
+
"\"": """,
|
|
230
|
+
"'": "'",
|
|
231
|
+
"`": "`"
|
|
282
232
|
};
|
|
283
233
|
const ATTR_ENCODE_REGEX = /["'`]/g;
|
|
284
234
|
/**
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
235
|
+
* Encode dangerous characters in attribute values to HTML entities
|
|
236
|
+
* This prevents attribute injection attacks where quotes/backticks break out of the attribute context
|
|
237
|
+
* @param {string} value Attribute value
|
|
238
|
+
* @returns {string} Encoded value
|
|
239
|
+
* @private
|
|
240
|
+
*/
|
|
291
241
|
function encodeAttrValue(value) {
|
|
292
|
-
|
|
242
|
+
return value.replace(ATTR_ENCODE_REGEX, (char) => ATTR_ENCODE_MAP[char]);
|
|
293
243
|
}
|
|
294
244
|
/**
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
245
|
+
* Sanitize attribute value using whitelist approach
|
|
246
|
+
* @param {string} name Attribute name
|
|
247
|
+
* @param {string} value Attribute value
|
|
248
|
+
* @param {boolean} wasUnquoted Whether the value was originally unquoted
|
|
249
|
+
* @returns {string|null} Sanitized value if safe, null if should be removed
|
|
250
|
+
* @private
|
|
251
|
+
*/
|
|
302
252
|
function sanitizeAttrValue(name, value, wasUnquoted = false) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
return null;
|
|
316
|
-
}
|
|
317
|
-
// Encode dangerous characters in style values
|
|
318
|
-
return wasUnquoted ? encodeAttrValue(sanitizedStyle) : sanitizedStyle;
|
|
319
|
-
}
|
|
320
|
-
// For other attributes, check for embedded event handlers
|
|
321
|
-
const decoded = decodeHTMLEntities(value).toLowerCase().replace(/\s/g, "");
|
|
322
|
-
if (/\bon\w+=/.test(decoded)) {
|
|
323
|
-
return null;
|
|
324
|
-
}
|
|
325
|
-
// Encode dangerous characters to prevent attribute injection
|
|
326
|
-
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
253
|
+
if (URI_ATTRS.has(name)) {
|
|
254
|
+
if (!isSafeURI(value)) return null;
|
|
255
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
256
|
+
}
|
|
257
|
+
if (name === "style") {
|
|
258
|
+
const sanitizedStyle = sanitizeStyleValue(value);
|
|
259
|
+
if (sanitizedStyle === null) return null;
|
|
260
|
+
return wasUnquoted ? encodeAttrValue(sanitizedStyle) : sanitizedStyle;
|
|
261
|
+
}
|
|
262
|
+
const decoded = decodeHTMLEntities(value).toLowerCase().replace(/\s/g, "");
|
|
263
|
+
if (/\bon\w+=/.test(decoded)) return null;
|
|
264
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
327
265
|
}
|
|
328
266
|
/**
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
267
|
+
* Extract tag name from a tag string
|
|
268
|
+
* Returns null if not a valid tag format
|
|
269
|
+
* @param {string} tag Tag string starting with <
|
|
270
|
+
* @returns {string|null} Lowercase tag name or null
|
|
271
|
+
* @private
|
|
272
|
+
*/
|
|
335
273
|
function extractTagName(tag) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
return match ? match[1].toLowerCase() : null;
|
|
274
|
+
const match = tag.match(TAG_NAME_REGEX);
|
|
275
|
+
return match ? match[1].toLowerCase() : null;
|
|
339
276
|
}
|
|
340
277
|
/**
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
278
|
+
* Check if a tag is in the whitelist
|
|
279
|
+
* @param {string} tag Tag string
|
|
280
|
+
* @returns {boolean} Whether tag is allowed
|
|
281
|
+
* @private
|
|
282
|
+
*/
|
|
346
283
|
function isAllowedTag(tag) {
|
|
347
|
-
|
|
348
|
-
|
|
284
|
+
const tagName = extractTagName(tag);
|
|
285
|
+
return tagName !== null && TAG_CASE_MAP.has(tagName);
|
|
349
286
|
}
|
|
350
287
|
/**
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
288
|
+
* Sanitize a single HTML/SVG tag (only called for allowed tags)
|
|
289
|
+
* @param {string} fullTag The full tag string including < and >
|
|
290
|
+
* @returns {string} Sanitized tag
|
|
291
|
+
* @private
|
|
292
|
+
*/
|
|
356
293
|
function sanitizeTag(fullTag) {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
quoteChar = "\"";
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
// Boolean attribute (no value)
|
|
402
|
-
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
403
|
-
allowedAttrs.push(canonicalAttrName);
|
|
404
|
-
}
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
408
|
-
const wasUnquoted = unquotedValue !== undefined;
|
|
409
|
-
const sanitizedValue = sanitizeAttrValue(lowerAttrName, attrValue, wasUnquoted);
|
|
410
|
-
if (sanitizedValue !== null) {
|
|
411
|
-
allowedAttrs.push(`${canonicalAttrName}=${quoteChar}${sanitizedValue}${quoteChar}`);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
const attrsStr = allowedAttrs.length > 0 ? ` ${allowedAttrs.join(" ")}` : "";
|
|
416
|
-
const selfCloseStr = selfClose ? "/>" : ">";
|
|
417
|
-
return `<${canonicalTagName}${attrsStr}${selfCloseStr}`;
|
|
294
|
+
const closingMatch = fullTag.match(CLOSING_TAG_REGEX);
|
|
295
|
+
if (closingMatch) {
|
|
296
|
+
const lowerName = closingMatch[1].toLowerCase();
|
|
297
|
+
return `</${TAG_CASE_MAP.get(lowerName) ?? lowerName}>`;
|
|
298
|
+
}
|
|
299
|
+
const openingMatch = fullTag.match(OPENING_TAG_REGEX);
|
|
300
|
+
if (!openingMatch) return "";
|
|
301
|
+
const [, tagName, attrString, selfClose] = openingMatch;
|
|
302
|
+
const lowerTagName = tagName.toLowerCase();
|
|
303
|
+
const canonicalTagName = TAG_CASE_MAP.get(lowerTagName) ?? lowerTagName;
|
|
304
|
+
const allowedAttrs = [];
|
|
305
|
+
ATTR_REGEX.lastIndex = 0;
|
|
306
|
+
let attrMatch;
|
|
307
|
+
while ((attrMatch = ATTR_REGEX.exec(attrString)) !== null) {
|
|
308
|
+
const lowerAttrName = attrMatch[1].toLowerCase();
|
|
309
|
+
const doubleQuotedValue = attrMatch[2];
|
|
310
|
+
const singleQuotedValue = attrMatch[3];
|
|
311
|
+
const unquotedValue = attrMatch[4];
|
|
312
|
+
if (lowerAttrName.startsWith("on")) continue;
|
|
313
|
+
const canonicalAttrName = ATTR_CASE_MAP.get(lowerAttrName) ?? lowerAttrName;
|
|
314
|
+
let attrValue;
|
|
315
|
+
let quoteChar;
|
|
316
|
+
if (doubleQuotedValue !== void 0) {
|
|
317
|
+
attrValue = doubleQuotedValue;
|
|
318
|
+
quoteChar = "\"";
|
|
319
|
+
} else if (singleQuotedValue !== void 0) {
|
|
320
|
+
attrValue = singleQuotedValue;
|
|
321
|
+
quoteChar = "'";
|
|
322
|
+
} else if (unquotedValue !== void 0) {
|
|
323
|
+
attrValue = unquotedValue;
|
|
324
|
+
quoteChar = "\"";
|
|
325
|
+
} else {
|
|
326
|
+
if (ATTR_CASE_MAP.has(lowerAttrName)) allowedAttrs.push(canonicalAttrName);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
330
|
+
const sanitizedValue = sanitizeAttrValue(lowerAttrName, attrValue, unquotedValue !== void 0);
|
|
331
|
+
if (sanitizedValue !== null) allowedAttrs.push(`${canonicalAttrName}=${quoteChar}${sanitizedValue}${quoteChar}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return `<${canonicalTagName}${allowedAttrs.length > 0 ? ` ${allowedAttrs.join(" ")}` : ""}${selfClose ? "/>" : ">"}`;
|
|
418
335
|
}
|
|
419
336
|
/**
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
337
|
+
* Sanitize HTML string to prevent XSS attacks
|
|
338
|
+
* Pure whitelist approach - allowed tags are sanitized, others are escaped
|
|
339
|
+
* @param {string} str Target string value
|
|
340
|
+
* @returns {string} Sanitized string with only allowed elements
|
|
341
|
+
* @private
|
|
342
|
+
*/
|
|
426
343
|
function sanitize(str) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (match.startsWith("<!--")) {
|
|
435
|
-
return "";
|
|
436
|
-
}
|
|
437
|
-
// Orphaned fragment (e.g., "ipt>") → escape '>'
|
|
438
|
-
if (!match.startsWith("<")) {
|
|
439
|
-
return match.slice(0, -1) + ">";
|
|
440
|
-
}
|
|
441
|
-
// Allowed tag → sanitize attributes
|
|
442
|
-
if (isAllowedTag(match)) {
|
|
443
|
-
return sanitizeTag(match);
|
|
444
|
-
}
|
|
445
|
-
// Disallowed tag → escape all '<' to prevent execution
|
|
446
|
-
return match.replace(/</g, "<");
|
|
447
|
-
});
|
|
344
|
+
if (typeof str !== "string" || !str || str.indexOf("<") === -1) return str;
|
|
345
|
+
return str.replace(/<\/?[^>]*>|[^<>\s]+>/g, (match) => {
|
|
346
|
+
if (match.startsWith("<!--")) return "";
|
|
347
|
+
if (!match.startsWith("<")) return match.slice(0, -1) + ">";
|
|
348
|
+
if (isAllowedTag(match)) return sanitizeTag(match);
|
|
349
|
+
return match.replace(/</g, "<");
|
|
350
|
+
});
|
|
448
351
|
}
|
|
449
|
-
|
|
352
|
+
//#endregion
|
|
450
353
|
export { sanitize };
|