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

More fine-grained parsing of data attributes #219

Closed
bencroker opened this issue Nov 19, 2024 · 8 comments
Closed

More fine-grained parsing of data attributes #219

bencroker opened this issue Nov 19, 2024 · 8 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@bencroker
Copy link
Collaborator

Someone reported having a data attribute collision between a Craft plugin they use and Datastar. The plugin uses data-show-processing-spinner to mark elements as processing., which results in this error.

Uncaught (in promise) Error: Expression function not created

@delaneyj put aside any thoughts of “forms are bad”, because this is a plugin for collecting user input from the frontend. So it is less about forms and more about dealing with naming collisions.

Would it be possible to get rid of mustHaveEmptyKey and replace mustNotEmptyKey with mustHaveKey? That way, data-show-processing-spinner will not be confused with the data-show attribute.

@bencroker bencroker changed the title More fine-grained data-* attributes More fine-grained with data-* Nov 19, 2024
@bencroker bencroker changed the title More fine-grained with data-* More fine-grained parsing with data-* Nov 19, 2024
@delaneyj
Copy link
Collaborator

I think this way lies madness. There's too many edge cases to do this properly probably.

If you don't want name space clashing

  1. Modify the other library
  2. Make a custom bundle with different names

Class.name = 'clazz'
Data.load(Class)

Now have data-clazz

@bencroker
Copy link
Collaborator Author

bencroker commented Nov 19, 2024

I’m going to push back on this. If the attribute plugin’s name is show, and it does not accept a key, then the plugin should ignore data-show-* attributes. I don’t see any madness here. On the contrary, this seems more logical than the current implementation, which errors out when it encounters data-show-processing-spinner. This may require a canHaveKey option, but either way, I think it merits exploring for 0.21.0.

@bencroker bencroker changed the title More fine-grained parsing with data-* More fine-grained parsing of data attributes Nov 19, 2024
@bencroker
Copy link
Collaborator Author

Proof-of-concept in engine.ts.

Before:

if (p.mustHaveEmptyKey && key.length > 0) {
    // must have empty key
    throw ERR_BAD_ARGS;
}
if (p.mustNotEmptyKey && key.length === 0) {
    // must have non-empty key
    throw ERR_BAD_ARGS;
}

After:

if (!p.canHaveKey && key.length > 0) {
    // skip
    continue;
}
if (p.mustHaveKey && key.length === 0) {
    // must have a key
    throw ERR_BAD_ARGS;
}

@delaneyj
Copy link
Collaborator

i still have a major problem with trying to maintain this separation... we may add modifiers in the future, we can "solve" this for now but it seems like a game of whack a mole

@delaneyj
Copy link
Collaborator

Proof-of-concept in engine.ts.

Before:

if (p.mustHaveEmptyKey && key.length > 0) {
    // must have empty key
    throw ERR_BAD_ARGS;
}
if (p.mustNotEmptyKey && key.length === 0) {
    // must have non-empty key
    throw ERR_BAD_ARGS;
}

After:

if (!p.canHaveKey && key.length > 0) {
    // skip
    continue;
}
if (p.mustHaveKey && key.length === 0) {
    // must have a key
    throw ERR_BAD_ARGS;
}

yeah, this is a valid point, but i meant more trying to avoid name clashes... as the data-* first framework they can change if there is a "real" clash. this change makes sense to me

@bencroker bencroker transferred this issue from another repository Nov 22, 2024
@delaneyj delaneyj added the enhancement New feature or request label Nov 26, 2024
@bencroker
Copy link
Collaborator Author

We agreed that we want to keep the possibility of using data-class-* attributes, so closing this.

@bencroker
Copy link
Collaborator Author

Reopening to explore whether there’s a case for not throwing an error, for better compatibility with other libraries.

@bencroker bencroker reopened this Dec 1, 2024
@bencroker bencroker self-assigned this Dec 1, 2024
@bencroker bencroker added this to the v0.21.0 milestone Dec 1, 2024
@bencroker
Copy link
Collaborator Author

bencroker commented Dec 1, 2024

Decided that we’re going with throwing errors instead of failing silently, so the only way forward for allowing data-show-* attributes for other use-cases is to create a custom Datastar bundle that does not include the show plugin.

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

No branches or pull requests

2 participants