Prefer matching fragments within a given { origin } · Issue #197 · unpoly/unpoly · GitHub
Skip to content

Prefer matching fragments within a given { origin } #197

Open

Description

@boxxxie

Bug description

discovered this when my forms were set to target an inner element, but instead the first matching element on the page was selected (this is a page with a list of forms)

<div>
    <div class="auto-submit d-flex"></div>
    <form
      {% if id %}id="{{id}}"{% else %}id="form_{{name}}"{% endif %}
      up-transition="cross-fade"
      up-target=".auto-submit"
      up-method="{{up-method|default:PATCH}}"
      up-autosubmit
      up-delay="{{up-delay|default:1000}}"
      enctype="multipart/form-data"
      up-history="false"
      action="{{action}}">
        {% block component %}{% endblock %}
        <div class="auto-submit d-flex"> </div>
    </form>
</div>

response

<div class="auto-submit d-flex">
    <img src="/assets/Font-Awesome/svgs/solid/check-circle.svg">
    <p>Profile Upadted !</p>
</div>

my workaround/fix

var selector_closest_fixed = false;
(function fix_selector_closest(){
  if(!selector_closest_fixed){
    selector_closest_fixed = true
    up.Selector.prototype.closest = function(element) {
      var parentElement;
      let child_target = element.querySelector(this.unionSelector);
      if (child_target){
        return child_target
      }
      else if (this.matches(element)) {
        return element;
      } else if ((parentElement = element.parentElement)) {
        return this.closest(parentElement);
      }
    };
  }
})();

Activity

  1. adam12 commented on Jun 26, 2021

    @adam12
    Member

    Can you confirm you're using Unpoly 2?

    up.selector.closest() doesn't look at children, AFAIK. You might need to narrow the selector if it's ambiguous to Unpoly. If you don't want to add another id or class attribute, you could use up-id.

    I wasn't able to follow the exact issue from your example. If you have an opportunity and could make a reproducible example using Glitch, it might help. There's a Glitch template to start you off, if so.

  2. boxxxie commented on Jun 26, 2021

    @boxxxie
    Author

    i'm using unpoly ~2.0.1 (whatever is latest on CDN)
    i tried many selectors for solutions, even up-id
    i spent a lot of time on this issue, trying to figure out why my form post was changing html in some random place on my page, nowhere near the origin.
    the way selector closest works either is not intuitive, incorrect, or maybe the function should be renamed.

    closest really sounds like it should try to do just querySelector(selector), because the closest selection will be that. but closest is doing a recursive search of parents, so it's name should probably be something similar to what it's actually doing, like search_parents or something

    since unpoly has the up-content att, i think i can do an example without needing a server. i'll post it soon.

  3. boxxxie commented on Jun 26, 2021

    @boxxxie
    Author

    https://glitch.com/edit/#!/ember-mousy-canvas

    js has my fix, i can't get the up-document/up-content working (maybe another bug)

    i added some style to show what unpoly is changing, js has my fix, remove the fix and see the behaviour difference.

  4. adam12 commented on Jun 26, 2021

    @adam12
    Member

    I removed your Javascript changes and updated both up-target to this.

    up-target="& .auto-submit"
    

    I believe it achieves what you were looking for. Can you confirm? https://glitch.com/edit/#!/radial-attractive-equinox

  5. boxxxie commented on Jun 26, 2021

    @boxxxie
    Author

    yes, maybe this should be emphasised in the documentation. i tried to use :origin in my target, but it didn't work, the & is very unfamiliar to me, and i didn't attempt to use it as a selector.

    seems that origin should be implied, though.

    would be common that the selector you used would break things for people? for me it seems like the intuitive behaviour and & is redundant.

  6. adam12 commented on Jun 27, 2021

    @adam12
    Member

    I did try :origin .auto-submit as a selector and it appears to work. The & selector was linked in the v1 up-target docs, but that's not the case for v2. If you think you can make the docs more expressive in this case, please submit a PR. To be honest, & is more familiar to me than :origin, as I have past experience with Sass.

    seems that origin should be implied, though.

    I haven't ran into any issues here, so I can't really agree. Maybe Henning can provide an opinion here.

    I'm going to close this for now since up.fragment.closest documents that it only looks at itself and then it's ancestors, never its children. If Henning deems this a bug we can re-open.

    Please feel free to submit a PR for the :origin documentation if you can find a way to improve it.

  7. changed the title [-]up.Selector.closest() doesn't consider selector on children targets[/-] [+]Prefer matching fragments within a given { origin }[/+] on Jun 28, 2021
  8. triskweline commented on Jun 28, 2021

    @triskweline
    Contributor

    Both :origin and & are supported, as documented here.

    Even without :origin in the selector, Unpoly 2 tries to take a given { origin } into account when matching fragments. Currently that mostly looks for a parent element around the origin.

    I haven't had a use case where I needed to auto-match an origin child, but I guess we could support that.

    The fix wouldn't go in up.Selector though, but here:

    return @findAroundOrigin() || @findInLayer()

  9. triskweline commented on Jun 28, 2021

    @triskweline
    Contributor

    Re-opening this issue for a while in case someone wants to tackle that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions