Browse Source

Disallow spaces inside links

1. between link label and reference: `[foo] [bar]`
 2. inside angular brackets: `[](<foo bar>)`
pull/204/merge
Alex Kocharin 8 years ago
parent
commit
1482c3e64f
  1. 5
      lib/helpers/parse_link_destination.js
  2. 7
      lib/rules_inline/image.js
  3. 7
      lib/rules_inline/link.js
  4. 84
      test/fixtures/commonmark/bad.txt
  5. 59
      test/fixtures/commonmark/good.txt

5
lib/helpers/parse_link_destination.js

@ -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) {
@ -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));

7
lib/rules_inline/image.js

@ -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);

7
lib/rules_inline/link.js

@ -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);

84
test/fixtures/commonmark/bad.txt

@ -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

59
test/fixtures/commonmark/good.txt

@ -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
@ -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
@ -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
@ -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

Loading…
Cancel
Save