domAttrConfig.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { makeMap } from './makeMap'
  2. /**
  3. * On the client we only need to offer special cases for boolean attributes that
  4. * have different names from their corresponding dom properties:
  5. * - itemscope -> N/A
  6. * - allowfullscreen -> allowFullscreen
  7. * - formnovalidate -> formNoValidate
  8. * - ismap -> isMap
  9. * - nomodule -> noModule
  10. * - novalidate -> noValidate
  11. * - readonly -> readOnly
  12. */
  13. const specialBooleanAttrs = `itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly`
  14. export const isSpecialBooleanAttr: (key: string) => boolean =
  15. /*@__PURE__*/ makeMap(specialBooleanAttrs)
  16. /**
  17. * The full list is needed during SSR to produce the correct initial markup.
  18. */
  19. export const isBooleanAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
  20. specialBooleanAttrs +
  21. `,async,autofocus,autoplay,controls,default,defer,disabled,hidden,` +
  22. `inert,loop,open,required,reversed,scoped,seamless,` +
  23. `checked,muted,multiple,selected`,
  24. )
  25. /**
  26. * Boolean attributes should be included if the value is truthy or ''.
  27. * e.g. `<select multiple>` compiles to `{ multiple: '' }`
  28. */
  29. export function includeBooleanAttr(value: unknown): boolean {
  30. return !!value || value === ''
  31. }
  32. const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/
  33. const attrValidationCache: Record<string, boolean> = {}
  34. export function isSSRSafeAttrName(name: string): boolean {
  35. if (attrValidationCache.hasOwnProperty(name)) {
  36. return attrValidationCache[name]
  37. }
  38. const isUnsafe = unsafeAttrCharRE.test(name)
  39. if (isUnsafe) {
  40. console.error(`unsafe attribute name: ${name}`)
  41. }
  42. return (attrValidationCache[name] = !isUnsafe)
  43. }
  44. export const propsToAttrMap: Record<string, string | undefined> = {
  45. acceptCharset: 'accept-charset',
  46. className: 'class',
  47. htmlFor: 'for',
  48. httpEquiv: 'http-equiv',
  49. }
  50. /**
  51. * Known attributes, this is used for stringification of runtime static nodes
  52. * so that we don't stringify bindings that cannot be set from HTML.
  53. * Don't also forget to allow `data-*` and `aria-*`!
  54. * Generated from https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  55. */
  56. export const isKnownHtmlAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
  57. `accept,accept-charset,accesskey,action,align,allow,alt,async,` +
  58. `autocapitalize,autocomplete,autofocus,autoplay,background,bgcolor,` +
  59. `border,buffered,capture,challenge,charset,checked,cite,class,code,` +
  60. `codebase,color,cols,colspan,content,contenteditable,contextmenu,controls,` +
  61. `coords,crossorigin,csp,data,datetime,decoding,default,defer,dir,dirname,` +
  62. `disabled,download,draggable,dropzone,enctype,enterkeyhint,for,form,` +
  63. `formaction,formenctype,formmethod,formnovalidate,formtarget,headers,` +
  64. `height,hidden,high,href,hreflang,http-equiv,icon,id,importance,inert,integrity,` +
  65. `ismap,itemprop,keytype,kind,label,lang,language,loading,list,loop,low,` +
  66. `manifest,max,maxlength,minlength,media,min,multiple,muted,name,novalidate,` +
  67. `open,optimum,pattern,ping,placeholder,poster,preload,radiogroup,readonly,` +
  68. `referrerpolicy,rel,required,reversed,rows,rowspan,sandbox,scope,scoped,` +
  69. `selected,shape,size,sizes,slot,span,spellcheck,src,srcdoc,srclang,srcset,` +
  70. `start,step,style,summary,tabindex,target,title,translate,type,usemap,` +
  71. `value,width,wrap`,
  72. )
  73. /**
  74. * Generated from https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
  75. */
  76. export const isKnownSvgAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
  77. `xmlns,accent-height,accumulate,additive,alignment-baseline,alphabetic,amplitude,` +
  78. `arabic-form,ascent,attributeName,attributeType,azimuth,baseFrequency,` +
  79. `baseline-shift,baseProfile,bbox,begin,bias,by,calcMode,cap-height,class,` +
  80. `clip,clipPathUnits,clip-path,clip-rule,color,color-interpolation,` +
  81. `color-interpolation-filters,color-profile,color-rendering,` +
  82. `contentScriptType,contentStyleType,crossorigin,cursor,cx,cy,d,decelerate,` +
  83. `descent,diffuseConstant,direction,display,divisor,dominant-baseline,dur,dx,` +
  84. `dy,edgeMode,elevation,enable-background,end,exponent,fill,fill-opacity,` +
  85. `fill-rule,filter,filterRes,filterUnits,flood-color,flood-opacity,` +
  86. `font-family,font-size,font-size-adjust,font-stretch,font-style,` +
  87. `font-variant,font-weight,format,from,fr,fx,fy,g1,g2,glyph-name,` +
  88. `glyph-orientation-horizontal,glyph-orientation-vertical,glyphRef,` +
  89. `gradientTransform,gradientUnits,hanging,height,href,hreflang,horiz-adv-x,` +
  90. `horiz-origin-x,id,ideographic,image-rendering,in,in2,intercept,k,k1,k2,k3,` +
  91. `k4,kernelMatrix,kernelUnitLength,kerning,keyPoints,keySplines,keyTimes,` +
  92. `lang,lengthAdjust,letter-spacing,lighting-color,limitingConeAngle,local,` +
  93. `marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,` +
  94. `mask,maskContentUnits,maskUnits,mathematical,max,media,method,min,mode,` +
  95. `name,numOctaves,offset,opacity,operator,order,orient,orientation,origin,` +
  96. `overflow,overline-position,overline-thickness,panose-1,paint-order,path,` +
  97. `pathLength,patternContentUnits,patternTransform,patternUnits,ping,` +
  98. `pointer-events,points,pointsAtX,pointsAtY,pointsAtZ,preserveAlpha,` +
  99. `preserveAspectRatio,primitiveUnits,r,radius,referrerPolicy,refX,refY,rel,` +
  100. `rendering-intent,repeatCount,repeatDur,requiredExtensions,requiredFeatures,` +
  101. `restart,result,rotate,rx,ry,scale,seed,shape-rendering,slope,spacing,` +
  102. `specularConstant,specularExponent,speed,spreadMethod,startOffset,` +
  103. `stdDeviation,stemh,stemv,stitchTiles,stop-color,stop-opacity,` +
  104. `strikethrough-position,strikethrough-thickness,string,stroke,` +
  105. `stroke-dasharray,stroke-dashoffset,stroke-linecap,stroke-linejoin,` +
  106. `stroke-miterlimit,stroke-opacity,stroke-width,style,surfaceScale,` +
  107. `systemLanguage,tabindex,tableValues,target,targetX,targetY,text-anchor,` +
  108. `text-decoration,text-rendering,textLength,to,transform,transform-origin,` +
  109. `type,u1,u2,underline-position,underline-thickness,unicode,unicode-bidi,` +
  110. `unicode-range,units-per-em,v-alphabetic,v-hanging,v-ideographic,` +
  111. `v-mathematical,values,vector-effect,version,vert-adv-y,vert-origin-x,` +
  112. `vert-origin-y,viewBox,viewTarget,visibility,width,widths,word-spacing,` +
  113. `writing-mode,x,x-height,x1,x2,xChannelSelector,xlink:actuate,xlink:arcrole,` +
  114. `xlink:href,xlink:role,xlink:show,xlink:title,xlink:type,xmlns:xlink,xml:base,xml:lang,` +
  115. `xml:space,y,y1,y2,yChannelSelector,z,zoomAndPan`,
  116. )
  117. /**
  118. * Generated from https://developer.mozilla.org/en-US/docs/Web/MathML/Attribute
  119. */
  120. export const isKnownMathMLAttr: (key: string) => boolean =
  121. /*@__PURE__*/ makeMap(
  122. `accent,accentunder,actiontype,align,alignmentscope,altimg,altimg-height,` +
  123. `altimg-valign,altimg-width,alttext,bevelled,close,columnsalign,columnlines,` +
  124. `columnspan,denomalign,depth,dir,display,displaystyle,encoding,` +
  125. `equalcolumns,equalrows,fence,fontstyle,fontweight,form,frame,framespacing,` +
  126. `groupalign,height,href,id,indentalign,indentalignfirst,indentalignlast,` +
  127. `indentshift,indentshiftfirst,indentshiftlast,indextype,justify,` +
  128. `largetop,largeop,lquote,lspace,mathbackground,mathcolor,mathsize,` +
  129. `mathvariant,maxsize,minlabelspacing,mode,other,overflow,position,` +
  130. `rowalign,rowlines,rowspan,rquote,rspace,scriptlevel,scriptminsize,` +
  131. `scriptsizemultiplier,selection,separator,separators,shift,side,` +
  132. `src,stackalign,stretchy,subscriptshift,superscriptshift,symmetric,` +
  133. `voffset,width,widths,xlink:href,xlink:show,xlink:type,xmlns`,
  134. )
  135. /**
  136. * Shared between server-renderer and runtime-core hydration logic
  137. */
  138. export function isRenderableAttrValue(value: unknown): boolean {
  139. if (value == null) {
  140. return false
  141. }
  142. const type = typeof value
  143. return type === 'string' || type === 'number' || type === 'boolean'
  144. }
  145. /*
  146. * The following attributes must be set as attribute
  147. */
  148. export function shouldSetAsAttr(tagName: string, key: string): boolean {
  149. // these are enumerated attrs, however their corresponding DOM properties
  150. // are actually booleans - this leads to setting it with a string "false"
  151. // value leading it to be coerced to `true`, so we need to always treat
  152. // them as attributes.
  153. // Note that `contentEditable` doesn't have this problem: its DOM
  154. // property is also enumerated string values.
  155. if (key === 'spellcheck' || key === 'draggable' || key === 'translate') {
  156. return true
  157. }
  158. // #1787, #2840 form property on form elements is readonly and must be set as
  159. // attribute.
  160. if (key === 'form') {
  161. return true
  162. }
  163. // #1526 <input list> must be set as attribute
  164. if (key === 'list' && tagName === 'INPUT') {
  165. return true
  166. }
  167. // #2766 <textarea type> must be set as attribute
  168. if (key === 'type' && tagName === 'TEXTAREA') {
  169. return true
  170. }
  171. // #8780 the width or height of embedded tags must be set as attribute
  172. if (
  173. (key === 'width' || key === 'height') &&
  174. (tagName === 'IMG' ||
  175. tagName === 'VIDEO' ||
  176. tagName === 'CANVAS' ||
  177. tagName === 'SOURCE')
  178. ) {
  179. return true
  180. }
  181. return false
  182. }