diff options
author | Alexander Schmitz <arschmitz@gmail.com> | 2015-07-15 18:14:26 -0400 |
---|---|---|
committer | Alexander Schmitz <arschmitz@gmail.com> | 2015-08-08 00:29:36 -0400 |
commit | 2c92f1074718ee23081765da0b86a8e3fe906874 (patch) | |
tree | 07c2ec6c1b20f138bb70df073cfb66eeb67cf486 /ui/labels.js | |
parent | bddb7efe000cf60e455d48f28acef0ef2f295d9d (diff) | |
download | jquery-ui-2c92f1074718ee23081765da0b86a8e3fe906874.tar.gz jquery-ui-2c92f1074718ee23081765da0b86a8e3fe906874.zip |
Core: Move labels into its own module
Ref #9647
Diffstat (limited to 'ui/labels.js')
-rw-r--r-- | ui/labels.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/ui/labels.js b/ui/labels.js new file mode 100644 index 000000000..2a78d886b --- /dev/null +++ b/ui/labels.js @@ -0,0 +1,62 @@ +/*! + * jQuery UI Labels @VERSION + * http://jqueryui.com + * + * Copyright jQuery Foundation and other contributors + * Released under the MIT license. + * http://jquery.org/license + */ + +//>>label: labels +//>>group: Core +//>>description: Find all the labels associated with a given input +//>>docs: http://api.jqueryui.com/labels/ + +( function( factory ) { + if ( typeof define === "function" && define.amd ) { + + // AMD. Register as an anonymous module. + define( [ "jquery", "./version", "./escape-selector" ], factory ); + } else { + + // Browser globals + factory( jQuery ); + } +} ( function( $ ) { + +return $.fn.labels = function() { + var ancestor, selector, id, labels, ancestors; + + // Check control.labels first + if ( this[ 0 ].labels && this[ 0 ].labels.length ) { + return this.pushStack( this[ 0 ].labels ); + } + + // Support: IE <= 11, FF <= 37, Android <= 2.3 only + // Above browsers do not support control.labels. Everything below is to support them + // as well as document fragments. control.labels does not work on document fragments + labels = this.eq( 0 ).parents( "label" ); + + // Look for the label based on the id + id = this.attr( "id" ); + if ( id ) { + + // We don't search against the document in case the element + // is disconnected from the DOM + ancestor = this.eq( 0 ).parents().last(); + + // Get a full set of top level ancestors + ancestors = ancestor.add( ancestor.length ? ancestor.siblings() : this.siblings() ); + + // Create a selector for the label based on the id + selector = "label[for='" + $.ui.escapeSelector( id ) + "']"; + + labels = labels.add( ancestors.find( selector ).addBack( selector ) ); + + } + + // Return whatever we have found for labels + return this.pushStack( labels ); +}; + +} ) ); |