Skip to content

Commit

Permalink
feat: allow to explicitly declare attributes as global
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku authored and fake-join[bot] committed Dec 20, 2022
1 parent 9de9478 commit e7ccc60
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Properties.prototype.set = function(target, name, value) {
if (property) {
delete target[propertyName];
} else {
delete target.$attrs[name];
delete target.$attrs[stripGlobal(name)];
}
} else {

Expand All @@ -51,7 +51,7 @@ Properties.prototype.set = function(target, name, value) {
defineProperty(target, property, value);
}
} else {
target.$attrs[name] = value;
target.$attrs[stripGlobal(name)] = value;
}
}
};
Expand All @@ -69,7 +69,7 @@ Properties.prototype.get = function(target, name) {
var property = this.getProperty(target, name);

if (!property) {
return target.$attrs[name];
return target.$attrs[stripGlobal(name)];
}

var propertyName = property.name;
Expand Down Expand Up @@ -173,4 +173,8 @@ function defineProperty(target, property, value) {
value: value,
configurable: true
});
}

function stripGlobal(name) {
return name.replace(/^:/, '');
}
51 changes: 51 additions & 0 deletions test/spec/moddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,38 @@ describe('moddle', function() {
expect(element.get('props:count')).to.eql(10);
});


it('should access global name', function() {

// when
const element = moddle.create('props:ComplexCount', {
':xmlns': 'http://foo'
});

// then
expect(element.get(':xmlns')).to.eql('http://foo');
expect(element.get('xmlns')).to.eql('http://foo');

// available as extension attribute
expect(element.$attrs).to.have.property('xmlns');
});


it('should access global name (no prefix)', function() {

// when
const element = moddle.create('props:ComplexCount', {
'xmlns': 'http://foo'
});

// then
expect(element.get(':xmlns')).to.eql('http://foo');
expect(element.get('xmlns')).to.eql('http://foo');

// available as extension attribute
expect(element.$attrs).to.have.property('xmlns');
});

});


Expand Down Expand Up @@ -438,6 +470,25 @@ describe('moddle', function() {
});


it('should access global name', function() {

// when
const element = moddle.create('props:ComplexCount', {
':xmlns': 'http://foo'
});

// then
expect(element.get(':xmlns')).to.eql('http://foo');

expect(() => {
element.get('xmlns');
}).to.throw(/unknown property <xmlns> on <props:ComplexCount>/);

// available as extension attribute
expect(element.$attrs).to.have.property('xmlns');
});


it('fail accessing unknown property', function() {

// when
Expand Down

0 comments on commit e7ccc60

Please sign in to comment.