Skip to content

Commit

Permalink
Fix empty alt value for imageReference, image
Browse files Browse the repository at this point in the history
In the below example, the image node had an `alt` property set to `null`,
whereas the reference had an `alt` set to `""`.

```md
![](/xyz.png)

![][1]

[1]: /xyz.png
```

This update ensures both have `null` as the value corresponding to an
empty `alt`.

Closes GH-103.
  • Loading branch information
wooorm committed Dec 23, 2015
1 parent acebf81 commit 698d569
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4577,10 +4577,6 @@ function tokenizeReference(eat, value, silent) {
index++;
}

if (!queue) {
return;
}

subvalue = text = queue;
character = value.charAt(index);

Expand All @@ -4606,6 +4602,10 @@ function tokenizeReference(eat, value, silent) {
character = value.charAt(index);

if (character !== C_BRACKET_OPEN) {
if (!text) {
return;
}

identifier = text;
} else {
identifier = EMPTY;
Expand Down Expand Up @@ -4699,7 +4699,7 @@ function tokenizeReference(eat, value, silent) {
node.children = self.tokenizeInline(text, now);
exitLink();
} else if (type === T_IMAGE) {
node.alt = decode(self.descape(text), eat);
node.alt = decode(self.descape(text), eat) || null;
}

return eat(subvalue)(node);
Expand Down
2 changes: 1 addition & 1 deletion lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ compilerPrototype.linkReference = function (node) {
* @return {string} - Markdown image reference.
*/
compilerPrototype.imageReference = function (node) {
var alt = this.encode(node.alt, node);
var alt = this.encode(node.alt, node) || EMPTY;

return EXCLAMATION_MARK +
SQUARE_BRACKET_OPEN + alt + SQUARE_BRACKET_CLOSE +
Expand Down
3 changes: 3 additions & 0 deletions test/input/reference-image-empty-alt.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
![][1]

[1]: /xyz.png
65 changes: 65 additions & 0 deletions test/tree/reference-image-empty-alt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "imageReference",
"identifier": "1",
"referenceType": "full",
"alt": null,
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 7
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 7
},
"indent": []
}
},
{
"type": "definition",
"identifier": "1",
"title": null,
"link": "/xyz.png",
"position": {
"start": {
"line": 3,
"column": 1
},
"end": {
"line": 3,
"column": 14
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 4,
"column": 1
}
}
}

0 comments on commit 698d569

Please sign in to comment.