dom-serializer
Advanced tools
Comparing version 0.0.0 to 0.0.1
33
index.js
@@ -51,3 +51,3 @@ /* | ||
*/ | ||
var formatAttrs = function(attributes) { | ||
function formatAttrs(attributes, opts) { | ||
if (!attributes) return; | ||
@@ -68,3 +68,3 @@ | ||
} else { | ||
output += key + '="' + entities.escape(value || '') + '"'; | ||
output += key + '="' + (opts.decodeEntities ? entities.encodeXML(value) : value) + '"'; | ||
} | ||
@@ -74,3 +74,3 @@ } | ||
return output; | ||
}; | ||
} | ||
@@ -120,3 +120,5 @@ /* | ||
if (ElementType.isTag(elem)) | ||
if (elem.type === 'root') | ||
output += render(elem.children, opts); | ||
else if (ElementType.isTag(elem)) | ||
output += renderTag(elem, opts); | ||
@@ -130,3 +132,3 @@ else if (elem.type === ElementType.Directive) | ||
else | ||
output += renderText(elem); | ||
output += renderText(elem, opts); | ||
} | ||
@@ -139,3 +141,3 @@ | ||
var tag = '<' + elem.name, | ||
attribs = formatAttrs(elem.attribs); | ||
attribs = formatAttrs(elem.attribs, opts); | ||
@@ -154,3 +156,3 @@ if (attribs) { | ||
tag += render(elem.children, opts); | ||
if (!singleTag[elem.name] || opts.xmlMode) { | ||
@@ -161,4 +163,4 @@ tag += '</' + elem.name + '>'; | ||
return tag; | ||
@@ -171,11 +173,12 @@ } | ||
var renderText = function(elem) { | ||
var data = elem.data || ''; | ||
if (!(elem.parent && elem.parent.name in unencodedElements)) { | ||
function renderText(elem, opts) { | ||
var data = elem.data || ''; | ||
// if entities weren't decoded, no need to encode them back | ||
if (opts.decodeEntities && !(elem.parent && elem.parent.name in unencodedElements)) { | ||
data = entities.encodeXML(data); | ||
} | ||
return data; | ||
}; | ||
} | ||
@@ -182,0 +185,0 @@ function renderCdata(elem) { |
{ | ||
"name": "dom-serializer", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "render dom nodes to string", | ||
@@ -5,0 +5,0 @@ "author": "Felix Boehm <me@feedic.com>", |
111
test.js
@@ -7,6 +7,6 @@ var expect = require('expect.js'), | ||
var html = function(str, options) { | ||
options = _.defaults(options || {}, defaultOpts); | ||
var html = function(preset, str, options) { | ||
options = _.defaults(options || {}, _.defaults(preset, defaultOpts)); | ||
var dom = parse(str, options); | ||
return render(dom); | ||
return render(dom, options); | ||
}; | ||
@@ -23,49 +23,21 @@ | ||
// only test applicable to the default setup | ||
describe('(html)', function() { | ||
it('should render <br /> tags correctly', function() { | ||
var str = '<br />'; | ||
expect(html(str)).to.equal('<br>'); | ||
}); | ||
var htmlFunc = _.partial(html, {}); | ||
// it doesn't really make sense for {decodeEntities: false} | ||
// since currently it will convert <hr class='blah'> into <hr class="blah"> anyway. | ||
it('should handle double quotes within single quoted attributes properly', function() { | ||
var str = '<hr class=\'an "edge" case\' />'; | ||
expect(html(str)).to.equal('<hr class="an "edge" case">'); | ||
expect(htmlFunc(str)).to.equal('<hr class="an "edge" case">'); | ||
}); | ||
}); | ||
it('should retain encoded HTML content within attributes', function() { | ||
var str = '<hr class="cheerio & node = happy parsing" />'; | ||
expect(html(str)).to.equal('<hr class="cheerio & node = happy parsing">'); | ||
}); | ||
// run html with default options | ||
describe('(html, {})', _.partial( testBody, _.partial(html, {}) )); | ||
it('should shorten the "checked" attribute when it contains the value "checked"', function() { | ||
var str = '<input checked/>'; | ||
expect(html(str)).to.equal('<input checked>'); | ||
}); | ||
// run html with turned off decodeEntities | ||
describe('(html, {decodeEntities: false})', _.partial( testBody, _.partial(html, {decodeEntities: false}) )); | ||
it('should not shorten the "name" attribute when it contains the value "name"', function() { | ||
var str = '<input name="name"/>'; | ||
expect(html(str)).to.equal('<input name="name">'); | ||
}); | ||
describe('(xml)', function() { | ||
it('should render comments correctly', function() { | ||
var str = '<!-- comment -->'; | ||
expect(html(str)).to.equal('<!-- comment -->'); | ||
}); | ||
it('should render whitespace by default', function() { | ||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah</a>'; | ||
expect(html(str)).to.equal(str); | ||
}); | ||
it('should normalize whitespace if specified', function() { | ||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>'; | ||
expect(html(str, { normalizeWhitespace: true })).to.equal('<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>'); | ||
}); | ||
it('should preserve multiple hyphens in data attributes', function() { | ||
var str = '<div data-foo-bar-baz="value"></div>'; | ||
expect(html(str)).to.equal('<div data-foo-bar-baz="value"></div>'); | ||
}); | ||
it('should render CDATA correctly', function() { | ||
@@ -79,1 +51,56 @@ var str = '<a> <b> <![CDATA[ asdf&asdf ]]> <c/> <![CDATA[ asdf&asdf ]]> </b> </a>'; | ||
}); | ||
function testBody(html) { | ||
it('should render <br /> tags correctly', function() { | ||
var str = '<br />'; | ||
expect(html(str)).to.equal('<br>'); | ||
}); | ||
it('should retain encoded HTML content within attributes', function() { | ||
var str = '<hr class="cheerio & node = happy parsing" />'; | ||
expect(html(str)).to.equal('<hr class="cheerio & node = happy parsing">'); | ||
}); | ||
it('should shorten the "checked" attribute when it contains the value "checked"', function() { | ||
var str = '<input checked/>'; | ||
expect(html(str)).to.equal('<input checked>'); | ||
}); | ||
it('should not shorten the "name" attribute when it contains the value "name"', function() { | ||
var str = '<input name="name"/>'; | ||
expect(html(str)).to.equal('<input name="name">'); | ||
}); | ||
it('should render comments correctly', function() { | ||
var str = '<!-- comment -->'; | ||
expect(html(str)).to.equal('<!-- comment -->'); | ||
}); | ||
it('should render whitespace by default', function() { | ||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah</a>'; | ||
expect(html(str)).to.equal(str); | ||
}); | ||
it('should normalize whitespace if specified', function() { | ||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>'; | ||
expect(html(str, { normalizeWhitespace: true })).to.equal('<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>'); | ||
}); | ||
it('should preserve multiple hyphens in data attributes', function() { | ||
var str = '<div data-foo-bar-baz="value"></div>'; | ||
expect(html(str)).to.equal('<div data-foo-bar-baz="value"></div>'); | ||
}); | ||
it('should not encode characters in script tag', function() { | ||
var str = '<script>alert("hello world")</script>'; | ||
expect(html(str)).to.equal(str); | ||
}); | ||
it('should not encode json data', function() { | ||
var str = '<script>var json = {"simple_value": "value", "value_with_tokens": ""here & \'there\'""};</script>'; | ||
expect(html(str)).to.equal(str); | ||
}); | ||
} |
7565
4
235