Comparing version 1.2.0 to 1.2.1
@@ -27,2 +27,3 @@ declare module "cruftless" { | ||
} | ||
export interface CruftlessOpts { | ||
@@ -35,2 +36,8 @@ types: CruftlessTypes; | ||
kindProperty?: string; | ||
/** | ||
* Prefix mapping for namespaces. Prefixes are the keys, namespaces are the values. | ||
* Used for xsi:type canonicalization. | ||
*/ | ||
prefixes?: Record<string, string>; | ||
} | ||
@@ -37,0 +44,0 @@ |
@@ -19,2 +19,3 @@ // Generated by CoffeeScript 2.5.1 | ||
opts.kindProperty = opts.kindProperty || 'kind'; | ||
opts.prefixes = opts.prefixes || {}; | ||
element = require('./model/element')(opts); | ||
@@ -21,0 +22,0 @@ attr = require('./model/attr')(opts); |
@@ -19,5 +19,11 @@ // Generated by CoffeeScript 2.5.1 | ||
module.exports = function({types, kindProperty, format = _.identity}) { | ||
module.exports = function({types, kindProperty, format = _.identity, prefixes}) { | ||
return function(name) { | ||
var exposed, meta; | ||
/* | ||
This is a simple workaround for | ||
https://github.com/wspringer/cruftless/issues/60 If there *are* prefixes | ||
registered, it will try to rewrite the type to a canonical version of it, | ||
using a registered prefix. Otherwise, it will use the value of xsi:type as is. | ||
*/ | ||
var canonicalizedXsiType, exposed, meta; | ||
meta = { | ||
@@ -46,2 +52,20 @@ name: name, | ||
}; | ||
canonicalizedXsiType = function(elem) { | ||
var localName, ns, prefix, qname, qualifier; | ||
qname = elem.getAttributeNS(xsiNS, 'type'); | ||
if (qname.indexOf(':') > 0) { | ||
[prefix, localName] = qname.split(':'); | ||
ns = elem.lookupNamespaceURI(prefix); | ||
qualifier = Object.keys(prefixes).find(function(key) { | ||
return prefixes[key] === ns; | ||
}); | ||
if (qualifier != null) { | ||
return `${qualifier}:${localName}`; | ||
} else { | ||
return qname; | ||
} | ||
} else { | ||
return qname; | ||
} | ||
}; | ||
exposed = { | ||
@@ -253,3 +277,3 @@ optional: function() { | ||
matches: function(elem) { | ||
return elem.nodeType === 1 && elem.localName === meta.name && (!(meta.ns != null) || meta.ns === elem.namespaceURI) && (_.isUndefined(meta.kind) || elem.getAttributeNS(xsiNS, 'type') === meta.kind); | ||
return elem.nodeType === 1 && elem.localName === meta.name && (!(meta.ns != null) || meta.ns === elem.namespaceURI) && (_.isUndefined(meta.kind) || canonicalizedXsiType(elem) === meta.kind); | ||
}, | ||
@@ -256,0 +280,0 @@ extract: function(elem, target = {}, raw = false) { |
{ | ||
"name": "cruftless", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Yet another simple way to parse and generate XML", | ||
@@ -5,0 +5,0 @@ "main": "lib/cruftless.js", |
@@ -338,3 +338,4 @@ ```javascript --hide | ||
```javascript --run simple-2 | ||
template = parse(` | ||
template = parse( | ||
` | ||
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
@@ -344,5 +345,8 @@ <person xsi:type="Student" c-bind="people|array" nickName="{{name}}" grade="{{grade}}" /> | ||
</people> | ||
`.trim()); | ||
`.trim() | ||
); | ||
console.log(template.fromXML(` | ||
console.log( | ||
template.fromXML( | ||
` | ||
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
@@ -353,3 +357,5 @@ <person xsi:type="Student" nickName="Jesse" grade="D" /> | ||
</people> | ||
`.trim())); | ||
`.trim() | ||
) | ||
); | ||
``` | ||
@@ -362,2 +368,12 @@ | ||
Now, `xsi:type` values are assumed to `QName`s. That means that there might be a | ||
prefix in the name that resolves to a namespace. The documents that you _parse_ | ||
might use different namespace prefixes than the binding template. In order to | ||
avoid issues with that, Cruftless accepts a `prefixes` option specifically for | ||
normalization of the prefixes. So, if your template refers to a type with an | ||
`ns1:` prefix, and the document you are passing is using the `ns0:` prefix, then | ||
you can make sure the types in the documents you are parsing are rewritten to | ||
`ns1`, by the namespace of `ns1` in the configuration options of your Cruftless | ||
instance. (See <https://github.com/wspringer/cruftless/blob/60-xsitype-should-always-be-interpreted-as-a-qname/test/model/xsi-type-test.coffee#L84>.) | ||
**NOTE:** There are [various | ||
@@ -364,0 +380,0 @@ issues](https://github.com/wspringer/cruftless/issues/50) with the way RelaxNG |
@@ -428,3 +428,4 @@ <!-- | ||
```javascript | ||
template = parse(` | ||
template = parse( | ||
` | ||
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
@@ -434,5 +435,8 @@ <person xsi:type="Student" c-bind="people|array" nickName="{{name}}" grade="{{grade}}" /> | ||
</people> | ||
`.trim()); | ||
`.trim() | ||
); | ||
console.log(template.fromXML(` | ||
console.log( | ||
template.fromXML( | ||
` | ||
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
@@ -443,3 +447,5 @@ <person xsi:type="Student" nickName="Jesse" grade="D" /> | ||
</people> | ||
`.trim())); | ||
`.trim() | ||
) | ||
); | ||
⇒ { | ||
@@ -459,2 +465,12 @@ ⇒ people: [ | ||
Now, `xsi:type` values are assumed to `QName`s. That means that there might be a | ||
prefix in the name that resolves to a namespace. The documents that you _parse_ | ||
might use different namespace prefixes than the binding template. In order to | ||
avoid issues with that, Cruftless accepts a `prefixes` option specifically for | ||
normalization of the prefixes. So, if your template refers to a type with an | ||
`ns1:` prefix, and the document you are passing is using the `ns0:` prefix, then | ||
you can make sure the types in the documents you are parsing are rewritten to | ||
`ns1`, by the namespace of `ns1` in the configuration options of your Cruftless | ||
instance. (See <https://github.com/wspringer/cruftless/blob/60-xsitype-should-always-be-interpreted-as-a-qname/test/model/xsi-type-test.coffee#L84>.) | ||
**NOTE:** There are [various | ||
@@ -461,0 +477,0 @@ issues](https://github.com/wspringer/cruftless/issues/50) with the way RelaxNG |
78062
20
1246
530