commonmark.js icon indicating copy to clipboard operation
commonmark.js copied to clipboard

Empty link, image, definition titles should output `title=""` on `a`, `img`

Open wooorm opened this issue 5 years ago • 2 comments

The following CM:

[](<> ""), [](<> "x")

![](<> ""), ![](<> "x")

[][y], [][z]

![][y], ![][z]

[y]: <> ""
[z]: <> "w"

Yields:

<p><a href=""></a>, <a href="" title="x"></a></p>
<p><img src="" alt="" />, <img src="" alt="" title="x" /></p>
<p><a href=""></a>, <a href="" title="w"></a></p>
<p><img src="" alt="" />, <img src="" alt="" title="w" /></p>

I believe commonmark.js should output title="" if an empty title is defined in Markdown ("", '', ()). This might also be clarified in the spec with examples.

wooorm avatar Jul 26 '20 12:07 wooorm

EDIT: Is this a theoretical question, or are there cases where it genuinely matters whether an empty title attribute is used or the title attribute is simply omitted?

jgm avatar Jul 27 '20 19:07 jgm

It is rather meaningless, in HTML. Although there is a difference:

If this attribute is omitted from an element, then it implies that the title attribute of the nearest ancestor HTML element with a title attribute set is also relevant to this element. Setting the attribute overrides this, explicitly stating that the advisory information of any ancestors is not relevant to this element. Setting the attribute to the empty string indicates that the element has no advisory information. — https://html.spec.whatwg.org/multipage/dom.html#the-title-attribute

Hence, say there is an image in a link. If the link has a title, then the image will inherit that description. If the image has an empty title attribute, it would be removed explicitly. Other cases would be if a title was set explicitly in wrapping HTML and then title explicitly set to empty in nested images / links. As there is a clear difference between [](<>) and [](<> ""), I think it makes sense to support the difference between the two.


Background: I was recently looking at “empty” attributes. Other cases are:

[](), [](<>), ![](), ![](<>)

All produce a href="" or src="" respectively. There is also a difference between an a without href, and one with: w/o href, the link is a placeholder (the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents). Whereas an a w/ href="" means a link to the current document. img elements are required to have src attributes. The two images also produce alt="". I think this is the correct way to go: omitting alt means the user forgot to add alt text. alt="" means the image explicitly has no alt text. As markdown cannot differentiate between the two, alt="" makes the most sense.

wooorm avatar Jul 27 '20 19:07 wooorm