Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

moddle-xml

Package Overview
Dependencies
Maintainers
3
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moddle-xml - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

24

lib/common.js
'use strict';
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lower(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
function hasLowerCaseAlias(pkg) {

@@ -15,19 +7,5 @@ return pkg.xml && pkg.xml.tagAlias === 'lowerCase';

module.exports.hasLowerCaseAlias = hasLowerCaseAlias;
module.exports.aliasToName = function(alias, pkg) {
if (hasLowerCaseAlias(pkg)) {
return capitalize(alias);
} else {
return alias;
}
};
module.exports.nameToAlias = function(name, pkg) {
if (hasLowerCaseAlias(pkg)) {
return lower(name);
} else {
return name;
}
};
module.exports.DEFAULT_NS_MAP = {

@@ -34,0 +12,0 @@ 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'

85

lib/reader.js

@@ -18,19 +18,25 @@ 'use strict';

serializeAsType = common.serializeAsType,
aliasToName = common.aliasToName;
hasLowerCaseAlias = common.hasLowerCaseAlias;
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function normalizeTypeName(name, model) {
function aliasToName(aliasNs, pkg) {
var nameNs = parseNameNs(name);
var pkg = model.getPackage(nameNs.prefix);
if (!hasLowerCaseAlias(pkg)) {
return aliasNs.name;
}
var typePrefix = pkg.xml && pkg.xml.typePrefix;
return aliasNs.prefix + ':' + capitalize(aliasNs.localName);
}
if (!typePrefix) {
return name;
}
function prefixedToName(nameNs, pkg) {
var localName = nameNs.localName;
var name = nameNs.name,
localName = nameNs.localName;
if (localName.indexOf(typePrefix) === 0) {
var typePrefix = pkg.xml && pkg.xml.typePrefix;
if (typePrefix && localName.indexOf(typePrefix) === 0) {
return nameNs.prefix + ':' + localName.slice(typePrefix.length);

@@ -42,2 +48,10 @@ } else {

function normalizeXsiTypeName(name, model) {
var nameNs = parseNameNs(name);
var pkg = model.getPackage(nameNs.prefix);
return prefixedToName(nameNs, pkg);
}
function error(message) {

@@ -241,5 +255,5 @@ return new Error(message);

*/
function ElementHandler(model, type, context) {
function ElementHandler(model, typeName, context) {
this.model = model;
this.type = model.getType(type);
this.type = model.getType(typeName);
this.context = context;

@@ -319,3 +333,4 @@ }

var nameNs = parseNameNs(node.name);
var name = node.name;
var nameNs = parseNameNs(name);

@@ -344,3 +359,3 @@ var type = this.type,

// into account, i.e.: xsi:type="t{ActualType}"
elementTypeName = normalizeTypeName(elementTypeName, model);
elementTypeName = normalizeXsiTypeName(elementTypeName, model);

@@ -357,7 +372,6 @@ elementType = model.getType(elementTypeName);

var pkg = model.getPackage(nameNs.prefix);
if (pkg) {
elementTypeName = nameNs.prefix + ':' + aliasToName(nameNs.localName, descriptor.$pkg);
elementTypeName = aliasToName(nameNs, pkg);
elementType = model.getType(elementTypeName);

@@ -457,4 +471,39 @@

/**
* An element handler that performs special validation
* to ensure the node it gets initialized with matches
* the handlers type (namespace wise).
*
* @param {Moddle} model
* @param {String} typeName
* @param {Context} context
*/
function RootElementHandler(model, typeName, context) {
ElementHandler.call(this, model, typeName, context);
}
function GenericElementHandler(model, type, context) {
RootElementHandler.prototype = Object.create(ElementHandler.prototype);
RootElementHandler.prototype.createElement = function(node) {
var name = node.name,
nameNs = parseNameNs(name),
model = this.model,
type = this.type,
pkg = model.getPackage(nameNs.prefix),
typeName = pkg && aliasToName(nameNs, pkg) || name;
// verify the correct namespace if we parse
// the first element in the handler tree
//
// this ensures we don't mistakenly import wrong namespace elements
if (!type.hasType(typeName)) {
throw error('unexpected element <' + node.originalName + '>');
}
return ElementHandler.prototype.createElement.call(this, node);
};
function GenericElementHandler(model, typeName, context) {
this.model = model;

@@ -753,3 +802,3 @@ this.context = context;

XMLReader.prototype.handler = function(name) {
return new ElementHandler(this.model, name);
return new RootElementHandler(this.model, name);
};

@@ -756,0 +805,0 @@

@@ -12,3 +12,3 @@ 'use strict';

common = require('./common'),
nameToAlias = common.nameToAlias,
hasLowerCaseAlias = common.hasLowerCaseAlias,
serializeAsType = common.serializeAsType,

@@ -23,2 +23,14 @@ serializeAsProperty = common.serializeAsProperty;

function lower(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
function nameToAlias(name, pkg) {
if (hasLowerCaseAlias(pkg)) {
return lower(name);
} else {
return name;
}
}
function inherits(ctor, superCtor) {

@@ -25,0 +37,0 @@ ctor.super_ = superCtor;

{
"name": "moddle-xml",
"version": "2.0.0",
"version": "2.0.1",
"description": "XML import/export for documents described with moddle",

@@ -10,3 +10,3 @@ "directories": {

"all": "run-s lint test",
"lint": "eslint {lib,test}/**/*.js",
"lint": "eslint .",
"dev": "npm test -- --watch",

@@ -13,0 +13,0 @@ "test": "mocha -r ./test/expect --reporter=spec test/**/*.js"

@@ -520,5 +520,5 @@ 'use strict';

var xml = '<r:extension xmlns:r="http://replace">' +
var xml = '<r:Extension xmlns:r="http://replace">' +
'<r:value></r:value>' +
'</r:extension>';
'</r:Extension>';

@@ -804,2 +804,54 @@ // when

describe('should not import', function() {
var model = createModel([ 'properties' ]);
describe('wrong namespace', function() {
it('same alias', function(done) {
// given
var reader = new Reader(model);
var rootHandler = reader.handler('props:Root');
var xml = '<props:root xmlns:props="http://invalid">' +
'<props:referencingSingle id="C_4" />' +
'</props:root>';
// when
reader.fromXML(xml, rootHandler, function(err, result) {
expect(err).to.exist;
expect(result).not.to.exist;
done();
});
});
it('different alias', function(done) {
// given
var reader = new Reader(model);
var rootHandler = reader.handler('props:Root');
var xml = '<props1:root xmlns:props1="http://invalid">' +
'<props1:referencingSingle id="C_4" />' +
'</props1:root>';
// when
reader.fromXML(xml, rootHandler, function(err, result) {
expect(err).to.exist;
expect(result).not.to.exist;
done();
});
});
});
});
describe('internal', function() {

@@ -809,2 +861,3 @@

describe('should identify references', function() {

@@ -976,28 +1029,78 @@

it('should handle invalid root element', function(done) {
describe('should handle invalid root element', function() {
var xml = '<props:referencingCollection xmlns:props="http://properties" id="C_4">' +
'<props:references>C_2</props:references>' +
'<props:references>C_5</props:references>' +
'</props:referencingCollection>';
it('wrong type', function(done) {
var reader = new Reader(model);
var rootHandler = reader.handler('props:ComplexAttrs');
var xml = '<props:referencingCollection xmlns:props="http://properties" id="C_4">' +
'<props:references>C_2</props:references>' +
'<props:references>C_5</props:references>' +
'</props:referencingCollection>';
var expectedError =
'unparsable content <props:references> detected\n\t' +
'line: 0\n\t' +
'column: 70\n\t' +
'nested error: unknown type <props:References>';
var reader = new Reader(model);
var rootHandler = reader.handler('props:ComplexAttrs');
// when
reader.fromXML(xml, rootHandler, function(err, result) {
var expectedError =
'unparsable content <props:referencingCollection> detected\n\t' +
'line: 0\n\t' +
'column: 0\n\t' +
'nested error: unexpected element <props:referencingCollection>';
expect(err).to.exist;
expect(err.message).to.eql(expectedError);
// when
reader.fromXML(xml, rootHandler, function(err, result) {
expect(result).not.to.exist;
expect(err).to.exist;
expect(err.message).to.eql(expectedError);
done();
expect(result).not.to.exist;
done();
});
});
it('wrong uri', function(done) {
// given
var reader = new Reader(model);
var rootHandler = reader.handler('props:Root');
var xml = '<props:root xmlns:props="http://invalid">' +
'<props:referencingSingle id="C_4" />' +
'</props:root>';
// when
reader.fromXML(xml, rootHandler, function(err, result) {
expect(err).to.exist;
expect(err.message).to.match(/unexpected element <props:root>/);
expect(result).not.to.exist;
done();
});
});
it('unknown uri + prefix', function(done) {
// given
var reader = new Reader(model);
var rootHandler = reader.handler('props:Root');
var xml = '<props1:root xmlns:props1="http://invalid">' +
'<props1:referencingSingle id="C_4" />' +
'</props1:root>';
// when
reader.fromXML(xml, rootHandler, function(err, result) {
expect(err).to.exist;
expect(err.message).to.match(/unexpected element <props1:root>/);
expect(result).not.to.exist;
done();
});
});
});

@@ -1062,2 +1165,3 @@

it('should handle duplicate id', function(done) {

@@ -1064,0 +1168,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc