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

Commit ea60dd3

Browse files
committed
fix(toast): wrap toast content with .md-toast-content
1 parent f376178 commit ea60dd3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/components/toast/toast.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ function MdToastDirective($mdToast) {
156156
* have an outer `md-toast` element.
157157
* - `template` - `{string=}`: Same as templateUrl, except this is an actual
158158
* template string.
159+
* - `autoWrap` - `{boolean=}`: Whether or not to automatically wrap the template content with a
160+
* `<div class="md-toast-content">` if one is not provided. Defaults to true. Can be disabled if you provide a
161+
* custom toast directive.
159162
* - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified, it will create a new child scope.
160163
* This scope will be destroyed when the toast is removed unless `preserveScope` is set to true.
161164
* - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
@@ -277,7 +280,28 @@ function MdToastProvider($$interimElementProvider) {
277280
onRemove: onRemove,
278281
position: 'bottom left',
279282
themable: true,
280-
hideDelay: 3000
283+
hideDelay: 3000,
284+
autoWrap: true,
285+
transformTemplate: function(template, options) {
286+
var shouldAddWrapper = options.autoWrap && template && !/md-toast-content/g.test(template);
287+
288+
if (shouldAddWrapper) {
289+
// Root element of template will be <md-toast>. We need to wrap all of its content inside of
290+
// of <div class="md-toast-content">. All templates provided here should be static, developer-controlled
291+
// content (meaning we're not attempting to guard against XSS).
292+
var parsedTemplate = angular.element(template);
293+
var wrappedContent = '<div class="md-toast-content">' + parsedTemplate.html() + '</div>';
294+
295+
parsedTemplate.empty().append(wrappedContent);
296+
297+
// Underlying interimElement expects a template string.
298+
return parsedTemplate[0].outerHTML;
299+
}
300+
301+
return shouldAddWrapper ?
302+
'<div class="md-toast-content">' + template + '</div>' :
303+
template || '';
304+
}
281305
};
282306

283307
function onShow(scope, element, options) {

src/components/toast/toast.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ describe('$mdToast service', function() {
168168
expect(toast.hasClass('md-left')).toBe(true);
169169
}));
170170

171+
it('should wrap toast content with .md-toast-content', inject(function($rootElement, $timeout) {
172+
setup({
173+
template: '<md-toast><p>Charmander</p></md-toast>',
174+
position: 'top left'
175+
});
176+
var toast = $rootElement.find('md-toast')[0];
177+
$timeout.flush();
178+
179+
expect(toast.children.length).toBe(1);
180+
expect(toast.children[0].classList.contains('md-toast-content'));
181+
expect(toast.children[0].textContent).toMatch('Charmander');
182+
}));
183+
184+
185+
171186
describe('sm screen', function () {
172187
beforeEach(function () {
173188
module(function ($provide) {

0 commit comments

Comments
 (0)