parserOptionsMinimal.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. TextModes,
  3. ParserOptions,
  4. ElementNode,
  5. Namespaces,
  6. NodeTypes
  7. } from '@vue/compiler-core'
  8. import { isVoidTag, isHTMLTag, isSVGTag } from './tagConfig'
  9. import { makeMap } from '@vue/shared'
  10. const isRawTextContainer = /*#__PURE__*/ makeMap(
  11. 'style,iframe,script,noscript',
  12. true
  13. )
  14. export const enum DOMNamespaces {
  15. HTML = Namespaces.HTML,
  16. SVG,
  17. MATH_ML
  18. }
  19. export const parserOptionsMinimal: ParserOptions = {
  20. isVoidTag,
  21. isNativeTag: tag => isHTMLTag(tag) || isSVGTag(tag),
  22. isPreTag: tag => tag === 'pre',
  23. // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction-dispatcher
  24. getNamespace(tag: string, parent: ElementNode | undefined): DOMNamespaces {
  25. let ns = parent ? parent.ns : DOMNamespaces.HTML
  26. if (parent && ns === DOMNamespaces.MATH_ML) {
  27. if (parent.tag === 'annotation-xml') {
  28. if (tag === 'svg') {
  29. return DOMNamespaces.SVG
  30. }
  31. if (
  32. parent.props.some(
  33. a =>
  34. a.type === NodeTypes.ATTRIBUTE &&
  35. a.name === 'encoding' &&
  36. a.value != null &&
  37. (a.value.content === 'text/html' ||
  38. a.value.content === 'application/xhtml+xml')
  39. )
  40. ) {
  41. ns = DOMNamespaces.HTML
  42. }
  43. } else if (
  44. /^m(?:[ions]|text)$/.test(parent.tag) &&
  45. tag !== 'mglyph' &&
  46. tag !== 'malignmark'
  47. ) {
  48. ns = DOMNamespaces.HTML
  49. }
  50. } else if (parent && ns === DOMNamespaces.SVG) {
  51. if (
  52. parent.tag === 'foreignObject' ||
  53. parent.tag === 'desc' ||
  54. parent.tag === 'title'
  55. ) {
  56. ns = DOMNamespaces.HTML
  57. }
  58. }
  59. if (ns === DOMNamespaces.HTML) {
  60. if (tag === 'svg') {
  61. return DOMNamespaces.SVG
  62. }
  63. if (tag === 'math') {
  64. return DOMNamespaces.MATH_ML
  65. }
  66. }
  67. return ns
  68. },
  69. // https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments
  70. getTextMode(tag: string, ns: DOMNamespaces): TextModes {
  71. if (ns === DOMNamespaces.HTML) {
  72. if (tag === 'textarea' || tag === 'title') {
  73. return TextModes.RCDATA
  74. }
  75. if (isRawTextContainer(tag)) {
  76. return TextModes.RAWTEXT
  77. }
  78. }
  79. return TextModes.DATA
  80. }
  81. }