Open ahopkins opened 1 year ago
Looks good. Neat how compact & fast implementation you got for this.
Remaining considerations: should we catch/handle if the same attributes are defined as kwargs too (I believe now you get another attribute by the same name in HTML)? Possibly even restructure html5tagger such that the whole opening tag is written at once instead of appending attributes to HTML snippet the way it currently does.
Needs README update I guess, but is there anything else stopping this from merging?
The goal of this PR is to come up with a nicer API for assigning HTML
id
andclass
attributes that clash with Python syntax. One approach is to overload the__getitem__
method to allow for this:There are a few considerations:
__getitem__
should be a single string, or a tuple of strings:["#id", "class", ...]
.
) prepended:.one.two.three
vone two three
The implementation I proposed answers these as by opting for a single string that follows CSS-style selector. While potentially we could go with
#some-id one two three
and just check the first character for a#
, in which case we pop off the one value, this pattern was not chosen because it then means that this lib would be introducing a new pattern that is not found in HTML.Alternatively, we could have multiple values
builder["#some-id","one two three"]
. While this is valid Python, it is a bit awkward (granted, any overload of__getitem__
is certainly non-standard).Finally, assuming
"#some-id.one.two.three"
is preferred over"#some-id one two three"
, the next logical question is why not arbitrary attribute assignment with[foo=bar]
style syntax.As a side benefit, it would allow a
data-foo_bar=thing
attribute, if that is desired.