@prunellejs/icons 0.7.0 → 0.7.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/svg-dir.ts +58 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/icons",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Compile-time Iconify SVG icon plugin (~icons/<set>/<name>) for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,6 +24,6 @@
24
24
  "directory": "packages/plugin-icons"
25
25
  },
26
26
  "peerDependencies": {
27
- "prunelle": "^0.7.0"
27
+ "prunelle": "^0.7.1"
28
28
  }
29
29
  }
package/src/svg-dir.ts CHANGED
@@ -4,8 +4,10 @@ import type { IconifyIcon, IconSet } from './types'
4
4
  import { existsSync } from 'node:fs'
5
5
  import { join } from 'node:path'
6
6
 
7
- const SVG_RE = /<svg\b([^>]*)>([\s\S]*)<\/svg\s*>/i
7
+ const SVG_RE = /<svg\b((?:[^>"']|"[^"]*"|'[^']*')*)>([\s\S]*)<\/svg\s*>/i
8
8
  const ATTR_RE = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g
9
+ const TAG_RE = /<[a-z][\w:-]*(?:[^>"']|"[^"]*"|'[^']*')*>/gi
10
+ const TAG_ATTR_RE = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+))/g
9
11
  const COMMENT_RE = /<!--[\s\S]*?-->/g
10
12
  const NUMBER_RE = /^\s*(-?[\d.]+)(?:px)?\s*$/
11
13
  const BOX_SEPARATOR_RE = /[\s,]+/
@@ -21,8 +23,21 @@ export interface SvgDirSet {
21
23
  }
22
24
 
23
25
  const SCRIPT_TAG_RE = /<script\b/i
24
- const EVENT_ATTR_RE = /<[^>]*\son[a-z]+\s*=/i
25
- const SCRIPT_URL_RE = /javascript\s*:/i
26
+ const EVENT_ATTR_RE = /(?:[\s/"'])on[a-z]+\s*=/i
27
+ const HTML_ENTITY_RE = /&(?:#x([0-9a-f]+);?|#(\d+);?|([a-z]+);)/gi
28
+ const JAVASCRIPT_URL_RE = /(?:^|;)javascript:/i
29
+ const VBSCRIPT_URL_RE = /(?:^|;)vbscript:/i
30
+ const DATA_HTML_URL_RE = /(?:^|;)data:text\/html/i
31
+ const NAMED_ENTITIES: Record<string, string> = {
32
+ colon: ':',
33
+ tab: '\t',
34
+ newline: '\n',
35
+ amp: '&',
36
+ lt: '<',
37
+ gt: '>',
38
+ quot: '"',
39
+ apos: '\'',
40
+ }
26
41
 
27
42
  function attributes(source: string): Map<string, string> {
28
43
  const found = new Map<string, string>()
@@ -36,15 +51,51 @@ function number(value: string | undefined): number | undefined {
36
51
  return match === null ? undefined : Number(match[1])
37
52
  }
38
53
 
54
+ function decodeHtmlEntities(value: string): string {
55
+ return value.replace(HTML_ENTITY_RE, (full, hex?: string, dec?: string, named?: string) => {
56
+ if (hex !== undefined || dec !== undefined) {
57
+ const code = hex !== undefined ? Number.parseInt(hex, 16) : Number.parseInt(dec!, 10)
58
+ return Number.isFinite(code) && code >= 0 && code <= 0x10FFFF ? String.fromCodePoint(code) : ''
59
+ }
60
+ return NAMED_ENTITIES[named!.toLowerCase()] ?? full
61
+ })
62
+ }
63
+
64
+ function stripAsciiControlAndSpace(value: string): string {
65
+ let out = ''
66
+ for (let i = 0; i < value.length; i++) {
67
+ if (value.charCodeAt(i) > 0x20)
68
+ out += value[i]
69
+ }
70
+ return out
71
+ }
72
+
73
+ function urlHazard(rawValue: string): string | undefined {
74
+ const normalized = stripAsciiControlAndSpace(decodeHtmlEntities(rawValue))
75
+ if (JAVASCRIPT_URL_RE.test(normalized))
76
+ return 'a javascript: URL'
77
+ if (VBSCRIPT_URL_RE.test(normalized))
78
+ return 'a vbscript: URL'
79
+ if (DATA_HTML_URL_RE.test(normalized))
80
+ return 'a data:text/html URL'
81
+ return undefined
82
+ }
83
+
39
84
  /** What an SVG file's source holds that runs script once the icon is inlined in a page, undefined when it holds none. */
40
85
  export function svgHazard(source: string): string | undefined {
41
86
  const text = source.replace(COMMENT_RE, '')
42
87
  if (SCRIPT_TAG_RE.test(text))
43
88
  return 'a <script> element'
44
- if (EVENT_ATTR_RE.test(text))
45
- return 'an on* event attribute'
46
- if (SCRIPT_URL_RE.test(text))
47
- return 'a javascript: URL'
89
+ for (const [tag] of text.matchAll(TAG_RE)) {
90
+ const inner = tag.slice(1, -1)
91
+ if (EVENT_ATTR_RE.test(inner))
92
+ return 'an on* event attribute'
93
+ for (const [, , double, single, unquoted] of inner.matchAll(TAG_ATTR_RE)) {
94
+ const hazard = urlHazard(double ?? single ?? unquoted ?? '')
95
+ if (hazard !== undefined)
96
+ return hazard
97
+ }
98
+ }
48
99
  return undefined
49
100
  }
50
101