parse.spec.ts 16 KB

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