Assign className in DOMElementPool when building new elements

Also, improve test coverage
This commit is contained in:
Nathan Sobo 2017-02-13 11:27:44 -07:00 committed by Antonio Scandurra
parent 6fa5c17cfb
commit ee749bf286
2 changed files with 19 additions and 2 deletions

View File

@ -8,7 +8,7 @@ describe('DOMElementPool', function () {
it('builds DOM nodes, recycling them when they are freed', function () {
let elements
const [div, span1, span2, span3, span4, span5, textNode] = Array.from(elements = [
domElementPool.buildElement('div'),
domElementPool.buildElement('div', 'foo'),
domElementPool.buildElement('span'),
domElementPool.buildElement('span'),
domElementPool.buildElement('span'),
@ -17,6 +17,13 @@ describe('DOMElementPool', function () {
domElementPool.buildText('Hello world!')
])
expect(div.className).toBe('foo')
div.textContent = 'testing'
div.style.backgroundColor = 'red'
div.dataset.foo = 'bar'
expect(textNode.textContent).toBe('Hello world!')
div.appendChild(span1)
span1.appendChild(span2)
div.appendChild(span3)
@ -37,6 +44,13 @@ describe('DOMElementPool', function () {
expect(elements.includes(domElementPool.buildElement('div'))).toBe(false)
expect(elements.includes(domElementPool.buildElement('span'))).toBe(false)
expect(elements.includes(domElementPool.buildText('unexisting'))).toBe(false)
expect(div.className).toBe('')
expect(div.textContent).toBe('')
expect(div.style.backgroundColor).toBe('')
expect(div.dataset.foo).toBeUndefined()
expect(textNode.textContent).toBe('another text')
})
it('forgets free nodes after being cleared', function () {

View File

@ -18,7 +18,7 @@ class DOMElementPool {
if (element) {
for (let dataId in element.dataset) { delete element.dataset[dataId] }
element.removeAttribute('style')
if (className != null) {
if (className) {
element.className = className
} else {
element.removeAttribute('class')
@ -29,6 +29,9 @@ class DOMElementPool {
this.freedElements.delete(element)
} else {
element = document.createElement(tagName)
if (className) {
element.className = className
}
this.managedElements.add(element)
}
return element