New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create attribute with null value #598
Comments
https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js#L177 You may override attrs renderer to support empty values. |
Understood, thanks. Could you consider making a |
That's strange - nobody asked such feature for 5 years. But i have no principal objections. |
Alternative suggestion would be to represent empty values as I think many people would expect |
I'd suggest use notation |
I don't think any change is required to support boolean attributeYour "empty attribute" is canonically named "boolean attribute". The following work: attrSet("download", "")
attrSet("download", "download") I'm afraid the discussion here reveals a few potential bugs of
|
/** | |
* Token.attrGet(name) | |
* | |
* Get the value of attribute `name`, or null if it does not exist. | |
**/ | |
Token.prototype.attrGet = function attrGet(name) { | |
var idx = this.attrIndex(name), value = null; | |
if (idx >= 0) { | |
value = this.attrs[idx][1]; | |
} | |
return value; | |
}; |
attrGet()
uses null
to indicate the absence. Thus, both null
and undefined
should be reserved. But attrSet()
and renderAttrs()
don't validate it:
Lines 153 to 167 in 6e2de08
/** | |
* Token.attrSet(name, value) | |
* | |
* Set `name` attribute to `value`. Override old value if exists. | |
**/ | |
Token.prototype.attrSet = function attrSet(name, value) { | |
var idx = this.attrIndex(name), | |
attrData = [ name, value ]; | |
if (idx < 0) { | |
this.attrPush(attrData); | |
} else { | |
this.attrs[idx] = attrData; | |
} | |
}; |
Lines 168 to 185 in 6e2de08
/** | |
* Renderer.renderAttrs(token) -> String | |
* | |
* Render token attributes to string. | |
**/ | |
Renderer.prototype.renderAttrs = function renderAttrs(token) { | |
var i, l, result; | |
if (!token.attrs) { return ''; } | |
result = ''; | |
for (i = 0, l = token.attrs.length; i < l; i++) { | |
result += ' ' + escapeHtml(token.attrs[i][0]) + '="' + escapeHtml(token.attrs[i][1]) + '"'; | |
} | |
return result; | |
}; |
Besides, the access control on Token
should be reinforced.
At least, the following should be marked as either "internal" or "deprecated", because they allow users to add attributes that may never take effect, or even destroy the attribute records.
attrs
attrPush()
The following can be replaced by clearer things. You can take JavaScript Map
as a reference.
attrIndex()
HTML Standard
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
13.2.5.33 Attribute name state:
if there is already an attribute on the token with the exact same name, then this is a duplicate-attribute parse error and the new attribute must be removed from the token.
Guarantees of 100% validate-able html are out of scope. If using |
Can extensions generate an empty attribute, e.g.
<a href="..." download>...</a>
?If not, could that be enabled?
I tried these:
The text was updated successfully, but these errors were encountered: