parserOptions.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Namespaces, NodeTypes, type ParserOptions } from '@vue/compiler-core'
  2. import { isHTMLTag, isMathMLTag, isSVGTag, isVoidTag } from '@vue/shared'
  3. import { TRANSITION, TRANSITION_GROUP } from './runtimeHelpers'
  4. import { decodeHtmlBrowser } from './decodeHtmlBrowser'
  5. export const parserOptions: ParserOptions = {
  6. parseMode: 'html',
  7. isVoidTag,
  8. isNativeTag: tag => isHTMLTag(tag) || isSVGTag(tag) || isMathMLTag(tag),
  9. isPreTag: tag => tag === 'pre',
  10. decodeEntities: __BROWSER__ ? decodeHtmlBrowser : undefined,
  11. isBuiltInComponent: tag => {
  12. if (tag === 'Transition' || tag === 'transition') {
  13. return TRANSITION
  14. } else if (tag === 'TransitionGroup' || tag === 'transition-group') {
  15. return TRANSITION_GROUP
  16. }
  17. },
  18. // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction-dispatcher
  19. getNamespace(tag, parent, rootNamespace) {
  20. let ns = parent ? parent.ns : rootNamespace
  21. if (parent && ns === Namespaces.MATH_ML) {
  22. if (parent.tag === 'annotation-xml') {
  23. if (tag === 'svg') {
  24. return Namespaces.SVG
  25. }
  26. if (
  27. parent.props.some(
  28. a =>
  29. a.type === NodeTypes.ATTRIBUTE &&
  30. a.name === 'encoding' &&
  31. a.value != null &&
  32. (a.value.content === 'text/html' ||
  33. a.value.content === 'application/xhtml+xml'),
  34. )
  35. ) {
  36. ns = Namespaces.HTML
  37. }
  38. } else if (
  39. /^m(?:[ions]|text)$/.test(parent.tag) &&
  40. tag !== 'mglyph' &&
  41. tag !== 'malignmark'
  42. ) {
  43. ns = Namespaces.HTML
  44. }
  45. } else if (parent && ns === Namespaces.SVG) {
  46. if (
  47. parent.tag === 'foreignObject' ||
  48. parent.tag === 'desc' ||
  49. parent.tag === 'title'
  50. ) {
  51. ns = Namespaces.HTML
  52. }
  53. }
  54. if (ns === Namespaces.HTML) {
  55. if (tag === 'svg') {
  56. return Namespaces.SVG
  57. }
  58. if (tag === 'math') {
  59. return Namespaces.MATH_ML
  60. }
  61. }
  62. return ns
  63. },
  64. }