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

Commit aea5437

Browse files
crisbetoThomasBurleson
authored andcommitted
fix(theming): potentially generating invalid CSS
If a theme that ends in a newline gets passed to the theming provider, it will generate a CSS syntax error: a double closing bracket would be added, instead of a single one (e.g. `}}` instead of `}`). Closes #8953
1 parent fbcf35c commit aea5437

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/core/services/theming/theming.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ function generateAllThemes($injector, $mdTheming) {
622622
// Break the CSS into individual rules
623623
var rules = themeCss
624624
.split(/\}(?!(\}|'|"|;))/)
625-
.filter(function(rule) { return rule && rule.length; })
625+
.filter(function(rule) { return rule && rule.trim().length; })
626626
.map(function(rule) { return rule.trim() + '}'; });
627627

628628

src/core/services/theming/theming.spec.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('$mdThemingProvider', function() {
163163
expect(themingProvider._PALETTES.extended['100']).toEqual(testPalette['100']);
164164
expect(themingProvider._PALETTES.extended['50']).toEqual('newValue');
165165
});
166-
});
166+
});
167167

168168
describe('css template parsing', function() {
169169
beforeEach(setup);
@@ -351,11 +351,11 @@ describe('$mdThemeProvider with custom styles', function() {
351351
$mdThemingProvider.registerStyles('/*test*/');
352352
$mdThemingProvider.theme('register-custom-styles');
353353
});
354-
354+
355355
// Verify that $MD_THEME_CSS is still set to '/**/' in the test environment.
356-
// Check angular-material-mocks.js for $MD_THEME_CSS latest value if this test starts to fail.
356+
// Check angular-material-mocks.js for $MD_THEME_CSS latest value if this test starts to fail.
357357
inject(function($MD_THEME_CSS) { expect($MD_THEME_CSS).toBe('/**/'); });
358-
358+
359359
// Find the string '/**//*test*/' in the head tag.
360360
expect(document.head.innerHTML).toContain('/*test*/');
361361
});
@@ -419,6 +419,23 @@ describe('$mdThemeProvider with on-demand generation', function() {
419419
});
420420
});
421421

422+
describe('$mdThemeProvider with a theme that ends in a newline', function() {
423+
beforeEach(function() {
424+
module('material.core', function($provide) {
425+
// Note that it should end with a newline
426+
$provide.constant('$MD_THEME_CSS', "sparkle.md-THEME_NAME-theme { color: '{{primary-color}}' }\n");
427+
});
428+
429+
inject(function($mdTheming) {});
430+
});
431+
432+
it('should not add an extra closing bracket if the stylesheet ends with a newline', function() {
433+
var style = document.head.querySelector('style[md-theme-style]');
434+
expect(style.innerText).not.toContain('}}');
435+
style.parentNode.removeChild(style);
436+
});
437+
});
438+
422439
describe('$mdThemeProvider with nonce', function() {
423440
beforeEach(function() {
424441

@@ -593,7 +610,7 @@ describe('md-themable directive', function() {
593610
$rootScope.themey = 'red';
594611
var el = $compile('<div md-theme="{{themey}}"><span md-themable md-theme-watch></span></div>')($rootScope);
595612
$rootScope.$apply();
596-
613+
597614
expect(el.children().hasClass('md-red-theme')).toBe(true);
598615
$rootScope.$apply('themey = "blue"');
599616
expect(el.children().hasClass('md-blue-theme')).toBe(true);
@@ -604,7 +621,7 @@ describe('md-themable directive', function() {
604621
$rootScope.themey = 'red';
605622
var el = $compile('<div md-theme="{{themey}}"><span md-themable></span></div>')($rootScope);
606623
$rootScope.$apply();
607-
624+
608625
expect(el.children().hasClass('md-red-theme')).toBe(true);
609626
$rootScope.$apply('themey = "blue"');
610627
expect(el.children().hasClass('md-blue-theme')).toBe(false);

0 commit comments

Comments
 (0)