domAttrConfig.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = /*#__PURE__*/ makeMap(specialBooleanAttrs)
  15. /**
  16. * The full list is needed during SSR to produce the correct initial markup.
  17. */
  18. export const isBooleanAttr = /*#__PURE__*/ makeMap(
  19. specialBooleanAttrs +
  20. `,async,autofocus,autoplay,controls,default,defer,disabled,hidden,` +
  21. `loop,open,required,reversed,scoped,seamless,` +
  22. `checked,muted,multiple,selected`
  23. )
  24. const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/
  25. const attrValidationCache: Record<string, boolean> = {}
  26. export function isSSRSafeAttrName(name: string): boolean {
  27. if (attrValidationCache.hasOwnProperty(name)) {
  28. return attrValidationCache[name]
  29. }
  30. const isUnsafe = unsafeAttrCharRE.test(name)
  31. if (isUnsafe) {
  32. console.error(`unsafe attribute name: ${name}`)
  33. }
  34. return (attrValidationCache[name] = !isUnsafe)
  35. }
  36. export const propsToAttrMap: Record<string, string | undefined> = {
  37. acceptCharset: 'accept-charset',
  38. className: 'class',
  39. htmlFor: 'for',
  40. httpEquiv: 'http-equiv'
  41. }
  42. /**
  43. * CSS properties that accept plain numbers
  44. */
  45. export const isNoUnitNumericStyleProp = /*#__PURE__*/ makeMap(
  46. `animation-iteration-count,border-image-outset,border-image-slice,` +
  47. `border-image-width,box-flex,box-flex-group,box-ordinal-group,column-count,` +
  48. `columns,flex,flex-grow,flex-positive,flex-shrink,flex-negative,flex-order,` +
  49. `grid-row,grid-row-end,grid-row-span,grid-row-start,grid-column,` +
  50. `grid-column-end,grid-column-span,grid-column-start,font-weight,line-clamp,` +
  51. `line-height,opacity,order,orphans,tab-size,widows,z-index,zoom,` +
  52. // SVG
  53. `fill-opacity,flood-opacity,stop-opacity,stroke-dasharray,stroke-dashoffset,` +
  54. `stroke-miterlimit,stroke-opacity,stroke-width`
  55. )
  56. /**
  57. * Known attributes, this is used for stringification of runtime static nodes
  58. * so that we don't stringify bindings that cannot be set from HTML.
  59. * Don't also forget to allow `data-*` and `aria-*`!
  60. * Generated from https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  61. */
  62. export const isKnownAttr = /*#__PURE__*/ makeMap(
  63. `accept,accept-charset,accesskey,action,align,allow,alt,async,` +
  64. `autocapitalize,autocomplete,autofocus,autoplay,background,bgcolor,` +
  65. `border,buffered,capture,challenge,charset,checked,cite,class,code,` +
  66. `codebase,color,cols,colspan,content,contenteditable,contextmenu,controls,` +
  67. `coords,crossorigin,csp,data,datetime,decoding,default,defer,dir,dirname,` +
  68. `disabled,download,draggable,dropzone,enctype,enterkeyhint,for,form,` +
  69. `formaction,formenctype,formmethod,formnovalidate,formtarget,headers,` +
  70. `height,hidden,high,href,hreflang,http-equiv,icon,id,importance,integrity,` +
  71. `ismap,itemprop,keytype,kind,label,lang,language,loading,list,loop,low,` +
  72. `manifest,max,maxlength,minlength,media,min,multiple,muted,name,novalidate,` +
  73. `open,optimum,pattern,ping,placeholder,poster,preload,radiogroup,readonly,` +
  74. `referrerpolicy,rel,required,reversed,rows,rowspan,sandbox,scope,scoped,` +
  75. `selected,shape,size,sizes,slot,span,spellcheck,src,srcdoc,srclang,srcset,` +
  76. `start,step,style,summary,tabindex,target,title,translate,type,usemap,` +
  77. `value,width,wrap`
  78. )