From 3bcac07aab4df4043080feb4b6fef39d0c3c5869 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Mon, 22 Apr 2013 13:29:10 -0700 Subject: [PATCH] Fix ve.ui.get$$'s handling of jQuery selection Turns out, the context property of a jQuery selection isn't always there. For example: $( 'body' ).context === document $( '
' ).context === undefined Even if you later attach that div, so long as you have the old selection around, the cached `context` property won't magically be updated. This makes sense (although it's poorly documented in the jQuery API) but causes issues for us, and pretty much makes the context property useless. Instead, we can just use the standard `ownerDocument` property, available on all DOM elements. This change also add support for passing in a DOM element directly, in addition to the existing support of passing jQuery or Document objects in. Change-Id: Ib8a31b74f2a4f455b1318be9f5c7805a2a193c79 --- modules/ve/ui/ve.ui.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/ve/ui/ve.ui.js b/modules/ve/ui/ve.ui.js index 9ee6bde5c0..d204ba3854 100644 --- a/modules/ve/ui/ve.ui.js +++ b/modules/ve/ui/ve.ui.js @@ -18,17 +18,29 @@ ve.ui = { /** * Gets a jQuery function within a specific document. * - * @param {jQuery|HTMLDocument} context Context to bind the function to + * @param {jQuery|HTMLElement|HTMLDocument} context Context to bind the function to * @param {ve.ui.Frame} [frame] Frame of the document context * @returns {Function} Bound jQuery function */ ve.ui.get$$ = function ( context, frame ) { - function $$( selector ) { - return $( selector, context instanceof jQuery ? context.context : context ); + function wrapper( selector ) { + return $( selector, wrapper.context ); + } + if ( context instanceof jQuery ) { + // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable + wrapper.context = context[0].ownerDocument; + } else if ( context.ownerDocument ) { + // HTMLElement + wrapper.context = context.ownerDocument; + } else { + // HTMLDocument + wrapper.context = context; + } + if ( !wrapper.context ) { + throw new Error( 'Invalid context' ); } - $$.context = context; if ( frame ) { - $$.frame = frame; + wrapper.frame = frame; } - return $$; + return wrapper; };