Skip to content

Commit 831be4f

Browse files
committed
updateNativeStyles should only remove styles set by updateNativeStyles
Fixes #3800
1 parent 43955ea commit 831be4f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/lib/style-properties.html

+9-3
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,23 @@
518518
* support element.customStyle / element.updateStyles)
519519
*/
520520
updateNativeStyleProperties: function(element, properties) {
521-
// remove existing properties
522-
for (var i=0; i < element.style.length; i++) {
523-
element.style.removeProperty(element.style[i]);
521+
var oldPropertyNames = element.__customStyleProperties;
522+
if (oldPropertyNames) {
523+
// remove previous properties
524+
for (var i=0; i < oldPropertyNames.length; i++) {
525+
element.style.removeProperty(oldPropertyNames[i]);
526+
}
524527
}
528+
var propertyNames = [];
525529
// apply properties
526530
for (var p in properties) {
527531
// NOTE: for bc with shim, don't apply null values.
528532
if (properties[p] !== null) {
529533
element.style.setProperty(p, properties[p]);
534+
propertyNames.push(p);
530535
}
531536
}
537+
element.__customStyleProperties = propertyNames;
532538
},
533539

534540
rx: styleUtil.rx,

test/unit/styling-cross-scope-var.html

+27
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,33 @@
11631163
CustomElements.takeRecords();
11641164
assertComputed(e, 'rgb(102, 205, 170)', null, 'color');
11651165
});
1166+
1167+
test('updateStyles plays nicely with inline styling', function() {
1168+
if (!Polymer.Settings.useNativeCSSProperties) {
1169+
this.skip();
1170+
}
1171+
var e = document.createElement('x-dynamic');
1172+
document.body.appendChild(e);
1173+
CustomElements.takeRecords();
1174+
e.style.cssText = 'background-color: rgb(255, 0, 0);';
1175+
e.updateStyles();
1176+
assertComputed(e, 'rgb(255, 0, 0)', null, 'background-color');
1177+
});
1178+
test('updateStyles removes the correct number of style properties', function() {
1179+
if (!Polymer.Settings.useNativeCSSProperties) {
1180+
this.skip();
1181+
}
1182+
var e = document.createElement('x-dynamic');
1183+
document.body.appendChild(e);
1184+
CustomElements.takeRecords();
1185+
e.style.cssText = 'background-color: rgb(255, 0, 0);';
1186+
e.updateStyles({'--dynamic': '2px solid black'});
1187+
assertComputed(e, 'rgb(255, 0, 0)', null, 'background-color');
1188+
assertComputed(e, '2px');
1189+
e.updateStyles({'--dynamic': null});
1190+
assertComputed(e, 'rgb(255, 0, 0)', null, 'background-color');
1191+
assertComputed(e, '6px');
1192+
});
11661193
});
11671194

11681195
</script>

0 commit comments

Comments
 (0)