parse.spec.ts 12 KB

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