-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
1193 lines (1093 loc) · 27.1 KB
/
index.js
File metadata and controls
1193 lines (1093 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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
754
755
756
757
758
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { join, resolve, sep: separator } = require("path")
const { readFileSync, realpathSync, lstatSync } = require("fs")
const csstree = require("css-tree")
const { createHash } = require("./utilities/hash")
class TranslationError extends Error {
constructor(message) {
super(message)
this.name = "TranslationError"
Error.captureStackTrace(this, this.constructor)
}
}
class FileError extends Error {
constructor(message) {
super(message)
this.name = "FileError"
Error.captureStackTrace(this, this.constructor)
}
}
class RawError extends Error {
constructor(message) {
super(message)
this.name = "RawError"
Error.captureStackTrace(this, this.constructor)
}
}
class CSSError extends Error {
constructor(message) {
super(message)
this.name = "CSSError"
Error.captureStackTrace(this, this.constructor)
}
}
class ImageError extends Error {
constructor(message) {
super(message)
this.name = "ImageError"
Error.captureStackTrace(this, this.constructor)
}
}
class SVGError extends Error {
constructor(message) {
super(message)
this.name = "SVGError"
Error.captureStackTrace(this, this.constructor)
}
}
class JSONError extends Error {
constructor(message) {
super(message)
this.name = "JSONError"
Error.captureStackTrace(this, this.constructor)
}
}
class ComponentError extends Error {
constructor(message) {
super(message)
this.name = "ComponentError"
Error.captureStackTrace(this, this.constructor)
}
}
function compile(path) {
const fn = require(path)
return {
template(options) {
const nonce = options && options.nonce
const tree = fn(...arguments)
const nodes = {}
const styles = []
const scripts = {
head: [],
body: [],
}
const walk = (node) => {
if (!node) {
return
}
if (node.name === "head") {
nodes.head = node
}
if (node.name === "body") {
nodes.body = node
}
if (node.name === "style") {
const css = node.children
if (!styles.includes(css)) {
styles.push(css)
}
node.ignore = true
}
if (node.name === "script") {
const attributes = node.attributes || {}
if (
attributes.src ||
["application/json", "application/ld+json"].includes(
attributes.type,
)
) {
node.ignore = false
return
} else {
const script = node.children
if (script) {
if (attributes.target === "head") {
if (!scripts.head.includes(script)) {
scripts.head.push(script)
}
} else {
if (!scripts.body.includes(script)) {
scripts.body.push(script)
}
}
}
node.ignore = true
}
}
if (Array.isArray(node)) {
node.forEach(walk)
} else if (Array.isArray(node.children)) {
node.children.forEach(walk)
}
}
walk(tree)
if (nodes.head) {
if (styles.length > 0) {
const styleNode = {
name: "style",
children: styles.join(""),
}
if (nonce) {
styleNode.attributes = { nonce }
}
nodes.head.children.push(styleNode)
}
if (scripts.head.length > 0) {
const scriptNode = {
name: "script",
children: scripts.head.join(""),
}
if (nonce) {
scriptNode.attributes = { nonce }
}
nodes.head.children.push(scriptNode)
}
}
if (nodes.body) {
if (scripts.body.length > 0) {
const scriptNode = {
name: "script",
children: scripts.body.join(""),
}
if (nonce) {
scriptNode.attributes = { nonce }
}
nodes.body.children.push(scriptNode)
}
}
return render(tree)
},
}
}
const escapeHTML = (string) => {
// Convert to string to handle non-string inputs safely
string = String(string)
// Fast path: if no special characters, return as-is
if (
!string.includes("&") &&
!string.includes("<") &&
!string.includes(">") &&
!string.includes("'") &&
!string.includes('"')
) {
return string
}
const len = string.length
let result = ""
let lastIndex = 0
for (let i = 0; i < len; i++) {
const char = string[i]
let replacement
switch (char) {
case "&":
replacement = "&"
break
case "<":
replacement = "<"
break
case ">":
replacement = ">"
break
case "'":
replacement = "'"
break
case '"':
replacement = """
break
default:
continue
}
if (lastIndex !== i) {
result += string.slice(lastIndex, i)
}
result += replacement
lastIndex = i + 1
}
if (lastIndex !== len) {
result += string.slice(lastIndex)
}
return result
}
const normalizePath = (path) => path.replace(/\\/g, "/").replace(/\/+$/, "")
const ALLOWED_RAW_EXTENSIONS = ["html", "txt"]
const ALLOWED_CODE_EXTENSIONS = ["js", "css", "json"]
const ALLOWED_IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "gif", "webp", "svg"]
const ALLOWED_READ_EXTENSIONS = [
...ALLOWED_RAW_EXTENSIONS,
...ALLOWED_IMAGE_EXTENSIONS,
...ALLOWED_CODE_EXTENSIONS,
]
function validateSymlinks(path, base) {
let relative = path.slice(base.length + 1).split(separator)
let current = base
for (const part of relative) {
if (!part) continue
current = resolve(current, part)
if (lstatSync(current).isSymbolicLink()) {
throw new FileError(`symlinks are not allowed ("${current}")`)
}
}
}
function validateFile(path, base) {
const normalizedPath = normalizePath(path)
const normalizedBase = normalizePath(base)
const type = extension(normalizedPath)
if (!type) {
throw new FileError(`path "${path}" has no extension`)
}
if (!ALLOWED_READ_EXTENSIONS.includes(type)) {
throw new FileError(`unsupported file type "${type}" for path "${path}"`)
}
const stats = lstatSync(normalizedPath)
if (!stats.isFile()) {
throw new FileError(`path "${path}" is not a file`)
}
if (stats.isSymbolicLink()) {
throw new FileError(`path "${path}" is a symbolic link`)
}
if (normalizedPath === normalizedBase) {
throw new FileError(
`path "${path}" is the same as the current working directory "${base}"`,
)
}
if (!normalizedPath.startsWith(normalizedBase + "/")) {
throw new FileError(
`real path "${normalizedPath}" is not within the current working directory "${normalizedBase}"`,
)
}
}
function readFile(path, encoding) {
try {
const base = process.cwd()
const absoluteBase = resolve(base)
const absolutePath = resolve(path)
const realBase = realpathSync(absoluteBase)
const realPath = realpathSync(absolutePath)
validateSymlinks(realPath, realBase)
validateFile(realPath, realBase)
return readFileSync(path, encoding)
} catch (exception) {
throw new FileError(`cannot read file "${path}": ${exception.message}`)
}
}
const BOOLEAN_ATTRIBUTES = new Set([
"async",
"autofocus",
"autoplay",
"border",
"challenge",
"checked",
"compact",
"contenteditable",
"controls",
"default",
"defer",
"disabled",
"formnovalidate",
"frameborder",
"hidden",
"indeterminate",
"ismap",
"loop",
"multiple",
"muted",
"nohref",
"noresize",
"noshade",
"novalidate",
"nowrap",
"open",
"readonly",
"required",
"reversed",
"scoped",
"scrolling",
"seamless",
"selected",
"sortable",
"spellcheck",
"translate",
])
const ALIASES = {
className: "class",
htmlFor: "for",
}
// Pre-compiled regex for better performance
const KEY_VALIDATION_REGEX = /^[a-zA-Z0-9\-_:]+$/
const isKeyValid = (key) => KEY_VALIDATION_REGEX.test(key)
const attributes = (options) => {
if (!options) {
return ""
}
const result = []
for (const key in options) {
if (!isKeyValid(key)) {
continue
}
const value = options[key]
if (
typeof value === "string" ||
typeof value === "number" ||
value === true ||
Array.isArray(value)
) {
if (BOOLEAN_ATTRIBUTES.has(key)) {
result.push(key)
} else {
const name = ALIASES[key] || key
const content = Array.isArray(value) ? classes(...value) : value
result.push(`${name}="${escapeHTML(content)}"`)
}
} else if (key === "style" && typeof value === "object") {
const styles = []
for (const param in value) {
if (!isKeyValid(param)) {
continue
}
const result = value[param]
if (
(param === "padding" || param === "margin") &&
typeof result === "object"
) {
const top = result.top || "0"
const right = result.right || "0"
const bottom = result.bottom || "0"
const left = result.left || "0"
styles.push(
`${decamelize(param)}:${escapeHTML(
`${top} ${right} ${bottom} ${left}`,
)}`,
)
} else if (typeof result === "string" || typeof result === "number") {
styles.push(`${decamelize(param)}:${escapeHTML(result)}`)
}
}
if (styles.length > 0) {
result.push(`style="${styles.join(";")}"`)
}
}
}
return result.join(" ")
}
const SELF_CLOSING_TAGS = new Set([
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
"!DOCTYPE html",
])
const UNESCAPED_TAGS = new Set(["script", "style", "template"])
const render = (input, escape = true) => {
// Most common case: string (~50% of nodes)
if (typeof input === "string") {
return escape ? escapeHTML(input) : input
}
// Second most common: arrays (~20% of nodes)
if (Array.isArray(input)) {
let result = ""
for (let i = 0, ilen = input.length; i < ilen; i++) {
result += render(input[i], escape)
}
return result
}
// Early exit for null/undefined/false/true
if (
input === null ||
input === undefined ||
input === false ||
input === true
) {
return ""
}
// Objects (elements) - check ignore flag first
if (input.ignore) {
return ""
}
if (input.name === "raw") {
return render(input.children, false)
}
if (SELF_CLOSING_TAGS.has(input.name)) {
const attrs = input.attributes ? attributes(input.attributes) : ""
return attrs ? `<${input.name} ${attrs}>` : `<${input.name}>`
}
if (input.name) {
const attrs = input.attributes ? attributes(input.attributes) : ""
const children = render(input.children, !UNESCAPED_TAGS.has(input.name))
return attrs
? `<${input.name} ${attrs}>${children}</${input.name}>`
: `<${input.name}>${children}</${input.name}>`
}
if (typeof input === "number") {
return input.toString()
}
if (typeof input === "object" && input instanceof Date) {
return input.toString()
}
return ""
}
const raw = (children) => {
return { name: "raw", children }
}
/**
* Never trust HTML files from untrusted sources.
*
* This function is a basic sanitization of HTML content in case
* you've accidentally included a "trusted", but malicious HTML file that was downloaded
* from the internet or other untrusted sources.
*
* This function removes script and style tags, inline event handlers,
* and any href attributes that use JavaScript. It does not
* guarantee complete security, but it helps to mitigate some common
* XSS attacks that can be embedded in HTML files.
*
* It is recommended to check all HTML files before using them
* in your application.
*
* Never trust user-generated content.
*/
const sanitizeHTML = (content) => {
return content
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "")
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "")
.replace(/\son\w+="[^"]*"/gi, "")
.replace(/\son\w+='[^']*'/gi, "")
.replace(/(href|xlink:href)\s*=\s*(['"])javascript:[^'"]*\2/gi, "")
}
/*
* Raw content is a special case where we want to allow
* unescaped HTML content to be rendered directly.
* This is useful for cases where we want to
* include HTML fragments or templates that are
* not meant to be escaped, like large blocks of HTML,
* or when integrating with third-party libraries
* that require raw HTML.
*
* Please note that this should be used with caution,
* as it can lead to XSS vulnerabilities if the content
* is not properly sanitized.
*
* It should only be used for trusted content
* or in controlled environments.
*
* Should not be used for user-generated content.
*/
raw.load = function (path, options = {}) {
const type = extension(path)
if (!ALLOWED_RAW_EXTENSIONS.includes(type)) {
throw new RawError(`unsupported raw type "${type}" for path "${path}"`)
}
let content = readFile(path, "utf8")
if (type === "html" && options.sanitize !== false) {
content = sanitizeHTML(content)
} else if (type === "txt" && options.escape !== false) {
content = escapeHTML(content)
}
return raw(content)
}
const tag = (tagName, attrsOrChildren, ...restChildren) => {
// Check if second argument is children (not attributes)
const isChildrenNotAttributes =
typeof attrsOrChildren === "string" ||
typeof attrsOrChildren === "number" ||
Array.isArray(attrsOrChildren) ||
(attrsOrChildren &&
typeof attrsOrChildren === "object" &&
"name" in attrsOrChildren &&
"children" in attrsOrChildren)
// If we have rest arguments, they must be additional children
if (restChildren.length > 0) {
if (isChildrenNotAttributes) {
// tagName is name, attrsOrChildren is first child, restChildren are more children
return {
name: tagName,
children: [attrsOrChildren, ...restChildren],
}
} else {
// tagName is name, attrsOrChildren is attributes, restChildren are children
return {
name: tagName,
attributes: attrsOrChildren,
children: restChildren,
}
}
}
// Original two-argument logic
if (isChildrenNotAttributes) {
return {
name: tagName,
children: attrsOrChildren,
}
}
// attrsOrChildren is attributes, no children provided
return {
name: tagName,
children: [],
attributes: attrsOrChildren,
}
}
function decamelize(string) {
return string.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()
}
function stylesheet(input) {
const object = { ...input }
function render(object, selector = "") {
let result = []
for (const key in object) {
const value = object[key]
if (value && typeof value === "object") {
if (key.startsWith("@")) {
result.push(`${key}{${render(value, selector)}}`)
} else {
const nextSelector = selector ? `${selector} ${key}` : key
result.push(render(value, nextSelector))
}
} else {
if (selector) {
result.push(`${selector}{${decamelize(key)}:${value};}`)
} else {
result.push(`${decamelize(key)}:${value};`)
}
}
}
return result.join("")
}
return {
add(item) {
for (const key in item) {
object[key] = item[key]
}
},
set(key, value) {
object[key] = value
},
toString() {
return render(object)
},
}
}
function css(inputs) {
let result = ""
for (let i = 0, ilen = inputs.length; i < ilen; i += 1) {
const input = inputs[i]
const value = arguments[i + 1]
if (value) {
result += input + value
} else {
result += input
}
}
const tree = csstree.parse(result)
const classes = {}
csstree.walk(tree, (node) => {
if (node.type === "ClassSelector") {
const hash = createHash(result + node.name)
const name = hash
classes[node.name] = name
node.name = name
}
})
return {
...classes,
css: tag("style", csstree.generate(tree)),
}
}
function occurrences(input, string) {
if (string.length <= 0) {
return input.length + 1
}
let count = 0
let position = 0
const step = string.length
while (true) {
position = input.indexOf(string, position)
if (position >= 0) {
count += 1
position += step
} else {
break
}
}
return count
}
const validateCSS = (content, character1, character2) => {
const count1 = occurrences(content, character1)
const count2 = occurrences(content, character2)
if (count1 !== count2) {
return {
valid: false,
message: `Mismatched count of ${character1} and ${character2}`,
}
}
return { valid: true }
}
const CSS_PAIRS = [
["{", "}"],
["(", ")"],
["[", "]"],
]
function isCSSValid(content) {
for (const [left, right] of CSS_PAIRS) {
const { valid, message } = validateCSS(content, left, right)
if (!valid) {
return { valid, message: message }
}
}
return { valid: true }
}
css.load = function (path) {
const file = path.endsWith(".css") ? path : join(path, "index.css")
const content = readFile(file, "utf8")
const { valid, message } = isCSSValid(content)
if (!valid) {
throw new CSSError(`invalid CSS for path "${file}": ${message}`)
}
return css`
${content}
`
}
css.create = function (object) {
return stylesheet(object)
}
css.inline = function (object) {
return stylesheet(object).toString()
}
function js(inputs) {
let result = ""
for (let i = 0, ilen = inputs.length; i < ilen; i += 1) {
const input = inputs[i]
const value = arguments[i + 1]
if (value) {
result += input + value
} else {
result += input
}
}
return {
js: tag("script", result),
}
}
/*
* Load a JavaScript file and return a script tag.
*
* Please note that this should be used with caution,
* as it can lead to XSS vulnerabilities if the content
* is not properly sanitized.
*
* It should only be used for trusted content
* or in controlled environments.
*
* Should not be used for user-generated content.
*/
js.load = function (path, options = {}) {
const file = path.endsWith(".js") ? path : join(path, "index.js")
const content = readFile(file, "utf8")
const attributes = options.target ? { target: options.target } : {}
if (options && options.transform) {
return {
js: tag("script", attributes, options.transform(content)),
}
}
return { js: tag("script", attributes, content) }
}
const node =
(name) =>
(options, ...children) =>
tag(name, options, ...children)
const Doctype = node("!DOCTYPE html")
const nodes = [
"a",
"abbr",
"address",
"animate",
"animateMotion",
"animateTransform",
"area",
"article",
"aside",
"audio",
"b",
"base",
"bdi",
"bdo",
"blockquote",
"body",
"br",
"button",
"canvas",
"caption",
"circle",
"cite",
"clipPath",
"code",
"col",
"colgroup",
"data",
"datalist",
"dd",
"defs",
"del",
"desc",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"ellipse",
"embed",
"fieldset",
"figcaption",
"figure",
"filter",
"footer",
"foreignObject",
"form",
"g",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"hr",
"html",
"i",
"iframe",
"image",
"img",
"input",
"ins",
"kbd",
"label",
"legend",
"li",
"line",
"linearGradient",
"link",
"main",
"map",
"mark",
"marker",
"mask",
"menu",
"meta",
"metadata",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"path",
"pattern",
"picture",
"polygon",
"polyline",
"pre",
"progress",
"q",
"radialGradient",
"rect",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"set",
"slot",
"small",
"source",
"span",
"stop",
"strong",
"style",
"sub",
"summary",
"sup",
"svg",
"switch",
"symbol",
"table",
"tbody",
"td",
"template",
"text",
"textarea",
"textPath",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"tspan",
"u",
"ul",
"use",
"var",
"video",
"view",
"wbr",
].reduce((result, name) => {
const pascalName = name.charAt(0).toUpperCase() + name.slice(1)
result[pascalName] = node(name)
return result
}, {})
function extension(path) {
const parts = path.split(".")
return parts[parts.length - 1].toLowerCase()
}
function media(path) {
const type = extension(path)
if (type === "svg") {
return "image/svg+xml"
}
return `image/${type === "jpg" ? "jpeg" : type}`
}
function base64({ content, path }) {
return `data:${media(path)};base64,${content}`
}
nodes.Img.load = function (path) {
const type = extension(path)
if (!ALLOWED_IMAGE_EXTENSIONS.includes(type)) {
throw new ImageError(`unsupported image type "${type}" for path "${path}"`)
}
const content = readFile(path, "base64")
return (options) => {
return nodes.Img({ src: base64({ content, path }), ...options })
}
}
/*
Never trust SVG files from untrusted sources.
This function is a basic sanitization of SVG content in case
you've accidentally included a "trusted", but malicious SVG file that was downloaded
from the internet or other untrusted sources.
This function removes script and style tags, inline event handlers,
and any href attributes that use JavaScript. It does not
guarantee complete security, but it helps to mitigate some common
XSS attacks that can be embedded in SVG files.
It is recommended to check all SVG files before using them
in your application.
*/
const sanitizeSVG = (content) => {
return content
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "")
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "")
.replace(/\son\w+="[^"]*"/gi, "")
.replace(/\son\w+='[^']*'/gi, "")
.replace(/(href|xlink:href)\s*=\s*(['"])javascript:[^'"]*\2/gi, "")
}
/*
* SVG files are a special case where we want to allow
* unescaped SVG content to be rendered directly.
* This is useful for cases where we want to
* include SVG fragments or templates that are
* not meant to be escaped, like large blocks of SVG,
* or when integrating with third-party libraries
* that require raw SVG.
*
* Please note that this should be used with caution,