Skip to content

Commit 74d6989

Browse files
committed
FIX: If an attribute was undefined as the result of processing,
the HTML escaping would raise an error. This adds safety and prevents that from happening.
1 parent 6b4917e commit 74d6989

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/render_tree.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if (typeof define !== 'function') { var define = require('amdefine')(module) }
1+
if (typeof define !== 'function') { var define = require('amdefine')(module); }
22

33
define(['./core', './markdown_helpers'], function(Markdown, MarkdownHelpers) {
44

@@ -88,11 +88,15 @@ define(['./core', './markdown_helpers'], function(Markdown, MarkdownHelpers) {
8888

8989

9090
function escapeHTML( text ) {
91-
return text.replace( /&/g, "&" )
92-
.replace( /</g, "&lt;" )
93-
.replace( />/g, "&gt;" )
94-
.replace( /"/g, "&quot;" )
95-
.replace( /'/g, "&#39;" );
91+
if (text && text.length > 0) {
92+
return text.replace( /&/g, "&amp;" )
93+
.replace( /</g, "&lt;" )
94+
.replace( />/g, "&gt;" )
95+
.replace( /"/g, "&quot;" )
96+
.replace( /'/g, "&#39;" );
97+
} else {
98+
return "";
99+
}
96100
}
97101

98102
function render_tree( jsonml ) {
@@ -116,8 +120,12 @@ define(['./core', './markdown_helpers'], function(Markdown, MarkdownHelpers) {
116120
delete attributes.src;
117121
}
118122

119-
for ( var a in attributes )
120-
tag_attrs += " " + a + '="' + escapeHTML( attributes[ a ] ) + '"';
123+
for ( var a in attributes ) {
124+
var escaped = escapeHTML( attributes[ a ]);
125+
if (escaped && escaped.length) {
126+
tag_attrs += " " + a + '="' + escaped + '"';
127+
}
128+
}
121129

122130
// be careful about adding whitespace here for inline elements
123131
if ( tag === "img" || tag === "br" || tag === "hr" )

test/render_tree.t.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var markdown = require("../src/markdown"),
2+
tap = require("tap");
3+
4+
tap.test("undefined attribute", function(t) {
5+
var tree = markdown.renderJsonML( ['html', ['p', {style: undefined }, 'hello'] ] );
6+
t.equivalent( tree, '<p>hello</p>' );
7+
t.end();
8+
});
9+
10+
tap.test("escaped attribute", function(t) {
11+
var tree = markdown.renderJsonML( ['html', ['p', {style: "color: blue" }, 'hello'] ] );
12+
t.equivalent( tree, '<p style="color: blue">hello</p>' );
13+
t.end();
14+
});

0 commit comments

Comments
 (0)