sanic-org / html5tagger

Create HTML documents from Python
The Unlicense
29 stars 4 forks source link

Add CSS-style attribute assignment #9

Open ahopkins opened 1 year ago

ahopkins commented 1 year ago

The goal of this PR is to come up with a nicer API for assigning HTML id and class attributes that clash with Python syntax. One approach is to overload the __getitem__ method to allow for this:

from html5tagger import Document

doc = Document("Test")
doc.div["#some-id.one.two.three[href=/some/path][another='foo bar']"]

print(str(doc))
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test</title>
<div id="some-id" href="/some/path" another="foo bar" class="one two three"></div>

There are a few considerations:

  1. Whether the value passed to __getitem__ should be a single string, or a tuple of strings: ["#id", "class", ...]
  2. Whether the values (particularly class names) should be dot (.) prepended: .one.two.three v one two three
  3. Whether arbitrary attributes should be supported in this pattern

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.

Tronic commented 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.

Tronic commented 1 year ago

Needs README update I guess, but is there anything else stopping this from merging?