parser.spec.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import { parse } from 'compiler/parser/index'
  2. import { extend } from 'shared/util'
  3. import { baseOptions } from 'web/compiler/options'
  4. import { isIE, isEdge } from 'core/util/env'
  5. describe('parser', () => {
  6. it('simple element', () => {
  7. const ast = parse('<h1>hello world</h1>', baseOptions)
  8. expect(ast.tag).toBe('h1')
  9. expect(ast.plain).toBe(true)
  10. expect(ast.children[0].text).toBe('hello world')
  11. })
  12. it('interpolation in element', () => {
  13. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  14. expect(ast.tag).toBe('h1')
  15. expect(ast.plain).toBe(true)
  16. expect(ast.children[0].expression).toBe('_s(msg)')
  17. })
  18. it('child elements', () => {
  19. const ast = parse('<ul><li>hello world</li></ul>', baseOptions)
  20. expect(ast.tag).toBe('ul')
  21. expect(ast.plain).toBe(true)
  22. expect(ast.children[0].tag).toBe('li')
  23. expect(ast.children[0].plain).toBe(true)
  24. expect(ast.children[0].children[0].text).toBe('hello world')
  25. expect(ast.children[0].parent).toBe(ast)
  26. })
  27. it('unary element', () => {
  28. const ast = parse('<hr>', baseOptions)
  29. expect(ast.tag).toBe('hr')
  30. expect(ast.plain).toBe(true)
  31. expect(ast.children.length).toBe(0)
  32. })
  33. it('svg element', () => {
  34. const ast = parse('<svg><text>hello world</text></svg>', baseOptions)
  35. expect(ast.tag).toBe('svg')
  36. expect(ast.ns).toBe('svg')
  37. expect(ast.plain).toBe(true)
  38. expect(ast.children[0].tag).toBe('text')
  39. expect(ast.children[0].children[0].text).toBe('hello world')
  40. expect(ast.children[0].parent).toBe(ast)
  41. })
  42. it('camelCase element', () => {
  43. const ast = parse('<MyComponent><p>hello world</p></MyComponent>', baseOptions)
  44. expect(ast.tag).toBe('MyComponent')
  45. expect(ast.plain).toBe(true)
  46. expect(ast.children[0].tag).toBe('p')
  47. expect(ast.children[0].plain).toBe(true)
  48. expect(ast.children[0].children[0].text).toBe('hello world')
  49. expect(ast.children[0].parent).toBe(ast)
  50. })
  51. it('forbidden element', () => {
  52. // style
  53. const styleAst = parse('<style>error { color: red; }</style>', baseOptions)
  54. expect(styleAst.tag).toBe('style')
  55. expect(styleAst.plain).toBe(true)
  56. expect(styleAst.forbidden).toBe(true)
  57. expect(styleAst.children[0].text).toBe('error { color: red; }')
  58. expect('Templates should only be responsible for mapping the state').toHaveBeenWarned()
  59. // script
  60. const scriptAst = parse('<script type="text/javascript">alert("hello world!")</script>', baseOptions)
  61. expect(scriptAst.tag).toBe('script')
  62. expect(scriptAst.plain).toBe(false)
  63. expect(scriptAst.forbidden).toBe(true)
  64. expect(scriptAst.children[0].text).toBe('alert("hello world!")')
  65. expect('Templates should only be responsible for mapping the state').toHaveBeenWarned()
  66. })
  67. it('not contain root element', () => {
  68. parse('hello world', baseOptions)
  69. expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
  70. })
  71. it('warn text before root element', () => {
  72. parse('before root {{ interpolation }}<div></div>', baseOptions)
  73. expect('text "before root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
  74. })
  75. it('warn text after root element', () => {
  76. parse('<div></div>after root {{ interpolation }}', baseOptions)
  77. expect('text "after root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
  78. })
  79. it('warn multiple root elements', () => {
  80. parse('<div></div><div></div>', baseOptions)
  81. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  82. })
  83. it('remove duplicate whitespace text nodes caused by comments', () => {
  84. const ast = parse(`<div><a></a> <!----> <a></a></div>`, baseOptions)
  85. expect(ast.children.length).toBe(3)
  86. expect(ast.children[0].tag).toBe('a')
  87. expect(ast.children[1].text).toBe(' ')
  88. expect(ast.children[2].tag).toBe('a')
  89. })
  90. it('remove text nodes between v-if conditions', () => {
  91. const ast = parse(`<div><div v-if="1"></div> <div v-else-if="2"></div> <div v-else></div> <span></span></div>`, baseOptions)
  92. expect(ast.children.length).toBe(3)
  93. expect(ast.children[0].tag).toBe('div')
  94. expect(ast.children[0].ifConditions.length).toBe(3)
  95. expect(ast.children[1].text).toBe(' ') // text
  96. expect(ast.children[2].tag).toBe('span')
  97. })
  98. it('warn non whitespace text between v-if conditions', () => {
  99. parse(`<div><div v-if="1"></div> foo <div v-else></div></div>`, baseOptions)
  100. expect(`text "foo" between v-if and v-else(-if) will be ignored`).toHaveBeenWarned()
  101. })
  102. it('not warn 2 root elements with v-if and v-else', () => {
  103. parse('<div v-if="1"></div><div v-else></div>', baseOptions)
  104. expect('Component template should contain exactly one root element')
  105. .not.toHaveBeenWarned()
  106. })
  107. it('not warn 3 root elements with v-if, v-else-if and v-else', () => {
  108. parse('<div v-if="1"></div><div v-else-if="2"></div><div v-else></div>', baseOptions)
  109. expect('Component template should contain exactly one root element')
  110. .not.toHaveBeenWarned()
  111. })
  112. it('not warn 2 root elements with v-if and v-else on separate lines', () => {
  113. parse(`
  114. <div v-if="1"></div>
  115. <div v-else></div>
  116. `, baseOptions)
  117. expect('Component template should contain exactly one root element')
  118. .not.toHaveBeenWarned()
  119. })
  120. it('not warn 3 or more root elements with v-if, v-else-if and v-else on separate lines', () => {
  121. parse(`
  122. <div v-if="1"></div>
  123. <div v-else-if="2"></div>
  124. <div v-else></div>
  125. `, baseOptions)
  126. expect('Component template should contain exactly one root element')
  127. .not.toHaveBeenWarned()
  128. parse(`
  129. <div v-if="1"></div>
  130. <div v-else-if="2"></div>
  131. <div v-else-if="3"></div>
  132. <div v-else-if="4"></div>
  133. <div v-else></div>
  134. `, baseOptions)
  135. expect('Component template should contain exactly one root element')
  136. .not.toHaveBeenWarned()
  137. })
  138. it('generate correct ast for 2 root elements with v-if and v-else on separate lines', () => {
  139. const ast = parse(`
  140. <div v-if="1"></div>
  141. <p v-else></p>
  142. `, baseOptions)
  143. expect(ast.tag).toBe('div')
  144. expect(ast.ifConditions[1].block.tag).toBe('p')
  145. })
  146. it('generate correct ast for 3 or more root elements with v-if and v-else on separate lines', () => {
  147. const ast = parse(`
  148. <div v-if="1"></div>
  149. <span v-else-if="2"></span>
  150. <p v-else></p>
  151. `, baseOptions)
  152. expect(ast.tag).toBe('div')
  153. expect(ast.ifConditions[0].block.tag).toBe('div')
  154. expect(ast.ifConditions[1].block.tag).toBe('span')
  155. expect(ast.ifConditions[2].block.tag).toBe('p')
  156. const astMore = parse(`
  157. <div v-if="1"></div>
  158. <span v-else-if="2"></span>
  159. <div v-else-if="3"></div>
  160. <span v-else-if="4"></span>
  161. <p v-else></p>
  162. `, baseOptions)
  163. expect(astMore.tag).toBe('div')
  164. expect(astMore.ifConditions[0].block.tag).toBe('div')
  165. expect(astMore.ifConditions[1].block.tag).toBe('span')
  166. expect(astMore.ifConditions[2].block.tag).toBe('div')
  167. expect(astMore.ifConditions[3].block.tag).toBe('span')
  168. expect(astMore.ifConditions[4].block.tag).toBe('p')
  169. })
  170. it('warn 2 root elements with v-if', () => {
  171. parse('<div v-if="1"></div><div v-if="2"></div>', baseOptions)
  172. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  173. })
  174. it('warn 3 root elements with v-if and v-else on first 2', () => {
  175. parse('<div v-if="1"></div><div v-else></div><div></div>', baseOptions)
  176. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  177. })
  178. it('warn 3 root elements with v-if and v-else-if on first 2', () => {
  179. parse('<div v-if="1"></div><div v-else-if></div><div></div>', baseOptions)
  180. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  181. })
  182. it('warn 4 root elements with v-if, v-else-if and v-else on first 2', () => {
  183. parse('<div v-if="1"></div><div v-else-if></div><div v-else></div><div></div>', baseOptions)
  184. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  185. })
  186. it('warn 2 root elements with v-if and v-else with v-for on 2nd', () => {
  187. parse('<div v-if="1"></div><div v-else v-for="i in [1]"></div>', baseOptions)
  188. expect('Cannot use v-for on stateful component root element because it renders multiple elements')
  189. .toHaveBeenWarned()
  190. })
  191. it('warn 2 root elements with v-if and v-else-if with v-for on 2nd', () => {
  192. parse('<div v-if="1"></div><div v-else-if="2" v-for="i in [1]"></div>', baseOptions)
  193. expect('Cannot use v-for on stateful component root element because it renders multiple elements')
  194. .toHaveBeenWarned()
  195. })
  196. it('warn <template> as root element', () => {
  197. parse('<template></template>', baseOptions)
  198. expect('Cannot use <template> as component root element').toHaveBeenWarned()
  199. })
  200. it('warn <slot> as root element', () => {
  201. parse('<slot></slot>', baseOptions)
  202. expect('Cannot use <slot> as component root element').toHaveBeenWarned()
  203. })
  204. it('warn v-for on root element', () => {
  205. parse('<div v-for="item in items"></div>', baseOptions)
  206. expect('Cannot use v-for on stateful component root element').toHaveBeenWarned()
  207. })
  208. it('warn <template> key', () => {
  209. parse('<div><template v-for="i in 10" :key="i"></template></div>', baseOptions)
  210. expect('<template> cannot be keyed').toHaveBeenWarned()
  211. })
  212. it('v-pre directive', () => {
  213. const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
  214. expect(ast.pre).toBe(true)
  215. expect(ast.attrs[0].name).toBe('id')
  216. expect(ast.attrs[0].value).toBe('"message1"')
  217. expect(ast.children[0].children[0].text).toBe('{{msg}}')
  218. })
  219. it('v-for directive basic syntax', () => {
  220. const ast = parse('<ul><li v-for="item in items"></li></ul>', baseOptions)
  221. const liAst = ast.children[0]
  222. expect(liAst.for).toBe('items')
  223. expect(liAst.alias).toBe('item')
  224. })
  225. it('v-for directive iteration syntax', () => {
  226. const ast = parse('<ul><li v-for="(item, index) in items"></li></ul>', baseOptions)
  227. const liAst = ast.children[0]
  228. expect(liAst.for).toBe('items')
  229. expect(liAst.alias).toBe('item')
  230. expect(liAst.iterator1).toBe('index')
  231. expect(liAst.iterator2).toBeUndefined()
  232. })
  233. it('v-for directive iteration syntax (multiple)', () => {
  234. const ast = parse('<ul><li v-for="(item, key, index) in items"></li></ul>', baseOptions)
  235. const liAst = ast.children[0]
  236. expect(liAst.for).toBe('items')
  237. expect(liAst.alias).toBe('item')
  238. expect(liAst.iterator1).toBe('key')
  239. expect(liAst.iterator2).toBe('index')
  240. })
  241. it('v-for directive key', () => {
  242. const ast = parse('<ul><li v-for="item in items" :key="item.uid"></li></ul>', baseOptions)
  243. const liAst = ast.children[0]
  244. expect(liAst.for).toBe('items')
  245. expect(liAst.alias).toBe('item')
  246. expect(liAst.key).toBe('item.uid')
  247. })
  248. it('v-for directive invalid syntax', () => {
  249. parse('<ul><li v-for="item into items"></li></ul>', baseOptions)
  250. expect('Invalid v-for expression').toHaveBeenWarned()
  251. })
  252. it('v-if directive syntax', () => {
  253. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  254. expect(ast.if).toBe('show')
  255. expect(ast.ifConditions[0].exp).toBe('show')
  256. })
  257. it('v-else-if directive syntax', () => {
  258. const ast = parse('<div><p v-if="show">hello</p><span v-else-if="2">elseif</span><p v-else>world</p></div>', baseOptions)
  259. const ifAst = ast.children[0]
  260. const conditionsAst = ifAst.ifConditions
  261. expect(conditionsAst.length).toBe(3)
  262. expect(conditionsAst[1].block.children[0].text).toBe('elseif')
  263. expect(conditionsAst[1].block.parent).toBe(ast)
  264. expect(conditionsAst[2].block.children[0].text).toBe('world')
  265. expect(conditionsAst[2].block.parent).toBe(ast)
  266. })
  267. it('v-else directive syntax', () => {
  268. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  269. const ifAst = ast.children[0]
  270. const conditionsAst = ifAst.ifConditions
  271. expect(conditionsAst.length).toBe(2)
  272. expect(conditionsAst[1].block.children[0].text).toBe('world')
  273. expect(conditionsAst[1].block.parent).toBe(ast)
  274. })
  275. it('v-else-if directive invalid syntax', () => {
  276. parse('<div><p v-else-if="1">world</p></div>', baseOptions)
  277. expect('v-else-if="1" used on element').toHaveBeenWarned()
  278. })
  279. it('v-else directive invalid syntax', () => {
  280. parse('<div><p v-else>world</p></div>', baseOptions)
  281. expect('v-else used on element').toHaveBeenWarned()
  282. })
  283. it('v-once directive syntax', () => {
  284. const ast = parse('<p v-once>world</p>', baseOptions)
  285. expect(ast.once).toBe(true)
  286. })
  287. it('slot tag single syntax', () => {
  288. const ast = parse('<div><slot></slot></div>', baseOptions)
  289. expect(ast.children[0].tag).toBe('slot')
  290. expect(ast.children[0].slotName).toBeUndefined()
  291. })
  292. it('slot tag named syntax', () => {
  293. const ast = parse('<div><slot name="one">hello world</slot></div>', baseOptions)
  294. expect(ast.children[0].tag).toBe('slot')
  295. expect(ast.children[0].slotName).toBe('"one"')
  296. })
  297. it('slot target', () => {
  298. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  299. expect(ast.slotTarget).toBe('"one"')
  300. })
  301. it('component properties', () => {
  302. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  303. expect(ast.attrs[0].name).toBe('msg')
  304. expect(ast.attrs[0].value).toBe('hello')
  305. })
  306. it('component "is" attribute', () => {
  307. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  308. expect(ast.component).toBe('"component1"')
  309. })
  310. it('component "inline-template" attribute', () => {
  311. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  312. expect(ast.inlineTemplate).toBe(true)
  313. })
  314. it('class binding', () => {
  315. // static
  316. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  317. expect(ast1.staticClass).toBe('"class1"')
  318. // dynamic
  319. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  320. expect(ast2.classBinding).toBe('class1')
  321. // interpolation warning
  322. parse('<p class="{{error}}">hello world</p>', baseOptions)
  323. expect('Interpolation inside attributes has been removed').toHaveBeenWarned()
  324. })
  325. it('style binding', () => {
  326. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  327. expect(ast.styleBinding).toBe('error')
  328. })
  329. it('attribute with v-bind', () => {
  330. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  331. expect(ast.attrsList[0].name).toBe('type')
  332. expect(ast.attrsList[0].value).toBe('text')
  333. expect(ast.attrsList[1].name).toBe('name')
  334. expect(ast.attrsList[1].value).toBe('field1')
  335. expect(ast.attrsMap['type']).toBe('text')
  336. expect(ast.attrsMap['name']).toBe('field1')
  337. expect(ast.attrs[0].name).toBe('type')
  338. expect(ast.attrs[0].value).toBe('"text"')
  339. expect(ast.attrs[1].name).toBe('name')
  340. expect(ast.attrs[1].value).toBe('"field1"')
  341. expect(ast.props[0].name).toBe('value')
  342. expect(ast.props[0].value).toBe('msg')
  343. })
  344. // #6887
  345. it('special case static attribute that must be props', () => {
  346. const ast = parse('<video muted></video>', baseOptions)
  347. expect(ast.attrs[0].name).toBe('muted')
  348. expect(ast.attrs[0].value).toBe('""')
  349. expect(ast.props[0].name).toBe('muted')
  350. expect(ast.props[0].value).toBe('true')
  351. })
  352. it('attribute with v-on', () => {
  353. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  354. expect(ast.events.input.value).toBe('onInput')
  355. })
  356. it('attribute with directive', () => {
  357. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  358. expect(ast.directives[0].name).toBe('validate')
  359. expect(ast.directives[0].value).toBe('required')
  360. expect(ast.directives[0].arg).toBe('field1')
  361. })
  362. it('attribute with modifiered directive', () => {
  363. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  364. expect(ast.directives[0].modifiers.on).toBe(true)
  365. expect(ast.directives[0].modifiers.off).toBe(true)
  366. })
  367. it('literal attribute', () => {
  368. // basic
  369. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  370. expect(ast1.attrsList[0].name).toBe('type')
  371. expect(ast1.attrsList[0].value).toBe('text')
  372. expect(ast1.attrsList[1].name).toBe('name')
  373. expect(ast1.attrsList[1].value).toBe('field1')
  374. expect(ast1.attrsList[2].name).toBe('value')
  375. expect(ast1.attrsList[2].value).toBe('hello world')
  376. expect(ast1.attrsMap['type']).toBe('text')
  377. expect(ast1.attrsMap['name']).toBe('field1')
  378. expect(ast1.attrsMap['value']).toBe('hello world')
  379. expect(ast1.attrs[0].name).toBe('type')
  380. expect(ast1.attrs[0].value).toBe('"text"')
  381. expect(ast1.attrs[1].name).toBe('name')
  382. expect(ast1.attrs[1].value).toBe('"field1"')
  383. expect(ast1.attrs[2].name).toBe('value')
  384. expect(ast1.attrs[2].value).toBe('"hello world"')
  385. // interpolation warning
  386. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  387. expect('Interpolation inside attributes has been removed').toHaveBeenWarned()
  388. })
  389. if (!isIE && !isEdge) {
  390. it('duplicate attribute', () => {
  391. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  392. expect('duplicate attribute').toHaveBeenWarned()
  393. })
  394. }
  395. it('custom delimiter', () => {
  396. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  397. expect(ast.children[0].expression).toBe('_s(msg)')
  398. })
  399. it('not specified getTagNamespace option', () => {
  400. const options = extend({}, baseOptions)
  401. delete options.getTagNamespace
  402. const ast = parse('<svg><text>hello world</text></svg>', options)
  403. expect(ast.tag).toBe('svg')
  404. expect(ast.ns).toBeUndefined()
  405. })
  406. it('not specified mustUseProp', () => {
  407. const options = extend({}, baseOptions)
  408. delete options.mustUseProp
  409. const ast = parse('<input type="text" name="field1" :value="msg">', options)
  410. expect(ast.props).toBeUndefined()
  411. })
  412. it('use prop when prop modifier was explicitly declared', () => {
  413. const ast = parse('<component is="textarea" :value.prop="val" />', baseOptions)
  414. expect(ast.attrs).toBeUndefined()
  415. expect(ast.props.length).toBe(1)
  416. expect(ast.props[0].name).toBe('value')
  417. expect(ast.props[0].value).toBe('val')
  418. })
  419. it('pre/post transforms', () => {
  420. const options = extend({}, baseOptions)
  421. const spy1 = jasmine.createSpy('preTransform')
  422. const spy2 = jasmine.createSpy('postTransform')
  423. options.modules = options.modules.concat([{
  424. preTransformNode (el) {
  425. spy1(el.tag)
  426. },
  427. postTransformNode (el) {
  428. expect(el.attrs.length).toBe(1)
  429. spy2(el.tag)
  430. }
  431. }])
  432. parse('<img v-pre src="hi">', options)
  433. expect(spy1).toHaveBeenCalledWith('img')
  434. expect(spy2).toHaveBeenCalledWith('img')
  435. })
  436. it('preserve whitespace in <pre> tag', function () {
  437. const options = extend({}, baseOptions)
  438. const ast = parse('<pre><code> \n<span>hi</span>\n </code><span> </span></pre>', options)
  439. const code = ast.children[0]
  440. expect(code.children[0].type).toBe(3)
  441. expect(code.children[0].text).toBe(' \n')
  442. expect(code.children[2].type).toBe(3)
  443. expect(code.children[2].text).toBe('\n ')
  444. const span = ast.children[1]
  445. expect(span.children[0].type).toBe(3)
  446. expect(span.children[0].text).toBe(' ')
  447. })
  448. // #5992
  449. it('ignore the first newline in <pre> tag', function () {
  450. const options = extend({}, baseOptions)
  451. const ast = parse('<div><pre>\nabc</pre>\ndef<pre>\n\nabc</pre></div>', options)
  452. const pre = ast.children[0]
  453. expect(pre.children[0].type).toBe(3)
  454. expect(pre.children[0].text).toBe('abc')
  455. const text = ast.children[1]
  456. expect(text.type).toBe(3)
  457. expect(text.text).toBe('\ndef')
  458. const pre2 = ast.children[2]
  459. expect(pre2.children[0].type).toBe(3)
  460. expect(pre2.children[0].text).toBe('\nabc')
  461. })
  462. it('forgivingly handle < in plain text', () => {
  463. const options = extend({}, baseOptions)
  464. const ast = parse('<p>1 < 2 < 3</p>', options)
  465. expect(ast.tag).toBe('p')
  466. expect(ast.children.length).toBe(1)
  467. expect(ast.children[0].type).toBe(3)
  468. expect(ast.children[0].text).toBe('1 < 2 < 3')
  469. })
  470. it('IE conditional comments', () => {
  471. const options = extend({}, baseOptions)
  472. const ast = parse(`
  473. <div>
  474. <!--[if lte IE 8]>
  475. <p>Test 1</p>
  476. <![endif]-->
  477. </div>
  478. `, options)
  479. expect(ast.tag).toBe('div')
  480. expect(ast.children.length).toBe(0)
  481. })
  482. it('parse content in textarea as text', () => {
  483. const options = extend({}, baseOptions)
  484. const whitespace = parse(`
  485. <textarea>
  486. <p>Test 1</p>
  487. test2
  488. </textarea>
  489. `, options)
  490. expect(whitespace.tag).toBe('textarea')
  491. expect(whitespace.children.length).toBe(1)
  492. expect(whitespace.children[0].type).toBe(3)
  493. // textarea is whitespace sensitive
  494. expect(whitespace.children[0].text).toBe(` <p>Test 1</p>
  495. test2
  496. `)
  497. const comment = parse('<textarea><!--comment--></textarea>', options)
  498. expect(comment.tag).toBe('textarea')
  499. expect(comment.children.length).toBe(1)
  500. expect(comment.children[0].type).toBe(3)
  501. expect(comment.children[0].text).toBe('<!--comment-->')
  502. })
  503. // #5526
  504. it('should not decode text in script tags', () => {
  505. const options = extend({}, baseOptions)
  506. const ast = parse(`<script type="x/template">&gt;<foo>&lt;</script>`, options)
  507. expect(ast.children[0].text).toBe(`&gt;<foo>&lt;`)
  508. })
  509. it('should ignore comments', () => {
  510. const options = extend({}, baseOptions)
  511. const ast = parse(`<div>123<!--comment here--></div>`, options)
  512. expect(ast.tag).toBe('div')
  513. expect(ast.children.length).toBe(1)
  514. expect(ast.children[0].type).toBe(3)
  515. expect(ast.children[0].text).toBe('123')
  516. })
  517. it('should kept comments', () => {
  518. const options = extend({
  519. comments: true
  520. }, baseOptions)
  521. const ast = parse(`<div>123<!--comment here--></div>`, options)
  522. expect(ast.tag).toBe('div')
  523. expect(ast.children.length).toBe(2)
  524. expect(ast.children[0].type).toBe(3)
  525. expect(ast.children[0].text).toBe('123')
  526. expect(ast.children[1].type).toBe(3) // parse comment with ASTText
  527. expect(ast.children[1].isComment).toBe(true) // parse comment with ASTText
  528. expect(ast.children[1].text).toBe('comment here')
  529. })
  530. })