Comparing version 0.4.4 to 0.5.0
# Changes | ||
## 0.5.0 | ||
- Add rudimentary XInclude support. | ||
## 0.4.4 | ||
@@ -4,0 +8,0 @@ |
// Generated by CoffeeScript 2.5.1 | ||
(function() { | ||
var DOMParser, _, binding, curlyNS, forAllAttributes; | ||
var DOMParser, _, binding, curlyNS, forAllAttributes, xincludeNS; | ||
@@ -13,2 +13,4 @@ DOMParser = require('xmldom').DOMParser; | ||
xincludeNS = 'http://www.w3.org/2001/XInclude'; | ||
forAllAttributes = function(node, fn) { | ||
@@ -29,6 +31,15 @@ var i, length, result; | ||
({element, attr, text, comment, capture} = opts); | ||
parse = function(node) { | ||
var attrs, captured, childNodes, content, el, empty, piNode; | ||
parse = function(node, resolve) { | ||
var attrs, captured, childNodes, content, el, empty, href, next, piNode, xml; | ||
switch (node.nodeType) { | ||
case 1: | ||
if (node.namespaceURI === xincludeNS && node.localName === 'include' && (resolve != null)) { | ||
href = node.getAttributeNS(xincludeNS, 'href'); | ||
if (href != null) { | ||
[xml, next] = resolve(href); | ||
if (xml != null) { | ||
return parse(new DOMParser().parseFromString(xml).documentElement, next); | ||
} | ||
} | ||
} | ||
el = element(node.tagName); | ||
@@ -50,3 +61,5 @@ if (node.namespaceURI) { | ||
} | ||
content = childNodes.map(parse).filter(_.negate(_.isUndefined)); | ||
content = childNodes.map(function(node) { | ||
return parse(node, resolve); | ||
}).filter(_.negate(_.isUndefined)); | ||
el.content(...content); | ||
@@ -92,4 +105,4 @@ attrs = []; | ||
}; | ||
return function(xml) { | ||
return parse(new DOMParser().parseFromString(xml).documentElement); | ||
return function(xml, resolver) { | ||
return parse(new DOMParser().parseFromString(xml).documentElement, resolver); | ||
}; | ||
@@ -96,0 +109,0 @@ }; |
{ | ||
"name": "cruftless", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"description": "Yet another simple way to parse and generate XML", | ||
@@ -5,0 +5,0 @@ "main": "lib/cruftless.js", |
@@ -345,1 +345,28 @@ ```javascript --hide | ||
``` | ||
## Rudimentary xinclude support | ||
With cruftless, it often makes sense to break a larger template apart into | ||
smaller ones that can be referenced in various locations. To that end, we're | ||
relying on basic xinclude implementation. | ||
```javascript --run simple-2 | ||
resolve = (href) => { | ||
return ["<bla/>", resolve]; | ||
}; | ||
template = parse( | ||
`<foo xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="bla.xml"/></foo>`, | ||
resolve | ||
); | ||
console.log(template.toXML({})); | ||
``` | ||
Note that the resolve function is expected to resolve the href within a context | ||
and then return both the XML _and_ a new resolve function that is capable fo | ||
resolving hrefs from within the context of the resolved file. In this case, | ||
we're not really doing that. In fact, this resolver will **always** return the | ||
same snippet of XML, but it doesn't require a lot of imagination to figure out | ||
how to turn this resolver into something sensible. | ||
If you are not passing the resolve function, then it will simply leave the | ||
xinclude unharmed. |
@@ -438,3 +438,33 @@ <!-- | ||
## Rudimentary xinclude support | ||
With cruftless, it often makes sense to break a larger template apart into | ||
smaller ones that can be referenced in various locations. To that end, we're | ||
relying on basic xinclude implementation. | ||
```javascript | ||
resolve = (href) => { | ||
return ["<bla/>", resolve]; | ||
}; | ||
template = parse( | ||
`<foo xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="bla.xml"/></foo>`, | ||
resolve | ||
); | ||
console.log(template.toXML({})); | ||
⇒ <foo> | ||
⇒ <bla/> | ||
⇒ </foo> | ||
``` | ||
Note that the resolve function is expected to resolve the href within a context | ||
and then return both the XML _and_ a new resolve function that is capable fo | ||
resolving hrefs from within the context of the resolved file. In this case, | ||
we're not really doing that. In fact, this resolver will **always** return the | ||
same snippet of XML, but it doesn't require a lot of imagination to figure out | ||
how to turn this resolver into something sensible. | ||
If you are not passing the resolve function, then it will simply leave the | ||
xinclude unharmed. | ||
---- | ||
Markdown generated from [./README.js.md](./README.js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd) |
63879
1079
469