Skip to content

Commit 1e7b496

Browse files
committed
Initial commit of xml-element string
0 parents  commit 1e7b496

File tree

9 files changed

+276
-0
lines changed

9 files changed

+276
-0
lines changed

.eslintrc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"rules": {
3+
"indent": [2, 2, {"SwitchCase": 1}],
4+
"quotes": [2, "single", "avoid-escape"],
5+
"linebreak-style": [2, "unix"],
6+
"semi": [2, "never"],
7+
"eqeqeq": [2, "allow-null"],
8+
"no-extend-native": 2,
9+
"curly": [2, "all"],
10+
"strict": [2, "global"],
11+
"space-before-function-paren": [2, "never"],
12+
"space-unary-ops": [2, {"words": true, "nonwords": false}],
13+
"space-infix-ops": [2, {"int32Hint": false}],
14+
"keyword-spacing": 2,
15+
"spaced-comment": [2, "always", { "exceptions": ["-", "+"] }],
16+
"no-inline-comments": 2,
17+
"func-style": [2, "expression"],
18+
"callback-return": [2, ["done", "next"]],
19+
"no-path-concat": 2,
20+
"no-use-before-define": 2,
21+
"array-bracket-spacing": [2, "never"],
22+
"object-curly-spacing": [2, "never"],
23+
"key-spacing": [2, {"beforeColon": false, "afterColon": true}],
24+
"comma-spacing": [2, {"before": false, "after": true}],
25+
"brace-style": [2, "stroustrup", {"allowSingleLine": true}],
26+
"handle-callback-err": [2, "^(e|err|error)$" ],
27+
"one-var": [2, "never"],
28+
"one-var-declaration-per-line": [2, "always"],
29+
"newline-after-var": 2,
30+
"newline-before-return": 2,
31+
"no-else-return": 2,
32+
"no-unexpected-multiline": 2,
33+
"eol-last": 2
34+
},
35+
"env": {
36+
"node": true,
37+
"browser": true,
38+
"mocha": true
39+
},
40+
"extends": "eslint:recommended"
41+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
coverage

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- '0.10'
5+
- '4'
6+
- node
7+
matrix:
8+
include:
9+
- node_js: node
10+
env: TEST_BROWSERS=true
11+
script: npm run-script ci
12+
deploy:
13+
provider: npm
14+
email: mike@cousins.io
15+
api_key:
16+
secure: uyly+k9h9qabINIwG6n24bbxVuY7LBPSFueXTo9KGUDRekENcP9dyJj3sGGYGF88DCUVujNWtD643pQSfGYW7FVjjjcuSYzDVikMn5NmKeVSVTc4fWi0Jc/IhgU7N2V2X9reqlcjbeoFcVJK7DtD3u5bMtyQbfpmPAAISDWajdaaP0jUN08Gz9MuZP0+xde59T1ZktuCwym9HISpdVNOQgo3ZCMHKprp/+KKJ4rQvxoT7qi0gHtLHMCdEnr6LHAArFz6/ou4TZQb2/SdMutNSPee2+1V24Bbnh8Tnc0oMgESydtH/X2oa65fazp8Z7yTK/E5zIBOz6YWr6W0NDR278bw7oqAN7+FTLb+NR/e0VVrSe0PGGeW1oo/2D2evCF6cd9KNWL+V2w0KeYVfHNwQbK7vQqZzDtzP3R09J+6r0ZTzwhURufdwQgk0O+AAWVWmuhnfE8152Sq4yk7R7DzaNR5228hHSdRVBWX6qbToa59Vjnk9yrFGgnhn3a/c9q5iP+XEPLdeVrQR5EF0EeqP9uxF22kdIWX2eNbEM+4uYTU7k21zWYRL5Q5cM/EIHrFZlDJU3blWMxtcB9SIIdU3oEawOqN0T0tA2prKM5Y3dlZgDHAmL34RBJOnTqATmsIyY0h3v2hPxvoihZ8CvpDMkTQwfzYhu547xshmYqjEz0=
17+
on:
18+
node: node
19+
condition: "$TEST_BROWSERS = true"
20+
tags: true
21+
all_branches: true
22+
repo: tracespace/xml-element-string
23+
env:
24+
global:
25+
- secure: I/DCMfAXP1IREo636jjGGESXci3Fqk9Wv4Pj0l3u4eo/j/ep+EU/eUbJ6y0QfbPMg1aHSCRKLW7LUmSvNpqF16ePpFUv0cRKlrQ1Cv30s2gvK0CxZCNdi4UtKsBaGkvXaYrB/CsMY/1jlBKyvrlR0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lIkGjxE/+dd1HdIb0Mp1croZBZHW/h8/axY+gxq71sblEWgw74rzG+cMBdqzj3dle+IC5R9bczQEZGtK/HIgtxZe4DW4jmD9idYx105lG7L3vs1GSGjAmVsVelPuwzyYo5XCjbxzINOE=

.zuul.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ui: mocha-bdd
2+
browsers:
3+
- name: chrome
4+
version: -1..latest
5+
- name: safari
6+
version: -1..latest
7+
- name: firefox
8+
version: -1..latest
9+
- name: ie
10+
version: -1..latest
11+
- name: microsoftedge
12+
version: latest

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2015 by Mike Cousins (mike@cousins.io) and contributors, shared under the terms of the MIT License.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# xml element string
2+
3+
[![npm](https://img.shields.io/npm/v/xml-element-string.svg?style=flat-square)](https://www.npmjs.com/package/xml-element-string)
4+
[![Travis](https://img.shields.io/travis/tracespace/xml-element-string.svg?style=flat-square)](https://travis-ci.org/tracespace/xml-element-string)
5+
[![Coveralls](https://img.shields.io/coveralls/tracespace/xml-element-string.svg?style=flat-square)](https://coveralls.io/github/tracespace/xml-element-string)
6+
[![David](https://img.shields.io/david/tracespace/xml-element-string.svg?style=flat-square)](https://david-dm.org/tracespace/xml-element-string)
7+
[![David](https://img.shields.io/david/dev/tracespace/xml-element-string.svg?style=flat-square)](https://david-dm.org/tracespace/xml-element-string#info=devDependencies)
8+
9+
Takes a tag-name, attributes object, and an array of children and turns them into an xml string.
10+
11+
## usage
12+
13+
```
14+
$ npm install --save xml-element-string
15+
```
16+
17+
then
18+
19+
``` js
20+
var createElement = require('xml-element-string')
21+
var element = createElement(tagName, optionalAttributesObject, optionalChildArray)
22+
```
23+
24+
## example
25+
26+
``` js
27+
var createElement = require('xml-element-string')
28+
29+
var element = createElement('hello', {from: 'the'}, [
30+
createElement('other', {side: 'I'}),
31+
createElement('must', {have: 'called'}, [
32+
createElement('a', {thousand: 'times'})
33+
])
34+
])
35+
36+
console.log(element)
37+
// logs
38+
// <hello from="the"><other side="I"/><must have="called"><a thousand="times"/></must></hello>
39+
```
40+
41+
## features
42+
43+
Not much going on here, but this module will:
44+
45+
* Close or self-close the nodes as necessary
46+
* Ignore attributes that resolve to `null` or `undefined`
47+
* Escape html characters in the tag, attribute names, and attribute values by way of [escape-html](https://github.com/component/escape-html)
48+
49+
## developing and contributing
50+
51+
Clone and then `$ npm install`. Please accompany all PRs with applicable tests. Please test your code in browsers, as Travis CI cannot run browser tests for PRs.
52+
53+
### unit testing
54+
55+
This module uses [Mocha](http://mochajs.org/) and [Chai](http://chaijs.com/) for unit testing, [Istanbul](https://github.com/gotwarlost/istanbul) for coverage, and [ESLint](http://eslint.org/) for linting.
56+
57+
* `$ npm test` - run the tests, calculate coverage, and lint
58+
* `$ npm run test:watch` - run the tests on code changes (does not lint nor cover)
59+
* `$ npm run lint` - lint the code (will be run as a pre-commit script)
60+
61+
### browser testing
62+
63+
Browser tests are run with [Zuul](https://github.com/defunctzombie/zuul) and [Sauce Labs](https://saucelabs.com/opensauce/) on the last two versions of Chrome, Firefox, Safari, and Internet Explorer as well as the latest version of Edge.
64+
65+
* `$ npm run test:browser` - run the unit tests in a local browser
66+
* `$ npm run test:sauce` - run the units tests in several browsers using Open Sauce (Sauce Labs account and local [.zuulrc](https://github.com/defunctzombie/zuul/wiki/Zuulrc) required)

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// create an xml element string
2+
'use strict'
3+
4+
var escapeHtml = require('escape-html')
5+
6+
module.exports = function createXmlString(tag, attributes, children) {
7+
attributes = attributes || {}
8+
children = children || []
9+
10+
var start = '<' + escapeHtml(tag)
11+
12+
var middle = Object.keys(attributes).reduce(function(result, key) {
13+
var value = attributes[key]
14+
var attr = (value != null)
15+
? (' ' + escapeHtml(key) + '="' + escapeHtml(value) + '"')
16+
: ''
17+
18+
return result + attr
19+
}, '')
20+
21+
var end = (children.length)
22+
? '>' + children.join('') + '</' + tag + '>'
23+
: '/>'
24+
25+
return start + middle + end
26+
}

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "xml-element-string",
3+
"version": "0.0.0",
4+
"description": "Takes a tag-name, attributes object, and an array of children and turns them into an xml string",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha",
8+
"lint": "eslint *.js",
9+
"test": "istanbul cover _mocha",
10+
"posttest": "npm run lint",
11+
"test:watch": "mocha --reporter dot --watch",
12+
"test:browser": "zuul --local -- test.js",
13+
"test:sauce": "zuul -- test.js",
14+
"ci": "npm test && if [ \"${TEST_BROWSERS}\" = \"true\" ]; then npm run ci:browser; fi",
15+
"ci:browser": "if [ \"${TRAVIS_PULL_REQUEST}\" = \"false\" ]; then npm run test:sauce; fi",
16+
"postci": "coveralls < ./coverage/lcov.info"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/tracespace/xml-element-string.git"
21+
},
22+
"keywords": [
23+
"xml",
24+
"svg"
25+
],
26+
"author": "Mike Cousins <mike@cousins.io> (http://cousins.io)",
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/tracespace/xml-element-string/issues"
30+
},
31+
"homepage": "https://github.com/tracespace/xml-element-string#readme",
32+
"devDependencies": {
33+
"chai": "^3.5.0",
34+
"eslint": "^2.12.0",
35+
"istanbul": "^0.4.3",
36+
"mocha": "^2.5.3",
37+
"zuul": "^3.10.1"
38+
}
39+
}

test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// test suite for the xml element string function
2+
'use strict'
3+
4+
var expect = require('chai').expect
5+
6+
var element = require('./')
7+
8+
describe('xml element string function', function() {
9+
it('should be able to create a node', function() {
10+
var result = element('node')
11+
var expected = '<node/>'
12+
13+
expect(result).to.equal(expected)
14+
})
15+
16+
it('should be able to create a node with attributes', function() {
17+
var result = element('foo', {bar: 'baz', quux: 'hello'})
18+
var expected = '<foo bar="baz" quux="hello"/>'
19+
20+
expect(result).to.equal(expected)
21+
})
22+
23+
it('should not write null attributes', function() {
24+
var result = element('foo', {bar: undefined, baz: 'quux', nope: null})
25+
var expected = '<foo baz="quux"/>'
26+
27+
expect(result).to.equal(expected)
28+
})
29+
30+
it('should escape html characters in the tag', function() {
31+
var result = element('foo/>danger')
32+
var expected = '<foo/&gt;danger/>'
33+
34+
expect(result).to.equal(expected)
35+
})
36+
37+
it('should escape html characters in the attribute names', function() {
38+
var result = element('foo', {'/>danger': 'bar'})
39+
var expected = '<foo /&gt;danger="bar"/>'
40+
41+
expect(result).to.equal(expected)
42+
})
43+
44+
it('should escape html characters in the attribute values', function() {
45+
var result = element('foo', {bar: '"/>danger'})
46+
var expected = '<foo bar="&quot;/&gt;danger"/>'
47+
48+
expect(result).to.equal(expected)
49+
})
50+
51+
it('should handle an array of children', function() {
52+
var result = element('node', {}, ['<child/>', '<child/>'])
53+
var expected = '<node><child/><child/></node>'
54+
55+
expect(result).to.equal(expected)
56+
})
57+
})

0 commit comments

Comments
 (0)