Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc86468

Browse files
committed
Merge pull request #9 from NowTV-Iceberg/master
Add xml declaration as option
2 parents 7170341 + d544519 commit dc86468

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

‎lib/xml.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,19 @@ function xml(input, options) {
5858
}
5959
}
6060

61+
function addXmlDeclaration(declaration) {
62+
var encoding = declaration.encoding || 'UTF-8';
63+
var standalone = declaration.standalone ? ' standalone="' + declaration.standalone +'"' : '';
64+
output = '<?xml version="1.0" encoding="'+ encoding +'"' + standalone + '?>'
65+
}
66+
6167
// disable delay delayed
6268
delay(function () { instant = false });
6369

70+
if (options.declaration) {
71+
addXmlDeclaration(options.declaration);
72+
}
73+
6474
if (input && input.forEach) {
6575
input.forEach(function (value, i) {
6676
var last;

‎readme.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ data: </toys>
8484
```
8585

8686

87+
`declartion` {_options_} Add default xml declaration as first node.
88+
89+
_options_ are:
90+
* encoding: 'value'
91+
* standalone: 'value'
92+
93+
**Declartion Example**
94+
95+
```js
96+
xml([ { a: 'test' }], { declaration: true }) === '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'
97+
xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }}) === '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
98+
```
99+
87100
## Examples
88101

89102
**Simple Example**
@@ -190,4 +203,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
190203
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
191204
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
192205
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
193-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
206+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎test/xml.test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,13 @@ describe('xml module', function(done) {
8585
}, 10);
8686
});
8787

88+
it('xml declaration options', function(done) {
89+
expect(xml([ { a: 'test' }], { declaration: true })).to.equal('<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
90+
expect(xml([ { a: 'test' }], { declaration: {encoding: 'foo' }})).to.equal('<?xml version="1.0" encoding="foo"?><a>test</a>');
91+
expect(xml([ { a: 'test' }], { declaration: {standalone: 'yes' }})).to.equal('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
92+
expect(xml([ { a: 'test' }], { declaration: false })).to.equal('<a>test</a>');
93+
expect(xml([ { a: 'test' }], {})).to.equal('<a>test</a>');
94+
done();
95+
});
96+
8897
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /