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

Clone input nodes when inserting over a set #574

Merged
merged 1 commit into from
Dec 26, 2014

Conversation

jugglinmike
Copy link
Member

I noticed this bug while wrapping up gh-558 (c'mon that's funny). I think the changeset ended up being a little ugly, so please don't hesitate to offer suggestions for refactoring!

Commit message:

This patch increases parity with jQuery, specifically in cases where
manipulation methods are called with existing nodes on sets containing
more than one element. In such cases, the provided node should be cloned
prior to insertion (excepting the final element in the set, where the
original node should be inserted).

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) when pulling 7772bf6 on jugglinmike:clone-set into 6ba5338 on cheeriojs:master.

*/
exports.isHtml = function(str) {
// Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could replace str.length >= 3 with str !== '<>'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I'm not sure that would be an improvement since it duplicates information. The preceeding conditions already checked for the character values, after all.

If you're motivated by optimization, I wouldn't necessarily object (this is used on pretty hot code path, after all). In that case, though, we'll have to microbenchmark and consider a bunch of other alternatives (str.length > 2 comes to mind, along with comparing the relative speed of charAt and the array subscripting operator).

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) when pulling 8340187 on jugglinmike:clone-set into 6ba5338 on cheeriojs:master.

@jugglinmike
Copy link
Member Author

@davidchambers I just realized that it was coimpletely unecessary to move the isHtml function for this pull request. I've undone that change in an additional commit, which I will squash at your command. I've also opened gh-578 to discuss the issues we've identified around input interpretation/validation.

(In case you're curious: I noticed this bug while working on another feature branch. I cherry-picked that commit onto master and it looks like some extra changes came along for the ride.)

@davidchambers
Copy link
Contributor

In such cases, the provided node should be cloned prior to insertion (excepting the final element in the set, where the original node should be inserted).

Why is the last element in a set treated differently?

@jugglinmike
Copy link
Member Author

@davidchambers I want to match jQuery's behavior. From the API documentation on append:

Important: If there is more than one target element, however, cloned copies of the inserted element will be created for each target except for the last one.

I can only guess at the rationale. Here goes:

When calling, for instance, $div.append($existingElement), one would expect the $existingElement to be removed from its original location and appended to the element in the$div selection. Identical behavior would not be possible if append were called on $divs--a collection of elements. My guess is that the behavior is an attempt to maintain both behaviors (content insertion and content movement) in cases where multiple target elements are specified.

Again, this is completely conjecture on my part, since the API documentation doesn't really cover motivation for design decisions. What do you think?

@davidchambers
Copy link
Contributor

That makes sense, @jugglinmike! Thanks for the explanation. :)

@fb55
Copy link
Member

fb55 commented Dec 25, 2014

Needs a rebase, LGTM, though.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) when pulling 56fbefb on jugglinmike:clone-set into d05ce4c on cheeriojs:master.

This patch increases parity with jQuery, specifically in cases where
manipulation methods are called with existing nodes on sets containing
more than one element. In such cases, the provided node should be cloned
prior to insertion (excepting the final element in the set, where the
original node should be inserted).
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) when pulling 466e34a on jugglinmike:clone-set into 3b4fb1e on cheeriojs:master.

@jugglinmike
Copy link
Member Author

@fb55 I've updated this one, too

fb55 added a commit that referenced this pull request Dec 26, 2014
Clone input nodes when inserting over a set
@fb55 fb55 merged commit 5385e3f into cheeriojs:master Dec 26, 2014
@fb55
Copy link
Member

fb55 commented Dec 26, 2014

And merged.

@jugglinmike
Copy link
Member Author

Yessss

@fb55
Copy link
Member

fb55 commented Dec 26, 2014

:D

@jugglinmike jugglinmike deleted the clone-set branch May 28, 2015 22:56
This was referenced May 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants