Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow style object to accept array of values #723

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/dom/CSSPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ var CSSPropertyOperations = {
continue;
}
var styleValue = styles[styleName];
if (styleValue != null) {
if (Array.isArray(styleValue)) {
styleValue.forEach(function(styleValue) {
serialized += processStyleName(styleName) + ':';
serialized += dangerousStyleValue(styleName, styleValue) + ';';
});
} else if (styleValue != null && styleValue.length !== 0) {
serialized += processStyleName(styleName) + ':';
serialized += dangerousStyleValue(styleName, styleValue) + ';';
}
Expand All @@ -74,9 +79,13 @@ var CSSPropertyOperations = {
if (!styles.hasOwnProperty(styleName)) {
continue;
}
var styleValue = dangerousStyleValue(styleName, styles[styleName]);
if (styleValue) {
style[styleName] = styleValue;
var styleValue = styles[styleName];
if (Array.isArray(styleValue) && styleValue.length) {
styleValue.forEach(function(styleValue) {
style[styleName] = dangerousStyleValue(styleName, styleValue);
});
} else if (styleValue != null && styleValue.length !== 0) {
style[styleName] = dangerousStyleValue(styleName, styleValue);
} else {
var expansion = CSSProperty.shorthandPropertyExpansions[styleName];
if (expansion) {
Expand Down
32 changes: 32 additions & 0 deletions src/dom/__tests__/CSSPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ describe('CSSPropertyOperations', function() {
})).toBe('display:none;');
});

it('should ignore empty strings', function() {
expect(CSSPropertyOperations.createMarkupForStyles({
backgroundColor: '',
display: 'none'
})).toBe('display:none;');
});

it('should add multiple styles', function() {
expect(CSSPropertyOperations.createMarkupForStyles({
cursor: ['-webkit-grab', '-moz-grab', 'grab']
})).toBe('cursor:-webkit-grab;cursor:-moz-grab;cursor:grab;');
});

it('should return null for no styles', function() {
expect(CSSPropertyOperations.createMarkupForStyles({
backgroundColor: null,
Expand Down Expand Up @@ -105,4 +118,23 @@ describe('CSSPropertyOperations', function() {
expect(/style=".*"/.test(root.innerHTML)).toBe(false);
});

it('should set style for node', function() {
var div = document.createElement('div');
CSSPropertyOperations.setValueForStyles(div, {
backgroundColor: null,
display: 'none'
});
expect(div.style.getPropertyValue('background-color')).toBe(null);
expect(div.style.getPropertyValue('display')).toBe('none');
});

it('should set multiple style rules for node', function() {
var div = document.createElement('div');
// Last valid style wins
CSSPropertyOperations.setValueForStyles(div, {
cursor: ['crosshair', 'auto']
});
expect(div.style.getPropertyValue('cursor')).toBe('auto');
});

});