Support arbitrary selectors for targets #45
Conversation
This change looks good to me. Makes sense that these sorts of selectors can be supported... we can make the presumption that tour authors won't pick selectors like "div" that'll return a lot of potentially unintended matches (FWIW, you could end up in the same scenario with classes anyway, in which case Hopscotch just picks the first match). |
@zimmi88 that was my thought as well. They are also likely to test their implementation, even at a cursory level, to vet selector issues. |
// Check if it's querySelector-eligible. Only accepting IDs and classes, | ||
// because that's the only thing that makes sense. Tag name and pseudo-class | ||
// are just silly. | ||
if (/^[#\.]/.test(target)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the only thing you need to do to allow arbitrary selectors is to remove this if statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not true - that would break cases where someone had passed for use in document.getElementById
(that is, an id that did not begin with "#"). If we simply removed this if
, then you'd return document.querySelector(target)
, which would be null
in the case of an id parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex makes sure that selector begins with #
or .
. So you should not have any ids that did not begin with #
. This change basically makes it so any string will be treated like an id selector. This seems confusing to me. Now we support both CSS style selectors and random string that will be treated like IDs.
I agree, having arbitrary selectors provides more options. This is definitely something I would like Hopscotch to support. |
Yeah, this was kind of an arbitrary restriction that I put in when I wrote it, mainly because I was imagining people putting in selectors such as "div" or "p" and found that kind of silly. That being said, I do think it'd be perfectly fine to let tours define targets with arbitrary selectors. They probably know what they're doing. The fastest way to see this added is to submit a PR -- I don't have much time to look at these issues anymore, but would be happy to review. |
Whoops, did not realize that I was commenting within a PR! My bad. (Thought it was an issue.) I will take a look soon. |
@kate2753 I can provide some additional unit tests to prove out the backwards compatibility, if you like, and then we can use them to iterate on implementation. |
This looks fine to me. The only concern to me was confusing an id for a querySelector, which @joshjordan has accounted for. Thanks for the PR, Josh! |
Support arbitrary selectors for targets
I think we should support CSS style selectors only. And the logic of this function should be:
This should be backward compatible and more efficient, since we do only one document.getElementById lookup. |
Sounds mostly fine to me, with two exceptions.
|
Doh! I've missed original If we want to continue to be backward compatible, we'll have to stick with this implementation. It seems a bit inefficient to run document.getElementById for every target even if we end up using jQuery or sizzle. I do not see a way around it though. I would still swap the order of jquery and document.querySelector. Also, we can probably eliminate second document.getElementById. This seems to be backward compatible:
After giving it some thought |
Cause why not? For my team, this was the expected behavior and hopscotch does not currently throw a useful error message when it does not get what it expects. We were confused when encountering exceptions when given a perfectly valid selector, especially since the code that calls this handles multiple matches gracefully.
This implementation passes all tests that are currently passing in master, and preserves backwards compatibility for existing calls to
getStepTargetHelper
.