Skip to content

Commit 3ede092

Browse files
committed
chore: 100% coveralls
1 parent 0a9c789 commit 3ede092

File tree

3 files changed

+198
-29
lines changed

3 files changed

+198
-29
lines changed

lib/index.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ function render (tree, options) {
8888

8989
/** @private */
9090
function traverse (tree, cb) {
91-
if (Array.isArray(tree)) {
91+
if (tree !== undefined) {
9292
for (var i = 0, length = tree.length; i < length; i++) {
9393
traverse(cb(tree[i]), cb)
9494
}
95-
} else if (typeof tree === 'object' && tree.hasOwnProperty('content')) {
96-
traverse(tree.content, cb)
9795
}
98-
99-
return tree
10096
}
10197

10298
/**
@@ -109,12 +105,15 @@ function render (tree, options) {
109105
function html (tree) {
110106
var result = ''
111107

112-
tree = Array.isArray(tree) ? tree : [tree]
108+
if (!Array.isArray(tree)) {
109+
tree = [tree]
110+
}
113111

114112
traverse(tree, function (node) {
115113
// undefined, null, '', [], NaN
116114
if (node === undefined ||
117115
node === null ||
116+
node === false ||
118117
node.length === 0 ||
119118
Number.isNaN(node)) {
120119
return
@@ -135,18 +134,20 @@ function render (tree, options) {
135134

136135
// skip node
137136
if (node.tag === false) {
138-
if (typeof node.content === 'string' || typeof node.content === 'number') {
139-
result += node.content
140-
}
137+
result += html(node.content)
141138

142-
return node.content
139+
return
143140
}
144141

145142
var tag = node.tag || 'div'
146143

147-
if (isSingleTag(tag)) {
148-
result += '<' + tag + attrs(node.attrs)
144+
result += '<' + tag
145+
146+
if (node.attrs) {
147+
result += attrs(node.attrs)
148+
}
149149

150+
if (isSingleTag(tag)) {
150151
switch (closingSingleTag) {
151152
case 'tag':
152153
result += '></' + tag + '>'
@@ -160,23 +161,9 @@ function render (tree, options) {
160161
result += '>'
161162
}
162163

163-
if (node.hasOwnProperty('content')) {
164-
return node.content
165-
}
164+
result += html(node.content)
166165
} else {
167-
result += '<' + tag
168-
169-
if (node.attrs) {
170-
result += attrs(node.attrs)
171-
}
172-
173-
result += '>'
174-
175-
if (node.hasOwnProperty('content')) {
176-
result += html(node.content)
177-
}
178-
179-
result += '</' + tag + '>'
166+
result += '>' + html(node.content) + '</' + tag + '>'
180167
}
181168
})
182169

test/templates/parser.js

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,182 @@
11
/* jshint maxlen: false */
2-
module.exports = ['<!DOCTYPE html>', '\n', { 'tag': 'html', 'attrs': { 'xmlns': 'http://www.w3.org/1999/xhtml', 'lang': 'en-US' }, 'content': ['\n', { 'tag': 'head', 'content': ['\n ', { 'tag': 'meta', 'attrs': { 'charset': 'utf-8' } }, '\n ', { 'tag': 'title', 'content': ['Html'] }, '\n ', { 'tag': 'meta', 'attrs': { 'http-equiv': 'X-UA-Compatible', 'content': 'IE=EmulateIE7, IE=9' } }, '\n ', { 'tag': 'meta', 'attrs': { 'name': 'viewport', 'content': 'width=device-width, initial-scale=1' } }, '\n ', { 'tag': 'meta', 'attrs': { 'name': 'description', 'content': 'Description' } }, '\n\n ', { 'tag': 'meta', 'attrs': { 'property': 'og:url', 'content': 'http://github.com/posthtml' } }, '\n ', { 'tag': 'meta', 'attrs': { 'property': 'og:type', 'content': 'website' } }, '\n ', { 'tag': 'meta', 'attrs': { 'property': 'og:site_name', 'content': 'PostHTML' } }, '\n\n ', { 'tag': 'link', 'attrs': { 'rel': 'stylesheet', 'type': 'text/css', 'href': 'path/to/file.css' } }, '\n ', { 'tag': 'script', 'attrs': { 'src': 'path/to/file.js', 'type': 'text/javascript', 'charset': 'utf-8' } }, '\n\n ', { 'tag': 'script', 'content': ["\n console.log('PostHTML!');\n "] }, '\n'] }, '\n', { 'tag': 'body', 'attrs': { 'onload': "try{if(!google.j.b){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}catch(e){}if(document.images)new Image().src='/images/nav_logo231.png'" }, 'content': ['\n\n ', { 'tag': 'h1', 'content': ['Title'] }, '\n ', { 'tag': 'p', 'content': ['Lorem ipsum dolor sit amet...'] }, '\n\n ', { 'tag': 'section', 'attrs': { 'class': 'foo', 'style': 'color: red;' }, 'content': ['\n ', { 'tag': 'header', 'attrs': { 'class': 'foo bar', 'style': 'color: blue; border: 1px solid', 'id': 'id' }, 'content': ['\n ', { 'tag': 'div', 'attrs': { 'class': 'foo bar baz', 'id': 'idd', 'data-url': 'url/to/' }, 'content': ['\n ', { 'tag': 'span', 'attrs': { 'id': 'idd', 'data-data': "{ foo: 'bar' }" }, 'content': ['\n ', { 'tag': 'a', 'attrs': { 'href': '#' }, 'content': ['\n ', { 'tag': 'img', 'attrs': { 'src': 'path/to/img' } }, '\n Link\n '] }, '\n '] }, '\n '] }, '\n '] }, '\n '] }, '\n\n ', { 'tag': 'script', 'attrs': { 'type': 'text/javascript' }, 'content': ['\n (function(){function k(a){++b;a=a||window.event;google.iTick(a.target||a.srcElement)}if(google.timers&&google.timers.load.t){var c,b,f;google.c.c.a&&(google.startTick("aft"),google.afte=!1);var g=document.getElementsByTagName("img");c=g.length;for(var d=b=0,a;d', '<c;++d)if(a=g[d],google.c.c.i&&"none"==a.style.display)++b;else{var h="string"!=typeof a.src||!a.src,e=h||a.complete;google.c.c.d?a.getAttribute("data-deferred")&&(e=!1,a.removeAttribute("data-deferred")):google.c.c.m&&h&&a.getAttribute("data-bsrc")&&\n (e=!1);e?++b:google.rll(a,!0,k)}f=c-b;google.rll(window,!1,function(){google.tick("load","ol");google.c.e("load","imc",String(b));google.c.e("load","imn",String(c));google.c.e("load","imp",String(f));google.unblockCSI("load","ol")});google.tick("load",["prt","iml"])}})();\n '] }, '\n\n ', { 'tag': 'script', 'attrs': { 'src': 'path/to/file2.js', 'type': 'text/javascript', 'charset': 'utf-8' } }, '\n'] }, '\n'] }, '\n']
2+
module.exports = ['<!DOCTYPE html>', '\n', {
3+
'tag': 'html',
4+
'attrs': {
5+
'xmlns': 'http://www.w3.org/1999/xhtml',
6+
'lang': 'en-US'
7+
},
8+
'content': ['\n', {
9+
'tag': 'head',
10+
'content': ['\n ', {
11+
'tag': 'meta',
12+
'attrs': {
13+
'charset': 'utf-8'
14+
}
15+
}, '\n ', {
16+
'tag': 'title',
17+
'content': ['Html']
18+
}, '\n ', {
19+
'tag': 'meta',
20+
'attrs': {
21+
'http-equiv': 'X-UA-Compatible',
22+
'content': 'IE=EmulateIE7, IE=9'
23+
}
24+
}, '\n ', {
25+
'tag': 'meta',
26+
'attrs': {
27+
'name': 'viewport',
28+
'content': 'width=device-width, initial-scale=1'
29+
}
30+
}, '\n ', {
31+
'tag': 'meta',
32+
'attrs': {
33+
'name': 'description',
34+
'content': 'Description'
35+
}
36+
}, '\n\n ', {
37+
'tag': 'meta',
38+
'attrs': {
39+
'property': 'og:url',
40+
'content': 'http://github.com/posthtml'
41+
}
42+
}, '\n ', {
43+
'tag': 'meta',
44+
'attrs': {
45+
'property': 'og:type',
46+
'content': 'website'
47+
}
48+
}, '\n ', {
49+
'tag': 'meta',
50+
'attrs': {
51+
'property': 'og:site_name',
52+
'content': 'PostHTML'
53+
}
54+
}, '\n\n ', {
55+
'tag': 'link',
56+
'attrs': {
57+
'rel': 'stylesheet',
58+
'type': 'text/css',
59+
'href': 'path/to/file.css'
60+
}
61+
}, '\n ', {
62+
'tag': 'script',
63+
'attrs': {
64+
'src': 'path/to/file.js',
65+
'type': 'text/javascript',
66+
'charset': 'utf-8'
67+
}
68+
}, '\n\n ', {
69+
'tag': 'script',
70+
'content': ["\n console.log('PostHTML!');\n "]
71+
}, '\n']
72+
}, '\n', {
73+
'tag': 'body',
74+
'attrs': {
75+
'onload': "try{if(!google.j.b){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}catch(e){}if(document.images)new Image().src='/images/nav_logo231.png'"
76+
},
77+
'content': ['\n\n ', {
78+
'tag': 'h1',
79+
'content': ['Title']
80+
}, '\n ', {
81+
'tag': 'p',
82+
'content': ['Lorem ipsum dolor sit amet...']
83+
}, '\n\n ', {
84+
'tag': 'section',
85+
'attrs': {
86+
'class': 'foo',
87+
'style': 'color: red;'
88+
},
89+
'content': ['\n ', {
90+
'tag': 'header',
91+
'attrs': {
92+
'class': 'foo bar',
93+
'style': 'color: blue; border: 1px solid',
94+
'id': 'id'
95+
},
96+
'content': ['\n ', {
97+
'tag': 'div',
98+
'attrs': {
99+
'class': 'foo bar baz',
100+
'id': 'idd',
101+
'data-url': 'url/to/'
102+
},
103+
'content': ['\n ', {
104+
'tag': 'span',
105+
'attrs': {
106+
'id': 'idd',
107+
'data-data': "{ foo: 'bar' }"
108+
},
109+
'content': ['\n ', {
110+
'tag': 'a',
111+
'attrs': {
112+
'href': '#'
113+
},
114+
'content': ['\n ', {
115+
'tag': 'img',
116+
'attrs': {
117+
'src': 'path/to/img'
118+
}
119+
}, '\n Link\n ']
120+
}, '\n ']
121+
}, '\n ']
122+
}, '\n ']
123+
}, '\n ']
124+
}, '\n\n ', {
125+
'content': {
126+
'tag': 'hr'
127+
}
128+
}, '\n\n ', {
129+
'tag': 'script',
130+
'attrs': {
131+
'type': 'text/javascript'
132+
},
133+
'content': [
134+
'\n (function(){function k(a){++b;a=a||window.event;google.iTick(a.target||a.srcElement)}if(google.timers&&google.timers.load.t){var c,b,f;google.c.c.a&&(google.startTick("aft"),google.afte=!1);var g=document.getElementsByTagName("img");c=g.length;for(var d=b=0,a;d',
135+
'<c;++d)if(a=g[d],google.c.c.i&&"none"==a.style.display)++b;else{var h="string"!=typeof a.src||!a.src,e=h||a.complete;google.c.c.d?a.getAttribute("data-deferred")&&(e=!1,a.removeAttribute("data-deferred")):google.c.c.m&&h&&a.getAttribute("data-bsrc")&&\n (e=!1);e?++b:google.rll(a,!0,k)}f=c-b;google.rll(window,!1,function(){google.tick("load","ol");google.c.e("load","imc",String(b));google.c.e("load","imn",String(c));google.c.e("load","imp",String(f));google.unblockCSI("load","ol")});google.tick("load",["prt","iml"])}})();\n '
136+
]
137+
}, '\n\n ', {
138+
'tag': 'script',
139+
'attrs': {
140+
'src': 'path/to/file2.js',
141+
'type': 'text/javascript',
142+
'charset': 'utf-8'
143+
}
144+
}, '\n']
145+
}, '\n']
146+
}, '\n', {
147+
'tag': false,
148+
'content': [NaN]
149+
}, {
150+
'tag': false,
151+
'content': [[]]
152+
}, {
153+
'tag': false,
154+
'content': ['']
155+
}, {
156+
'tag': false,
157+
'content': [null]
158+
}, {
159+
'tag': false,
160+
'content': [false]
161+
}, {
162+
'tag': false,
163+
'content': [undefined]
164+
}, {
165+
'tag': false,
166+
'content': NaN
167+
}, {
168+
'tag': false,
169+
'content': []
170+
}, {
171+
'tag': false,
172+
'content': ''
173+
}, {
174+
'tag': false,
175+
'content': null
176+
}, {
177+
'tag': false,
178+
'content': false
179+
}, {
180+
'tag': false,
181+
'content': undefined
182+
}, NaN, [], '', null, false, undefined]

test/templates/render.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ <h1>Title</h1>
3636
</header>
3737
</section>
3838

39+
<div><hr></div>
40+
3941
<script type="text/javascript">
4042
(function(){function k(a){++b;a=a||window.event;google.iTick(a.target||a.srcElement)}if(google.timers&&google.timers.load.t){var c,b,f;google.c.c.a&&(google.startTick("aft"),google.afte=!1);var g=document.getElementsByTagName("img");c=g.length;for(var d=b=0,a;d<c;++d)if(a=g[d],google.c.c.i&&"none"==a.style.display)++b;else{var h="string"!=typeof a.src||!a.src,e=h||a.complete;google.c.c.d?a.getAttribute("data-deferred")&&(e=!1,a.removeAttribute("data-deferred")):google.c.c.m&&h&&a.getAttribute("data-bsrc")&&
4143
(e=!1);e?++b:google.rll(a,!0,k)}f=c-b;google.rll(window,!1,function(){google.tick("load","ol");google.c.e("load","imc",String(b));google.c.e("load","imn",String(c));google.c.e("load","imp",String(f));google.unblockCSI("load","ol")});google.tick("load",["prt","iml"])}})();

0 commit comments

Comments
 (0)