parse.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import {
  2. type AttributeNode,
  3. ConstantTypes,
  4. type ElementNode,
  5. ElementTypes,
  6. type InterpolationNode,
  7. Namespaces,
  8. NodeTypes,
  9. type TextNode,
  10. baseParse as parse,
  11. } from '@vue/compiler-core'
  12. import { parserOptions } 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 entities', () => {
  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: Namespaces.HTML,
  257. tag: 'img',
  258. tagType: ElementTypes.ELEMENT,
  259. props: [],
  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>',
  290. parserOptions,
  291. )
  292. const element = ast.children[0] as ElementNode
  293. const text = element.children[0] as TextNode
  294. expect(ast.children.length).toBe(1)
  295. expect(text).toStrictEqual({
  296. type: NodeTypes.TEXT,
  297. content: 'hello</textarea</textarea0>',
  298. loc: {
  299. start: { offset: 10, line: 1, column: 11 },
  300. end: { offset: 37, line: 1, column: 38 },
  301. source: 'hello</textarea</textarea0>',
  302. },
  303. })
  304. })
  305. })
  306. describe('Namespaces', () => {
  307. test('HTML namespace', () => {
  308. const ast = parse('<html>test</html>', parserOptions)
  309. const element = ast.children[0] as ElementNode
  310. expect(element.ns).toBe(Namespaces.HTML)
  311. })
  312. test('SVG namespace', () => {
  313. const ast = parse('<svg>test</svg>', parserOptions)
  314. const element = ast.children[0] as ElementNode
  315. expect(element.ns).toBe(Namespaces.SVG)
  316. })
  317. test('MATH_ML namespace', () => {
  318. const ast = parse('<math>test</math>', parserOptions)
  319. const element = ast.children[0] as ElementNode
  320. expect(element.ns).toBe(Namespaces.MATH_ML)
  321. })
  322. test('SVG in MATH_ML namespace', () => {
  323. const ast = parse(
  324. '<math><annotation-xml><svg></svg></annotation-xml></math>',
  325. parserOptions,
  326. )
  327. const elementMath = ast.children[0] as ElementNode
  328. const elementAnnotation = elementMath.children[0] as ElementNode
  329. const elementSvg = elementAnnotation.children[0] as ElementNode
  330. expect(elementMath.ns).toBe(Namespaces.MATH_ML)
  331. expect(elementSvg.ns).toBe(Namespaces.SVG)
  332. })
  333. test('html text/html in MATH_ML namespace', () => {
  334. const ast = parse(
  335. '<math><annotation-xml encoding="text/html"><test/></annotation-xml></math>',
  336. parserOptions,
  337. )
  338. const elementMath = ast.children[0] as ElementNode
  339. const elementAnnotation = elementMath.children[0] as ElementNode
  340. const element = elementAnnotation.children[0] as ElementNode
  341. expect(elementMath.ns).toBe(Namespaces.MATH_ML)
  342. expect(element.ns).toBe(Namespaces.HTML)
  343. })
  344. test('html application/xhtml+xml in MATH_ML namespace', () => {
  345. const ast = parse(
  346. '<math><annotation-xml encoding="application/xhtml+xml"><test/></annotation-xml></math>',
  347. parserOptions,
  348. )
  349. const elementMath = ast.children[0] as ElementNode
  350. const elementAnnotation = elementMath.children[0] as ElementNode
  351. const element = elementAnnotation.children[0] as ElementNode
  352. expect(elementMath.ns).toBe(Namespaces.MATH_ML)
  353. expect(element.ns).toBe(Namespaces.HTML)
  354. })
  355. test('mtext malignmark in MATH_ML namespace', () => {
  356. const ast = parse(
  357. '<math><mtext><malignmark/></mtext></math>',
  358. parserOptions,
  359. )
  360. const elementMath = ast.children[0] as ElementNode
  361. const elementText = elementMath.children[0] as ElementNode
  362. const element = elementText.children[0] as ElementNode
  363. expect(elementMath.ns).toBe(Namespaces.MATH_ML)
  364. expect(element.ns).toBe(Namespaces.MATH_ML)
  365. })
  366. test('mtext and not malignmark tag in MATH_ML namespace', () => {
  367. const ast = parse('<math><mtext><test/></mtext></math>', parserOptions)
  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(Namespaces.MATH_ML)
  372. expect(element.ns).toBe(Namespaces.HTML)
  373. })
  374. test('foreignObject tag in SVG namespace', () => {
  375. const ast = parse(
  376. '<svg><foreignObject><test/></foreignObject></svg>',
  377. parserOptions,
  378. )
  379. const elementSvg = ast.children[0] as ElementNode
  380. const elementForeignObject = elementSvg.children[0] as ElementNode
  381. const element = elementForeignObject.children[0] as ElementNode
  382. expect(elementSvg.ns).toBe(Namespaces.SVG)
  383. expect(element.ns).toBe(Namespaces.HTML)
  384. })
  385. test('desc tag in SVG namespace', () => {
  386. const ast = parse('<svg><desc><test/></desc></svg>', parserOptions)
  387. const elementSvg = ast.children[0] as ElementNode
  388. const elementDesc = elementSvg.children[0] as ElementNode
  389. const element = elementDesc.children[0] as ElementNode
  390. expect(elementSvg.ns).toBe(Namespaces.SVG)
  391. expect(element.ns).toBe(Namespaces.HTML)
  392. })
  393. test('title tag in SVG namespace', () => {
  394. const ast = parse('<svg><title><test/></title></svg>', parserOptions)
  395. const elementSvg = ast.children[0] as ElementNode
  396. const elementTitle = elementSvg.children[0] as ElementNode
  397. const element = elementTitle.children[0] as ElementNode
  398. expect(elementSvg.ns).toBe(Namespaces.SVG)
  399. expect(element.ns).toBe(Namespaces.HTML)
  400. })
  401. test('SVG in HTML namespace', () => {
  402. const ast = parse('<html><svg></svg></html>', parserOptions)
  403. const elementHtml = ast.children[0] as ElementNode
  404. const element = elementHtml.children[0] as ElementNode
  405. expect(elementHtml.ns).toBe(Namespaces.HTML)
  406. expect(element.ns).toBe(Namespaces.SVG)
  407. })
  408. test('MATH in HTML namespace', () => {
  409. const ast = parse('<html><math></math></html>', parserOptions)
  410. const elementHtml = ast.children[0] as ElementNode
  411. const element = elementHtml.children[0] as ElementNode
  412. expect(elementHtml.ns).toBe(Namespaces.HTML)
  413. expect(element.ns).toBe(Namespaces.MATH_ML)
  414. })
  415. test('root ns', () => {
  416. const ast = parse('<foreignObject><test/></foreignObject>', {
  417. ...parserOptions,
  418. ns: Namespaces.SVG,
  419. })
  420. const elementForieng = ast.children[0] as ElementNode
  421. const element = elementForieng.children[0] as ElementNode
  422. expect(elementForieng.ns).toBe(Namespaces.SVG)
  423. expect(element.ns).toBe(Namespaces.HTML)
  424. })
  425. test('correct XML handling with root ns', () => {
  426. // when root ns is an XML namespace, there should be no special content
  427. // treatment for <script>, <style>, <textarea> etc.
  428. const ast = parse('<script><g/><g/></script>', {
  429. ...parserOptions,
  430. ns: Namespaces.SVG,
  431. })
  432. const elementSvg = ast.children[0] as ElementNode
  433. // should parse as nodes instead of text
  434. expect(elementSvg.children).toMatchObject([
  435. { type: NodeTypes.ELEMENT, tag: 'g' },
  436. { type: NodeTypes.ELEMENT, tag: 'g' },
  437. ])
  438. })
  439. })
  440. })