Skip to content

Commit 83f9cba

Browse files
author
Nadeem Shadan
committed
handle inherit in style resolution and add regression coverage
1 parent 76f2c98 commit 83f9cba

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/StyleContextStack.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isString, isValue } from './helpers/variableType';
22

3+
const isInheritValue = value => isString(value) && value.toLowerCase() === 'inherit';
4+
35
/**
46
* Used for style inheritance and style overrides
57
*/
@@ -127,7 +129,9 @@ class StyleContextStack {
127129
}
128130

129131
if (isValue(style[property])) {
130-
return style[property];
132+
if (!isInheritValue(style[property])) {
133+
return style[property];
134+
}
131135
}
132136

133137
if (style.extends) {
@@ -153,12 +157,16 @@ class StyleContextStack {
153157
return value;
154158
}
155159
} else if (isValue(item[property])) { // style-overrides-object
160+
if (isInheritValue(item[property])) {
161+
continue;
162+
}
156163
return item[property];
157164
}
158165
}
159166
}
160167

161-
return this.defaultStyle && this.defaultStyle[property];
168+
let defaultValue = this.defaultStyle && this.defaultStyle[property];
169+
return isInheritValue(defaultValue) ? undefined : defaultValue;
162170
}
163171

164172
/**
@@ -172,7 +180,9 @@ class StyleContextStack {
172180
let value;
173181

174182
if (isValue(item[property])) { // item defines this property
175-
return item[property];
183+
if (!isInheritValue(item[property])) {
184+
return item[property];
185+
}
176186
}
177187

178188
if (!styleContextStack) {

tests/unit/StyleContextStack.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,37 @@ describe('StyleContextStack', function () {
195195
assert.equal(StyleContextStack.getStyleProperty({ font: 'ZapfDingbats', bold: true }, null, 'font', 'Arial'), 'ZapfDingbats');
196196
});
197197

198+
it('should resolve "inherit" from node style overrides', function () {
199+
var stack = new StyleContextStack({
200+
parent: {
201+
color: 'blue',
202+
},
203+
child: {
204+
extends: 'parent'
205+
}
206+
}, {
207+
color: 'black'
208+
});
209+
210+
assert.equal(StyleContextStack.getStyleProperty({ style: 'child', color: 'inherit' }, stack, 'color', 'red'), 'blue');
211+
});
212+
213+
it('should resolve "inherit" declared in named style definitions', function () {
214+
var stack = new StyleContextStack({
215+
parent: {
216+
color: 'blue',
217+
},
218+
child: {
219+
color: 'inherit',
220+
extends: 'parent'
221+
}
222+
}, {
223+
color: 'black'
224+
});
225+
226+
assert.equal(StyleContextStack.getStyleProperty({ style: 'child' }, stack, 'color', 'red'), 'blue');
227+
});
228+
198229
it('should process extends styles', function () {
199230
var stack = new StyleContextStack({
200231
header: {

0 commit comments

Comments
 (0)