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

Commit f1e4fcb

Browse files
jelbournandrewseguin
authored andcommitted
fix(theming): don't assume selector corresponds to expression (#10818)
Before this change, the theming code assume that if you have a selector like "md-primary", all of the expressions therein will only be in terms of "primary". This was not the case, causing one style to leave an unevaluated expression in the css output. This change *also* deletes the troublesome style because it seemed to be incorrect anyway. Fixes #10793
1 parent a91c99a commit f1e4fcb

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/components/radioButton/radio-button-theme.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ md-radio-group.md-THEME_NAME-theme {
8787
&.md-primary .md-checked:not([disabled]) .md-ink-ripple, .md-checked:not([disabled]).md-primary .md-ink-ripple {
8888
color: '{{primary-color-0.26}}';
8989
}
90-
.md-checked.md-primary .md-ink-ripple {
91-
color: '{{warn-color-0.26}}';
92-
}
9390
}
9491

9592
md-radio-group.md-THEME_NAME-theme.md-focused:not(:empty) {

src/core/services/theming/theming.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -880,14 +880,8 @@ function parseRules(theme, colorType, rules) {
880880
checkValidPalette(theme, colorType);
881881

882882
rules = rules.replace(/THEME_NAME/g, theme.name);
883-
var generatedRules = [];
884-
var color = theme.colors[colorType];
885-
886883
var themeNameRegex = new RegExp('\\.md-' + theme.name + '-theme', 'g');
887-
// Matches '{{ primary-color }}', etc
888-
var hueRegex = new RegExp('(\'|")?{{\\s*(' + colorType + ')-(color|contrast)-?(\\d\\.?\\d*)?\\s*}}(\"|\')?','g');
889884
var simpleVariableRegex = /'?"?\{\{\s*([a-zA-Z]+)-(A?\d+|hue\-[0-3]|shadow|default)-?(\d\.?\d*)?(contrast)?\s*\}\}'?"?/g;
890-
var palette = PALETTES[color.name];
891885

892886
// find and replace simple variables where we use a specific hue, not an entire palette
893887
// eg. "{{primary-100}}"
@@ -910,10 +904,17 @@ function parseRules(theme, colorType, rules) {
910904
return rgba( (PALETTES[ theme.colors[colorType].name ][hue] || '')[contrast ? 'contrast' : 'value'], opacity );
911905
});
912906

907+
// Matches '{{ primary-color }}', etc
908+
var hueRegex = new RegExp('(\'|")?{{\\s*([a-zA-Z]+)-(color|contrast)-?(\\d\\.?\\d*)?\\s*}}(\"|\')?','g');
909+
var generatedRules = [];
910+
913911
// For each type, generate rules for each hue (ie. default, md-hue-1, md-hue-2, md-hue-3)
914-
angular.forEach(color.hues, function(hueValue, hueName) {
912+
angular.forEach(['default', 'hue-1', 'hue-2', 'hue-3'], function(hueName) {
915913
var newRule = rules
916-
.replace(hueRegex, function(match, _, colorType, hueType, opacity) {
914+
.replace(hueRegex, function(match, _, matchedColorType, hueType, opacity) {
915+
var color = theme.colors[matchedColorType];
916+
var palette = PALETTES[color.name];
917+
var hueValue = color.hues[hueName];
917918
return rgba(palette[hueValue][hueType === 'color' ? 'value' : 'contrast'], opacity);
918919
});
919920
if (hueName !== 'default') {
@@ -961,17 +962,12 @@ function generateAllThemes($injector, $mdTheming) {
961962
.filter(function(rule) { return rule && rule.trim().length; })
962963
.map(function(rule) { return rule.trim() + '}'; });
963964

964-
965-
var ruleMatchRegex = new RegExp('md-(' + THEME_COLOR_TYPES.join('|') + ')', 'g');
966-
967965
THEME_COLOR_TYPES.forEach(function(type) {
968966
rulesByType[type] = '';
969967
});
970968

971-
972969
// Sort the rules based on type, allowing us to do color substitution on a per-type basis
973970
rules.forEach(function(rule) {
974-
var match = rule.match(ruleMatchRegex);
975971
// First: test that if the rule has '.md-accent', it goes into the accent set of rules
976972
for (var i = 0, type; type = THEME_COLOR_TYPES[i]; i++) {
977973
if (rule.indexOf('.md-' + type) > -1) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ describe('$mdThemingProvider', function() {
250250
expect(parse('.md-THEME_NAME-theme { color: "{{foreground-shadow}}"; }')[0].content)
251251
.toEqual('color: ;');
252252
});
253+
253254
it('for a dark theme', function() {
254255
testTheme.dark();
255256
expect(parse('.md-THEME_NAME-theme { color: "{{foreground-1}}"; }')[0].content)
@@ -264,6 +265,7 @@ describe('$mdThemingProvider', function() {
264265
.toEqual('color: 1px 1px 0px rgba(0,0,0,0.4), -1px -1px 0px rgba(0,0,0,0.4);');
265266
});
266267
});
268+
267269
it('parses contrast colors', function() {
268270
testTheme.primaryPalette('testPalette', {
269271
'default': '50'
@@ -283,6 +285,7 @@ describe('$mdThemingProvider', function() {
283285
expect(parse('{ color: "{{primary-contrast}}"; }')[0].content)
284286
.toEqual('color: rgb(255,255,255);');
285287
});
288+
286289
it('generates base, non-colorType-specific, rules', function() {
287290
var accent100 = themingProvider._rgba(themingProvider._PALETTES.testPalette['100'].value);
288291
var result = parse('.md-THEME_NAME-theme { color: "{{accent-100}}"; }');
@@ -296,6 +299,7 @@ describe('$mdThemingProvider', function() {
296299
expect(result[3].hue).toBe('hue-3');
297300
expect(result.length).toBe(4);
298301
});
302+
299303
it('generates colorType-specific rules for each hue', function() {
300304
var primary = themingProvider._rgba(themingProvider._PALETTES.testPalette['500'].value);
301305
var hue1 = themingProvider._rgba(themingProvider._PALETTES.testPalette['300'].value);
@@ -309,6 +313,14 @@ describe('$mdThemingProvider', function() {
309313
expect(result.length).toEqual(4);
310314
});
311315

316+
it('should generate styles when a md-something selector has an expression for a different palette', function() {
317+
// The selector has `md-primary` in the name, but the expression is for md-warn.
318+
var result = parse('.md-THEME_NAME-theme.md-primary { color: "{{warn-color}}"; }');
319+
320+
// This should not leave an unevaluated expression in the output.
321+
expect(result.join(' ')).not.toContain('{{');
322+
});
323+
312324
it('generates colorType-specific rules for each hue with opacity', function() {
313325
var primary = themingProvider._rgba(themingProvider._PALETTES.testPalette['500'].value, '0.3');
314326
var hue1 = themingProvider._rgba(themingProvider._PALETTES.testPalette['300'].value, '0.3');

0 commit comments

Comments
 (0)