htmlparser-to-html
Advanced tools
Comparing version 0.0.2 to 0.0.3
82
index.js
@@ -27,2 +27,10 @@ var emptyTags = { | ||
function escapeAttrib(s) { | ||
if(typeof s == 'number' || typeof s == 'boolean') return s.toString(); | ||
if(typeof s != 'string') { | ||
if(!s.toString || typeof s.toString != 'function') { | ||
return ''; | ||
} else { | ||
s = s.toString(); | ||
} | ||
} | ||
// Escaping '=' defangs many UTF-7 and SGML short-tag attacks. | ||
@@ -33,40 +41,54 @@ return s.replace(ampRe, '&').replace(ltRe, '<').replace(gtRe, '>') | ||
function html(item) { | ||
function html(item, parent, eachFn) { | ||
// apply recursively to arrays | ||
if(Array.isArray(item)) { | ||
return item.map(html).join(''); | ||
return item.map(function(subitem) { | ||
// parent, not item: the parent of an array item is not the array, | ||
// but rather the element that contained the array | ||
return html(subitem, parent, eachFn); | ||
}).join(''); | ||
} | ||
switch(item.type) { | ||
case 'text': | ||
return item.data; | ||
case 'directive': | ||
return '<'+item.data+'>'; | ||
case 'comment': | ||
return '<!--'+item.data+'-->'; | ||
case 'style': | ||
case 'script': | ||
case 'tag': | ||
var result = '<'+item.name; | ||
if(item.attribs && Object.keys(item.attribs).length > 0) { | ||
result += ' '+Object.keys(item.attribs).map(function(key){ | ||
return key + '="'+escapeAttrib(item.attribs[key])+'"'; | ||
}).join(' '); | ||
} | ||
if(item.children) { | ||
result += '>'+html(item.children)+(emptyTags[item.name] ? '' : '</'+item.name+'>'); | ||
} else { | ||
if(emptyTags[item.name]) { | ||
result += '>'; | ||
var orig = item; | ||
if(eachFn) { | ||
item = eachFn(item, parent); | ||
} | ||
if(typeof item != 'undefined' && typeof item.type != 'undefined') { | ||
switch(item.type) { | ||
case 'text': | ||
return item.data; | ||
case 'directive': | ||
return '<'+item.data+'>'; | ||
case 'comment': | ||
return '<!--'+item.data+'-->'; | ||
case 'style': | ||
case 'script': | ||
case 'tag': | ||
var result = '<'+item.name; | ||
if(item.attribs && Object.keys(item.attribs).length > 0) { | ||
result += ' '+Object.keys(item.attribs).map(function(key){ | ||
return key + '="'+escapeAttrib(item.attribs[key])+'"'; | ||
}).join(' '); | ||
} | ||
if(item.children) { | ||
// parent becomes the current element | ||
// check if the current item (before any eachFns are run) - is a renderable | ||
if(!orig.render) { | ||
orig = parent; | ||
} | ||
result += '>'+html(item.children, orig, eachFn)+(emptyTags[item.name] ? '' : '</'+item.name+'>'); | ||
} else { | ||
result += '></'+item.name+'>'; | ||
if(emptyTags[item.name]) { | ||
result += '>'; | ||
} else { | ||
result += '></'+item.name+'>'; | ||
} | ||
} | ||
} | ||
return result; | ||
case 'cdata': | ||
return '<!CDATA['+item.data+']]>'; | ||
default: | ||
return item; | ||
return result; | ||
case 'cdata': | ||
return '<!CDATA['+item.data+']]>'; | ||
} | ||
} | ||
return item; | ||
} | ||
module.exports = html; |
{ | ||
"name": "htmlparser-to-html", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Converts the JSON that the htmlparser/htmlparser2 package produces back to HTML.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,2 +9,12 @@ # htmlparser-to-html | ||
## API | ||
Returns a single function `html(tree, [parent, mapFn])` which returns a html string. | ||
Optionally, you can apply a function to each element just before they are converted to HTML - for example, converting items that are not in the right format into htmlparser-compatible input. | ||
- `tree`: a tree structure produced by htmlparser | ||
- `parent`: optional param - a parent element, only used for the `mapFn`. | ||
- `mapFn`: a function(item, parent) that is applied to each element just before the element is converted into html. The parent parameter is either the original value of the parent (default: null), or the parent element of this child element. | ||
## Usage | ||
@@ -11,0 +21,0 @@ |
13651
219
45