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

Modified extend to take into account primitive and absent values #2810

Merged
merged 2 commits into from
Jun 26, 2017
Merged
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
34 changes: 33 additions & 1 deletion src/extras/primitives/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports.registerPrimitive = function registerPrimitive (name, definition)
mixins.forEach(function applyMixin (mixinId) {
var mixinComponents = self.sceneEl.querySelector('#' + mixinId).componentCache;
Object.keys(mixinComponents).forEach(function setComponent (name) {
data[name] = utils.extendDeep(data[name] || {}, mixinComponents[name]);
data[name] = extend(data[name], mixinComponents[name]);
});
});
}
Expand All @@ -100,6 +100,38 @@ module.exports.registerPrimitive = function registerPrimitive (name, definition)
}

return data;

/**
* For the base to be extensible, both objects must be pure JavaScript objects.
* The function assumes that base is undefined, or null or a pure object.
*/
function extend (base, extension) {
if (isUndefined(base)) {
return copy(extension);
}
if (isUndefined(extension)) {
return copy(base);
}
if (isPureObject(base) && isPureObject(extension)) {
return utils.extendDeep(base, extension);
}
return copy(extension);
}

function isUndefined (value) {
return typeof value === 'undefined';
}

function copy (value) {
if (isPureObject(value)) {
return utils.extendDeep({}, value);
}
return value;
}

function isPureObject (value) {
return value !== null && value.constructor === Object;
}
}
},

Expand Down
65 changes: 65 additions & 0 deletions tests/extras/primitives/primitives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,71 @@ suite('registerPrimitive (using innerHTML)', function () {
});
});

test('merges mixin for multi-prop component', function (done) {
primitiveFactory({
defaultComponents: {
material: {color: 'blue'}
}
}, 'mixin="foo"', function postCreation (el) {
assert.equal(el.getAttribute('material').color, 'blue');
assert.equal(el.getAttribute('material').shader, 'flat');
el.setAttribute('material', {side: 'double'});
assert.equal(el.getAttribute('material').color, 'blue');
assert.equal(el.getAttribute('material').shader, 'flat');
assert.equal(el.getAttribute('material').side, 'double');
done();
}, function preCreation (sceneEl) {
helpers.mixinFactory('foo', {material: 'shader: flat'}, sceneEl);
});
});

test('applies boolean mixin', function (done) {
primitiveFactory({
defaultComponents: {
visible: {default: true}
}
}, 'mixin="foo"', function postCreation (el) {
assert.equal(el.getAttribute('visible'), false);
el.setAttribute('visible', true);
assert.equal(el.getAttribute('visible'), true);
done();
}, function preCreation (sceneEl) {
helpers.mixinFactory('foo', {visible: false}, sceneEl);
});
});

test('applies single-prop value mixin', function (done) {
AFRAME.registerComponent('test', {
schema: {default: 'foo'}
});
primitiveFactory({
defaultComponents: {}
}, 'mixin="foo"', function postCreation (el) {
assert.equal(el.getAttribute('test'), 'bar');
done();
}, function preCreation (sceneEl) {
helpers.mixinFactory('foo', {test: 'bar'}, sceneEl);
});
});

test('applies empty mixin', function (done) {
AFRAME.registerComponent('test', {
schema: {
foo: {default: 'foo'},
bar: {default: 'bar'}
}
});
primitiveFactory({
defaultComponents: {}
}, 'mixin="foo"', function postCreation (el) {
assert.equal(el.getAttribute('test').foo, 'foo');
assert.equal(el.getAttribute('test').bar, 'bar');
done();
}, function preCreation (sceneEl) {
helpers.mixinFactory('foo', {test: ''}, sceneEl);
});
});

test('prioritizes mapping over mixin', function (done) {
primitiveFactory({
defaultComponents: {
Expand Down