From 43d53878e14b5ff3f4e319993168e8254228bd73 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 6 Jan 2016 13:47:57 +0100 Subject: [PATCH] fix(toast): better toast templating allowing comments and sibling elements Fixes #6259 Closes #6494 --- src/components/toast/toast.js | 22 ++++++++++++++-------- src/components/toast/toast.spec.js | 13 +++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/components/toast/toast.js b/src/components/toast/toast.js index 70df58d94ea..d3906e751a8 100644 --- a/src/components/toast/toast.js +++ b/src/components/toast/toast.js @@ -333,18 +333,22 @@ function MdToastProvider($$interimElementProvider) { // Root element of template will be . We need to wrap all of its content inside of // of
. 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 = '
' + parsedTemplate.html() + '
'; + 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('
'); + 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 ? - '
' + template + '
' : - template || ''; + return template || ''; } }; @@ -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.',''); diff --git a/src/components/toast/toast.spec.js b/src/components/toast/toast.spec.js index 67f96913732..e4f71ddc400 100644 --- a/src/components/toast/toast.spec.js +++ b/src/components/toast/toast.spec.js @@ -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('
'); + setup({ + template: '{{1}}234', + 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: '',