Skip to content
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

Add CSS-style attribute assignment #9

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Aug 27, 2023

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
Copy link
Member

Tronic commented Sep 1, 2023

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
Copy link
Member

Tronic commented Sep 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants