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

fix(toast): better toast templating allowing comments and sibling elements #6494

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/components/toast/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,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 @@ -310,6 +314,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 @@ -157,6 +157,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