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

Commit 43d5387

Browse files
devversionThomasBurleson
authored andcommitted
fix(toast): better toast templating allowing comments and sibling elements
Fixes #6259 Closes #6494
1 parent 90a7183 commit 43d5387

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/components/toast/toast.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,22 @@ function MdToastProvider($$interimElementProvider) {
333333
// Root element of template will be <md-toast>. We need to wrap all of its content inside of
334334
// of <div class="md-toast-content">. All templates provided here should be static, developer-controlled
335335
// content (meaning we're not attempting to guard against XSS).
336-
var parsedTemplate = angular.element(template);
337-
var wrappedContent = '<div class="md-toast-content">' + parsedTemplate.html() + '</div>';
336+
var templateRoot = document.createElement('md-template');
337+
templateRoot.innerHTML = template;
338+
339+
for (var i = 0; i < templateRoot.children.length; i++) {
340+
if (templateRoot.children[i].nodeName === 'MD-TOAST') {
341+
var wrapper = angular.element('<div class="md-toast-content">');
342+
wrapper.append(templateRoot.children[i].children);
343+
templateRoot.children[i].appendChild(wrapper[0]);
344+
}
345+
}
338346

339-
parsedTemplate.empty().append(wrappedContent);
340347

341-
// Underlying interimElement expects a template string.
342-
return parsedTemplate[0].outerHTML;
348+
return templateRoot.outerHTML;
343349
}
344350

345-
return shouldAddWrapper ?
346-
'<div class="md-toast-content">' + template + '</div>' :
347-
template || '';
351+
return template || '';
348352
}
349353
};
350354

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

356360
element = $mdUtil.extractElementByName(element, 'md-toast', true);
361+
options.element = element;
362+
357363
options.onSwipe = function(ev, gesture) {
358364
//Add the relevant swipe class to the element so it can animate correctly
359365
var swipe = ev.type.replace('$md.','');

src/components/toast/toast.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ describe('$mdToast service', function() {
180180
expect(toast.text().trim()).toBe('hello, 1');
181181
}));
182182

183+
it('should not throw an error if template starts with comment', inject(function($timeout, $rootScope, $rootElement) {
184+
var parent = angular.element('<div>');
185+
setup({
186+
template: '<!-- COMMENT --><md-toast>{{1}}234</md-toast>',
187+
appendTo: parent
188+
});
189+
190+
var toast = $rootElement.find('md-toast');
191+
$timeout.flush();
192+
193+
expect(toast.length).not.toBe(0);
194+
}));
195+
183196
it('should add position class to toast', inject(function($rootElement, $timeout) {
184197
setup({
185198
template: '<md-toast>',

0 commit comments

Comments
 (0)