-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathdomAttrConfig.ts
More file actions
167 lines (155 loc) · 8.18 KB
/
domAttrConfig.ts
File metadata and controls
167 lines (155 loc) · 8.18 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
import { makeMap } from './makeMap'
/**
* On the client we only need to offer special cases for boolean attributes that
* have different names from their corresponding dom properties:
* - itemscope -> N/A
* - allowfullscreen -> allowFullscreen
* - formnovalidate -> formNoValidate
* - ismap -> isMap
* - nomodule -> noModule
* - novalidate -> noValidate
* - readonly -> readOnly
*/
const specialBooleanAttrs = `itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly`
export const isSpecialBooleanAttr: (key: string) => boolean =
/*@__PURE__*/ makeMap(specialBooleanAttrs)
/**
* The full list is needed during SSR to produce the correct initial markup.
*/
export const isBooleanAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
specialBooleanAttrs +
`,async,autofocus,autoplay,controls,default,defer,disabled,` +
`inert,loop,open,required,reversed,scoped,seamless,` +
`checked,muted,multiple,selected`,
)
/**
* Boolean attributes should be included if the value is truthy or ''.
* e.g. `<select multiple>` compiles to `{ multiple: '' }`
*/
export function includeBooleanAttr(value: unknown): boolean {
return !!value || value === ''
}
const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/
const attrValidationCache: Record<string, boolean> = {}
export function isSSRSafeAttrName(name: string): boolean {
if (attrValidationCache.hasOwnProperty(name)) {
return attrValidationCache[name]
}
const isUnsafe = unsafeAttrCharRE.test(name)
if (isUnsafe) {
console.error(`unsafe attribute name: ${name}`)
}
return (attrValidationCache[name] = !isUnsafe)
}
export const propsToAttrMap: Record<string, string | undefined> = {
acceptCharset: 'accept-charset',
className: 'class',
htmlFor: 'for',
httpEquiv: 'http-equiv',
}
/**
* Known attributes, this is used for stringification of runtime static nodes
* so that we don't stringify bindings that cannot be set from HTML.
* Don't also forget to allow `data-*` and `aria-*`!
* Generated from https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
*/
export const isKnownHtmlAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
`accept,accept-charset,accesskey,action,align,allow,alt,async,` +
`autocapitalize,autocomplete,autofocus,autoplay,background,bgcolor,` +
`border,buffered,capture,challenge,charset,checked,cite,class,code,` +
`codebase,color,cols,colspan,content,contenteditable,contextmenu,controls,` +
`coords,crossorigin,csp,data,datetime,decoding,default,defer,dir,dirname,` +
`disabled,download,draggable,dropzone,enctype,enterkeyhint,for,form,` +
`formaction,formenctype,formmethod,formnovalidate,formtarget,headers,` +
`height,hidden,high,href,hreflang,http-equiv,icon,id,importance,inert,integrity,` +
`ismap,itemprop,keytype,kind,label,lang,language,loading,list,loop,low,` +
`manifest,max,maxlength,minlength,media,min,multiple,muted,name,novalidate,` +
`open,optimum,pattern,ping,placeholder,poster,preload,radiogroup,readonly,` +
`referrerpolicy,rel,required,reversed,rows,rowspan,sandbox,scope,scoped,` +
`selected,shape,size,sizes,slot,span,spellcheck,src,srcdoc,srclang,srcset,` +
`start,step,style,summary,tabindex,target,title,translate,type,usemap,` +
`value,width,wrap`,
)
/**
* Generated from https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
*/
export const isKnownSvgAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
`xmlns,accent-height,accumulate,additive,alignment-baseline,alphabetic,amplitude,` +
`arabic-form,ascent,attributeName,attributeType,azimuth,baseFrequency,` +
`baseline-shift,baseProfile,bbox,begin,bias,by,calcMode,cap-height,class,` +
`clip,clipPathUnits,clip-path,clip-rule,color,color-interpolation,` +
`color-interpolation-filters,color-profile,color-rendering,` +
`contentScriptType,contentStyleType,crossorigin,cursor,cx,cy,d,decelerate,` +
`descent,diffuseConstant,direction,display,divisor,dominant-baseline,dur,dx,` +
`dy,edgeMode,elevation,enable-background,end,exponent,fill,fill-opacity,` +
`fill-rule,filter,filterRes,filterUnits,flood-color,flood-opacity,` +
`font-family,font-size,font-size-adjust,font-stretch,font-style,` +
`font-variant,font-weight,format,from,fr,fx,fy,g1,g2,glyph-name,` +
`glyph-orientation-horizontal,glyph-orientation-vertical,glyphRef,` +
`gradientTransform,gradientUnits,hanging,height,href,hreflang,horiz-adv-x,` +
`horiz-origin-x,id,ideographic,image-rendering,in,in2,intercept,k,k1,k2,k3,` +
`k4,kernelMatrix,kernelUnitLength,kerning,keyPoints,keySplines,keyTimes,` +
`lang,lengthAdjust,letter-spacing,lighting-color,limitingConeAngle,local,` +
`marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,` +
`mask,maskContentUnits,maskUnits,mathematical,max,media,method,min,mode,` +
`name,numOctaves,offset,opacity,operator,order,orient,orientation,origin,` +
`overflow,overline-position,overline-thickness,panose-1,paint-order,path,` +
`pathLength,patternContentUnits,patternTransform,patternUnits,ping,` +
`pointer-events,points,pointsAtX,pointsAtY,pointsAtZ,preserveAlpha,` +
`preserveAspectRatio,primitiveUnits,r,radius,referrerPolicy,refX,refY,rel,` +
`rendering-intent,repeatCount,repeatDur,requiredExtensions,requiredFeatures,` +
`restart,result,rotate,rx,ry,scale,seed,shape-rendering,slope,spacing,` +
`specularConstant,specularExponent,speed,spreadMethod,startOffset,` +
`stdDeviation,stemh,stemv,stitchTiles,stop-color,stop-opacity,` +
`strikethrough-position,strikethrough-thickness,string,stroke,` +
`stroke-dasharray,stroke-dashoffset,stroke-linecap,stroke-linejoin,` +
`stroke-miterlimit,stroke-opacity,stroke-width,style,surfaceScale,` +
`systemLanguage,tabindex,tableValues,target,targetX,targetY,text-anchor,` +
`text-decoration,text-rendering,textLength,to,transform,transform-origin,` +
`type,u1,u2,underline-position,underline-thickness,unicode,unicode-bidi,` +
`unicode-range,units-per-em,v-alphabetic,v-hanging,v-ideographic,` +
`v-mathematical,values,vector-effect,version,vert-adv-y,vert-origin-x,` +
`vert-origin-y,viewBox,viewTarget,visibility,width,widths,word-spacing,` +
`writing-mode,x,x-height,x1,x2,xChannelSelector,xlink:actuate,xlink:arcrole,` +
`xlink:href,xlink:role,xlink:show,xlink:title,xlink:type,xmlns:xlink,xml:base,xml:lang,` +
`xml:space,y,y1,y2,yChannelSelector,z,zoomAndPan`,
)
/**
* Generated from https://developer.mozilla.org/en-US/docs/Web/MathML/Attribute
*/
export const isKnownMathMLAttr: (key: string) => boolean =
/*@__PURE__*/ makeMap(
`accent,accentunder,actiontype,align,alignmentscope,altimg,altimg-height,` +
`altimg-valign,altimg-width,alttext,bevelled,close,columnsalign,columnlines,` +
`columnspan,denomalign,depth,dir,display,displaystyle,encoding,` +
`equalcolumns,equalrows,fence,fontstyle,fontweight,form,frame,framespacing,` +
`groupalign,height,href,id,indentalign,indentalignfirst,indentalignlast,` +
`indentshift,indentshiftfirst,indentshiftlast,indextype,justify,` +
`largetop,largeop,lquote,lspace,mathbackground,mathcolor,mathsize,` +
`mathvariant,maxsize,minlabelspacing,mode,other,overflow,position,` +
`rowalign,rowlines,rowspan,rquote,rspace,scriptlevel,scriptminsize,` +
`scriptsizemultiplier,selection,separator,separators,shift,side,` +
`src,stackalign,stretchy,subscriptshift,superscriptshift,symmetric,` +
`voffset,width,widths,xlink:href,xlink:show,xlink:type,xmlns`,
)
/**
* Shared between server-renderer and runtime-core hydration logic
*/
export function isRenderableAttrValue(value: unknown): boolean {
if (value == null) {
return false
}
const type = typeof value
return type === 'string' || type === 'number' || type === 'boolean'
}
/**
* An attribute that can be used as a flag as well as with a value.
* When `true`, it should be present (set either to an empty string or its name).
* When `false`, it should be omitted.
* For any other value, should be present with that value.
*/
export const isOverloadedBooleanAttr: (key: string) => boolean =
/*@__PURE__*/ makeMap('hidden')
export function isBooleanAttrValue(value: unknown): boolean {
return typeof value === 'boolean' || value === ''
}