Skip to content

Commit

Permalink
Make sure fragment caching is only happening on nodes in the main doc…
Browse files Browse the repository at this point in the history
…ument. Fixes #5978.
  • Loading branch information
jeresig committed Jan 29, 2010
1 parent 388a00f commit 4f2e209
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/manipulation.js
Expand Up @@ -384,10 +384,15 @@ function cloneCopyEvent(orig, ret) {
}

function buildFragment( args, nodes, scripts ) {
var fragment, cacheable, cacheresults, doc;
var fragment, cacheable, cacheresults,
doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);

// Only cache "small" (1/2 KB) strings that are associated with the main document
// Cloning options loses the selected state, so don't cache them
// Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && doc === document &&
args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {

// webkit does not clone 'checked' attribute of radio inputs on cloneNode, so don't cache if string has a checked
if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
cacheable = true;
cacheresults = jQuery.fragments[ args[0] ];
if ( cacheresults ) {
Expand All @@ -398,7 +403,6 @@ function buildFragment( args, nodes, scripts ) {
}

if ( !fragment ) {
doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
fragment = doc.createDocumentFragment();
jQuery.clean( args, doc, fragment, scripts );
}
Expand Down

5 comments on commit 4f2e209

@jdalton
Copy link
Member

Choose a reason for hiding this comment

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

yay for more descriptive comments, nom nom I eat that stuff up.

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on 4f2e209 Jan 29, 2010

Choose a reason for hiding this comment

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

@john : erm, if it is a string, maybe you want to trim the args[0] before checking is it shorter than 512 ?

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

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

@DBJDBJ: We can't really reasonably trim the string since the whitespace is frequently important. It's better to just leave it as is.

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on 4f2e209 Jan 29, 2010

Choose a reason for hiding this comment

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

@john : I believe you ;o) ... and I do not have the full picture I guess ... but how can be preceeding and trailing white space important ... when building a frament ?

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

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

For example: .append(" test") is different from .append("test") (hope the spacing comes through in the comments). If we trimmed the whitespace they would be considered to be equal - even though they definitely aren't.

Please sign in to comment.