commonmark.js
commonmark.js copied to clipboard
Empty link, image, definition titles should output `title=""` on `a`, `img`
The following CM:
[](<> ""), [](<> "x")
, 
[][y], [][z]
![][y], ![][z]
[y]: <> ""
[z]: <> "w"
<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.
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?
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.