summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-14 19:26:10 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-14 19:26:10 +0100
commit9de6bed1cd4f24f591c4aa2a3012501bd792c157 (patch)
treefbbab3fd96a8c48c124aef2e4ec34317c7a40440
parente55b8f2614fccb2ac54b3ddebe1ed3eeb12e32c9 (diff)
Don't escape double quotes outside of attributes
-rw-r--r--NEWS2
-rw-r--r--lib/writers/html-writer.js12
-rw-r--r--tests/html/write.tests.js12
3 files changed, 23 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index bd5e2cf..9027366 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,8 @@
* Collapse similar non-fresh HTML elements together.
+* Don't escape double quotes outside of attributes.
+
# 0.3.25
* When the styleMap option is passed as a string, ignore lines beginning with #.
diff --git a/lib/writers/html-writer.js b/lib/writers/html-writer.js
index f1761c9..456e894 100644
--- a/lib/writers/html-writer.js
+++ b/lib/writers/html-writer.js
@@ -114,12 +114,12 @@ function simpleWriter() {
function generateAttributeString(attributes) {
return _.map(attributes, function(value, key) {
- return util.format(' %s="%s"', key, escapeHtml(value));
+ return util.format(' %s="%s"', key, escapeHtmlAttribute(value));
}).join("");
}
function text(value) {
- fragments.push(escapeHtml(value));
+ fragments.push(escapeHtmlText(value));
}
function append(html) {
@@ -140,8 +140,14 @@ function simpleWriter() {
};
}
+function escapeHtmlText(value) {
+ return value
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;');
+}
-function escapeHtml(value) {
+function escapeHtmlAttribute(value) {
return value
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
diff --git a/tests/html/write.tests.js b/tests/html/write.tests.js
index 34eaf15..e4a5813 100644
--- a/tests/html/write.tests.js
+++ b/tests/html/write.tests.js
@@ -12,12 +12,24 @@ describe("HTML write", function() {
"&lt;&gt;&amp;");
});
+ test("double quotes outside of attributes are not escaped", function() {
+ assert.equal(
+ generateString(html.text('"')),
+ '"');
+ });
+
test("element attributes are HTML escaped", function() {
assert.equal(
generateString(html.freshElement("p", {"x": "<"})),
'<p x="&lt;"></p>');
});
+ test("double quotes inside attributes are escaped", function() {
+ assert.equal(
+ generateString(html.freshElement("p", {"x": '"'})),
+ '<p x="&quot;"></p>');
+ });
+
test("element children are written", function() {
assert.equal(
generateString(html.freshElement("p", {}, [html.text("Hello")])),