parse.spec.ts 15 KB

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