919d52d69524a37928adf906c046ed75682416f4bd82f1d002a3a13b0c49a65965bded97c0246cf3fe3413ce2ef7d0aaa729db5c3febd5708750ff93b5b77c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* @flow */
  2. import { inBrowser } from 'core/util/env'
  3. import { makeMap } from 'shared/util'
  4. export const namespaceMap = {
  5. svg: 'http://www.w3.org/2000/svg',
  6. math: 'http://www.w3.org/1998/Math/MathML'
  7. }
  8. export const isHTMLTag = makeMap(
  9. 'html,body,base,head,link,meta,style,title,' +
  10. 'address,article,aside,footer,header,h1,h2,h3,h4,h5,h6,hgroup,nav,section,' +
  11. 'div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,' +
  12. 'a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,rtc,ruby,' +
  13. 's,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,' +
  14. 'embed,object,param,source,canvas,script,noscript,del,ins,' +
  15. 'caption,col,colgroup,table,thead,tbody,td,th,tr,' +
  16. 'button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,' +
  17. 'output,progress,select,textarea,' +
  18. 'details,dialog,menu,menuitem,summary,' +
  19. 'content,element,shadow,template,blockquote,iframe,tfoot'
  20. )
  21. // this map is intentionally selective, only covering SVG elements that may
  22. // contain child elements.
  23. export const isSVG = makeMap(
  24. 'svg,animate,circle,clippath,cursor,defs,desc,ellipse,filter,font-face,' +
  25. 'foreignObject,g,glyph,image,line,marker,mask,missing-glyph,path,pattern,' +
  26. 'polygon,polyline,rect,switch,symbol,text,textpath,tspan,use,view',
  27. true
  28. )
  29. export const isPreTag = (tag: ?string): boolean => tag === 'pre'
  30. export const isReservedTag = (tag: string): ?boolean => {
  31. return isHTMLTag(tag) || isSVG(tag)
  32. }
  33. export function getTagNamespace (tag: string): ?string {
  34. if (isSVG(tag)) {
  35. return 'svg'
  36. }
  37. // basic support for MathML
  38. // note it doesn't support other MathML elements being component roots
  39. if (tag === 'math') {
  40. return 'math'
  41. }
  42. }
  43. const unknownElementCache = Object.create(null)
  44. export function isUnknownElement (tag: string): boolean {
  45. /* istanbul ignore if */
  46. if (!inBrowser) {
  47. return true
  48. }
  49. if (isReservedTag(tag)) {
  50. return false
  51. }
  52. tag = tag.toLowerCase()
  53. /* istanbul ignore if */
  54. if (unknownElementCache[tag] != null) {
  55. return unknownElementCache[tag]
  56. }
  57. const el = document.createElement(tag)
  58. if (tag.indexOf('-') > -1) {
  59. // http://stackoverflow.com/a/28210364/1070244
  60. return (unknownElementCache[tag] = (
  61. el.constructor === window.HTMLUnknownElement ||
  62. el.constructor === window.HTMLElement
  63. ))
  64. } else {
  65. return (unknownElementCache[tag] = /HTMLUnknownElement/.test(el.toString()))
  66. }
  67. }
  68. export const isTextInputType = makeMap('text,number,password,search,email,tel,url')