Skip to content

Commit

Permalink
Change in the function upgradElementsInternal to enable the upgrade o…
Browse files Browse the repository at this point in the history
…f child elements created by parent element's upgrade, like RippleContainerElement in Checkbox. The problem was happening when the element was created dynamically.
  • Loading branch information
dopic committed Aug 29, 2015
1 parent 3d9b5dd commit 13f8653
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/mdlComponentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ window.componentHandler = (function() {
for (var i = 0, n = elements.length, element; i < n; i++) {
element = elements[i];
if (element instanceof HTMLElement) {
upgradeElementInternal(element);
if (element.children.length > 0) {
upgradeElementsInternal(element.children);
}
upgradeElementInternal(element);
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion test/unit/componentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ function createNestedElementsForComponentHandlerTest() {
return container;
}

function createCheckbox(){
var label = document.createElement('label');
label.className = 'mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect';
label.htmlFor = 'checkbox1';

var input = document.createElement('input');
input.setAttribute('type','checkbox');
input.className = 'mdl-checkbox__input';
input.id = 'checkbox1';
label.appendChild(input);

var span = document.createElement('span');
span.className = 'mdl-checkbox__label';
span.innerHTML = 'checkbox';
label.appendChild(span);

return label;
}

describe('componentHandler', function() {

it('should be globally available', function() {
Expand Down Expand Up @@ -93,7 +112,7 @@ describe('componentHandler', function() {
componentHandler.upgradeElement(el, 'MaterialButton');
componentHandler.upgradeElement(el, 'MaterialRipple');
expect($(el)).to.have.data('upgraded', ',MaterialButton,MaterialRipple');
});
});

it('should upgrade a single component to an element by using its CSS classes', function() {
var el = document.createElement('button');
Expand Down Expand Up @@ -124,6 +143,15 @@ describe('componentHandler', function() {
expect($(el)).to.have.data('upgraded', ',MaterialButtonPostfix,MaterialButton');
});

it('should upgrade child elements created by parent upgrade', function () {
var checkbox = createCheckbox();

componentHandler.upgradeElements(checkbox);

var child = checkbox.lastChild;
expect($(child)).to.have.data('upgraded', ',MaterialRipple');
});

it('should upgrade all elements and their children within an HTMLCollection', function() {
var container = createNestedElementsForComponentHandlerTest();
var buttons = document.querySelectorAll('.mdl-js-button');
Expand Down

0 comments on commit 13f8653

Please sign in to comment.