moddle-xml
Advanced tools
Comparing version 0.9.4 to 0.10.0
@@ -98,3 +98,18 @@ 'use strict'; | ||
function error(message) { | ||
return new Error(message); | ||
} | ||
/** | ||
* Get the moddle descriptor for a given instance or type. | ||
* | ||
* @param {ModdleElement|Function} element | ||
* | ||
* @return {Object} the moddle descriptor | ||
*/ | ||
function getModdleDescriptor(element) { | ||
return element.$descriptor; | ||
} | ||
/** | ||
* A parse context. | ||
@@ -105,3 +120,3 @@ * | ||
* @param {Object} options | ||
* @param {ElementHandler} options.parseRoot the root handler for parsing a document | ||
* @param {ElementHandler} options.rootHandler the root handler for parsing a document | ||
* @param {boolean} [options.lax=false] whether or not to ignore invalid elements | ||
@@ -112,3 +127,3 @@ */ | ||
/** | ||
* @property {ElementHandler} parseRoot | ||
* @property {ElementHandler} rootHandler | ||
*/ | ||
@@ -122,21 +137,49 @@ | ||
var elementsById = this.elementsById = {}; | ||
var references = this.references = []; | ||
var warnings = this.warnings = []; | ||
this.elementsById = {}; | ||
this.references = []; | ||
this.warnings = []; | ||
/** | ||
* Add an unresolved reference. | ||
* | ||
* @param {Object} reference | ||
*/ | ||
this.addReference = function(reference) { | ||
references.push(reference); | ||
this.references.push(reference); | ||
}; | ||
this.addElement = function(id, element) { | ||
/** | ||
* Add a processed element. | ||
* | ||
* @param {ModdleElement} element | ||
*/ | ||
this.addElement = function(element) { | ||
if (!id || !element) { | ||
throw new Error('[xml-reader] id or ctx must not be null'); | ||
if (!element) { | ||
throw error('expected element'); | ||
} | ||
elementsById[id] = element; | ||
var descriptor = getModdleDescriptor(element); | ||
var idProperty = descriptor.idProperty, | ||
id; | ||
if (idProperty) { | ||
id = element.get(idProperty.name); | ||
if (id) { | ||
this.elementsById[id] = element; | ||
} | ||
} | ||
}; | ||
this.addWarning = function (w) { | ||
warnings.push(w); | ||
/** | ||
* Add an import warning. | ||
* | ||
* @param {Object} warning | ||
* @param {String} warning.message | ||
* @param {Error} [warning.error] | ||
*/ | ||
this.addWarning = function(warning) { | ||
this.warnings.push(warning); | ||
}; | ||
@@ -185,3 +228,3 @@ } | ||
if (this.element) { | ||
throw new Error('expected no sub nodes'); | ||
throw error('expected no sub nodes'); | ||
} else { | ||
@@ -234,12 +277,8 @@ this.element = this.createReference(node); | ||
var parser = this, | ||
element = this.element, | ||
id; | ||
element = this.element; | ||
if (!element) { | ||
element = this.element = this.createElement(node); | ||
id = element.id; | ||
if (id) { | ||
this.context.addElement(id, element); | ||
} | ||
this.context.addElement(element); | ||
} else { | ||
@@ -272,3 +311,3 @@ parser = this.handleChild(node); | ||
element = this.element, | ||
descriptor = element.$descriptor, | ||
descriptor = getModdleDescriptor(element), | ||
bodyProperty = descriptor.bodyProperty; | ||
@@ -290,3 +329,3 @@ | ||
Type = this.type, | ||
descriptor = Type.$descriptor, | ||
descriptor = getModdleDescriptor(Type), | ||
context = this.context, | ||
@@ -339,3 +378,3 @@ instance = new Type({}); | ||
model = this.model, | ||
descriptor = type.$descriptor; | ||
descriptor = getModdleDescriptor(type); | ||
@@ -364,3 +403,3 @@ var propertyName = nameNs.name, | ||
return assign({}, property, { effectiveType: elementType.$descriptor.name }); | ||
return assign({}, property, { effectiveType: getModdleDescriptor(elementType).name }); | ||
} | ||
@@ -386,3 +425,3 @@ } | ||
if (property) { | ||
return assign({}, property, { effectiveType: elementType.$descriptor.name }); | ||
return assign({}, property, { effectiveType: getModdleDescriptor(elementType).name }); | ||
} | ||
@@ -400,7 +439,7 @@ } else { | ||
throw new Error('unrecognized element <' + nameNs.name + '>'); | ||
throw error('unrecognized element <' + nameNs.name + '>'); | ||
}; | ||
ElementHandler.prototype.toString = function() { | ||
return 'ElementDescriptor[' + this.type.$descriptor.name + ']'; | ||
return 'ElementDescriptor[' + getModdleDescriptor(this.type).name + ']'; | ||
}; | ||
@@ -540,11 +579,33 @@ | ||
XMLReader.prototype.fromXML = function(xml, rootHandler, done) { | ||
/** | ||
* Parse the given XML into a moddle document tree. | ||
* | ||
* @param {String} xml | ||
* @param {ElementHandler|Object} options or rootHandler | ||
* @param {Function} done | ||
*/ | ||
XMLReader.prototype.fromXML = function(xml, options, done) { | ||
var rootHandler = options.rootHandler; | ||
if (options instanceof ElementHandler) { | ||
// root handler passed via (xml, { rootHandler: ElementHandler }, ...) | ||
rootHandler = options; | ||
options = {}; | ||
} else { | ||
if (typeof options === 'string') { | ||
// rootHandler passed via (xml, 'someString', ...) | ||
rootHandler = this.handler(options); | ||
options = {}; | ||
} else if (typeof rootHandler === 'string') { | ||
// rootHandler passed via (xml, { rootHandler: 'someString' }, ...) | ||
rootHandler = this.handler(rootHandler); | ||
} | ||
} | ||
var model = this.model, | ||
lax = this.lax, | ||
context = new Context({ | ||
parseRoot: rootHandler | ||
}); | ||
lax = this.lax; | ||
var parser = new SaxParser(true, { xmlns: true, trim: true }), | ||
var context = new Context(assign({}, options, { rootHandler: rootHandler })), | ||
parser = new SaxParser(true, { xmlns: true, trim: true }), | ||
stack = new Stack(); | ||
@@ -568,3 +629,3 @@ | ||
var reference = elementsById[r.id]; | ||
var property = element.$descriptor.propertiesByName[r.property]; | ||
var property = getModdleDescriptor(element).propertiesByName[r.property]; | ||
@@ -639,3 +700,3 @@ if (!reference) { | ||
throw new Error(message); | ||
throw error(message); | ||
} | ||
@@ -642,0 +703,0 @@ } |
@@ -345,3 +345,6 @@ 'use strict'; | ||
} catch (e) { | ||
console.warn('[writer] missing namespace information for ', attr.name, '=', attr.value, 'on', element, e); | ||
console.warn( | ||
'missing namespace information for ', | ||
attr.name, '=', attr.value, 'on', element, | ||
e); | ||
} | ||
@@ -348,0 +351,0 @@ }); |
{ | ||
"name": "moddle-xml", | ||
"version": "0.9.4", | ||
"version": "0.10.0", | ||
"description": "XML import/export for documents described with moddle", | ||
@@ -54,3 +54,3 @@ "directories": { | ||
"lodash": "^3.0.0", | ||
"moddle": "^0.6.2", | ||
"moddle": "^0.7.0", | ||
"sax": "~0.6.0", | ||
@@ -57,0 +57,0 @@ "tiny-stack": "^0.1.0" |
@@ -12,3 +12,3 @@ { | ||
"properties": [ | ||
{ "name": "id", "type": "String" }, | ||
{ "name": "id", "type": "String", "isId": true }, | ||
{ "name": "extensions", "type": "Element", "isMany" : true } | ||
@@ -15,0 +15,0 @@ ] |
@@ -9,3 +9,3 @@ { | ||
"properties": [ | ||
{ "name": "id", "type": "Integer", "isAttr": true } | ||
{ "name": "id", "type": "Integer", "isAttr": true, "isId": true } | ||
] | ||
@@ -12,0 +12,0 @@ } |
@@ -12,3 +12,3 @@ { | ||
"properties": [ | ||
{ "name": "id", "type": "String", "isAttr": true } | ||
{ "name": "id", "type": "String", "isAttr": true, "isId": true } | ||
] | ||
@@ -99,3 +99,3 @@ }, | ||
"properties": [ | ||
{ "name": "id", "type": "String", "isAttr": true } | ||
{ "name": "id", "type": "String", "isAttr": true, "isId": true } | ||
] | ||
@@ -107,3 +107,3 @@ }, | ||
"properties": [ | ||
{ "name": "idNumeric", "type": "String", "isAttr": true, "redefines": "BaseWithId#id" } | ||
{ "name": "idNumeric", "type": "String", "isAttr": true, "redefines": "BaseWithId#id", "isId": true } | ||
] | ||
@@ -153,3 +153,4 @@ }, | ||
"type": "String", | ||
"isAttr": true | ||
"isAttr": true, | ||
"isId": true | ||
} | ||
@@ -156,0 +157,0 @@ ] |
@@ -1704,2 +1704,88 @@ 'use strict'; | ||
describe('fake ids', function() { | ||
var fakeIdsModel = createModel([ 'fake-id' ]); | ||
it('should ignore (non-id) id attribute', function(done) { | ||
// given | ||
var reader = new Reader(fakeIdsModel); | ||
var rootHandler = reader.handler('fi:Root'); | ||
var xml = | ||
'<fi:Root xmlns:fi="http://fakeid">' + | ||
'<fi:ChildWithFakeId id="FOO" />' + | ||
'</fi:Root>'; | ||
// when | ||
reader.fromXML(xml, rootHandler, function(err, result, context) { | ||
if (err) { | ||
return done(err); | ||
} | ||
// then | ||
expect(result).to.jsonEqual({ | ||
$type: 'fi:Root', | ||
children: [ | ||
{ | ||
$type: 'fi:ChildWithFakeId', | ||
id: 'FOO' | ||
} | ||
] | ||
}); | ||
expect(context.elementsById).to.be.empty; | ||
done(); | ||
}); | ||
}); | ||
it('should not-resolve (non-id) id references', function(done) { | ||
// given | ||
var reader = new Reader(fakeIdsModel); | ||
var rootHandler = reader.handler('fi:Root'); | ||
var xml = | ||
'<fi:Root xmlns:fi="http://fakeid">' + | ||
'<fi:ChildWithFakeId id="FOO" />' + | ||
'<fi:ChildWithFakeId ref="FOO" />' + | ||
'</fi:Root>'; | ||
// when | ||
reader.fromXML(xml, rootHandler, function(err, result, context) { | ||
if (err) { | ||
return done(err); | ||
} | ||
// then | ||
expect(result).to.jsonEqual({ | ||
$type: 'fi:Root', | ||
children: [ | ||
{ | ||
$type: 'fi:ChildWithFakeId', | ||
id: 'FOO' | ||
}, | ||
{ | ||
$type: 'fi:ChildWithFakeId' | ||
} | ||
] | ||
}); | ||
expect(context.warnings).to.have.length(1); | ||
expect(context.warnings[0].message).to.eql('unresolved reference <FOO>'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126652
30
3664
+ Addedmoddle@0.7.0(transitive)
- Removedmoddle@0.6.4(transitive)
Updatedmoddle@^0.7.0