Skip to content
Myr

utils.ts

This file

Path

src/jsx/utils.ts

202 lines · 5.6 KiB · TypeScript

Last changed by

fix(jsx): add px to numeric gridGap, gridRowGap and gridColumnGap (#5487)Rup Sarmahbefore adoptione5bb206

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–187why, one step back
1const normalizeElementKeyMap: Map<string, string> = new Map([
2 ['className', 'class'],3 ['htmlFor', 'for'],4 ['crossOrigin', 'crossorigin'],5 ['httpEquiv', 'http-equiv'],6 ['itemProp', 'itemprop'],7 ['fetchPriority', 'fetchpriority'],8 ['noModule', 'nomodule'],9 ['formAction', 'formaction'],10])11export const normalizeIntrinsicElementKey = (key: string): string =>12 normalizeElementKeyMap.get(key) || key1314// eslint-disable-next-line no-control-regex15const invalidAttributeNameCharRe = /[\s"'<>/=`\\\x00-\x1f\x7f-\x9f]/16const validAttributeNameCache = new Set<string>()17const validAttributeNameCacheMax = 102418// reject HTML parser-control tag starters ('!' for comments/doctype, '?' for19// processing instructions) in addition to the shared invalid-char set20// eslint-disable-next-line no-control-regex21const invalidTagNameCharRe = /^[!?]|[\s"'<>/=`\\\x00-\x1f\x7f-\x9f]/22const validTagNameCache = new Set<unknown>()23const validTagNameCacheMax = 2562425const cacheValidName = (cache: Set<unknown>, max: number, name: string): void => {26 if (cache.size >= max) {27 cache.clear()28 }29 cache.add(name)30}3132export const isValidTagName = (name: unknown): name is string => {33 if (validTagNameCache.has(name)) {34 return true35 }36 if (typeof name !== 'string') {37 return false38 }39 if (name.length === 0) {40 return true41 }42 if (invalidTagNameCharRe.test(name)) {43 return false44 }45 cacheValidName(validTagNameCache, validTagNameCacheMax, name)46 return true47}4849export const isValidAttributeName = (name: string): boolean => {50 if (validAttributeNameCache.has(name)) {51 return true52 }53 const len = name.length54 if (len === 0) {55 return false56 }57 for (let i = 0; i < len; i++) {58 const c = name.charCodeAt(i)59 if (60 !(61 (c >= 0x61 && c <= 0x7a) || // a-z62 (c >= 0x41 && c <= 0x5a) || // A-Z63 (c >= 0x30 && c <= 0x39) || // 0-964 c === 0x2d || // -65 c === 0x5f || // _66 c === 0x2e || // .67 c === 0x3a // :68 )69 ) {70 // non-fast-path character found — fall back to regex for the full name71 if (!invalidAttributeNameCharRe.test(name)) {72 cacheValidName(validAttributeNameCache, validAttributeNameCacheMax, name)73 return true74 } else {75 return false76 }77 }78 }79 cacheValidName(validAttributeNameCache, validAttributeNameCacheMax, name)80 return true81}8283// eslint-disable-next-line no-control-regex84const invalidStylePropertyNameCharRe = /[\s"'():;\\/\[\]{}\x00-\x1f\x7f-\x9f]/85const validStylePropertyNameCache = new Set<string>()86const validStylePropertyNameCacheMax = 10248788const isValidStylePropertyName = (name: string): boolean => {89 if (validStylePropertyNameCache.has(name)) {90 return true91 }92 const len = name.length93 if (len === 0) {94 return false95 }96 for (let i = 0; i < len; i++) {97 const c = name.charCodeAt(i)98 if (99 !(100 (c >= 0x61 && c <= 0x7a) || // a-z101 (c >= 0x41 && c <= 0x5a) || // A-Z102 (c >= 0x30 && c <= 0x39) || // 0-9103 c === 0x2d || // -104 c === 0x5f // _105 )106 ) {107 // non-fast-path character found — fall back to regex for the full name108 if (!invalidStylePropertyNameCharRe.test(name)) {109 cacheValidName(validStylePropertyNameCache, validStylePropertyNameCacheMax, name)110 return true111 } else {112 return false113 }114 }115 }116 cacheValidName(validStylePropertyNameCache, validStylePropertyNameCacheMax, name)117 return true118}119120const unsafeStyleValueCharRe = /[;"'\\/\[\](){}]/121122const hasUnsafeStyleValue = (value: string): boolean => {123 if (!unsafeStyleValueCharRe.test(value)) {124 return false125 }126127 let quote = 0128 const blockStack: number[] = []129 for (let i = 0, len = value.length; i < len; i++) {130 const c = value.charCodeAt(i)131 if (c === 0x5c) {132 if (i === len - 1) {133 return true134 }135 i++136 } else if (quote !== 0) {137 if (c === 0x0a || c === 0x0c || c === 0x0d) {138 return true139 }140 if (c === quote) {141 quote = 0142 }143 } else if (c === 0x2f && value.charCodeAt(i + 1) === 0x2a) {144 const end = value.indexOf('*/', i + 2)145 if (end === -1) {146 return true147 }148 i = end + 1149 } else if (c === 0x22 || c === 0x27) {150 quote = c151 } else if (c === 0x28) {152 blockStack.push(0x29)153 } else if (c === 0x5b) {154 blockStack.push(0x5d)155 } else if (c === 0x7b || c === 0x7d) {156 return true157 } else if (c === 0x29 || c === 0x5d) {158 if (blockStack[blockStack.length - 1] !== c) {159 return true160 }161 blockStack.pop()162 } else if (c === 0x3b && blockStack.length === 0) {163 return true164 }165 }166 return quote !== 0 || blockStack.length !== 0167}168169export const styleObjectForEach = (170 style: Record<string, unknown>,171 fn: (key: string, value: string | null) => void172): void => {173 for (const [k, v] of Object.entries(style)) {174 const key =175 k[0] === '-' || !/[A-Z]/.test(k)176 ? k // a CSS variable or a lowercase only property177 : k.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`) // a camelCase property. convert to kebab-case178 if (!isValidStylePropertyName(key)) {179 continue180 }181 if (v == null) {182 fn(key, null)183 continue184 }185 let value: string186 if (typeof v === 'number') {187 value = !key.match(

fix(jsx): add px to numeric gridGap, gridRowGap and gridColumnGap (#5487)Rup Sarmahbefore adoptione5bb206

line 188why, one step back
188 /^(?:a|border-im|column(?:-c|s)|flex(?:$|-[^b])|grid-(?!.*gap)(?:ar|[^a])|font-w|li|or|sca|st|ta|wido|z)|ty$/

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 189–202why, one step back
189 )
190 ? `${v}px`191 : `${v}`192 } else if (typeof v === 'string') {193 if (hasUnsafeStyleValue(v)) {194 continue195 }196 value = v197 } else {198 continue199 }200 fn(key, value)201 }202}