Skip to content

Commit

Permalink
Disallow spaces inside links
Browse files Browse the repository at this point in the history
 1. between link label and reference: `[foo] [bar]`
 2. inside angular brackets: `[](<foo bar>)`
  • Loading branch information
rlidwka committed Jan 22, 2016
1 parent 637c776 commit 1482c3e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 100 deletions.
5 changes: 3 additions & 2 deletions lib/helpers/parse_link_destination.js
Expand Up @@ -3,7 +3,8 @@
'use strict';


var unescapeAll = require('../common/utils').unescapeAll;
var isSpace = require('../common/utils').isSpace;
var unescapeAll = require('../common/utils').unescapeAll;


module.exports = function parseLinkDestination(str, pos, max) {
Expand All @@ -21,7 +22,7 @@ module.exports = function parseLinkDestination(str, pos, max) {
pos++;
while (pos < max) {
code = str.charCodeAt(pos);
if (code === 0x0A /* \n */) { return result; }
if (code === 0x0A /* \n */ || isSpace(code)) { return result; }
if (code === 0x3E /* > */) {
result.pos = pos + 1;
result.str = unescapeAll(str.slice(start + 1, pos));
Expand Down
7 changes: 0 additions & 7 deletions lib/rules_inline/image.js
Expand Up @@ -100,13 +100,6 @@ module.exports = function image(state, silent) {
//
if (typeof state.env.references === 'undefined') { return false; }

// [foo] [bar]
// ^^ optional whitespace (can include newlines)
for (; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (!isSpace(code) && code !== 0x0A) { break; }
}

if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
start = pos + 1;
pos = parseLinkLabel(state, pos);
Expand Down
7 changes: 0 additions & 7 deletions lib/rules_inline/link.js
Expand Up @@ -97,13 +97,6 @@ module.exports = function link(state, silent) {
//
if (typeof state.env.references === 'undefined') { return false; }

// [foo] [bar]
// ^^ optional whitespace (can include newlines)
for (; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (!isSpace(code) && code !== 0x0A) { break; }
}

if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
start = pos + 1;
pos = parseLinkLabel(state, pos);
Expand Down
84 changes: 0 additions & 84 deletions test/fixtures/commonmark/bad.txt
Expand Up @@ -55,90 +55,6 @@ error:
</ul>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7098

.
[link](</my uri>)
.
<p>[link](&lt;/my uri&gt;)</p>
.

error:

<p><a href="/my%20uri">link</a></p>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7614

.
[foo] [bar]

[bar]: /url "title"
.
<p>[foo] <a href="/url" title="title">bar</a></p>
.

error:

<p><a href="/url" title="title">foo</a></p>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7623

.
[foo]
[bar]

[bar]: /url "title"
.
<p>[foo]
<a href="/url" title="title">bar</a></p>
.

error:

<p><a href="/url" title="title">foo</a></p>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7809

.
[foo]
[]

[foo]: /url "title"
.
<p><a href="/url" title="title">foo</a>
[]</p>
.

error:

<p><a href="/url" title="title">foo</a></p>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 8111

.
![foo]
[]

[foo]: /url "title"
.
<p><img src="/url" alt="foo" title="title" />
[]</p>
.

error:

<p><img src="/url" alt="foo" title="title" /></p>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 8248

Expand Down
59 changes: 59 additions & 0 deletions test/fixtures/commonmark/good.txt
Expand Up @@ -5746,6 +5746,15 @@ src line: 7091
<p>[link](/my uri)</p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7098

.
[link](</my uri>)
.
<p>[link](&lt;/my uri&gt;)</p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7105

Expand Down Expand Up @@ -6224,6 +6233,30 @@ src line: 7601
<p><a href="/url">Baz</a></p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7614

.
[foo] [bar]

[bar]: /url "title"
.
<p>[foo] <a href="/url" title="title">bar</a></p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7623

.
[foo]
[bar]

[bar]: /url "title"
.
<p>[foo]
<a href="/url" title="title">bar</a></p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7664

Expand Down Expand Up @@ -6367,6 +6400,19 @@ src line: 7796
<p><a href="/url" title="title">Foo</a></p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7809

.
[foo]
[]

[foo]: /url "title"
.
<p><a href="/url" title="title">foo</a>
[]</p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 7829

Expand Down Expand Up @@ -6653,6 +6699,19 @@ src line: 8099
<p><img src="/url" alt="Foo" title="title" /></p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 8111

.
![foo]
[]

[foo]: /url "title"
.
<p><img src="/url" alt="foo" title="title" />
[]</p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 8124

Expand Down

0 comments on commit 1482c3e

Please sign in to comment.