billboard.js 4.0.3 → 4.1.0-next.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/README.md +82 -7
- package/dist/billboard.css +1 -1
- package/dist/billboard.js +1848 -494
- package/dist/billboard.min.css +1 -1
- package/dist/billboard.min.js +19 -24
- package/dist/billboard.pkgd.js +1844 -679
- package/dist/billboard.pkgd.min.js +24 -29
- 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 +5 -3
- package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-sparkline.js +6 -3
- package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
- package/dist/plugin/billboardjs-plugin-stanford.js +6 -3
- package/dist/plugin/billboardjs-plugin-stanford.min.js +3 -3
- package/dist/plugin/billboardjs-plugin-tableview.js +59 -27
- package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
- package/dist/plugin/billboardjs-plugin-textoverlap.js +75 -55
- package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +5 -3
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +6 -3
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +6 -3
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +59 -27
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +68 -3557
- 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 -143
- 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 +967 -1153
- 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 +285 -236
- 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 -288
- 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 -425
- 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 -843
- 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/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 +4 -1
- package/src/ChartInternal/interactions/eventrect.ts +8 -2
- package/src/ChartInternal/interactions/subchart.ts +437 -66
- package/src/ChartInternal/internals/color.ts +2 -2
- 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 +18 -5
- package/src/ChartInternal/internals/transform.ts +8 -2
- package/src/ChartInternal/internals/type.ts +105 -1
- package/src/ChartInternal/shape/IShape.ts +6 -3
- package/src/ChartInternal/shape/bar.ts +62 -31
- 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/point.ts +2 -2
- package/src/ChartInternal/shape/shape.ts +149 -17
- 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/util/type-checks.ts +3 -1
- 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,591 +5,482 @@
|
|
|
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.2
|
|
9
9
|
* @requires billboard.js
|
|
10
10
|
* @summary billboard.js plugin
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @ignore
|
|
16
|
-
*/
|
|
12
|
+
//#region src/module/util/type-checks.ts
|
|
13
|
+
const isFunction = (v) => typeof v === "function";
|
|
14
|
+
const isString = (v) => typeof v === "string";
|
|
17
15
|
const isNumber = (v) => typeof v === "number";
|
|
18
16
|
const isDefined = (v) => typeof v !== "undefined";
|
|
17
|
+
const isBoolean = (v) => typeof v === "boolean";
|
|
19
18
|
const isObjectType = (v) => typeof v === "object";
|
|
20
|
-
|
|
21
19
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
* Check if is array
|
|
21
|
+
* @param {Array} arr Data to be checked
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
const isArray = (arr) => Array.isArray(arr);
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/module/sanitize.ts
|
|
28
|
+
/**
|
|
29
|
+
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
30
|
+
* billboard.js project is licensed under the MIT license
|
|
31
|
+
* @ignore
|
|
32
|
+
*/
|
|
26
33
|
/**
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
34
|
+
* HTML/SVG Sanitization module
|
|
35
|
+
* Pure whitelist approach - only explicitly allowed tags, attributes, and protocols pass through
|
|
36
|
+
*/
|
|
37
|
+
const ALLOWED_TAGS = /* @__PURE__ */ new Set([
|
|
38
|
+
"span",
|
|
39
|
+
"div",
|
|
40
|
+
"p",
|
|
41
|
+
"br",
|
|
42
|
+
"b",
|
|
43
|
+
"i",
|
|
44
|
+
"em",
|
|
45
|
+
"small",
|
|
46
|
+
"strong",
|
|
47
|
+
"mark",
|
|
48
|
+
"u",
|
|
49
|
+
"s",
|
|
50
|
+
"sub",
|
|
51
|
+
"sup",
|
|
52
|
+
"h1",
|
|
53
|
+
"h2",
|
|
54
|
+
"h3",
|
|
55
|
+
"h4",
|
|
56
|
+
"h5",
|
|
57
|
+
"h6",
|
|
58
|
+
"ul",
|
|
59
|
+
"ol",
|
|
60
|
+
"li",
|
|
61
|
+
"dl",
|
|
62
|
+
"dt",
|
|
63
|
+
"dd",
|
|
64
|
+
"table",
|
|
65
|
+
"thead",
|
|
66
|
+
"tbody",
|
|
67
|
+
"tfoot",
|
|
68
|
+
"tr",
|
|
69
|
+
"th",
|
|
70
|
+
"td",
|
|
71
|
+
"caption",
|
|
72
|
+
"colgroup",
|
|
73
|
+
"col",
|
|
74
|
+
"hr",
|
|
75
|
+
"pre",
|
|
76
|
+
"code",
|
|
77
|
+
"blockquote",
|
|
78
|
+
"abbr",
|
|
79
|
+
"ins",
|
|
80
|
+
"del",
|
|
81
|
+
"a",
|
|
82
|
+
"img",
|
|
83
|
+
"figure",
|
|
84
|
+
"figcaption",
|
|
85
|
+
"svg",
|
|
86
|
+
"g",
|
|
87
|
+
"path",
|
|
88
|
+
"circle",
|
|
89
|
+
"ellipse",
|
|
90
|
+
"rect",
|
|
91
|
+
"line",
|
|
92
|
+
"polyline",
|
|
93
|
+
"polygon",
|
|
94
|
+
"text",
|
|
95
|
+
"tspan",
|
|
96
|
+
"textPath",
|
|
97
|
+
"use",
|
|
98
|
+
"defs",
|
|
99
|
+
"symbol",
|
|
100
|
+
"clipPath",
|
|
101
|
+
"mask",
|
|
102
|
+
"linearGradient",
|
|
103
|
+
"radialGradient",
|
|
104
|
+
"stop",
|
|
105
|
+
"pattern",
|
|
106
|
+
"marker",
|
|
107
|
+
"title",
|
|
108
|
+
"desc"
|
|
109
|
+
]);
|
|
110
|
+
const ALLOWED_ATTRS = /* @__PURE__ */ new Set([
|
|
111
|
+
"class",
|
|
112
|
+
"id",
|
|
113
|
+
"style",
|
|
114
|
+
"title",
|
|
115
|
+
"lang",
|
|
116
|
+
"dir",
|
|
117
|
+
"href",
|
|
118
|
+
"src",
|
|
119
|
+
"alt",
|
|
120
|
+
"width",
|
|
121
|
+
"height",
|
|
122
|
+
"colspan",
|
|
123
|
+
"rowspan",
|
|
124
|
+
"scope",
|
|
125
|
+
"headers",
|
|
126
|
+
"d",
|
|
127
|
+
"points",
|
|
128
|
+
"x",
|
|
129
|
+
"y",
|
|
130
|
+
"x1",
|
|
131
|
+
"x2",
|
|
132
|
+
"y1",
|
|
133
|
+
"y2",
|
|
134
|
+
"cx",
|
|
135
|
+
"cy",
|
|
136
|
+
"r",
|
|
137
|
+
"rx",
|
|
138
|
+
"ry",
|
|
139
|
+
"dx",
|
|
140
|
+
"dy",
|
|
141
|
+
"viewBox",
|
|
142
|
+
"preserveAspectRatio",
|
|
143
|
+
"transform",
|
|
144
|
+
"fill",
|
|
145
|
+
"fill-opacity",
|
|
146
|
+
"fill-rule",
|
|
147
|
+
"stroke",
|
|
148
|
+
"stroke-width",
|
|
149
|
+
"stroke-opacity",
|
|
150
|
+
"stroke-linecap",
|
|
151
|
+
"stroke-linejoin",
|
|
152
|
+
"stroke-dasharray",
|
|
153
|
+
"stroke-dashoffset",
|
|
154
|
+
"opacity",
|
|
155
|
+
"clip-path",
|
|
156
|
+
"clip-rule",
|
|
157
|
+
"mask",
|
|
158
|
+
"font-family",
|
|
159
|
+
"font-size",
|
|
160
|
+
"font-weight",
|
|
161
|
+
"font-style",
|
|
162
|
+
"text-anchor",
|
|
163
|
+
"dominant-baseline",
|
|
164
|
+
"offset",
|
|
165
|
+
"stop-color",
|
|
166
|
+
"stop-opacity",
|
|
167
|
+
"gradientUnits",
|
|
168
|
+
"gradientTransform",
|
|
169
|
+
"spreadMethod",
|
|
170
|
+
"patternUnits",
|
|
171
|
+
"patternTransform",
|
|
172
|
+
"marker-start",
|
|
173
|
+
"marker-mid",
|
|
174
|
+
"marker-end",
|
|
175
|
+
"markerWidth",
|
|
176
|
+
"markerHeight",
|
|
177
|
+
"refX",
|
|
178
|
+
"refY",
|
|
179
|
+
"xlink:href"
|
|
105
180
|
]);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"dir",
|
|
115
|
-
// HTML specific
|
|
116
|
-
"href",
|
|
117
|
-
"src",
|
|
118
|
-
"alt",
|
|
119
|
-
"width",
|
|
120
|
-
"height",
|
|
121
|
-
"colspan",
|
|
122
|
-
"rowspan",
|
|
123
|
-
"scope",
|
|
124
|
-
"headers",
|
|
125
|
-
// SVG presentation attributes
|
|
126
|
-
"d",
|
|
127
|
-
"points",
|
|
128
|
-
"x",
|
|
129
|
-
"y",
|
|
130
|
-
"x1",
|
|
131
|
-
"x2",
|
|
132
|
-
"y1",
|
|
133
|
-
"y2",
|
|
134
|
-
"cx",
|
|
135
|
-
"cy",
|
|
136
|
-
"r",
|
|
137
|
-
"rx",
|
|
138
|
-
"ry",
|
|
139
|
-
"dx",
|
|
140
|
-
"dy",
|
|
141
|
-
"viewBox",
|
|
142
|
-
"preserveAspectRatio",
|
|
143
|
-
"transform",
|
|
144
|
-
"fill",
|
|
145
|
-
"fill-opacity",
|
|
146
|
-
"fill-rule",
|
|
147
|
-
"stroke",
|
|
148
|
-
"stroke-width",
|
|
149
|
-
"stroke-opacity",
|
|
150
|
-
"stroke-linecap",
|
|
151
|
-
"stroke-linejoin",
|
|
152
|
-
"stroke-dasharray",
|
|
153
|
-
"stroke-dashoffset",
|
|
154
|
-
"opacity",
|
|
155
|
-
"clip-path",
|
|
156
|
-
"clip-rule",
|
|
157
|
-
"mask",
|
|
158
|
-
"font-family",
|
|
159
|
-
"font-size",
|
|
160
|
-
"font-weight",
|
|
161
|
-
"font-style",
|
|
162
|
-
"text-anchor",
|
|
163
|
-
"dominant-baseline",
|
|
164
|
-
"offset",
|
|
165
|
-
"stop-color",
|
|
166
|
-
"stop-opacity",
|
|
167
|
-
"gradientUnits",
|
|
168
|
-
"gradientTransform",
|
|
169
|
-
"spreadMethod",
|
|
170
|
-
"patternUnits",
|
|
171
|
-
"patternTransform",
|
|
172
|
-
"marker-start",
|
|
173
|
-
"marker-mid",
|
|
174
|
-
"marker-end",
|
|
175
|
-
"markerWidth",
|
|
176
|
-
"markerHeight",
|
|
177
|
-
"refX",
|
|
178
|
-
"refY",
|
|
179
|
-
"xlink:href"
|
|
181
|
+
const TAG_CASE_MAP = /* @__PURE__ */ new Map();
|
|
182
|
+
ALLOWED_TAGS.forEach((tag) => TAG_CASE_MAP.set(tag.toLowerCase(), tag));
|
|
183
|
+
const ATTR_CASE_MAP = /* @__PURE__ */ new Map();
|
|
184
|
+
ALLOWED_ATTRS.forEach((attr) => ATTR_CASE_MAP.set(attr.toLowerCase(), attr));
|
|
185
|
+
const ALLOWED_URI_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
186
|
+
"http:",
|
|
187
|
+
"https:",
|
|
188
|
+
"mailto:"
|
|
180
189
|
]);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
ALLOWED_ATTRS.forEach(attr => ATTR_CASE_MAP.set(attr.toLowerCase(), attr));
|
|
186
|
-
// Whitelist of allowed URI protocols
|
|
187
|
-
const ALLOWED_URI_PROTOCOLS = new Set([
|
|
188
|
-
"http:",
|
|
189
|
-
"https:",
|
|
190
|
-
"mailto:"
|
|
190
|
+
const URI_ATTRS = /* @__PURE__ */ new Set([
|
|
191
|
+
"href",
|
|
192
|
+
"src",
|
|
193
|
+
"xlink:href"
|
|
191
194
|
]);
|
|
192
|
-
// Attributes that contain URIs
|
|
193
|
-
const URI_ATTRS = new Set(["href", "src", "xlink:href"]);
|
|
194
|
-
// Pre-compiled regex patterns for performance
|
|
195
195
|
const TAG_NAME_REGEX = /^<\/?([a-zA-Z][a-zA-Z0-9]*)/;
|
|
196
196
|
const CLOSING_TAG_REGEX = /^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/;
|
|
197
197
|
const OPENING_TAG_REGEX = /^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/;
|
|
198
198
|
const ATTR_REGEX = /([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
199
199
|
const URL_IN_STYLE_REGEX = /url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi;
|
|
200
|
-
// Dangerous CSS patterns
|
|
201
200
|
const DANGEROUS_CSS_PATTERNS = [
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
201
|
+
"expression(",
|
|
202
|
+
"behavior:",
|
|
203
|
+
"binding:",
|
|
204
|
+
"@import",
|
|
205
|
+
"@charset",
|
|
206
|
+
"-moz-binding:"
|
|
208
207
|
];
|
|
209
208
|
/**
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
209
|
+
* Decode HTML entities in a string
|
|
210
|
+
* @param {string} str String with potential HTML entities
|
|
211
|
+
* @returns {string} Decoded string
|
|
212
|
+
* @private
|
|
213
|
+
*/
|
|
215
214
|
function decodeHTMLEntities(str) {
|
|
216
|
-
|
|
217
|
-
// Named entities
|
|
218
|
-
.replace(/:/gi, ":")
|
|
219
|
-
.replace(/&newline;/gi, "\n")
|
|
220
|
-
.replace(/&tab;/gi, "\t")
|
|
221
|
-
.replace(/ /gi, " ")
|
|
222
|
-
.replace(/</gi, "<")
|
|
223
|
-
.replace(/>/gi, ">")
|
|
224
|
-
.replace(/&/gi, "&")
|
|
225
|
-
.replace(/"/gi, "\"")
|
|
226
|
-
.replace(/'/gi, "'")
|
|
227
|
-
// Numeric entities (decimal) - trailing semicolon is optional per HTML5 tokenizer
|
|
228
|
-
.replace(/&#(\d+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 10)))
|
|
229
|
-
// Numeric entities (hex) - trailing semicolon is optional per HTML5 tokenizer
|
|
230
|
-
.replace(/&#x([0-9a-f]+);?/gi, (_, code) => String.fromCharCode(parseInt(code, 16)));
|
|
215
|
+
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)));
|
|
231
216
|
}
|
|
232
217
|
/**
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
218
|
+
* Check if a URI is safe (whitelist approach)
|
|
219
|
+
* @param {string} uri URI to check
|
|
220
|
+
* @returns {boolean} Whether the URI is safe
|
|
221
|
+
* @private
|
|
222
|
+
*/
|
|
238
223
|
function isSafeURI(uri) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
// Empty or fragment-only URIs are safe
|
|
245
|
-
if (!normalized || normalized.startsWith("#")) {
|
|
246
|
-
return true;
|
|
247
|
-
}
|
|
248
|
-
// A ':' appearing before the first '/', '?' or '#' denotes a URI scheme.
|
|
249
|
-
// Reject the value unless that scheme is whitelisted - do NOT infer safety
|
|
250
|
-
// merely from the absence of a literal colon (which could be reconstructed
|
|
251
|
-
// from an alternate encoding once assigned to innerHTML).
|
|
252
|
-
const schemeMatch = normalized.match(/^[^/?#]*:/);
|
|
253
|
-
if (schemeMatch) {
|
|
254
|
-
return ALLOWED_URI_PROTOCOLS.has(schemeMatch[0]);
|
|
255
|
-
}
|
|
256
|
-
// No scheme → relative path / query / fragment only → safe
|
|
257
|
-
return true;
|
|
224
|
+
const normalized = decodeHTMLEntities(uri).trim().replace(/[\s\u0000-\u001f]/g, "").toLowerCase();
|
|
225
|
+
if (!normalized || normalized.startsWith("#")) return true;
|
|
226
|
+
const schemeMatch = normalized.match(/^[^/?#]*:/);
|
|
227
|
+
if (schemeMatch) return ALLOWED_URI_PROTOCOLS.has(schemeMatch[0]);
|
|
228
|
+
return true;
|
|
258
229
|
}
|
|
259
230
|
/**
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
231
|
+
* Check if a style value is safe (whitelist approach)
|
|
232
|
+
* @param {string} style Style attribute value
|
|
233
|
+
* @returns {string|null} Sanitized style or null if unsafe
|
|
234
|
+
* @private
|
|
235
|
+
*/
|
|
265
236
|
function sanitizeStyleValue(style) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
let match;
|
|
274
|
-
while ((match = URL_IN_STYLE_REGEX.exec(cleaned)) !== null) {
|
|
275
|
-
if (!isSafeURI(match[1])) {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
// Check for dangerous CSS patterns (expression, behavior, etc.)
|
|
280
|
-
const normalizedLower = cleaned.toLowerCase().replace(/\s/g, "");
|
|
281
|
-
for (const pattern of DANGEROUS_CSS_PATTERNS) {
|
|
282
|
-
if (normalizedLower.includes(pattern)) {
|
|
283
|
-
return null;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
return style;
|
|
237
|
+
const cleaned = decodeHTMLEntities(style).replace(/[\u0000-\u001f]/g, "");
|
|
238
|
+
URL_IN_STYLE_REGEX.lastIndex = 0;
|
|
239
|
+
let match;
|
|
240
|
+
while ((match = URL_IN_STYLE_REGEX.exec(cleaned)) !== null) if (!isSafeURI(match[1])) return null;
|
|
241
|
+
const normalizedLower = cleaned.toLowerCase().replace(/\s/g, "");
|
|
242
|
+
for (const pattern of DANGEROUS_CSS_PATTERNS) if (normalizedLower.includes(pattern)) return null;
|
|
243
|
+
return style;
|
|
287
244
|
}
|
|
288
|
-
// Lookup table for encoding dangerous characters in attribute values
|
|
289
245
|
const ATTR_ENCODE_MAP = {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
246
|
+
"\"": """,
|
|
247
|
+
"'": "'",
|
|
248
|
+
"`": "`"
|
|
293
249
|
};
|
|
294
250
|
const ATTR_ENCODE_REGEX = /["'`]/g;
|
|
295
251
|
/**
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
252
|
+
* Encode dangerous characters in attribute values to HTML entities
|
|
253
|
+
* This prevents attribute injection attacks where quotes/backticks break out of the attribute context
|
|
254
|
+
* @param {string} value Attribute value
|
|
255
|
+
* @returns {string} Encoded value
|
|
256
|
+
* @private
|
|
257
|
+
*/
|
|
302
258
|
function encodeAttrValue(value) {
|
|
303
|
-
|
|
259
|
+
return value.replace(ATTR_ENCODE_REGEX, (char) => ATTR_ENCODE_MAP[char]);
|
|
304
260
|
}
|
|
305
261
|
/**
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
262
|
+
* Sanitize attribute value using whitelist approach
|
|
263
|
+
* @param {string} name Attribute name
|
|
264
|
+
* @param {string} value Attribute value
|
|
265
|
+
* @param {boolean} wasUnquoted Whether the value was originally unquoted
|
|
266
|
+
* @returns {string|null} Sanitized value if safe, null if should be removed
|
|
267
|
+
* @private
|
|
268
|
+
*/
|
|
313
269
|
function sanitizeAttrValue(name, value, wasUnquoted = false) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return null;
|
|
327
|
-
}
|
|
328
|
-
// Encode dangerous characters in style values
|
|
329
|
-
return wasUnquoted ? encodeAttrValue(sanitizedStyle) : sanitizedStyle;
|
|
330
|
-
}
|
|
331
|
-
// For other attributes, check for embedded event handlers
|
|
332
|
-
const decoded = decodeHTMLEntities(value).toLowerCase().replace(/\s/g, "");
|
|
333
|
-
if (/\bon\w+=/.test(decoded)) {
|
|
334
|
-
return null;
|
|
335
|
-
}
|
|
336
|
-
// Encode dangerous characters to prevent attribute injection
|
|
337
|
-
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
270
|
+
if (URI_ATTRS.has(name)) {
|
|
271
|
+
if (!isSafeURI(value)) return null;
|
|
272
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
273
|
+
}
|
|
274
|
+
if (name === "style") {
|
|
275
|
+
const sanitizedStyle = sanitizeStyleValue(value);
|
|
276
|
+
if (sanitizedStyle === null) return null;
|
|
277
|
+
return wasUnquoted ? encodeAttrValue(sanitizedStyle) : sanitizedStyle;
|
|
278
|
+
}
|
|
279
|
+
const decoded = decodeHTMLEntities(value).toLowerCase().replace(/\s/g, "");
|
|
280
|
+
if (/\bon\w+=/.test(decoded)) return null;
|
|
281
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
338
282
|
}
|
|
339
283
|
/**
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
284
|
+
* Extract tag name from a tag string
|
|
285
|
+
* Returns null if not a valid tag format
|
|
286
|
+
* @param {string} tag Tag string starting with <
|
|
287
|
+
* @returns {string|null} Lowercase tag name or null
|
|
288
|
+
* @private
|
|
289
|
+
*/
|
|
346
290
|
function extractTagName(tag) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return match ? match[1].toLowerCase() : null;
|
|
291
|
+
const match = tag.match(TAG_NAME_REGEX);
|
|
292
|
+
return match ? match[1].toLowerCase() : null;
|
|
350
293
|
}
|
|
351
294
|
/**
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
295
|
+
* Check if a tag is in the whitelist
|
|
296
|
+
* @param {string} tag Tag string
|
|
297
|
+
* @returns {boolean} Whether tag is allowed
|
|
298
|
+
* @private
|
|
299
|
+
*/
|
|
357
300
|
function isAllowedTag(tag) {
|
|
358
|
-
|
|
359
|
-
|
|
301
|
+
const tagName = extractTagName(tag);
|
|
302
|
+
return tagName !== null && TAG_CASE_MAP.has(tagName);
|
|
360
303
|
}
|
|
361
304
|
/**
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
305
|
+
* Sanitize a single HTML/SVG tag (only called for allowed tags)
|
|
306
|
+
* @param {string} fullTag The full tag string including < and >
|
|
307
|
+
* @returns {string} Sanitized tag
|
|
308
|
+
* @private
|
|
309
|
+
*/
|
|
367
310
|
function sanitizeTag(fullTag) {
|
|
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
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
quoteChar = "\"";
|
|
410
|
-
}
|
|
411
|
-
else {
|
|
412
|
-
// Boolean attribute (no value)
|
|
413
|
-
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
414
|
-
allowedAttrs.push(canonicalAttrName);
|
|
415
|
-
}
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
|
-
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
419
|
-
const wasUnquoted = unquotedValue !== undefined;
|
|
420
|
-
const sanitizedValue = sanitizeAttrValue(lowerAttrName, attrValue, wasUnquoted);
|
|
421
|
-
if (sanitizedValue !== null) {
|
|
422
|
-
allowedAttrs.push(`${canonicalAttrName}=${quoteChar}${sanitizedValue}${quoteChar}`);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
const attrsStr = allowedAttrs.length > 0 ? ` ${allowedAttrs.join(" ")}` : "";
|
|
427
|
-
const selfCloseStr = selfClose ? "/>" : ">";
|
|
428
|
-
return `<${canonicalTagName}${attrsStr}${selfCloseStr}`;
|
|
311
|
+
const closingMatch = fullTag.match(CLOSING_TAG_REGEX);
|
|
312
|
+
if (closingMatch) {
|
|
313
|
+
const lowerName = closingMatch[1].toLowerCase();
|
|
314
|
+
return `</${TAG_CASE_MAP.get(lowerName) ?? lowerName}>`;
|
|
315
|
+
}
|
|
316
|
+
const openingMatch = fullTag.match(OPENING_TAG_REGEX);
|
|
317
|
+
if (!openingMatch) return "";
|
|
318
|
+
const [, tagName, attrString, selfClose] = openingMatch;
|
|
319
|
+
const lowerTagName = tagName.toLowerCase();
|
|
320
|
+
const canonicalTagName = TAG_CASE_MAP.get(lowerTagName) ?? lowerTagName;
|
|
321
|
+
const allowedAttrs = [];
|
|
322
|
+
ATTR_REGEX.lastIndex = 0;
|
|
323
|
+
let attrMatch;
|
|
324
|
+
while ((attrMatch = ATTR_REGEX.exec(attrString)) !== null) {
|
|
325
|
+
const lowerAttrName = attrMatch[1].toLowerCase();
|
|
326
|
+
const doubleQuotedValue = attrMatch[2];
|
|
327
|
+
const singleQuotedValue = attrMatch[3];
|
|
328
|
+
const unquotedValue = attrMatch[4];
|
|
329
|
+
if (lowerAttrName.startsWith("on")) continue;
|
|
330
|
+
const canonicalAttrName = ATTR_CASE_MAP.get(lowerAttrName) ?? lowerAttrName;
|
|
331
|
+
let attrValue;
|
|
332
|
+
let quoteChar;
|
|
333
|
+
if (doubleQuotedValue !== void 0) {
|
|
334
|
+
attrValue = doubleQuotedValue;
|
|
335
|
+
quoteChar = "\"";
|
|
336
|
+
} else if (singleQuotedValue !== void 0) {
|
|
337
|
+
attrValue = singleQuotedValue;
|
|
338
|
+
quoteChar = "'";
|
|
339
|
+
} else if (unquotedValue !== void 0) {
|
|
340
|
+
attrValue = unquotedValue;
|
|
341
|
+
quoteChar = "\"";
|
|
342
|
+
} else {
|
|
343
|
+
if (ATTR_CASE_MAP.has(lowerAttrName)) allowedAttrs.push(canonicalAttrName);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (ATTR_CASE_MAP.has(lowerAttrName)) {
|
|
347
|
+
const sanitizedValue = sanitizeAttrValue(lowerAttrName, attrValue, unquotedValue !== void 0);
|
|
348
|
+
if (sanitizedValue !== null) allowedAttrs.push(`${canonicalAttrName}=${quoteChar}${sanitizedValue}${quoteChar}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return `<${canonicalTagName}${allowedAttrs.length > 0 ? ` ${allowedAttrs.join(" ")}` : ""}${selfClose ? "/>" : ">"}`;
|
|
429
352
|
}
|
|
430
353
|
/**
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
354
|
+
* Sanitize HTML string to prevent XSS attacks
|
|
355
|
+
* Pure whitelist approach - allowed tags are sanitized, others are escaped
|
|
356
|
+
* @param {string} str Target string value
|
|
357
|
+
* @returns {string} Sanitized string with only allowed elements
|
|
358
|
+
* @private
|
|
359
|
+
*/
|
|
437
360
|
function sanitize(str) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
if (match.startsWith("<!--")) {
|
|
446
|
-
return "";
|
|
447
|
-
}
|
|
448
|
-
// Orphaned fragment (e.g., "ipt>") → escape '>'
|
|
449
|
-
if (!match.startsWith("<")) {
|
|
450
|
-
return match.slice(0, -1) + ">";
|
|
451
|
-
}
|
|
452
|
-
// Allowed tag → sanitize attributes
|
|
453
|
-
if (isAllowedTag(match)) {
|
|
454
|
-
return sanitizeTag(match);
|
|
455
|
-
}
|
|
456
|
-
// Disallowed tag → escape all '<' to prevent execution
|
|
457
|
-
return match.replace(/</g, "<");
|
|
458
|
-
});
|
|
361
|
+
if (typeof str !== "string" || !str || str.indexOf("<") === -1) return str;
|
|
362
|
+
return str.replace(/<\/?[^>]*>|[^<>\s]+>/g, (match) => {
|
|
363
|
+
if (match.startsWith("<!--")) return "";
|
|
364
|
+
if (!match.startsWith("<")) return match.slice(0, -1) + ">";
|
|
365
|
+
if (isAllowedTag(match)) return sanitizeTag(match);
|
|
366
|
+
return match.replace(/</g, "<");
|
|
367
|
+
});
|
|
459
368
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
463
|
-
* billboard.js project is licensed under the MIT license
|
|
464
|
-
* @ignore
|
|
465
|
-
*/
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region src/module/util/object.ts
|
|
466
371
|
/**
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
372
|
+
* Process the template & return bound string
|
|
373
|
+
* @param {string} tpl Template string
|
|
374
|
+
* @param {object} data Data value to be replaced
|
|
375
|
+
* @returns {string}
|
|
376
|
+
* @private
|
|
377
|
+
*/
|
|
473
378
|
function tplProcess(tpl, data) {
|
|
474
|
-
|
|
379
|
+
return sanitize(tpl.replace(/\{=([^}]+)\}/g, (_, key) => data[key] ?? ""));
|
|
475
380
|
}
|
|
476
|
-
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/config/config.ts
|
|
477
383
|
/**
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
384
|
+
* Load configuration option
|
|
385
|
+
* @param {object} config User's generation config value
|
|
386
|
+
* @private
|
|
387
|
+
*/
|
|
482
388
|
function loadConfig(config) {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
read = find();
|
|
502
|
-
if (isDefined(read)) {
|
|
503
|
-
thisConfig[key] = read;
|
|
504
|
-
}
|
|
505
|
-
});
|
|
506
|
-
// only should run in the ChartInternal context
|
|
507
|
-
if (this.api) {
|
|
508
|
-
this.state.orgConfig = config;
|
|
509
|
-
}
|
|
389
|
+
const thisConfig = this.config;
|
|
390
|
+
let target;
|
|
391
|
+
let keys;
|
|
392
|
+
let read;
|
|
393
|
+
const find = () => {
|
|
394
|
+
const key = keys.shift();
|
|
395
|
+
if (key && target && isObjectType(target) && key in target) {
|
|
396
|
+
target = target[key];
|
|
397
|
+
return find();
|
|
398
|
+
} else if (!key) return target;
|
|
399
|
+
};
|
|
400
|
+
Object.keys(thisConfig).forEach((key) => {
|
|
401
|
+
target = config;
|
|
402
|
+
keys = key.split("_");
|
|
403
|
+
read = find();
|
|
404
|
+
if (isDefined(read)) thisConfig[key] = read;
|
|
405
|
+
});
|
|
406
|
+
if (this.api) this.state.orgConfig = config;
|
|
510
407
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
514
|
-
* billboard.js project is licensed under the MIT license
|
|
515
|
-
*/
|
|
408
|
+
//#endregion
|
|
409
|
+
//#region src/Plugin/Plugin.ts
|
|
516
410
|
/**
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
* Version info string for plugin
|
|
522
|
-
* @name version
|
|
523
|
-
* @static
|
|
524
|
-
* @memberof Plugin
|
|
525
|
-
* @type {string}
|
|
526
|
-
* @example
|
|
527
|
-
* bb.plugin.stanford.version; // ex) 1.9.0
|
|
528
|
-
*/
|
|
529
|
-
class Plugin {
|
|
530
|
-
$$;
|
|
531
|
-
options;
|
|
532
|
-
config;
|
|
533
|
-
static version = "4.0.3";
|
|
534
|
-
/**
|
|
535
|
-
* Constructor
|
|
536
|
-
* @param {Any} options config option object
|
|
537
|
-
* @private
|
|
538
|
-
*/
|
|
539
|
-
constructor(options = {}) {
|
|
540
|
-
this.options = options;
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Load plugin config from options
|
|
544
|
-
* @private
|
|
545
|
-
*/
|
|
546
|
-
loadConfig() {
|
|
547
|
-
loadConfig.call(this, this.options);
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Lifecycle hook for 'beforeInit' phase.
|
|
551
|
-
* @private
|
|
552
|
-
*/
|
|
553
|
-
$beforeInit() { }
|
|
554
|
-
/**
|
|
555
|
-
* Lifecycle hook for 'init' phase.
|
|
556
|
-
* @private
|
|
557
|
-
*/
|
|
558
|
-
$init() { }
|
|
559
|
-
/**
|
|
560
|
-
* Lifecycle hook for 'afterInit' phase.
|
|
561
|
-
* @private
|
|
562
|
-
*/
|
|
563
|
-
$afterInit() { }
|
|
564
|
-
/**
|
|
565
|
-
* Lifecycle hook for 'redraw' phase.
|
|
566
|
-
* @private
|
|
567
|
-
*/
|
|
568
|
-
$redraw() { }
|
|
569
|
-
/**
|
|
570
|
-
* Lifecycle hook for 'willDestroy' phase.
|
|
571
|
-
* @private
|
|
572
|
-
*/
|
|
573
|
-
$willDestroy() {
|
|
574
|
-
Object.keys(this).forEach(key => {
|
|
575
|
-
this[key] = null;
|
|
576
|
-
delete this[key];
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
|
|
411
|
+
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
412
|
+
* billboard.js project is licensed under the MIT license
|
|
413
|
+
*/
|
|
581
414
|
/**
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
415
|
+
* Base class to generate billboard.js plugin
|
|
416
|
+
* @class Plugin
|
|
417
|
+
*/
|
|
585
418
|
/**
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
419
|
+
* Version info string for plugin
|
|
420
|
+
* @name version
|
|
421
|
+
* @static
|
|
422
|
+
* @memberof Plugin
|
|
423
|
+
* @type {string}
|
|
424
|
+
* @example
|
|
425
|
+
* bb.plugin.stanford.version; // ex) 1.9.0
|
|
426
|
+
*/
|
|
427
|
+
var Plugin = class {
|
|
428
|
+
$$;
|
|
429
|
+
options;
|
|
430
|
+
config;
|
|
431
|
+
static version = "4.1.0-next.2";
|
|
432
|
+
/**
|
|
433
|
+
* Constructor
|
|
434
|
+
* @param {Any} options config option object
|
|
435
|
+
* @private
|
|
436
|
+
*/
|
|
437
|
+
constructor(options = {}) {
|
|
438
|
+
this.options = options;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Load plugin config from options
|
|
442
|
+
* @private
|
|
443
|
+
*/
|
|
444
|
+
loadConfig() {
|
|
445
|
+
loadConfig.call(this, this.options);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Lifecycle hook for 'beforeInit' phase.
|
|
449
|
+
* @private
|
|
450
|
+
*/
|
|
451
|
+
$beforeInit() {}
|
|
452
|
+
/**
|
|
453
|
+
* Lifecycle hook for 'init' phase.
|
|
454
|
+
* @private
|
|
455
|
+
*/
|
|
456
|
+
$init() {}
|
|
457
|
+
/**
|
|
458
|
+
* Lifecycle hook for 'afterInit' phase.
|
|
459
|
+
* @private
|
|
460
|
+
*/
|
|
461
|
+
$afterInit() {}
|
|
462
|
+
/**
|
|
463
|
+
* Lifecycle hook for 'redraw' phase.
|
|
464
|
+
* @private
|
|
465
|
+
*/
|
|
466
|
+
$redraw() {}
|
|
467
|
+
/**
|
|
468
|
+
* Lifecycle hook for 'willDestroy' phase.
|
|
469
|
+
* @private
|
|
470
|
+
*/
|
|
471
|
+
$willDestroy() {
|
|
472
|
+
Object.keys(this).forEach((key) => {
|
|
473
|
+
this[key] = null;
|
|
474
|
+
delete this[key];
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
//#endregion
|
|
479
|
+
//#region src/Plugin/tableview/const.ts
|
|
589
480
|
const defaultStyle = {
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
481
|
+
id: "__tableview-style__",
|
|
482
|
+
class: "bb-tableview",
|
|
483
|
+
rule: `.bb-tableview {
|
|
593
484
|
border-collapse:collapse;
|
|
594
485
|
border-spacing:0;
|
|
595
486
|
background:#fff;
|
|
@@ -615,273 +506,302 @@ const defaultStyle = {
|
|
|
615
506
|
margin-bottom: -1px;
|
|
616
507
|
}`
|
|
617
508
|
};
|
|
618
|
-
// template
|
|
619
509
|
const tpl = {
|
|
620
|
-
|
|
510
|
+
body: `<caption>{=title}</caption>
|
|
621
511
|
<thead><tr>{=thead}</tr></thead>
|
|
622
512
|
<tbody>{=tbody}</tbody>`,
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
513
|
+
thead: `<th scope="col">{=title}</th>`,
|
|
514
|
+
tbodyHeader: `<th scope="row">{=value}</th>`,
|
|
515
|
+
tbody: `<td>{=value}</td>`
|
|
626
516
|
};
|
|
627
|
-
|
|
517
|
+
//#endregion
|
|
518
|
+
//#region src/Plugin/tableview/Options.ts
|
|
628
519
|
/**
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
520
|
+
* Copyright (c) 2021 ~ present NAVER Corp.
|
|
521
|
+
* billboard.js project is licensed under the MIT license
|
|
522
|
+
*/
|
|
632
523
|
/**
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
524
|
+
* TableView plugin option class
|
|
525
|
+
* @class TableviewOptions
|
|
526
|
+
* @param {Options} options TableView plugin options
|
|
527
|
+
* @augments Plugin
|
|
528
|
+
* @returns {TableviewOptions}
|
|
529
|
+
* @private
|
|
530
|
+
*/
|
|
531
|
+
var Options = class {
|
|
532
|
+
constructor() {
|
|
533
|
+
return {
|
|
534
|
+
/**
|
|
535
|
+
* Set tableview holder selector.
|
|
536
|
+
* - **NOTE:** If not set, will append new holder element dynamically right after chart element.
|
|
537
|
+
* @name selector
|
|
538
|
+
* @memberof plugin-tableview
|
|
539
|
+
* @type {string}
|
|
540
|
+
* @default undefined
|
|
541
|
+
* @example
|
|
542
|
+
* selector: "#table-holder"
|
|
543
|
+
*/
|
|
544
|
+
selector: void 0,
|
|
545
|
+
/**
|
|
546
|
+
* Set category title text
|
|
547
|
+
* @name categoryTitle
|
|
548
|
+
* @memberof plugin-tableview
|
|
549
|
+
* @type {string}
|
|
550
|
+
* @default "Category"
|
|
551
|
+
* @example
|
|
552
|
+
* categoryTitle: "#table-holder"
|
|
553
|
+
*/
|
|
554
|
+
categoryTitle: "Category",
|
|
555
|
+
/**
|
|
556
|
+
* Set category text format function.
|
|
557
|
+
* @name categoryFormat
|
|
558
|
+
* @memberof plugin-tableview
|
|
559
|
+
* @type {function}
|
|
560
|
+
* @returns {string}
|
|
561
|
+
* @default function(v) { // will return formatted value according x Axis type }}
|
|
562
|
+
* @example
|
|
563
|
+
* categoryFormat: "#table-holder"
|
|
564
|
+
*/
|
|
565
|
+
categoryFormat: function(v) {
|
|
566
|
+
let category = v;
|
|
567
|
+
if (this.$$.axis.isCategorized()) category = this.$$.categoryName(v);
|
|
568
|
+
else if (this.$$.axis.isTimeSeries()) category = v.toLocaleDateString();
|
|
569
|
+
return category;
|
|
570
|
+
},
|
|
571
|
+
/**
|
|
572
|
+
* Set tableview holder class name.
|
|
573
|
+
* @name class
|
|
574
|
+
* @memberof plugin-tableview
|
|
575
|
+
* @type {string}
|
|
576
|
+
* @default undefined
|
|
577
|
+
* @example
|
|
578
|
+
* class: "table-class-name"
|
|
579
|
+
*/
|
|
580
|
+
class: void 0,
|
|
581
|
+
/**
|
|
582
|
+
* Set to apply default style(`.bb-tableview`) to tableview element.
|
|
583
|
+
* @name style
|
|
584
|
+
* @memberof plugin-tableview
|
|
585
|
+
* @type {boolean}
|
|
586
|
+
* @default true
|
|
587
|
+
* @example
|
|
588
|
+
* style: false
|
|
589
|
+
*/
|
|
590
|
+
style: true,
|
|
591
|
+
/**
|
|
592
|
+
* Set tableview title text.
|
|
593
|
+
* - **NOTE:** If set [title.text](https://naver.github.io/billboard.js/release/latest/doc/Options.html#.title), will be used when this option value is empty.
|
|
594
|
+
* @name title
|
|
595
|
+
* @memberof plugin-tableview
|
|
596
|
+
* @type {string}
|
|
597
|
+
* @default undefined
|
|
598
|
+
* @example
|
|
599
|
+
* title: "Table Title Text"
|
|
600
|
+
*/
|
|
601
|
+
title: void 0,
|
|
602
|
+
/**
|
|
603
|
+
* Update tableview from data visibility update(ex. legend toggle).
|
|
604
|
+
* @name updateOnToggle
|
|
605
|
+
* @memberof plugin-tableview
|
|
606
|
+
* @type {boolean}
|
|
607
|
+
* @default true
|
|
608
|
+
* @example
|
|
609
|
+
* legendToggleUpdate: false
|
|
610
|
+
*/
|
|
611
|
+
updateOnToggle: true,
|
|
612
|
+
/**
|
|
613
|
+
* Set how null value to be shown.
|
|
614
|
+
* @name nullString
|
|
615
|
+
* @memberof plugin-tableview
|
|
616
|
+
* @type {string}
|
|
617
|
+
* @default "-"
|
|
618
|
+
* @example
|
|
619
|
+
* nullString: "N/A"
|
|
620
|
+
*/
|
|
621
|
+
nullString: "-",
|
|
622
|
+
/**
|
|
623
|
+
* Set number format function.
|
|
624
|
+
* @name numberFormat
|
|
625
|
+
* @memberof plugin-tableview
|
|
626
|
+
* @type {function}
|
|
627
|
+
* @returns {string}
|
|
628
|
+
* @default function(v) { // will return formatted value according to locale settings }
|
|
629
|
+
* @example
|
|
630
|
+
* numberFormat: function(v) {
|
|
631
|
+
* return v.toLocaleString();
|
|
632
|
+
* }
|
|
633
|
+
*/
|
|
634
|
+
numberFormat: function(v) {
|
|
635
|
+
return v.toLocaleString();
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
//#endregion
|
|
641
|
+
//#region src/Plugin/tableview/index.ts
|
|
754
642
|
/**
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
643
|
+
* Copyright (c) 2021 ~ present NAVER Corp.
|
|
644
|
+
* billboard.js project is licensed under the MIT license
|
|
645
|
+
*/
|
|
758
646
|
/**
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
* @example
|
|
792
|
-
* import {bb} from "billboard.js";
|
|
793
|
-
* import TableView from "billboard.js/dist/billboardjs-plugin-tableview";
|
|
794
|
-
*
|
|
795
|
-
* bb.generate({
|
|
796
|
-
* ...
|
|
797
|
-
* plugins: [
|
|
798
|
-
* new TableView({ ... })
|
|
799
|
-
* ]
|
|
800
|
-
* })
|
|
801
|
-
*/
|
|
802
|
-
class TableView extends Plugin {
|
|
803
|
-
element;
|
|
804
|
-
constructor(options) {
|
|
805
|
-
super(options);
|
|
806
|
-
this.config = new Options();
|
|
807
|
-
return this;
|
|
808
|
-
}
|
|
809
|
-
$beforeInit() {
|
|
810
|
-
this.loadConfig();
|
|
811
|
-
}
|
|
812
|
-
$init() {
|
|
813
|
-
const { class: className, selector, style } = this.config;
|
|
814
|
-
let element = document.querySelector(selector || `.${className || defaultStyle.class}`);
|
|
815
|
-
if (!element) {
|
|
816
|
-
const chart = this.$$.$el.chart.node();
|
|
817
|
-
element = document.createElement("table");
|
|
818
|
-
chart.parentNode.insertBefore(element, chart.nextSibling);
|
|
819
|
-
}
|
|
820
|
-
if (element.tagName !== "TABLE") {
|
|
821
|
-
const table = document.createElement("table");
|
|
822
|
-
element.appendChild(table);
|
|
823
|
-
element = table;
|
|
824
|
-
}
|
|
825
|
-
// append default css style
|
|
826
|
-
if (style && !document.getElementById(defaultStyle.id)) {
|
|
827
|
-
const s = document.createElement("style");
|
|
828
|
-
s.id = defaultStyle.id;
|
|
829
|
-
s.innerHTML = defaultStyle.rule;
|
|
830
|
-
(document.head || document.getElementsByTagName("head")[0])
|
|
831
|
-
.appendChild(s);
|
|
832
|
-
}
|
|
833
|
-
element.classList.add(...[style && defaultStyle.class, className].filter(Boolean));
|
|
834
|
-
this.element = element;
|
|
835
|
-
}
|
|
836
|
-
/**
|
|
837
|
-
* Generate table
|
|
838
|
-
* @private
|
|
839
|
-
*/
|
|
840
|
-
generateTable() {
|
|
841
|
-
const { $$, config, element } = this;
|
|
842
|
-
const dataToShow = $$.filterTargetsToShow($$.data.targets);
|
|
843
|
-
let thead = tplProcess(tpl.thead, {
|
|
844
|
-
title: dataToShow.length ? this.config.categoryTitle : ""
|
|
845
|
-
});
|
|
846
|
-
let tbody = "";
|
|
847
|
-
const rows = [];
|
|
848
|
-
dataToShow.forEach(v => {
|
|
849
|
-
thead += tplProcess(tpl.thead, { title: v.id });
|
|
850
|
-
// make up value rows
|
|
851
|
-
v.values.forEach((d, i) => {
|
|
852
|
-
if (!rows[i]) {
|
|
853
|
-
rows[i] = [d.x];
|
|
854
|
-
}
|
|
855
|
-
rows[i].push(d.value);
|
|
856
|
-
});
|
|
857
|
-
});
|
|
858
|
-
rows.forEach(v => {
|
|
859
|
-
tbody += `<tr>${v.map((d, i) => tplProcess(i ? tpl.tbody : tpl.tbodyHeader, {
|
|
860
|
-
value: i === 0 ?
|
|
861
|
-
config.categoryFormat.bind(this)(d) :
|
|
862
|
-
(isNumber(d) ? config.numberFormat.bind(this)(d) : config.nullString)
|
|
863
|
-
})).join("")}</tr>`;
|
|
864
|
-
});
|
|
865
|
-
element.innerHTML = tplProcess(tpl.body, {
|
|
866
|
-
...config,
|
|
867
|
-
title: config.title || $$.config.title_text || "",
|
|
868
|
-
thead,
|
|
869
|
-
tbody
|
|
870
|
-
});
|
|
871
|
-
}
|
|
872
|
-
$redraw() {
|
|
873
|
-
const { state } = this.$$;
|
|
874
|
-
const doNotUpdate = state.resizing || (!this.config.updateOnToggle && state.toggling);
|
|
875
|
-
!doNotUpdate && this.generateTable();
|
|
876
|
-
}
|
|
877
|
-
$willDestroy() {
|
|
878
|
-
this.element.parentNode?.removeChild(this.element);
|
|
879
|
-
// remove default css style when left one chart instance
|
|
880
|
-
if (this.$$.charts.length === 1) {
|
|
881
|
-
const s = document.getElementById(defaultStyle.id);
|
|
882
|
-
s?.parentNode?.removeChild(s);
|
|
883
|
-
}
|
|
884
|
-
}
|
|
647
|
+
* Expected value type for each supported option, used to validate user input.
|
|
648
|
+
* Acts as the single source of truth for the set of allowed option keys.
|
|
649
|
+
* @private
|
|
650
|
+
*/
|
|
651
|
+
const optionValidators = {
|
|
652
|
+
selector: isString,
|
|
653
|
+
categoryTitle: isString,
|
|
654
|
+
categoryFormat: isFunction,
|
|
655
|
+
class: isString,
|
|
656
|
+
style: isBoolean,
|
|
657
|
+
title: isString,
|
|
658
|
+
updateOnToggle: isBoolean,
|
|
659
|
+
nullString: isString,
|
|
660
|
+
numberFormat: isFunction
|
|
661
|
+
};
|
|
662
|
+
/**
|
|
663
|
+
* Check whether the given value is a valid TableView options object.
|
|
664
|
+
* An empty object is valid (every option falls back to its default) and
|
|
665
|
+
* unknown keys are ignored, so only a known option holding a value of the
|
|
666
|
+
* wrong type makes the object invalid. An explicit `undefined` value is
|
|
667
|
+
* allowed so a consumer can opt out of an option and let it fall back to
|
|
668
|
+
* the default.
|
|
669
|
+
* @param {unknown} options Value given to the TableView constructor
|
|
670
|
+
* @returns {boolean} `true` when `options` is a valid TableView options object
|
|
671
|
+
* @private
|
|
672
|
+
*/
|
|
673
|
+
function isValidTableViewOptions(options) {
|
|
674
|
+
if (!isObjectType(options) || isArray(options) || options === null) return false;
|
|
675
|
+
return Object.entries(options).every(([key, value]) => {
|
|
676
|
+
const validate = optionValidators[key];
|
|
677
|
+
return !isFunction(validate) || value === void 0 || validate(value);
|
|
678
|
+
});
|
|
885
679
|
}
|
|
886
|
-
|
|
887
|
-
|
|
680
|
+
/**
|
|
681
|
+
* Table view plugin.<br>
|
|
682
|
+
* Generates table view for bound dataset.
|
|
683
|
+
* - **NOTE:**
|
|
684
|
+
* - Plugins aren't built-in. Need to be loaded or imported to be used.
|
|
685
|
+
* - Non required modules from billboard.js core, need to be installed separately.
|
|
686
|
+
* @class plugin-tableview
|
|
687
|
+
* @param {object} options table view plugin options
|
|
688
|
+
* @augments Plugin
|
|
689
|
+
* @returns {TableView}
|
|
690
|
+
* @example
|
|
691
|
+
* // Plugin must be loaded before the use.
|
|
692
|
+
* <script src="$YOUR_PATH/plugin/billboardjs-plugin-tableview.js"><\/script>
|
|
693
|
+
*
|
|
694
|
+
* var chart = bb.generate({
|
|
695
|
+
* ...
|
|
696
|
+
* plugins: [
|
|
697
|
+
* new bb.plugin.tableview({
|
|
698
|
+
* selector: "#my-table-view",
|
|
699
|
+
* categoryTitle: "Category",
|
|
700
|
+
* categoryFormat: function(v) {
|
|
701
|
+
* // do some transformation
|
|
702
|
+
* ...
|
|
703
|
+
* return v;
|
|
704
|
+
* },
|
|
705
|
+
* class: "my-class-name",
|
|
706
|
+
* style: true,
|
|
707
|
+
* title: "My Data List",
|
|
708
|
+
* updateOnToggle: false,
|
|
709
|
+
* nullString: "N/A",
|
|
710
|
+
* numberFormat: function(v) {
|
|
711
|
+
* // do some transformation like number formatting
|
|
712
|
+
* // return typeof v === "number" ? v.toFixed(2) : v;
|
|
713
|
+
* // or use d3.format
|
|
714
|
+
* // return typeof v === "number" ? d3.format(".2f")(v) : v;
|
|
715
|
+
* // or use Intl.NumberFormat
|
|
716
|
+
* // return typeof v === "number" ? new Intl.NumberFormat("en-US", {minimumFractionDigits: 2, maximumFractionDigits: 2}).format(v) : v;
|
|
717
|
+
* // or any other number formatting library
|
|
718
|
+
* ...
|
|
719
|
+
* return v;
|
|
720
|
+
* }
|
|
721
|
+
* }),
|
|
722
|
+
* ]
|
|
723
|
+
* });
|
|
724
|
+
* @example
|
|
725
|
+
* import {bb} from "billboard.js";
|
|
726
|
+
* import TableView from "billboard.js/dist/billboardjs-plugin-tableview";
|
|
727
|
+
*
|
|
728
|
+
* bb.generate({
|
|
729
|
+
* ...
|
|
730
|
+
* plugins: [
|
|
731
|
+
* new TableView({ ... })
|
|
732
|
+
* ]
|
|
733
|
+
* })
|
|
734
|
+
*/
|
|
735
|
+
var TableView = class extends Plugin {
|
|
736
|
+
constructor(options = {}) {
|
|
737
|
+
super(options);
|
|
738
|
+
if (!isValidTableViewOptions(options)) console?.error?.("[billboard.js] TableView plugin received invalid options; unsupported values will be ignored.", options);
|
|
739
|
+
this.config = new Options();
|
|
740
|
+
return this;
|
|
741
|
+
}
|
|
742
|
+
$beforeInit() {
|
|
743
|
+
this.loadConfig();
|
|
744
|
+
}
|
|
745
|
+
$init() {
|
|
746
|
+
const { class: className, selector, style } = this.config;
|
|
747
|
+
let element = document.querySelector(selector || `.${className || defaultStyle.class}`);
|
|
748
|
+
if (!element) {
|
|
749
|
+
const chart = this.$$.$el.chart.node();
|
|
750
|
+
element = document.createElement("table");
|
|
751
|
+
chart.parentNode.insertBefore(element, chart.nextSibling);
|
|
752
|
+
}
|
|
753
|
+
if (element.tagName !== "TABLE") {
|
|
754
|
+
const table = document.createElement("table");
|
|
755
|
+
element.appendChild(table);
|
|
756
|
+
element = table;
|
|
757
|
+
}
|
|
758
|
+
if (style && !document.getElementById(defaultStyle.id)) {
|
|
759
|
+
const s = document.createElement("style");
|
|
760
|
+
s.id = defaultStyle.id;
|
|
761
|
+
s.innerHTML = defaultStyle.rule;
|
|
762
|
+
(document.head || document.getElementsByTagName("head")[0]).appendChild(s);
|
|
763
|
+
}
|
|
764
|
+
element.classList.add(...[style && defaultStyle.class, className].filter(Boolean));
|
|
765
|
+
this.element = element;
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Generate table
|
|
769
|
+
* @private
|
|
770
|
+
*/
|
|
771
|
+
generateTable() {
|
|
772
|
+
const { $$, config, element } = this;
|
|
773
|
+
const dataToShow = $$.filterTargetsToShow($$.data.targets);
|
|
774
|
+
let thead = tplProcess(tpl.thead, { title: dataToShow.length ? this.config.categoryTitle : "" });
|
|
775
|
+
let tbody = "";
|
|
776
|
+
const rows = [];
|
|
777
|
+
dataToShow.forEach((v) => {
|
|
778
|
+
thead += tplProcess(tpl.thead, { title: v.id });
|
|
779
|
+
v.values.forEach((d, i) => {
|
|
780
|
+
if (!rows[i]) rows[i] = [d.x];
|
|
781
|
+
rows[i].push(d.value);
|
|
782
|
+
});
|
|
783
|
+
});
|
|
784
|
+
rows.forEach((v) => {
|
|
785
|
+
tbody += `<tr>${v.map((d, i) => tplProcess(i ? tpl.tbody : tpl.tbodyHeader, { value: i === 0 ? config.categoryFormat.bind(this)(d) : isNumber(d) ? config.numberFormat.bind(this)(d) : config.nullString })).join("")}</tr>`;
|
|
786
|
+
});
|
|
787
|
+
element.innerHTML = tplProcess(tpl.body, {
|
|
788
|
+
...config,
|
|
789
|
+
title: config.title || $$.config.title_text || "",
|
|
790
|
+
thead,
|
|
791
|
+
tbody
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
$redraw() {
|
|
795
|
+
const { state } = this.$$;
|
|
796
|
+
!(state.resizing || !this.config.updateOnToggle && state.toggling) && this.generateTable();
|
|
797
|
+
}
|
|
798
|
+
$willDestroy() {
|
|
799
|
+
this.element.parentNode?.removeChild(this.element);
|
|
800
|
+
if (this.$$.charts.length === 1) {
|
|
801
|
+
const s = document.getElementById(defaultStyle.id);
|
|
802
|
+
s?.parentNode?.removeChild(s);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
//#endregion
|
|
807
|
+
export { TableView as default, isValidTableViewOptions };
|