| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- var transclude = require('../../../../src/compile/transclude')
- var _ = require('../../../../src/util')
- if (_.inBrowser) {
- describe('Transclude', function () {
- var el, options
- beforeEach(function () {
- el = document.createElement('div')
- options = {}
- spyOn(_, 'warn')
- })
- it('normal', function () {
- var res = transclude(el, options)
- expect(res).toBe(el)
- })
- it('template', function () {
- options.template = '{{hi}}'
- var res = transclude(el, options)
- expect(res).toBe(el)
- expect(res.innerHTML).toBe('{{hi}}')
- })
- it('template invalid', function () {
- options.template = '#non-existent-stuff'
- var res = transclude(el, options)
- expect(res).toBeUndefined()
- expect(_.warn).toHaveBeenCalled()
- })
- it('template replace', function () {
- el.className = 'hello'
- options.template = '<div>{{hi}}</div>'
- options.replace = true
- var res = transclude(el, options)
- expect(res).not.toBe(el)
- expect(res.tagName).toBe('DIV')
- expect(res.className).toBe('hello')
- expect(res.innerHTML).toBe('{{hi}}')
- })
- it('block instance', function () {
- var frag = document.createDocumentFragment()
- frag.appendChild(el)
- var res = transclude(frag, options)
- expect(res).toBe(frag)
- expect(res.childNodes.length).toBe(3)
- expect(res.firstChild.nodeType).toBe(8)
- expect(res.lastChild.nodeType).toBe(8)
- expect(res.childNodes[1]).toBe(el)
- })
- it('template element', function () {
- var tpl = document.createElement('template')
- tpl.innerHTML = '<div>123</div>'
- var res = transclude(tpl, options)
- expect(res.childNodes.length).toBe(3)
- expect(res.firstChild.nodeType).toBe(8)
- expect(res.lastChild.nodeType).toBe(8)
- expect(res.childNodes[1].textContent).toBe('123')
- })
- it('content transclusion', function () {
- el.innerHTML = '<p>hi</p>'
- options.template = '<div><content></content></div>'
- var res = transclude(el, options)
- expect(res.firstChild.tagName).toBe('DIV')
- expect(res.firstChild.firstChild.tagName).toBe('P')
- expect(res.firstChild.firstChild.textContent).toBe('hi')
- })
- it('fallback content', function () {
- options.template = '<content><p>fallback</p></content>'
- var res = transclude(el, options)
- expect(res.firstChild.tagName).toBe('P')
- expect(res.firstChild.textContent).toBe('fallback')
- })
- it('content transclusion with replace', function () {
- el.innerHTML = '<p>hi</p>'
- options.template = '<div><div><content></content></div></div>'
- options.replace = true
- var res = transclude(el, options)
- expect(res).not.toBe(el)
- expect(res.firstChild.tagName).toBe('DIV')
- expect(res.firstChild.firstChild.tagName).toBe('P')
- expect(res.firstChild.firstChild.textContent).toBe('hi')
- })
- it('block instance content transclusion', function () {
- el.innerHTML = '<p>hi</p><span>ho</span>'
- options.template = '<div></div><content select="p"></content><content select="span"></content>'
- var res = transclude(el, options)
- expect(res.firstChild.tagName).toBe('DIV')
- expect(res.childNodes[1].tagName).toBe('P')
- expect(res.childNodes[2].tagName).toBe('SPAN')
- })
- })
- }
|