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

Support derived attributes #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 61 additions & 5 deletions src/core/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ limitations under the License.
'select',
'textarea',
],
{
derived: true,
},
],
[
'autoPlay',
Expand All @@ -213,6 +216,9 @@ limitations under the License.
[
'input',
],
{
derived: true,
},
],
[
'challenge',
Expand All @@ -233,6 +239,9 @@ limitations under the License.
'command',
'input',
],
{
derived: true,
},
],
[
'cite',
Expand Down Expand Up @@ -348,6 +357,9 @@ limitations under the License.
'select',
'textarea',
],
{
derived: true,
},
],
[
'download',
Expand Down Expand Up @@ -463,6 +475,9 @@ limitations under the License.
[
'input',
],
{
derived: true,
},
],
[
'inputMode',
Expand Down Expand Up @@ -594,13 +609,19 @@ limitations under the License.
'input',
'select',
],
{
derived: true,
},
],
[
'muted',
[
'audio',
'video',
],
{
derived: true,
},
],
[
'name',
Expand All @@ -626,6 +647,9 @@ limitations under the License.
[
'form',
],
{
derived: true,
},
],
[
'open',
Expand Down Expand Up @@ -684,6 +708,9 @@ limitations under the License.
'input',
'textarea',
],
{
derived: true,
},
],
[
'rel',
Expand All @@ -700,6 +727,9 @@ limitations under the License.
'select',
'textarea',
],
{
derived: true,
},
],
[
'reversed',
Expand Down Expand Up @@ -746,6 +776,9 @@ limitations under the License.
[
'option',
],
{
derived: true,
},
],
[
'shape',
Expand All @@ -760,6 +793,9 @@ limitations under the License.
'input',
'select',
],
{
derived: true,
},
],
[
'sizes',
Expand Down Expand Up @@ -884,6 +920,10 @@ limitations under the License.
'progress',
'param',
],
{
derived: true,
normalize: false,
},
],
[
'width',
Expand Down Expand Up @@ -1050,8 +1090,16 @@ limitations under the License.
];

const supportedAttributes = new Map();
for (const [key, whitelist] of SUPPORTED_ATTRIBUTES) {
supportedAttributes.set(key, whitelist || '*');
const derivedAttributes = [];
for (const [key, whitelist, config] of SUPPORTED_ATTRIBUTES) {
const options = {
whitelist: whitelist || '*',
config: config || {},
};
supportedAttributes.set(key, options);
if (config && config.derived) {
derivedAttributes.push(key);
}
}

const Browser = {
Expand All @@ -1060,14 +1108,22 @@ limitations under the License.
return supportedAttributes.has(key);
},

isAttributeDerived(key) {
return derivedAttributes.includes(key);
},

isAttributeValid(key, element) {
const whitelist = supportedAttributes.get(key);
if (whitelist) {
return whitelist === '*' || whitelist.includes(element);
const options = supportedAttributes.get(key);
if (options) {
return options.whitelist === '*' || options.whitelist.includes(element);
}
return false;
},

getDerivedAttributes() {
return derivedAttributes;
},

getValidElementNamesFor(key) {
return supportedAttributes.get(key);
},
Expand Down
17 changes: 11 additions & 6 deletions src/core/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,19 @@ limitations under the License.
const changed = attrs.filter(
attr => nextAttrs.includes(attr) && current[attr] !== next[attr]);

for (let attr of added) {
this.addPatch(Patch.setAttribute(attr, next[attr], target, isCustom));
for (let attr of added.concat(changed)) {
if (opr.Toolkit.Browser.isAttributeDerived(attr)) {
this.addPatch(Patch.setProperty(attr, next[attr], target));
} else {
this.addPatch(Patch.setAttribute(attr, next[attr], target, isCustom));
}
}
for (let attr of removed) {
this.addPatch(Patch.removeAttribute(attr, target, isCustom));
}
for (let attr of changed) {
this.addPatch(Patch.setAttribute(attr, next[attr], target, isCustom));
if (opr.Toolkit.Browser.isAttributeDerived(attr)) {
this.addPatch(Patch.deleteProperty(attr, target));
} else {
this.addPatch(Patch.removeAttribute(attr, target, isCustom));
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/diff-calculate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Diff => calculate patches', () => {
const nextTemplate = [
'input',
{
value: 'next',
title: 'foobar',
},
];

Expand All @@ -90,8 +90,8 @@ describe('Diff => calculate patches', () => {
assert.equal(patches.length, 2);
assert.equal(patches[0].type, Patch.Type.SET_ATTRIBUTE);
assert(patches[0].target.isElement());
assert.equal(patches[0].name, 'value');
assert.equal(patches[0].value, 'next');
assert.equal(patches[0].name, 'title');
assert.equal(patches[0].value, 'foobar');

assertUpdatesNode(patches[1], element);
});
Expand Down
2 changes: 2 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const PORT = 3033;
app.set('port', PORT);

app.use('/', express.static('test'));
app.use('/functional', express.static('test/functional'));
app.use('/introspector', express.static('test/web/introspector'));
app.use('/config', express.static('test/config'));
app.use('/src', express.static('src'));
app.use('/core', express.static('src/core'));
Expand Down