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

Fix class name bindings #5517

Closed
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion lib/legacy/polymer.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ if (window['ShadyDOM'] && window['ShadyDOM']['inUse'] && window['ShadyDOM']['noP
]);

forwardProperties(DomApiNative.prototype, [
'textContent', 'innerHTML'
'textContent', 'innerHTML', 'className'
]);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/mixins/property-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ function setupCompoundStorage(node, binding) {
storage[target] = literals;
// Configure properties with their literal parts
if (binding.literal && binding.kind == 'property') {
// Note, className needs style scoping so this needs wrapping.
if (target === 'className') {
node = wrap(node);
}
node[target] = binding.literal;
}
}
Expand Down Expand Up @@ -1428,6 +1432,10 @@ export const PropertyEffects = dedupingMixin(superClass => {
// implement a whitelist of tag & property values that should never
// be reset (e.g. <input>.value && <select>.value)
if (value !== node[prop] || typeof value == 'object') {
// Note, className needs style scoping so this needs wrapping.
if (prop === 'className') {
node = wrap(node);
}
node[prop] = value;
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/utils/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const setSanitizeDOMValue = function(newSanitizeDOMValue) {
* scrolling performance.
* Defaults to `false` for backwards compatibility.
*/
export let passiveTouchGestures = false;
export let passiveTouchGestures = window.Polymer && window.Polymer.passiveTouchGestures || undefined;

/**
* Sets `passiveTouchGestures` globally for all elements using Polymer Gestures.
Expand All @@ -88,7 +88,7 @@ export const setPassiveTouchGestures = function(usePassive) {
* disallowed, `<dom-bind>` is disabled, and `<dom-if>`/`<dom-repeat>`
* templates will only evaluate in the context of a trusted element template.
*/
export let strictTemplatePolicy = false;
export let strictTemplatePolicy = window.Polymer && window.Polymer.strictTemplatePolicy || undefined;

/**
* Sets `strictTemplatePolicy` globally for all elements
Expand All @@ -107,7 +107,7 @@ export const setStrictTemplatePolicy = function(useStrictPolicy) {
* getter and the `html` tag function. To enable legacy loading of templates
* via dom-module, set this flag to true.
*/
export let allowTemplateFromDomModule = false;
export let allowTemplateFromDomModule = window.Polymer && window.Polymer.allowTemplateFromDomModule || undefined;

/**
* Sets `lookupTemplateFromDomModule` globally for all elements
Expand All @@ -127,7 +127,7 @@ export const setAllowTemplateFromDomModule = function(allowDomModule) {
* If no includes or relative urls are used in styles, these steps can be
* skipped as an optimization.
*/
export let legacyOptimizations = false;
export let legacyOptimizations = window.Polymer && window.Polymer.legacyOptimizations || undefined;

/**
* Sets `legacyOptimizations` globally for all elements to enable optimizations
Expand All @@ -144,7 +144,7 @@ export const setLegacyOptimizations = function(useLegacyOptimizations) {
/**
* Setting to add warnings useful when migrating from Polymer 1.x to 2.x.
*/
export let legacyWarnings = false;
export let legacyWarnings = window.Polymer && window.Polymer.legacyWarnings || undefined;

/**
* Sets `legacyWarnings` globally for all elements to migration warnings.
Expand All @@ -160,7 +160,7 @@ export const setLegacyWarnings = function(useLegacyWarnings) {
* Setting to perform initial rendering synchronously when running under ShadyDOM.
* This matches the behavior of Polymer 1.
*/
export let syncInitialRender = false;
export let syncInitialRender = window.Polymer && window.Polymer.syncInitialRender || undefined;

/**
* Sets `syncInitialRender` globally for all elements to enable synchronous
Expand All @@ -179,7 +179,7 @@ export const setSyncInitialRender = function(useSyncInitialRender) {
* observers around undefined values. Observers and computed property methods
* are not called until no argument is undefined.
*/
export let legacyUndefined = false;
export let legacyUndefined = window.Polymer && window.Polymer.legacyUndefined || undefined;

/**
* Sets `legacyUndefined` globally for all elements to enable legacy
Expand All @@ -197,7 +197,7 @@ export const setLegacyUndefined = function(useLegacyUndefined) {
* Setting to retain the legacy Polymer 1 behavior for setting properties. Does
* not batch property sets.
*/
export let legacyNoBatch = false;
export let legacyNoBatch = window.Polymer && window.Polymer.legacyNoBatch || undefined;

/**
* Sets `legacyNoBatch` globally for all elements to enable legacy
Expand All @@ -216,7 +216,7 @@ export const setLegacyNoBatch = function(useLegacyNoBatch) {
* fire change events with respect to other effects. In Polymer 1.x they fire
* before observers; in 2.x they fire after all other effect types.
*/
export let legacyNotifyOrder = false;
export let legacyNotifyOrder = window.Polymer && window.Polymer.legacyNotifyOrder || undefined;

/**
* Sets `legacyNotifyOrder` globally for all elements to enable legacy
Expand Down
62 changes: 62 additions & 0 deletions test/unit/polymer-dom-nopatch.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@
</script>
</dom-module>

<dom-module id="x-class-bindings">
<template>
<style>
.a {
border: 2px solid orange;
}

.aa {
border: 12px solid red;
}

.b {
padding: 4px;
}
</style>
<div>
<div id="class" class$="[[clazz]] b">class</div>
<div id="className" class-name="[[clazz]] b">class</div>
</div>
</template>
<script type="module">
import { Polymer } from '../../polymer-legacy.js';
Polymer({
properties: {
clazz: {type: String}
},
is: 'x-class-bindings'
});
</script>
</dom-module>

<test-fixture id="scoped">
<template>
<x-event-scoped></x-event-scoped>
Expand Down Expand Up @@ -493,6 +524,37 @@
assert.equal(el.className, 'foo');
});

test('className', function() {
dom(el).className = 'foo';
assert.isTrue(el.classList.contains('foo'));
});

test('class bindings work and remained scoped', function() {
const el = document.createElement('x-class-bindings');
document.body.appendChild(el);
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '0px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.isTrue(el.$.class.classList.contains('style-scope'));
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '0px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
assert.isTrue(el.$.className.classList.contains('style-scope'));
el.clazz = 'a';
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '2px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.isTrue(el.$.class.classList.contains('style-scope'));
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '2px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
assert.isTrue(el.$.className.classList.contains('style-scope'));
el.clazz = 'aa';
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '12px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.isTrue(el.$.class.classList.contains('style-scope'));
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '12px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
assert.isTrue(el.$.className.classList.contains('style-scope'));
document.body.removeChild(el);
})

});
</script>

Expand Down
58 changes: 57 additions & 1 deletion test/unit/polymer-dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,38 @@
is: 'x-focusable-in-shadow'
});
</script>
</dom-module>
</dom-module>

<dom-module id="x-class-bindings">
<template>
<style>
.a {
border: 2px solid orange;
}

.aa {
border: 12px solid red;
}

.b {
padding: 4px;
}
</style>
<div>
<div id="class" class$="[[clazz]] b">class</div>
<div id="className" class-name="[[clazz]] b">class</div>
</div>
</template>
<script type="module">
import { Polymer } from '../../polymer-legacy.js';
Polymer({
properties: {
clazz: {type: String}
},
is: 'x-class-bindings'
});
</script>
</dom-module>

<test-fixture id="scoped">
<template>
Expand Down Expand Up @@ -480,6 +511,31 @@
assert.equal(el.className, 'foo');
});

test('className', function() {
dom(el).className = 'foo';
assert.isTrue(el.classList.contains('foo'));
});

test('class bindings', function() {
const el = document.createElement('x-class-bindings');
document.body.appendChild(el);
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '0px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '0px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
el.clazz = 'a';
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '2px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '2px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
el.clazz = 'aa';
assert.equal(getComputedStyle(el.$.class)['border-top-width'], '12px');
assert.equal(getComputedStyle(el.$.class)['padding-top'], '4px');
assert.equal(getComputedStyle(el.$.className)['border-top-width'], '12px');
assert.equal(getComputedStyle(el.$.className)['padding-top'], '4px');
document.body.removeChild(el);
})

});
</script>

Expand Down