labels.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. * jQuery UI Labels 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: labels
  10. //>>group: Core
  11. //>>description: Find all the labels associated with a given input
  12. //>>docs: https://api.jqueryui.com/labels/
  13. ( function( factory ) {
  14. "use strict";
  15. if ( typeof define === "function" && define.amd ) {
  16. // AMD. Register as an anonymous module.
  17. define( [ "jquery", "./version" ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. } )( function( $ ) {
  23. "use strict";
  24. return $.fn.labels = function() {
  25. var ancestor, selector, id, labels, ancestors;
  26. if ( !this.length ) {
  27. return this.pushStack( [] );
  28. }
  29. // Check control.labels first
  30. if ( this[ 0 ].labels && this[ 0 ].labels.length ) {
  31. return this.pushStack( this[ 0 ].labels );
  32. }
  33. // If `control.labels` is empty - e.g. inside of document fragments - find
  34. // the labels manually
  35. labels = this.eq( 0 ).parents( "label" );
  36. // Look for the label based on the id
  37. id = this.attr( "id" );
  38. if ( id ) {
  39. // We don't search against the document in case the element
  40. // is disconnected from the DOM
  41. ancestor = this.eq( 0 ).parents().last();
  42. // Get a full set of top level ancestors
  43. ancestors = ancestor.add( ancestor.length ? ancestor.siblings() : this.siblings() );
  44. // Create a selector for the label based on the id
  45. selector = "label[for='" + CSS.escape( id ) + "']";
  46. labels = labels.add( ancestors.find( selector ).addBack( selector ) );
  47. }
  48. // Return whatever we have found for labels
  49. return this.pushStack( labels );
  50. };
  51. } );