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

Simplify retrieval of target elements (with data-target and href) #21654

Closed
wants to merge 18 commits into from

Conversation

pvdlg
Copy link
Contributor

@pvdlg pvdlg commented Jan 10, 2017

Fixes #18641, fixes #21328.

Util.getSelectorFromElement is used to obtains the selector, (and later then the matching elements), referenced by data-target or href in JS components.
If data-target is not defined it relies on a regex to determine if href is a selector or a regular link.

The problem is that there is no way to have a regex that will isolate URL from css selectors in 100% of the cases. For example href="div.class" can mean 'execute the JS action (collapse, dropdown, etc..) on all div with the class class or it can be a regular href URL redirecting to the page <context>/div.class

In order to solve this issue this PR remove the reliance on the regex and:

  • replace getSelectorFromElement by getTargets that returns a JQuery with matching targets
  • if data-target is defined, getTargets uses it
  • if data-target is not defined, getTargets try to use href and considers it as the attribute containing the target selector, if a JQuery selection on it actually returns elements

In addition the PR slightly simplifies the code to retrieve targeted elements in each modules.

Currently if a JS module trigger element (i.e. element with data-toggle) doesn't have a valid selector in data-target nor in href nothing happen except for dropdown and alert in which the parent of the trigger is considered the target. There is no error reported and the JS module is bypassed and has no effect. I kept the same behavior in the PR.

@pvdlg
Copy link
Contributor Author

pvdlg commented Jan 11, 2017

Added a test case for #21328

@pvdlg pvdlg changed the title Remove reliance on regex in Util.getSelectorFromElement Simplify retrieval of target elements (with data-target and href) Mar 19, 2017
@Johann-S
Copy link
Member

Thank you for your PR, but we want to stop relying on jQuery as much as possible, so in the futur we will have to work with selectors instead of jQuery object.

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

Successfully merging this pull request may close these issues.

3 participants