parse.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import {
  2. parse,
  3. NodeTypes,
  4. ElementNode,
  5. TextNode,
  6. ErrorCodes,
  7. ElementTypes,
  8. InterpolationNode
  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 InterpolationNode
  102. expect(interpolation).toStrictEqual({
  103. type: NodeTypes.INTERPOLATION,
  104. content: {
  105. type: NodeTypes.SIMPLE_EXPRESSION,
  106. content: `a < b`,
  107. isStatic: false,
  108. isConstant: false,
  109. loc: {
  110. start: { offset: 8, line: 1, column: 9 },
  111. end: { offset: 16, line: 1, column: 17 },
  112. source: 'a &lt; b'
  113. }
  114. },
  115. loc: {
  116. start: { offset: 5, line: 1, column: 6 },
  117. end: { offset: 19, line: 1, column: 20 },
  118. source: '{{ a &lt; b }}'
  119. }
  120. })
  121. })
  122. })
  123. describe('Element', () => {
  124. test('void element', () => {
  125. const ast = parse('<img>after', parserOptions)
  126. const element = ast.children[0] as ElementNode
  127. expect(element).toStrictEqual({
  128. type: NodeTypes.ELEMENT,
  129. ns: DOMNamespaces.HTML,
  130. tag: 'img',
  131. tagType: ElementTypes.ELEMENT,
  132. props: [],
  133. isSelfClosing: false,
  134. children: [],
  135. loc: {
  136. start: { offset: 0, line: 1, column: 1 },
  137. end: { offset: 5, line: 1, column: 6 },
  138. source: '<img>'
  139. },
  140. codegenNode: undefined
  141. })
  142. })
  143. test('native element', () => {
  144. const ast = parse('<div></div><comp></comp><Comp></Comp>', parserOptions)
  145. expect(ast.children[0]).toMatchObject({
  146. type: NodeTypes.ELEMENT,
  147. tag: 'div',
  148. tagType: ElementTypes.ELEMENT
  149. })
  150. expect(ast.children[1]).toMatchObject({
  151. type: NodeTypes.ELEMENT,
  152. tag: 'comp',
  153. tagType: ElementTypes.COMPONENT
  154. })
  155. expect(ast.children[2]).toMatchObject({
  156. type: NodeTypes.ELEMENT,
  157. tag: 'Comp',
  158. tagType: ElementTypes.COMPONENT
  159. })
  160. })
  161. test('Strict end tag detection for textarea.', () => {
  162. const ast = parse(
  163. '<textarea>hello</textarea</textarea0></texTArea a="<>">',
  164. {
  165. ...parserOptions,
  166. onError: err => {
  167. if (err.code !== ErrorCodes.END_TAG_WITH_ATTRIBUTES) {
  168. throw err
  169. }
  170. }
  171. }
  172. )
  173. const element = ast.children[0] as ElementNode
  174. const text = element.children[0] as TextNode
  175. expect(ast.children.length).toBe(1)
  176. expect(text).toStrictEqual({
  177. type: NodeTypes.TEXT,
  178. content: 'hello</textarea</textarea0>',
  179. isEmpty: false,
  180. loc: {
  181. start: { offset: 10, line: 1, column: 11 },
  182. end: { offset: 37, line: 1, column: 38 },
  183. source: 'hello</textarea</textarea0>'
  184. }
  185. })
  186. })
  187. })
  188. describe('Namespaces', () => {
  189. test('HTML namesapce', () => {
  190. const ast = parse('<html>test</html>', parserOptions)
  191. const element = ast.children[0] as ElementNode
  192. expect(element.ns).toBe(DOMNamespaces.HTML)
  193. })
  194. test('SVG namesapce', () => {
  195. const ast = parse('<svg>test</svg>', parserOptions)
  196. const element = ast.children[0] as ElementNode
  197. expect(element.ns).toBe(DOMNamespaces.SVG)
  198. })
  199. test('MATH_ML namesapce', () => {
  200. const ast = parse('<math>test</math>', parserOptions)
  201. const element = ast.children[0] as ElementNode
  202. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  203. })
  204. test('SVG in MATH_ML namesapce', () => {
  205. const ast = parse(
  206. '<math><annotation-xml><svg></svg></annotation-xml></math>',
  207. parserOptions
  208. )
  209. const elementMath = ast.children[0] as ElementNode
  210. const elementAnnotation = elementMath.children[0] as ElementNode
  211. const elementSvg = elementAnnotation.children[0] as ElementNode
  212. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  213. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  214. })
  215. test('html text/html in MATH_ML namesapce', () => {
  216. const ast = parse(
  217. '<math><annotation-xml encoding="text/html"><test/></annotation-xml></math>',
  218. parserOptions
  219. )
  220. const elementMath = ast.children[0] as ElementNode
  221. const elementAnnotation = elementMath.children[0] as ElementNode
  222. const element = elementAnnotation.children[0] as ElementNode
  223. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  224. expect(element.ns).toBe(DOMNamespaces.HTML)
  225. })
  226. test('html application/xhtml+xml in MATH_ML namesapce', () => {
  227. const ast = parse(
  228. '<math><annotation-xml encoding="application/xhtml+xml"><test/></annotation-xml></math>',
  229. parserOptions
  230. )
  231. const elementMath = ast.children[0] as ElementNode
  232. const elementAnnotation = elementMath.children[0] as ElementNode
  233. const element = elementAnnotation.children[0] as ElementNode
  234. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  235. expect(element.ns).toBe(DOMNamespaces.HTML)
  236. })
  237. test('mtext malignmark in MATH_ML namesapce', () => {
  238. const ast = parse(
  239. '<math><mtext><malignmark/></mtext></math>',
  240. parserOptions
  241. )
  242. const elementMath = ast.children[0] as ElementNode
  243. const elementText = elementMath.children[0] as ElementNode
  244. const element = elementText.children[0] as ElementNode
  245. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  246. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  247. })
  248. test('mtext and not malignmark tag in MATH_ML namesapce', () => {
  249. const ast = parse('<math><mtext><test/></mtext></math>', parserOptions)
  250. const elementMath = ast.children[0] as ElementNode
  251. const elementText = elementMath.children[0] as ElementNode
  252. const element = elementText.children[0] as ElementNode
  253. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  254. expect(element.ns).toBe(DOMNamespaces.HTML)
  255. })
  256. test('foreignObject tag in SVG namesapce', () => {
  257. const ast = parse(
  258. '<svg><foreignObject><test/></foreignObject></svg>',
  259. parserOptions
  260. )
  261. const elementSvg = ast.children[0] as ElementNode
  262. const elementForeignObject = elementSvg.children[0] as ElementNode
  263. const element = elementForeignObject.children[0] as ElementNode
  264. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  265. expect(element.ns).toBe(DOMNamespaces.HTML)
  266. })
  267. test('desc tag in SVG namesapce', () => {
  268. const ast = parse('<svg><desc><test/></desc></svg>', parserOptions)
  269. const elementSvg = ast.children[0] as ElementNode
  270. const elementDesc = elementSvg.children[0] as ElementNode
  271. const element = elementDesc.children[0] as ElementNode
  272. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  273. expect(element.ns).toBe(DOMNamespaces.HTML)
  274. })
  275. test('title tag in SVG namesapce', () => {
  276. const ast = parse('<svg><title><test/></title></svg>', parserOptions)
  277. const elementSvg = ast.children[0] as ElementNode
  278. const elementTitle = elementSvg.children[0] as ElementNode
  279. const element = elementTitle.children[0] as ElementNode
  280. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  281. expect(element.ns).toBe(DOMNamespaces.HTML)
  282. })
  283. test('SVG in HTML namesapce', () => {
  284. const ast = parse('<html><svg></svg></html>', parserOptions)
  285. const elementHtml = ast.children[0] as ElementNode
  286. const element = elementHtml.children[0] as ElementNode
  287. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  288. expect(element.ns).toBe(DOMNamespaces.SVG)
  289. })
  290. test('MATH in HTML namesapce', () => {
  291. const ast = parse('<html><math></math></html>', parserOptions)
  292. const elementHtml = ast.children[0] as ElementNode
  293. const element = elementHtml.children[0] as ElementNode
  294. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  295. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  296. })
  297. })
  298. })