Skip to content

Commit cffae42

Browse files
committed
basic tests
1 parent 0837e12 commit cffae42

File tree

11 files changed

+223
-2
lines changed

11 files changed

+223
-2
lines changed

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is for unifying the coding style for different editors and IDEs.
2+
# More information at http://EditorConfig.org
3+
4+
# No .editorconfig files above the root directory
5+
root = true
6+
7+
[*]
8+
charset = utf-8
9+
indent_size = 4
10+
end_of_line = lf
11+
indent_style = space
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true
14+
15+
[*.{bemjson.js,deps.js}]
16+
indent_size = 4
17+
18+
[{bower,package}.json]
19+
indent_size = 2
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# OS
2+
.DS_Store
3+
._*
4+
5+
# NODEJS
6+
node_modules
7+
npm-debug.log
8+
9+
coverage

.jscsrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"disallowSpacesInCallExpression": true,
3+
"disallowSpaceAfterObjectKeys": true,
4+
"disallowNewlineBeforeBlockStatements": true,
5+
"disallowMultipleLineBreaks": true,
6+
"requireSemicolons": true,
7+
"requireFunctionDeclarations": true,
8+
"requireCommaBeforeLineBreak": true,
9+
"disallowTrailingComma": true,
10+
"disallowTrailingWhitespace": true,
11+
"disallowSpacesInFunction": {
12+
"beforeOpeningRoundBrace": true
13+
},
14+
"requireSpacesInFunction": {
15+
"beforeOpeningCurlyBrace": true
16+
},
17+
"disallowSpacesInConditionalExpression": {
18+
"afterTest": true,
19+
"afterConsequent": true
20+
},
21+
"excludeFiles": [
22+
".git/**",
23+
"node_modules/**"
24+
],
25+
"fileExtensions": [".js"]
26+
}

.jshintignore

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

.jshintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"eqeqeq": true,
3+
"expr": true,
4+
"maxlen": 120,
5+
"undef": true,
6+
"unused": true,
7+
"node": true
8+
}

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
test/
3+
npm-debug.log
4+
.editorconfig
5+
.jscsrc
6+
.jshintignore
7+
.jshintrc

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- iojs
5+
- "0.12"
6+
- "0.10"
7+
8+
env:
9+
global:
10+
- ISTANBUL_COVERAGE: yes
11+
12+
after_success:
13+
- npm i coveralls
14+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && echo "Coverage data was sent to coveralls!"

index.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
function render(tree, options) {
2+
options = options || {};
3+
4+
/**
5+
* options parse
6+
* @param {Array} singleTags single tags array for extend default
7+
* @param {String} closingSingleTag option for closing single tag
8+
* Option:
9+
* default: `<br>`
10+
* slash: `<br />`
11+
* tag: `<br></br>`
12+
*
13+
*/
14+
15+
var optSingleTags = options.singleTags,
16+
closingSingleTag = options.closingSingleTag;
17+
18+
var SINGLE_TAGS = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen',
19+
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'];
20+
var singleTags = {};
21+
22+
for (var i = 0, leni = SINGLE_TAGS.length; i < leni; i++) {
23+
singleTags[SINGLE_TAGS[i]] = 1;
24+
}
25+
26+
if (optSingleTags) {
27+
for (var j = 0, lenj = optSingleTags.length; j < lenj; j++) {
28+
singleTags[optSingleTags[j]] = 1;
29+
}
30+
}
31+
32+
return html(tree);
33+
34+
function html(tree) {
35+
36+
if (typeof tree === 'string') {
37+
return tree;
38+
}
39+
40+
var buf = '';
41+
42+
traverse([].concat(tree), function(node) {
43+
if (!node) return;
44+
if (typeof node === 'string') {
45+
buf += node;
46+
return buf;
47+
}
48+
if (typeof node.tag === 'boolean' && !node.tag) return node.content;
49+
var tag = node.tag || 'div';
50+
if (singleTags[tag]) {
51+
buf += '<'+ tag + attrs(node.attrs);
52+
switch (closingSingleTag) {
53+
case 'slash':
54+
buf += ' />';
55+
break;
56+
case 'tag':
57+
buf += '></' + tag + '>';
58+
break;
59+
default:
60+
buf += '>';
61+
}
62+
} else {
63+
buf += '<' + tag + (node.attrs? attrs(node.attrs): '') + '>' +
64+
(node.content? html(node.content): '') +
65+
'</' + tag + '>';
66+
}
67+
});
68+
69+
return buf;
70+
71+
function attrs(obj) {
72+
var attr = '';
73+
for (var key in obj) {
74+
attr += key + (obj[key] && typeof obj[key] !== 'boolean')? '="' + obj[key] + '"': '';
75+
}
76+
return attr;
77+
}
78+
}
79+
}
80+
81+
function traverse(tree, cb) {
82+
if (Array.isArray(tree)) {
83+
for (var i = 0, len = tree.length; i < len; i++) {
84+
tree[i] = traverse(cb(tree[i]), cb);
85+
}
86+
} else if (typeof tree === 'object' && tree.hasOwnProperty('content')) traverse(tree.content, cb);
87+
return tree;
88+
}
89+
90+
module.exports = render;

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "Render PostHTMLTree to HTML/XML",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "npm run lint && mocha",
8+
"lint": "jshint . && jscs . -v",
9+
"coverage": "istanbul cover --report text --report html --report lcov node_modules/mocha/bin/_mocha -- -R dot",
10+
"coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls"
811
},
912
"repository": {
1013
"type": "git",
@@ -22,5 +25,12 @@
2225
"bugs": {
2326
"url": "https://github.com/voischev/posthtml-render/issues"
2427
},
25-
"homepage": "https://github.com/voischev/posthtml-render#readme"
28+
"homepage": "https://github.com/voischev/posthtml-render#readme",
29+
"devDependencies": {
30+
"chai": "^3.3.0",
31+
"istanbul": "^0.4.0",
32+
"jscs": "^2.3.4",
33+
"jshint": "^2.8.0",
34+
"mocha": "^2.3.3"
35+
}
2636
}

test/mocha.opts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--inline-diffs
2+
--reporter spec

0 commit comments

Comments
 (0)