Skip to content

Commit 0419b05

Browse files
committed
add replace option & tests
1 parent 795d6b9 commit 0419b05

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

src/compiler.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ CompilerProto.setupElement = function (options) {
136136
? document.querySelector(options.el)
137137
: options.el || document.createElement(options.tagName || 'div')
138138

139+
var template = options.template
140+
if (template) {
141+
// replace option: use the first node in
142+
// the template directly
143+
if (options.replace && template.childNodes.length === 1) {
144+
var replacer = template.childNodes[0].cloneNode(true)
145+
if (el.parentNode) {
146+
el.parentNode.insertBefore(replacer, el)
147+
el.parentNode.removeChild(el)
148+
}
149+
el = replacer
150+
} else {
151+
el.innerHTML = ''
152+
el.appendChild(template.cloneNode(true))
153+
}
154+
}
155+
139156
// apply element options
140157
if (options.id) el.id = options.id
141158
if (options.className) el.className = options.className
@@ -146,12 +163,6 @@ CompilerProto.setupElement = function (options) {
146163
}
147164
}
148165

149-
// initialize template
150-
var template = options.template
151-
if (template) {
152-
el.innerHTML = ''
153-
el.appendChild(template.cloneNode(true))
154-
}
155166
return el
156167
}
157168

test/unit/specs/api.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,49 @@ describe('UNIT: API', function () {
350350

351351
})
352352

353+
describe('replace', function () {
354+
355+
it('should replace an in DOM node', function () {
356+
var testId = 'replace-test'
357+
mock(testId, '<div>ho</div>')
358+
var old = document.getElementById(testId),
359+
parent = old.parentNode
360+
var Test = Seed.extend({
361+
template: '<p>hi</p>',
362+
replace: true
363+
})
364+
var t = new Test({
365+
el: '#' + testId
366+
})
367+
assert.strictEqual(t.$el.tagName, 'P')
368+
assert.strictEqual(t.$el.textContent, 'hi')
369+
assert.strictEqual(t.$el.parentNode, parent)
370+
var now = document.getElementById(testId)
371+
assert.strictEqual(now, null)
372+
})
373+
374+
it('should replace an off DOM Seed\'s $el', function () {
375+
var Test = Seed.extend({
376+
template: '<p>hi</p>',
377+
replace: true
378+
})
379+
var t = new Test()
380+
assert.strictEqual(t.$el.tagName, 'P')
381+
assert.strictEqual(t.$el.textContent, 'hi')
382+
})
383+
384+
it('should not work if template has more than one child node', function () {
385+
var Test = Seed.extend({
386+
template: '<p>hi</p><p>ho</p>',
387+
replace: true
388+
})
389+
var t = new Test()
390+
assert.notStrictEqual(t.$el.tagName, 'P')
391+
assert.strictEqual(t.$el.innerHTML, '<p>hi</p><p>ho</p>')
392+
})
393+
394+
})
395+
353396
describe('element options', function () {
354397

355398
it('should not accept el as an extension option', function () {

0 commit comments

Comments
 (0)