From e7ccc603fdd4310c97df3f339a058731a4be6344 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sat, 17 Dec 2022 17:35:27 +0100 Subject: [PATCH] feat: allow to explicitly declare attributes as global --- lib/properties.js | 10 ++++++--- test/spec/moddle.js | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/properties.js b/lib/properties.js index bc56234..f57e8c4 100644 --- a/lib/properties.js +++ b/lib/properties.js @@ -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 { @@ -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; } } }; @@ -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; @@ -173,4 +173,8 @@ function defineProperty(target, property, value) { value: value, configurable: true }); +} + +function stripGlobal(name) { + return name.replace(/^:/, ''); } \ No newline at end of file diff --git a/test/spec/moddle.js b/test/spec/moddle.js index 4460ecd..b80f55b 100644 --- a/test/spec/moddle.js +++ b/test/spec/moddle.js @@ -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'); + }); + }); @@ -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 on /); + + // available as extension attribute + expect(element.$attrs).to.have.property('xmlns'); + }); + + it('fail accessing unknown property', function() { // when