Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renderer HTML escaping #636

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,7 @@ InlineLexer.prototype.output = function(src) {
: this.mangle(cap[1]);
href = this.mangle('mailto:') + text;
} else {
text = escape(cap[1]);
href = text;
href = text = cap[1];
}
out += this.renderer.link(href, null, text);
continue;
Expand All @@ -593,8 +592,7 @@ InlineLexer.prototype.output = function(src) {
// url (gfm)
if (!this.inLink && (cap = this.rules.url.exec(src))) {
src = src.substring(cap[0].length);
text = escape(cap[1]);
href = text;
href = text = cap[1];
out += this.renderer.link(href, null, text);
continue;
}
Expand All @@ -607,10 +605,8 @@ InlineLexer.prototype.output = function(src) {
this.inLink = false;
}
src = src.substring(cap[0].length);
out += this.options.sanitize
? this.options.sanitizer
? this.options.sanitizer(cap[0])
: escape(cap[0])
out += this.options.sanitize && this.options.sanitizer
? this.options.sanitizer(cap[0])
: cap[0]
continue;
}
Expand Down Expand Up @@ -661,7 +657,7 @@ InlineLexer.prototype.output = function(src) {
// code
if (cap = this.rules.code.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.codespan(escape(cap[2], true));
out += this.renderer.codespan(cap[2]);
continue;
}

Expand All @@ -682,7 +678,7 @@ InlineLexer.prototype.output = function(src) {
// text
if (cap = this.rules.text.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.text(escape(this.smartypants(cap[0])));
out += this.renderer.text(this.smartypants(cap[0]));
continue;
}

Expand All @@ -700,12 +696,9 @@ InlineLexer.prototype.output = function(src) {
*/

InlineLexer.prototype.outputLink = function(cap, link) {
var href = escape(link.href)
, title = link.title ? escape(link.title) : null;

return cap[0].charAt(0) !== '!'
? this.renderer.link(href, title, this.output(cap[1]))
: this.renderer.image(href, title, escape(cap[1]));
? this.renderer.link(link.href, link.title, this.output(cap[1]), true)
: this.renderer.image(link.href, link.title, cap[1]);
};

/**
Expand Down Expand Up @@ -855,7 +848,7 @@ Renderer.prototype.em = function(text) {
};

Renderer.prototype.codespan = function(text) {
return '<code>' + text + '</code>';
return '<code>' + escape(text, true) + '</code>';
};

Renderer.prototype.br = function() {
Expand All @@ -866,7 +859,10 @@ Renderer.prototype.del = function(text) {
return '<del>' + text + '</del>';
};

Renderer.prototype.link = function(href, title, text) {
Renderer.prototype.link = function(href, title, text, escaped) {
href = escape(href);
title = title ? escape(title) : null;
text = escaped ? text : escape(text);
if (this.options.sanitize) {
try {
var prot = decodeURIComponent(unescape(href))
Expand All @@ -888,6 +884,8 @@ Renderer.prototype.link = function(href, title, text) {
};

Renderer.prototype.image = function(href, title, text) {
href = escape(href);
title = title ? escape(title) : null;
var out = '<img src="' + href + '" alt="' + text + '"';
if (title) {
out += ' title="' + title + '"';
Expand All @@ -897,7 +895,7 @@ Renderer.prototype.image = function(href, title, text) {
};

Renderer.prototype.text = function(text) {
return text;
return escape(text);
};

/**
Expand Down