parse.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import {
  2. baseParse as 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. loc: {
  27. start: { offset: 10, line: 1, column: 11 },
  28. end: { offset: 46, line: 1, column: 47 },
  29. source: 'some<div>text</div>and<!--comment-->'
  30. }
  31. })
  32. })
  33. test('textarea handles character references', () => {
  34. const ast = parse('<textarea>&amp;</textarea>', parserOptions)
  35. const element = ast.children[0] as ElementNode
  36. const text = element.children[0] as TextNode
  37. expect(text).toStrictEqual({
  38. type: NodeTypes.TEXT,
  39. content: '&',
  40. loc: {
  41. start: { offset: 10, line: 1, column: 11 },
  42. end: { offset: 15, line: 1, column: 16 },
  43. source: '&amp;'
  44. }
  45. })
  46. })
  47. test('textarea support interpolation', () => {
  48. const ast = parse('<textarea><div>{{ foo }}</textarea>', parserOptions)
  49. const element = ast.children[0] as ElementNode
  50. expect(element.children).toMatchObject([
  51. { type: NodeTypes.TEXT, content: `<div>` },
  52. {
  53. type: NodeTypes.INTERPOLATION,
  54. content: {
  55. type: NodeTypes.SIMPLE_EXPRESSION,
  56. content: `foo`,
  57. isStatic: false
  58. }
  59. }
  60. ])
  61. })
  62. test('style handles comments/elements as just a text', () => {
  63. const ast = parse(
  64. '<style>some<div>text</div>and<!--comment--></style>',
  65. parserOptions
  66. )
  67. const element = ast.children[0] as ElementNode
  68. const text = element.children[0] as TextNode
  69. expect(text).toStrictEqual({
  70. type: NodeTypes.TEXT,
  71. content: 'some<div>text</div>and<!--comment-->',
  72. loc: {
  73. start: { offset: 7, line: 1, column: 8 },
  74. end: { offset: 43, line: 1, column: 44 },
  75. source: 'some<div>text</div>and<!--comment-->'
  76. }
  77. })
  78. })
  79. test("style doesn't handle character references", () => {
  80. const ast = parse('<style>&amp;</style>', parserOptions)
  81. const element = ast.children[0] as ElementNode
  82. const text = element.children[0] as TextNode
  83. expect(text).toStrictEqual({
  84. type: NodeTypes.TEXT,
  85. content: '&amp;',
  86. loc: {
  87. start: { offset: 7, line: 1, column: 8 },
  88. end: { offset: 12, line: 1, column: 13 },
  89. source: '&amp;'
  90. }
  91. })
  92. })
  93. test('CDATA', () => {
  94. const ast = parse('<svg><![CDATA[some text]]></svg>', parserOptions)
  95. const text = (ast.children[0] as ElementNode).children![0] as TextNode
  96. expect(text).toStrictEqual({
  97. type: NodeTypes.TEXT,
  98. content: 'some text',
  99. loc: {
  100. start: { offset: 14, line: 1, column: 15 },
  101. end: { offset: 23, line: 1, column: 24 },
  102. source: 'some text'
  103. }
  104. })
  105. })
  106. test('<pre> tag should preserve raw whitespace', () => {
  107. const rawText = ` \na b \n c`
  108. const ast = parse(`<pre>${rawText}</pre>`, parserOptions)
  109. expect((ast.children[0] as ElementNode).children[0]).toMatchObject({
  110. type: NodeTypes.TEXT,
  111. content: rawText
  112. })
  113. })
  114. })
  115. describe('Interpolation', () => {
  116. test('HTML entities in interpolation should be translated for backward compatibility.', () => {
  117. const ast = parse('<div>{{ a &lt; b }}</div>', parserOptions)
  118. const element = ast.children[0] as ElementNode
  119. const interpolation = element.children[0] as InterpolationNode
  120. expect(interpolation).toStrictEqual({
  121. type: NodeTypes.INTERPOLATION,
  122. content: {
  123. type: NodeTypes.SIMPLE_EXPRESSION,
  124. content: `a < b`,
  125. isStatic: false,
  126. isConstant: false,
  127. loc: {
  128. start: { offset: 8, line: 1, column: 9 },
  129. end: { offset: 16, line: 1, column: 17 },
  130. source: 'a &lt; b'
  131. }
  132. },
  133. loc: {
  134. start: { offset: 5, line: 1, column: 6 },
  135. end: { offset: 19, line: 1, column: 20 },
  136. source: '{{ a &lt; b }}'
  137. }
  138. })
  139. })
  140. })
  141. describe('Element', () => {
  142. test('void element', () => {
  143. const ast = parse('<img>after', parserOptions)
  144. const element = ast.children[0] as ElementNode
  145. expect(element).toStrictEqual({
  146. type: NodeTypes.ELEMENT,
  147. ns: DOMNamespaces.HTML,
  148. tag: 'img',
  149. tagType: ElementTypes.ELEMENT,
  150. props: [],
  151. isSelfClosing: false,
  152. children: [],
  153. loc: {
  154. start: { offset: 0, line: 1, column: 1 },
  155. end: { offset: 5, line: 1, column: 6 },
  156. source: '<img>'
  157. },
  158. codegenNode: undefined
  159. })
  160. })
  161. test('native element', () => {
  162. const ast = parse('<div></div><comp></comp><Comp></Comp>', parserOptions)
  163. expect(ast.children[0]).toMatchObject({
  164. type: NodeTypes.ELEMENT,
  165. tag: 'div',
  166. tagType: ElementTypes.ELEMENT
  167. })
  168. expect(ast.children[1]).toMatchObject({
  169. type: NodeTypes.ELEMENT,
  170. tag: 'comp',
  171. tagType: ElementTypes.COMPONENT
  172. })
  173. expect(ast.children[2]).toMatchObject({
  174. type: NodeTypes.ELEMENT,
  175. tag: 'Comp',
  176. tagType: ElementTypes.COMPONENT
  177. })
  178. })
  179. test('Strict end tag detection for textarea.', () => {
  180. const ast = parse(
  181. '<textarea>hello</textarea</textarea0></texTArea a="<>">',
  182. {
  183. ...parserOptions,
  184. onError: err => {
  185. if (err.code !== ErrorCodes.END_TAG_WITH_ATTRIBUTES) {
  186. throw err
  187. }
  188. }
  189. }
  190. )
  191. const element = ast.children[0] as ElementNode
  192. const text = element.children[0] as TextNode
  193. expect(ast.children.length).toBe(1)
  194. expect(text).toStrictEqual({
  195. type: NodeTypes.TEXT,
  196. content: 'hello</textarea</textarea0>',
  197. loc: {
  198. start: { offset: 10, line: 1, column: 11 },
  199. end: { offset: 37, line: 1, column: 38 },
  200. source: 'hello</textarea</textarea0>'
  201. }
  202. })
  203. })
  204. })
  205. describe('Namespaces', () => {
  206. test('HTML namespace', () => {
  207. const ast = parse('<html>test</html>', parserOptions)
  208. const element = ast.children[0] as ElementNode
  209. expect(element.ns).toBe(DOMNamespaces.HTML)
  210. })
  211. test('SVG namespace', () => {
  212. const ast = parse('<svg>test</svg>', parserOptions)
  213. const element = ast.children[0] as ElementNode
  214. expect(element.ns).toBe(DOMNamespaces.SVG)
  215. })
  216. test('MATH_ML namespace', () => {
  217. const ast = parse('<math>test</math>', parserOptions)
  218. const element = ast.children[0] as ElementNode
  219. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  220. })
  221. test('SVG in MATH_ML namespace', () => {
  222. const ast = parse(
  223. '<math><annotation-xml><svg></svg></annotation-xml></math>',
  224. parserOptions
  225. )
  226. const elementMath = ast.children[0] as ElementNode
  227. const elementAnnotation = elementMath.children[0] as ElementNode
  228. const elementSvg = elementAnnotation.children[0] as ElementNode
  229. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  230. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  231. })
  232. test('html text/html in MATH_ML namespace', () => {
  233. const ast = parse(
  234. '<math><annotation-xml encoding="text/html"><test/></annotation-xml></math>',
  235. parserOptions
  236. )
  237. const elementMath = ast.children[0] as ElementNode
  238. const elementAnnotation = elementMath.children[0] as ElementNode
  239. const element = elementAnnotation.children[0] as ElementNode
  240. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  241. expect(element.ns).toBe(DOMNamespaces.HTML)
  242. })
  243. test('html application/xhtml+xml in MATH_ML namespace', () => {
  244. const ast = parse(
  245. '<math><annotation-xml encoding="application/xhtml+xml"><test/></annotation-xml></math>',
  246. parserOptions
  247. )
  248. const elementMath = ast.children[0] as ElementNode
  249. const elementAnnotation = elementMath.children[0] as ElementNode
  250. const element = elementAnnotation.children[0] as ElementNode
  251. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  252. expect(element.ns).toBe(DOMNamespaces.HTML)
  253. })
  254. test('mtext malignmark in MATH_ML namespace', () => {
  255. const ast = parse(
  256. '<math><mtext><malignmark/></mtext></math>',
  257. parserOptions
  258. )
  259. const elementMath = ast.children[0] as ElementNode
  260. const elementText = elementMath.children[0] as ElementNode
  261. const element = elementText.children[0] as ElementNode
  262. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  263. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  264. })
  265. test('mtext and not malignmark tag in MATH_ML namespace', () => {
  266. const ast = parse('<math><mtext><test/></mtext></math>', parserOptions)
  267. const elementMath = ast.children[0] as ElementNode
  268. const elementText = elementMath.children[0] as ElementNode
  269. const element = elementText.children[0] as ElementNode
  270. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  271. expect(element.ns).toBe(DOMNamespaces.HTML)
  272. })
  273. test('foreignObject tag in SVG namespace', () => {
  274. const ast = parse(
  275. '<svg><foreignObject><test/></foreignObject></svg>',
  276. parserOptions
  277. )
  278. const elementSvg = ast.children[0] as ElementNode
  279. const elementForeignObject = elementSvg.children[0] as ElementNode
  280. const element = elementForeignObject.children[0] as ElementNode
  281. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  282. expect(element.ns).toBe(DOMNamespaces.HTML)
  283. })
  284. test('desc tag in SVG namespace', () => {
  285. const ast = parse('<svg><desc><test/></desc></svg>', parserOptions)
  286. const elementSvg = ast.children[0] as ElementNode
  287. const elementDesc = elementSvg.children[0] as ElementNode
  288. const element = elementDesc.children[0] as ElementNode
  289. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  290. expect(element.ns).toBe(DOMNamespaces.HTML)
  291. })
  292. test('title tag in SVG namespace', () => {
  293. const ast = parse('<svg><title><test/></title></svg>', parserOptions)
  294. const elementSvg = ast.children[0] as ElementNode
  295. const elementTitle = elementSvg.children[0] as ElementNode
  296. const element = elementTitle.children[0] as ElementNode
  297. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  298. expect(element.ns).toBe(DOMNamespaces.HTML)
  299. })
  300. test('SVG in HTML namespace', () => {
  301. const ast = parse('<html><svg></svg></html>', parserOptions)
  302. const elementHtml = ast.children[0] as ElementNode
  303. const element = elementHtml.children[0] as ElementNode
  304. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  305. expect(element.ns).toBe(DOMNamespaces.SVG)
  306. })
  307. test('MATH in HTML namespace', () => {
  308. const ast = parse('<html><math></math></html>', parserOptions)
  309. const elementHtml = ast.children[0] as ElementNode
  310. const element = elementHtml.children[0] as ElementNode
  311. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  312. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  313. })
  314. })
  315. })