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

Properly escape inline link and image attributes. Fix #473. #474

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
38 changes: 30 additions & 8 deletions src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ rules.inlineLink = {
},

replacement: function (content, node) {
var href = node.getAttribute('href')
if (href) href = href.replace(/([()])/g, '\\$1')
var title = cleanAttribute(node.getAttribute('title'))
if (title) title = ' "' + title.replace(/"/g, '\\"') + '"'
return '[' + content + '](' + href + title + ')'
var href = escapeLinkDestination(node.getAttribute('href'))
var title = escapeLinkTitle(cleanAttribute(node.getAttribute('title')))
var titlePart = title ? ' "' + title + '"' : ''
return '[' + content + '](' + href + titlePart + ')'
}
}

Expand Down Expand Up @@ -250,10 +249,10 @@ rules.image = {
filter: 'img',

replacement: function (content, node) {
var alt = cleanAttribute(node.getAttribute('alt'))
var src = node.getAttribute('src') || ''
var alt = escapeMarkdown(cleanAttribute(node.getAttribute('alt')))
var src = escapeLinkDestination(node.getAttribute('src') || '')
var title = cleanAttribute(node.getAttribute('title'))
var titlePart = title ? ' "' + title + '"' : ''
var titlePart = title ? ' "' + escapeLinkTitle(title) + '"' : ''
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
}
}
Expand All @@ -262,4 +261,27 @@ function cleanAttribute (attribute) {
return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
}

var ESCAPE_PATTERNS = {
before: /([\\*`[\]_]|(?:^[-+>])|(?:^~~~)|(?:^#{1-6}))/g,
after: /((?:^\d+(?=\.)))/
}
var escapePattern = new RegExp(
'(?:' + ESCAPE_PATTERNS.before.source + '|' + ESCAPE_PATTERNS.after.source + ')',
'g'
)

function escapeMarkdown (content) {
return content.replace(escapePattern, function (match, before, after) {
return before ? '\\' + before : after + '\\'
})
}

function escapeLinkDestination (destination) {
return destination.replace(/([()])/g, '\\$1')
}

function escapeLinkTitle (title) {
return title.replace(/"/g, '\\"')
}

export default rules
20 changes: 20 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,21 @@
<pre class="expected">![img with alt](logo.png)</pre>
</div>

<div class="case" data-name="img with escaped content in alt">
<div class="input"><img src="logo.png" alt="_img_ *with* [alt]"></div>
<pre class="expected">![\_img\_ \*with\* \[alt\]](logo.png)</pre>
</div>

<div class="case" data-name="img with no src">
<div class="input"><img></div>
<pre class="expected"></pre>
</div>

<div class="case" data-name="img with parenthesis in src">
<div class="input"><img src="logo.png?(query)"></div>
<pre class="expected">![](logo.png?\(query\))</pre>
</div>

<div class="case" data-name="img with a new line in alt">
<div class="input"><img src="logo.png" alt="img with
alt"></div>
Expand All @@ -199,6 +209,11 @@
title")</pre>
</div>

<div class="case" data-name="img with quotes in title">
<div class="input"><img src="logo.png" title="&quot;hello&quot;"></div>
<pre class="expected">![](logo.png "\"hello\"")</pre>
</div>

<div class="case" data-name="a">
<div class="input"><a href="http://example.com">An anchor</a></div>
<pre class="expected">[An anchor](http://example.com)</pre>
Expand Down Expand Up @@ -237,6 +252,11 @@
<pre class="expected">[Some `code`](http://example.com/code)</pre>
</div>

<div class="case" data-name="a with a brackets in text">
<div class="input"><a href="http://example.com/code">Some [text]</a></div>
<pre class="expected">[Some \[text\]](http://example.com/code)</pre>
</div>

<div class="case" data-name="a reference" data-options='{"linkStyle": "referenced"}'>
<div class="input"><a href="http://example.com">Reference link</a></div>
<pre class="expected">[Reference link][1]
Expand Down