Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(toast): better toast templating allowing comments and sibling ele…
Browse files Browse the repository at this point in the history
…ments

Fixes #6259

Closes #6494
  • Loading branch information
devversion authored and ThomasBurleson committed Mar 30, 2016
1 parent 90a7183 commit 43d5387
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/components/toast/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,22 @@ function MdToastProvider($$interimElementProvider) {
// Root element of template will be <md-toast>. We need to wrap all of its content inside of
// of <div class="md-toast-content">. All templates provided here should be static, developer-controlled
// content (meaning we're not attempting to guard against XSS).
var parsedTemplate = angular.element(template);
var wrappedContent = '<div class="md-toast-content">' + parsedTemplate.html() + '</div>';
var templateRoot = document.createElement('md-template');
templateRoot.innerHTML = template;

for (var i = 0; i < templateRoot.children.length; i++) {
if (templateRoot.children[i].nodeName === 'MD-TOAST') {
var wrapper = angular.element('<div class="md-toast-content">');
wrapper.append(templateRoot.children[i].children);
templateRoot.children[i].appendChild(wrapper[0]);
}
}

parsedTemplate.empty().append(wrappedContent);

// Underlying interimElement expects a template string.
return parsedTemplate[0].outerHTML;
return templateRoot.outerHTML;
}

return shouldAddWrapper ?
'<div class="md-toast-content">' + template + '</div>' :
template || '';
return template || '';
}
};

Expand All @@ -354,6 +358,8 @@ function MdToastProvider($$interimElementProvider) {
var isSmScreen = !$mdMedia('gt-sm');

element = $mdUtil.extractElementByName(element, 'md-toast', true);
options.element = element;

options.onSwipe = function(ev, gesture) {
//Add the relevant swipe class to the element so it can animate correctly
var swipe = ev.type.replace('$md.','');
Expand Down
13 changes: 13 additions & 0 deletions src/components/toast/toast.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ describe('$mdToast service', function() {
expect(toast.text().trim()).toBe('hello, 1');
}));

it('should not throw an error if template starts with comment', inject(function($timeout, $rootScope, $rootElement) {
var parent = angular.element('<div>');
setup({
template: '<!-- COMMENT --><md-toast>{{1}}234</md-toast>',
appendTo: parent
});

var toast = $rootElement.find('md-toast');
$timeout.flush();

expect(toast.length).not.toBe(0);
}));

it('should add position class to toast', inject(function($rootElement, $timeout) {
setup({
template: '<md-toast>',
Expand Down

0 comments on commit 43d5387

Please sign in to comment.