parse.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. parse,
  3. NodeTypes,
  4. ElementNode,
  5. TextNode,
  6. ErrorCodes,
  7. ExpressionNode,
  8. ElementTypes
  9. } from '@vue/compiler-core'
  10. import {
  11. parserOptionsMinimal as parserOptions,
  12. DOMNamespaces
  13. } from '../src/parserOptionsMinimal'
  14. describe('DOM parser', () => {
  15. describe('Text', () => {
  16. test('textarea handles comments/elements as just text', () => {
  17. const ast = parse(
  18. '<textarea>some<div>text</div>and<!--comment--></textarea>',
  19. parserOptions
  20. )
  21. const element = ast.children[0] as ElementNode
  22. const text = element.children[0] as TextNode
  23. expect(text).toStrictEqual({
  24. type: NodeTypes.TEXT,
  25. content: 'some<div>text</div>and<!--comment-->',
  26. isEmpty: false,
  27. loc: {
  28. start: { offset: 10, line: 1, column: 11 },
  29. end: { offset: 46, line: 1, column: 47 },
  30. source: 'some<div>text</div>and<!--comment-->'
  31. }
  32. })
  33. })
  34. test('textarea handles character references', () => {
  35. const ast = parse('<textarea>&amp;</textarea>', parserOptions)
  36. const element = ast.children[0] as ElementNode
  37. const text = element.children[0] as TextNode
  38. expect(text).toStrictEqual({
  39. type: NodeTypes.TEXT,
  40. content: '&',
  41. isEmpty: false,
  42. loc: {
  43. start: { offset: 10, line: 1, column: 11 },
  44. end: { offset: 15, line: 1, column: 16 },
  45. source: '&amp;'
  46. }
  47. })
  48. })
  49. test('style handles comments/elements as just a text', () => {
  50. const ast = parse(
  51. '<style>some<div>text</div>and<!--comment--></style>',
  52. parserOptions
  53. )
  54. const element = ast.children[0] as ElementNode
  55. const text = element.children[0] as TextNode
  56. expect(text).toStrictEqual({
  57. type: NodeTypes.TEXT,
  58. content: 'some<div>text</div>and<!--comment-->',
  59. isEmpty: false,
  60. loc: {
  61. start: { offset: 7, line: 1, column: 8 },
  62. end: { offset: 43, line: 1, column: 44 },
  63. source: 'some<div>text</div>and<!--comment-->'
  64. }
  65. })
  66. })
  67. test("style doesn't handle character references", () => {
  68. const ast = parse('<style>&amp;</style>', parserOptions)
  69. const element = ast.children[0] as ElementNode
  70. const text = element.children[0] as TextNode
  71. expect(text).toStrictEqual({
  72. type: NodeTypes.TEXT,
  73. content: '&amp;',
  74. isEmpty: false,
  75. loc: {
  76. start: { offset: 7, line: 1, column: 8 },
  77. end: { offset: 12, line: 1, column: 13 },
  78. source: '&amp;'
  79. }
  80. })
  81. })
  82. test('CDATA', () => {
  83. const ast = parse('<svg><![CDATA[some text]]></svg>', parserOptions)
  84. const text = (ast.children[0] as ElementNode).children![0] as TextNode
  85. expect(text).toStrictEqual({
  86. type: NodeTypes.TEXT,
  87. content: 'some text',
  88. isEmpty: false,
  89. loc: {
  90. start: { offset: 14, line: 1, column: 15 },
  91. end: { offset: 23, line: 1, column: 24 },
  92. source: 'some text'
  93. }
  94. })
  95. })
  96. })
  97. describe('Interpolation', () => {
  98. test('HTML entities in interpolation should be translated for backward compatibility.', () => {
  99. const ast = parse('<div>{{ a &lt; b }}</div>', parserOptions)
  100. const element = ast.children[0] as ElementNode
  101. const interpolation = element.children[0] as ExpressionNode
  102. expect(interpolation).toStrictEqual({
  103. type: NodeTypes.EXPRESSION,
  104. content: 'a < b',
  105. isStatic: false,
  106. loc: {
  107. start: { offset: 5, line: 1, column: 6 },
  108. end: { offset: 19, line: 1, column: 20 },
  109. source: '{{ a &lt; b }}'
  110. }
  111. })
  112. })
  113. })
  114. describe('Element', () => {
  115. test('void element', () => {
  116. const ast = parse('<img>after', parserOptions)
  117. const element = ast.children[0] as ElementNode
  118. expect(element).toStrictEqual({
  119. type: NodeTypes.ELEMENT,
  120. ns: DOMNamespaces.HTML,
  121. tag: 'img',
  122. tagType: ElementTypes.ELEMENT,
  123. attrs: [],
  124. directives: [],
  125. isSelfClosing: false,
  126. children: [],
  127. loc: {
  128. start: { offset: 0, line: 1, column: 1 },
  129. end: { offset: 5, line: 1, column: 6 },
  130. source: '<img>'
  131. }
  132. })
  133. })
  134. test('Strict end tag detection for textarea.', () => {
  135. const ast = parse(
  136. '<textarea>hello</textarea</textarea0></texTArea a="<>">',
  137. {
  138. ...parserOptions,
  139. onError: err => {
  140. if (err.code !== ErrorCodes.END_TAG_WITH_ATTRIBUTES) {
  141. throw err
  142. }
  143. }
  144. }
  145. )
  146. const element = ast.children[0] as ElementNode
  147. const text = element.children[0] as TextNode
  148. expect(ast.children.length).toBe(1)
  149. expect(text).toStrictEqual({
  150. type: NodeTypes.TEXT,
  151. content: 'hello</textarea</textarea0>',
  152. isEmpty: false,
  153. loc: {
  154. start: { offset: 10, line: 1, column: 11 },
  155. end: { offset: 37, line: 1, column: 38 },
  156. source: 'hello</textarea</textarea0>'
  157. }
  158. })
  159. })
  160. })
  161. })