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

'vueish' html attributes with html2htpy #28

Closed
wants to merge 1 commit into from
Closed

Conversation

OleJoik
Copy link
Contributor

@OleJoik OleJoik commented Jun 15, 2024

'vueish' html attributes with html2htpy

html2htpy is not currently able to handle conversion of key word attributes prefixed with special characters such as @click or :show, which I stumped into while converting some templates containing attributes from alpine.js.

I added a test and a bit to the transformation algorithm to support converting these attributes to htpy's dictionary attr syntax.

def test_convert_vueish_attrs() -> None:
    input = """
      <button id="btn-id" @click="doSomething" type="submit">Submit</button>
      <img id="img-id" :src="imageSrc" />
    """

    actual = html2htpy(input, import_mode="no") 
    assert actual == (
        "["
        'button("#btn-id",{"@click":"doSomething"},type="submit")["Submit"],'
        'img("#img-id",{":src":"imageSrc"})'
        "]"
    )

@OleJoik OleJoik changed the title Bugfix: Handle 'vueish' attr names (@, : prefixed) 'vueish' html attributes with html2htpy Jun 15, 2024
@OleJoik
Copy link
Contributor Author

OleJoik commented Jun 15, 2024

Not sure though if you want to support all sorts of strange non-standard attributes that might show up. I guess these are fairly common though, so I thought why not.

@pelme
Copy link
Owner

pelme commented Jun 16, 2024

I think https://docs.python.org/3/library/stdtypes.html#str.isidentifier can be used to decide whether or not an attribute name should be a direct argument or be put into the dict:

>>> '@click'.isidentifier()
False
>>> ':src'.isidentifier()
False
>>> 'foo'.isidentifier()
True
>>> 'type'.isidentifier()
True

Also, keyword.iskeyword(x) can be used to add a trailing underscore consistently instead of hard coding special cases for for_ and class_.

@pelme pelme closed this in 8ebc8e7 Jun 25, 2024
@pelme
Copy link
Owner

pelme commented Jun 25, 2024

I added checks for keyword/identifiers+dict attributes so it is hopefully robust with all kinds of attributes now. Published htpy 24.6.1 to pypi with this fix :)

 $ echo '<img id="img-id" :src="imageSrc" @click.prevent="foo=bar">' | html2htpy
from htpy import img

img("#img-id", {":src": "imageSrc", "@click.prevent": "foo=bar"})

@OleJoik
Copy link
Contributor Author

OleJoik commented Jun 25, 2024

Apologies for leaving that stale, I became quite busy at work. Was planning to come back to it, but here we are!

@pelme
Copy link
Owner

pelme commented Jun 25, 2024

No worries! :)

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.

2 participants