parserOptions.ts 2.1 KB

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