parse.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. import {
  2. baseParse as parse,
  3. NodeTypes,
  4. ElementNode,
  5. TextNode,
  6. ErrorCodes,
  7. ElementTypes,
  8. InterpolationNode,
  9. AttributeNode,
  10. ConstantTypes
  11. } from '@vue/compiler-core'
  12. import { parserOptions, DOMNamespaces } from '../src/parserOptions'
  13. describe('DOM parser', () => {
  14. describe('Text', () => {
  15. test('textarea handles comments/elements as just text', () => {
  16. const ast = parse(
  17. '<textarea>some<div>text</div>and<!--comment--></textarea>',
  18. parserOptions
  19. )
  20. const element = ast.children[0] as ElementNode
  21. const text = element.children[0] as TextNode
  22. expect(text).toStrictEqual({
  23. type: NodeTypes.TEXT,
  24. content: 'some<div>text</div>and<!--comment-->',
  25. loc: {
  26. start: { offset: 10, line: 1, column: 11 },
  27. end: { offset: 46, line: 1, column: 47 },
  28. source: 'some<div>text</div>and<!--comment-->'
  29. }
  30. })
  31. })
  32. test('textarea handles character references', () => {
  33. const ast = parse('<textarea>&amp;</textarea>', parserOptions)
  34. const element = ast.children[0] as ElementNode
  35. const text = element.children[0] as TextNode
  36. expect(text).toStrictEqual({
  37. type: NodeTypes.TEXT,
  38. content: '&',
  39. loc: {
  40. start: { offset: 10, line: 1, column: 11 },
  41. end: { offset: 15, line: 1, column: 16 },
  42. source: '&amp;'
  43. }
  44. })
  45. })
  46. test('textarea support interpolation', () => {
  47. const ast = parse('<textarea><div>{{ foo }}</textarea>', parserOptions)
  48. const element = ast.children[0] as ElementNode
  49. expect(element.children).toMatchObject([
  50. { type: NodeTypes.TEXT, content: `<div>` },
  51. {
  52. type: NodeTypes.INTERPOLATION,
  53. content: {
  54. type: NodeTypes.SIMPLE_EXPRESSION,
  55. content: `foo`,
  56. isStatic: false
  57. }
  58. }
  59. ])
  60. })
  61. test('style handles comments/elements as just a text', () => {
  62. const ast = parse(
  63. '<style>some<div>text</div>and<!--comment--></style>',
  64. parserOptions
  65. )
  66. const element = ast.children[0] as ElementNode
  67. const text = element.children[0] as TextNode
  68. expect(text).toStrictEqual({
  69. type: NodeTypes.TEXT,
  70. content: 'some<div>text</div>and<!--comment-->',
  71. loc: {
  72. start: { offset: 7, line: 1, column: 8 },
  73. end: { offset: 43, line: 1, column: 44 },
  74. source: 'some<div>text</div>and<!--comment-->'
  75. }
  76. })
  77. })
  78. test("style doesn't handle character references", () => {
  79. const ast = parse('<style>&amp;</style>', parserOptions)
  80. const element = ast.children[0] as ElementNode
  81. const text = element.children[0] as TextNode
  82. expect(text).toStrictEqual({
  83. type: NodeTypes.TEXT,
  84. content: '&amp;',
  85. loc: {
  86. start: { offset: 7, line: 1, column: 8 },
  87. end: { offset: 12, line: 1, column: 13 },
  88. source: '&amp;'
  89. }
  90. })
  91. })
  92. test('CDATA', () => {
  93. const ast = parse('<svg><![CDATA[some text]]></svg>', parserOptions)
  94. const text = (ast.children[0] as ElementNode).children![0] as TextNode
  95. expect(text).toStrictEqual({
  96. type: NodeTypes.TEXT,
  97. content: 'some text',
  98. loc: {
  99. start: { offset: 14, line: 1, column: 15 },
  100. end: { offset: 23, line: 1, column: 24 },
  101. source: 'some text'
  102. }
  103. })
  104. })
  105. test('<pre> tag should preserve raw whitespace', () => {
  106. const rawText = ` \na <div>foo \n bar</div> \n c`
  107. const ast = parse(`<pre>${rawText}</pre>`, parserOptions)
  108. expect((ast.children[0] as ElementNode).children).toMatchObject([
  109. {
  110. type: NodeTypes.TEXT,
  111. content: ` \na `
  112. },
  113. {
  114. type: NodeTypes.ELEMENT,
  115. children: [
  116. {
  117. type: NodeTypes.TEXT,
  118. content: `foo \n bar`
  119. }
  120. ]
  121. },
  122. {
  123. type: NodeTypes.TEXT,
  124. content: ` \n c`
  125. }
  126. ])
  127. })
  128. // #908
  129. test('<pre> tag should remove leading newline', () => {
  130. const rawText = `\nhello<div>\nbye</div>`
  131. const ast = parse(`<pre>${rawText}</pre>`, parserOptions)
  132. expect((ast.children[0] as ElementNode).children).toMatchObject([
  133. {
  134. type: NodeTypes.TEXT,
  135. content: `hello`
  136. },
  137. {
  138. type: NodeTypes.ELEMENT,
  139. children: [
  140. {
  141. type: NodeTypes.TEXT,
  142. // should not remove the leading newline for nested elements
  143. content: `\nbye`
  144. }
  145. ]
  146. }
  147. ])
  148. })
  149. // #945
  150. test('&nbsp; should not be condensed', () => {
  151. const nbsp = String.fromCharCode(160)
  152. const ast = parse(`foo&nbsp;&nbsp;bar`, parserOptions)
  153. expect(ast.children[0]).toMatchObject({
  154. type: NodeTypes.TEXT,
  155. content: `foo${nbsp}${nbsp}bar`
  156. })
  157. })
  158. // https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
  159. test('HTML entities compatibility in text', () => {
  160. const ast = parse('&ampersand;', parserOptions)
  161. const text = ast.children[0] as TextNode
  162. expect(text).toStrictEqual({
  163. type: NodeTypes.TEXT,
  164. content: '&ersand;',
  165. loc: {
  166. start: { offset: 0, line: 1, column: 1 },
  167. end: { offset: 11, line: 1, column: 12 },
  168. source: '&ampersand;'
  169. }
  170. })
  171. })
  172. // https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
  173. test('HTML entities compatibility in attribute', () => {
  174. const ast = parse(
  175. '<div a="&ampersand;" b="&amp;ersand;" c="&amp!"></div>',
  176. parserOptions
  177. )
  178. const element = ast.children[0] as ElementNode
  179. const text1 = (element.props[0] as AttributeNode).value
  180. const text2 = (element.props[1] as AttributeNode).value
  181. const text3 = (element.props[2] as AttributeNode).value
  182. expect(text1).toStrictEqual({
  183. type: NodeTypes.TEXT,
  184. content: '&ampersand;',
  185. loc: {
  186. start: { offset: 7, line: 1, column: 8 },
  187. end: { offset: 20, line: 1, column: 21 },
  188. source: '"&ampersand;"'
  189. }
  190. })
  191. expect(text2).toStrictEqual({
  192. type: NodeTypes.TEXT,
  193. content: '&ersand;',
  194. loc: {
  195. start: { offset: 23, line: 1, column: 24 },
  196. end: { offset: 37, line: 1, column: 38 },
  197. source: '"&amp;ersand;"'
  198. }
  199. })
  200. expect(text3).toStrictEqual({
  201. type: NodeTypes.TEXT,
  202. content: '&!',
  203. loc: {
  204. start: { offset: 40, line: 1, column: 41 },
  205. end: { offset: 47, line: 1, column: 48 },
  206. source: '"&amp!"'
  207. }
  208. })
  209. })
  210. test('Some control character reference should be replaced.', () => {
  211. const ast = parse('&#x86;', parserOptions)
  212. const text = ast.children[0] as TextNode
  213. expect(text).toStrictEqual({
  214. type: NodeTypes.TEXT,
  215. content: '†',
  216. loc: {
  217. start: { offset: 0, line: 1, column: 1 },
  218. end: { offset: 6, line: 1, column: 7 },
  219. source: '&#x86;'
  220. }
  221. })
  222. })
  223. })
  224. describe('Interpolation', () => {
  225. test('HTML entities in interpolation should be translated for backward compatibility.', () => {
  226. const ast = parse('<div>{{ a &lt; b }}</div>', parserOptions)
  227. const element = ast.children[0] as ElementNode
  228. const interpolation = element.children[0] as InterpolationNode
  229. expect(interpolation).toStrictEqual({
  230. type: NodeTypes.INTERPOLATION,
  231. content: {
  232. type: NodeTypes.SIMPLE_EXPRESSION,
  233. content: `a < b`,
  234. isStatic: false,
  235. constType: ConstantTypes.NOT_CONSTANT,
  236. loc: {
  237. start: { offset: 8, line: 1, column: 9 },
  238. end: { offset: 16, line: 1, column: 17 },
  239. source: 'a &lt; b'
  240. }
  241. },
  242. loc: {
  243. start: { offset: 5, line: 1, column: 6 },
  244. end: { offset: 19, line: 1, column: 20 },
  245. source: '{{ a &lt; b }}'
  246. }
  247. })
  248. })
  249. })
  250. describe('Element', () => {
  251. test('void element', () => {
  252. const ast = parse('<img>after', parserOptions)
  253. const element = ast.children[0] as ElementNode
  254. expect(element).toStrictEqual({
  255. type: NodeTypes.ELEMENT,
  256. ns: DOMNamespaces.HTML,
  257. tag: 'img',
  258. tagType: ElementTypes.ELEMENT,
  259. props: [],
  260. isSelfClosing: false,
  261. children: [],
  262. loc: {
  263. start: { offset: 0, line: 1, column: 1 },
  264. end: { offset: 5, line: 1, column: 6 },
  265. source: '<img>'
  266. },
  267. codegenNode: undefined
  268. })
  269. })
  270. test('native element', () => {
  271. const ast = parse('<div></div><comp></comp><Comp></Comp>', parserOptions)
  272. expect(ast.children[0]).toMatchObject({
  273. type: NodeTypes.ELEMENT,
  274. tag: 'div',
  275. tagType: ElementTypes.ELEMENT
  276. })
  277. expect(ast.children[1]).toMatchObject({
  278. type: NodeTypes.ELEMENT,
  279. tag: 'comp',
  280. tagType: ElementTypes.COMPONENT
  281. })
  282. expect(ast.children[2]).toMatchObject({
  283. type: NodeTypes.ELEMENT,
  284. tag: 'Comp',
  285. tagType: ElementTypes.COMPONENT
  286. })
  287. })
  288. test('Strict end tag detection for textarea.', () => {
  289. const ast = parse(
  290. '<textarea>hello</textarea</textarea0></texTArea a="<>">',
  291. {
  292. ...parserOptions,
  293. onError: err => {
  294. if (err.code !== ErrorCodes.END_TAG_WITH_ATTRIBUTES) {
  295. throw err
  296. }
  297. }
  298. }
  299. )
  300. const element = ast.children[0] as ElementNode
  301. const text = element.children[0] as TextNode
  302. expect(ast.children.length).toBe(1)
  303. expect(text).toStrictEqual({
  304. type: NodeTypes.TEXT,
  305. content: 'hello</textarea</textarea0>',
  306. loc: {
  307. start: { offset: 10, line: 1, column: 11 },
  308. end: { offset: 37, line: 1, column: 38 },
  309. source: 'hello</textarea</textarea0>'
  310. }
  311. })
  312. })
  313. })
  314. describe('Namespaces', () => {
  315. test('HTML namespace', () => {
  316. const ast = parse('<html>test</html>', parserOptions)
  317. const element = ast.children[0] as ElementNode
  318. expect(element.ns).toBe(DOMNamespaces.HTML)
  319. })
  320. test('SVG namespace', () => {
  321. const ast = parse('<svg>test</svg>', parserOptions)
  322. const element = ast.children[0] as ElementNode
  323. expect(element.ns).toBe(DOMNamespaces.SVG)
  324. })
  325. test('MATH_ML namespace', () => {
  326. const ast = parse('<math>test</math>', parserOptions)
  327. const element = ast.children[0] as ElementNode
  328. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  329. })
  330. test('SVG in MATH_ML namespace', () => {
  331. const ast = parse(
  332. '<math><annotation-xml><svg></svg></annotation-xml></math>',
  333. parserOptions
  334. )
  335. const elementMath = ast.children[0] as ElementNode
  336. const elementAnnotation = elementMath.children[0] as ElementNode
  337. const elementSvg = elementAnnotation.children[0] as ElementNode
  338. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  339. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  340. })
  341. test('html text/html in MATH_ML namespace', () => {
  342. const ast = parse(
  343. '<math><annotation-xml encoding="text/html"><test/></annotation-xml></math>',
  344. parserOptions
  345. )
  346. const elementMath = ast.children[0] as ElementNode
  347. const elementAnnotation = elementMath.children[0] as ElementNode
  348. const element = elementAnnotation.children[0] as ElementNode
  349. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  350. expect(element.ns).toBe(DOMNamespaces.HTML)
  351. })
  352. test('html application/xhtml+xml in MATH_ML namespace', () => {
  353. const ast = parse(
  354. '<math><annotation-xml encoding="application/xhtml+xml"><test/></annotation-xml></math>',
  355. parserOptions
  356. )
  357. const elementMath = ast.children[0] as ElementNode
  358. const elementAnnotation = elementMath.children[0] as ElementNode
  359. const element = elementAnnotation.children[0] as ElementNode
  360. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  361. expect(element.ns).toBe(DOMNamespaces.HTML)
  362. })
  363. test('mtext malignmark in MATH_ML namespace', () => {
  364. const ast = parse(
  365. '<math><mtext><malignmark/></mtext></math>',
  366. parserOptions
  367. )
  368. const elementMath = ast.children[0] as ElementNode
  369. const elementText = elementMath.children[0] as ElementNode
  370. const element = elementText.children[0] as ElementNode
  371. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  372. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  373. })
  374. test('mtext and not malignmark tag in MATH_ML namespace', () => {
  375. const ast = parse('<math><mtext><test/></mtext></math>', parserOptions)
  376. const elementMath = ast.children[0] as ElementNode
  377. const elementText = elementMath.children[0] as ElementNode
  378. const element = elementText.children[0] as ElementNode
  379. expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML)
  380. expect(element.ns).toBe(DOMNamespaces.HTML)
  381. })
  382. test('foreignObject tag in SVG namespace', () => {
  383. const ast = parse(
  384. '<svg><foreignObject><test/></foreignObject></svg>',
  385. parserOptions
  386. )
  387. const elementSvg = ast.children[0] as ElementNode
  388. const elementForeignObject = elementSvg.children[0] as ElementNode
  389. const element = elementForeignObject.children[0] as ElementNode
  390. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  391. expect(element.ns).toBe(DOMNamespaces.HTML)
  392. })
  393. test('desc tag in SVG namespace', () => {
  394. const ast = parse('<svg><desc><test/></desc></svg>', parserOptions)
  395. const elementSvg = ast.children[0] as ElementNode
  396. const elementDesc = elementSvg.children[0] as ElementNode
  397. const element = elementDesc.children[0] as ElementNode
  398. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  399. expect(element.ns).toBe(DOMNamespaces.HTML)
  400. })
  401. test('title tag in SVG namespace', () => {
  402. const ast = parse('<svg><title><test/></title></svg>', parserOptions)
  403. const elementSvg = ast.children[0] as ElementNode
  404. const elementTitle = elementSvg.children[0] as ElementNode
  405. const element = elementTitle.children[0] as ElementNode
  406. expect(elementSvg.ns).toBe(DOMNamespaces.SVG)
  407. expect(element.ns).toBe(DOMNamespaces.HTML)
  408. })
  409. test('SVG in HTML namespace', () => {
  410. const ast = parse('<html><svg></svg></html>', parserOptions)
  411. const elementHtml = ast.children[0] as ElementNode
  412. const element = elementHtml.children[0] as ElementNode
  413. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  414. expect(element.ns).toBe(DOMNamespaces.SVG)
  415. })
  416. test('MATH in HTML namespace', () => {
  417. const ast = parse('<html><math></math></html>', parserOptions)
  418. const elementHtml = ast.children[0] as ElementNode
  419. const element = elementHtml.children[0] as ElementNode
  420. expect(elementHtml.ns).toBe(DOMNamespaces.HTML)
  421. expect(element.ns).toBe(DOMNamespaces.MATH_ML)
  422. })
  423. })
  424. })