Comparing version 0.3.3 to 0.3.4
# Changes | ||
## 0.3.4 | ||
* Add CDATA support | ||
## 0.3.3 | ||
@@ -4,0 +8,0 @@ |
@@ -16,5 +16,10 @@ // Generated by CoffeeScript 2.5.1 | ||
required: false, | ||
valueType: types.string | ||
valueType: types.string, | ||
cdata: false | ||
}; | ||
exposed = { | ||
cdata: function() { | ||
meta.cdata = true; | ||
return exposed; | ||
}, | ||
required: function() { | ||
@@ -39,7 +44,7 @@ meta.required = true; | ||
value = extractValue(meta, obj); | ||
node = context.ownerDocument.createTextNode(meta.valueType.to(value)); | ||
node = meta.cdata ? context.ownerDocument.createCDATASection(meta.valueType.to(value)) : context.ownerDocument.createTextNode(meta.valueType.to(value)); | ||
context.appendChild(node); | ||
}, | ||
matches: function(node) { | ||
return node.nodeType === 3; | ||
return node.nodeType === 3 || node.nodeType === 4; | ||
}, | ||
@@ -46,0 +51,0 @@ sample: function(value) { |
{ | ||
"name": "cruftless", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Yet another simple way to parse and generate XML", | ||
@@ -30,4 +30,4 @@ "main": "lib/cruftless.js", | ||
"runmd": "^1.0.1", | ||
"xml-formatter": "^1.2.0" | ||
"xml-formatter": "^2.1.0" | ||
} | ||
} |
@@ -133,3 +133,4 @@ ```javascript --hide | ||
You can add your own value types to convert from and to the string literals included in the XML representation. | ||
You can add your own value types to convert from and to the string literals | ||
included in the XML representation. | ||
@@ -187,3 +188,4 @@ ```javascript --run simple-2 | ||
If you hate the magic `c-` prefixed attributes, then you can also a slightly less readable but admittedly more correct XML namespace: | ||
If you hate the magic `c-` prefixed attributes, then you can also a slightly | ||
less readable but admittedly more correct XML namespace: | ||
@@ -206,3 +208,7 @@ ```javascript --run simple-2 | ||
There may be times when you want to exclude entire sections of an XML structure if a particular condition is met. Cruftless has some basic support for that, albeit limited. You can set conditions on elements, using the `c-if` attribute. In that case, the element will only be included in case the expression of the `c-if` attribute is evaluating to something else than `undefined` or `null`. | ||
There may be times when you want to exclude entire sections of an XML structure | ||
if a particular condition is met. Cruftless has some basic support for that, | ||
albeit limited. You can set conditions on elements, using the `c-if` attribute. | ||
In that case, the element will only be included in case the expression of the | ||
`c-if` attribute is evaluating to something else than `undefined` or `null`. | ||
@@ -218,3 +224,8 @@ ```javascript --run simple-2 | ||
If your template contains variable references, and the data structure you are passing in does not contain these references, then — instead of generating the value `undefined`, Cruftless will drop the entire element. In fact, if a deeply nested element contains references to variable, and that variable is not defined, then it will not only drop *that* element, but all elements that included that element referring to a non-existing variable. | ||
If your template contains variable references, and the data structure you are | ||
passing in does not contain these references, then — instead of generating the | ||
value `undefined`, Cruftless will drop the entire element. In fact, if a deeply | ||
nested element contains references to variable, and that variable is not | ||
defined, then it will not only drop *that* element, but all elements that | ||
included that element referring to a non-existing variable. | ||
@@ -239,5 +250,38 @@ ```javascript --run simple-2 | ||
## CDATA | ||
Your XML documents might contain CDATA sections. Cruftless will treat those like | ||
ordinary text nodes. That is, if you have an element that has a text node bound | ||
to a variable, then it will resolve those values regardless of the fact if the | ||
incoming XML document has a text node or a CDATA node. | ||
```javascript --run simple-2 | ||
template = parse(`<person>{{name}}</person>`); | ||
console.log(template.fromXML(`<person>Alice</person>`)); | ||
``` | ||
```javascript --run simple-2 | ||
console.log(template.fromXML(`<person><![CDATA[Alice]]></person>`)); | ||
``` | ||
However, if you would *produce* XML, then — by default — it will always produce | ||
a text node: | ||
```javascript --run simple-2 | ||
console.log(template.toXML({ name: 'Alice' })); | ||
``` | ||
That is, unless you specifiy a `cdata` option in your binding: | ||
```javascript --run simple-2 | ||
template = parse(`<person>{{name|cdata}}</person>`); | ||
console.log(template.toXML({ name: 'Alice' })); | ||
``` | ||
## JSON-ish Schema (incomplete, subject to change) | ||
Since Cruftless has all of the metadata of your XML document and how it binds to your data structures at its disposal, it also allows you to generate a 'schema' of the data structure it expects. | ||
Since Cruftless has all of the metadata of your XML document and how it binds to | ||
your data structures at its disposal, it also allows you to generate a 'schema' | ||
of the data structure it expects. | ||
@@ -244,0 +288,0 @@ ```javascript --run simple-2 |
@@ -141,3 +141,4 @@ <!-- | ||
You can add your own value types to convert from and to the string literals included in the XML representation. | ||
You can add your own value types to convert from and to the string literals | ||
included in the XML representation. | ||
@@ -209,3 +210,4 @@ ```javascript | ||
If you hate the magic `c-` prefixed attributes, then you can also a slightly less readable but admittedly more correct XML namespace: | ||
If you hate the magic `c-` prefixed attributes, then you can also a slightly | ||
less readable but admittedly more correct XML namespace: | ||
@@ -238,3 +240,7 @@ ```javascript | ||
There may be times when you want to exclude entire sections of an XML structure if a particular condition is met. Cruftless has some basic support for that, albeit limited. You can set conditions on elements, using the `c-if` attribute. In that case, the element will only be included in case the expression of the `c-if` attribute is evaluating to something else than `undefined` or `null`. | ||
There may be times when you want to exclude entire sections of an XML structure | ||
if a particular condition is met. Cruftless has some basic support for that, | ||
albeit limited. You can set conditions on elements, using the `c-if` attribute. | ||
In that case, the element will only be included in case the expression of the | ||
`c-if` attribute is evaluating to something else than `undefined` or `null`. | ||
@@ -250,3 +256,8 @@ ```javascript | ||
If your template contains variable references, and the data structure you are passing in does not contain these references, then — instead of generating the value `undefined`, Cruftless will drop the entire element. In fact, if a deeply nested element contains references to variable, and that variable is not defined, then it will not only drop *that* element, but all elements that included that element referring to a non-existing variable. | ||
If your template contains variable references, and the data structure you are | ||
passing in does not contain these references, then — instead of generating the | ||
value `undefined`, Cruftless will drop the entire element. In fact, if a deeply | ||
nested element contains references to variable, and that variable is not | ||
defined, then it will not only drop *that* element, but all elements that | ||
included that element referring to a non-existing variable. | ||
@@ -284,5 +295,44 @@ ```javascript | ||
## CDATA | ||
Your XML documents might contain CDATA sections. Cruftless will treat those like | ||
ordinary text nodes. That is, if you have an element that has a text node bound | ||
to a variable, then it will resolve those values regardless of the fact if the | ||
incoming XML document has a text node or a CDATA node. | ||
```javascript | ||
template = parse(`<person>{{name}}</person>`); | ||
console.log(template.fromXML(`<person>Alice</person>`)); | ||
⇒ { name: 'Alice' } | ||
``` | ||
```javascript | ||
console.log(template.fromXML(`<person><![CDATA[Alice]]></person>`)); | ||
⇒ { name: 'Alice' } | ||
``` | ||
However, if you would *produce* XML, then — by default — it will always produce | ||
a text node: | ||
```javascript | ||
console.log(template.toXML({ name: 'Alice' })); | ||
⇒ <person>Alice</person> | ||
``` | ||
That is, unless you specifiy a `cdata` option in your binding: | ||
```javascript | ||
template = parse(`<person>{{name|cdata}}</person>`); | ||
console.log(template.toXML({ name: 'Alice' })); | ||
⇒ <person> | ||
⇒ <![CDATA[Alice]]> | ||
⇒ </person> | ||
``` | ||
## JSON-ish Schema (incomplete, subject to change) | ||
Since Cruftless has all of the metadata of your XML document and how it binds to your data structures at its disposal, it also allows you to generate a 'schema' of the data structure it expects. | ||
Since Cruftless has all of the metadata of your XML document and how it binds to | ||
your data structures at its disposal, it also allows you to generate a 'schema' | ||
of the data structure it expects. | ||
@@ -295,7 +345,4 @@ ```javascript | ||
⇒ "keys": { | ||
⇒ "b": { | ||
⇒ "name": { | ||
⇒ "type": "string" | ||
⇒ }, | ||
⇒ "a": { | ||
⇒ "type": "string" | ||
⇒ } | ||
@@ -302,0 +349,0 @@ ⇒ } |
Sorry, the diff of this file is not supported yet
53776
942
406