Skip to content

Commit

Permalink
dont double escape certain entities/character references, fix encodin…
Browse files Browse the repository at this point in the history
…g test
  • Loading branch information
chjj committed Nov 30, 2011
1 parent 9ab262b commit b702e91
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
10 changes: 6 additions & 4 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ inline.lexer = function(str) {
if (cap = inline.code.exec(str)) {
str = str.substring(cap[0].length);
out += '<code>'
+ escape(cap[2] || cap[1])
+ escape(cap[2] || cap[1], true)
+ '</code>';
continue;
}
Expand Down Expand Up @@ -413,7 +413,7 @@ var tok = function() {
}
case 'code': {
return '<pre><code>'
+ escape(token.text)
+ escape(token.text, true)
+ '</code></pre>';
}
case 'blockquote_start': {
Expand Down Expand Up @@ -508,9 +508,11 @@ var parse = function(src) {
* Helpers
*/

var escape = function(html) {
var escape = function(html, dbl) {
return html
.replace(/&/g, '&amp;')
.replace(!dbl
? /&(?!#?\w+;)/g
: /&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
Expand Down
17 changes: 6 additions & 11 deletions test/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ fs.readdirSync(dir).forEach(function(file) {
fs.writeFileSync(file, text);
});

// markdown avoids double encoding half of the time
// and does it the other half. this behavior will be
// implemented eventually, but for now, this needs to
// be changed, because i want to see if the other tests
// included in this file pass.
// markdown is weird with encoding
(function() {
var file = dir + '/amps_and_angles_encoding.html';
var html = fs.readFileSync(file, 'utf8')
.replace('6 > 5.', '6 &gt; 5.')
.replace('AT&amp;T is another', 'AT&amp;amp;T is another');

fs.writeFileSync(file, html);
})();
Expand All @@ -73,7 +68,7 @@ fs.readdirSync(dir).forEach(function(file) {
fs.writeFileSync(file, text);
})();

// fix strange markup that isnt likely
// fix strange markup that isnt likely
// to exist in the reality
(function _(ext) {
var file = dir + '/inline_html_advanced.' + ext;
Expand All @@ -86,17 +81,17 @@ fs.readdirSync(dir).forEach(function(file) {
('html');

// markdown parses backslashes in a very primitive
// way because it's not a real parser. i cannot
// way because it's not a real parser. i cannot
// include this test, because marked parses backslashes
// in a very different way.
(function(ext) {
fs.writeFileSync(dir + '/backslash_escapes.text',
fs.writeFileSync(dir + '/backslash_escapes.text',
'hello world \\[how](are you) today');
fs.writeFileSync(dir + '/backslash_escapes.html',
fs.writeFileSync(dir + '/backslash_escapes.html',
'<p>hello world [how](are you) today</p>');
})();

// can't do this for performance reasons
// can't do this for performance reasons
// right now
(function _(name) {
fs.unlinkSync(dir + '/' + name + '.text');
Expand Down
2 changes: 1 addition & 1 deletion test/tests/amps_and_angles_encoding.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p>AT&amp;T has an ampersand in their name.</p>

<p>AT&amp;amp;T is another way to write it.</p>
<p>AT&amp;T is another way to write it.</p>

<p>This &amp; that.</p>

Expand Down

0 comments on commit b702e91

Please sign in to comment.