mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.js
Trevor Parscal 3bcac07aab 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
$( '<div>' ).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
2013-04-22 20:36:42 +00:00

47 lines
1.2 KiB
JavaScript

/*!
* VisualEditor UserInterface namespace.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Namespace for all VisualEditor UserInterface classes, static methods and static properties.
* @class
* @singleton
*/
ve.ui = {
//'inspectorFactory': Initialized in ve.ui.InspectorFactory.js
//'toolFactory': Initialized in ve.ui.ToolFactory.js
};
/**
* Gets a jQuery function within a specific document.
*
* @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 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' );
}
if ( frame ) {
wrapper.frame = frame;
}
return wrapper;
};