1/*! http://mths.be/placeholder v2.1.0 by @mathias */ 2(function(factory) { 3 if (typeof define === 'function' && define.amd) { 4 // AMD 5 define(['jquery'], factory); 6 } else { 7 // Browser globals 8 factory(jQuery); 9 } 10}(function($) { 11 12 // Opera Mini v7 doesn't support placeholder although its DOM seems to indicate so 13 var isOperaMini = Object.prototype.toString.call(window.operamini) == '[object OperaMini]'; 14 var isInputSupported = 'placeholder' in document.createElement('input') && !isOperaMini; 15 var isTextareaSupported = 'placeholder' in document.createElement('textarea') && !isOperaMini; 16 var valHooks = $.valHooks; 17 var propHooks = $.propHooks; 18 var hooks; 19 var placeholder; 20 21 if (isInputSupported && isTextareaSupported) { 22 23 placeholder = $.fn.placeholder = function() { 24 return this; 25 }; 26 27 placeholder.input = placeholder.textarea = true; 28 29 } else { 30 31 var settings = {}; 32 33 placeholder = $.fn.placeholder = function(options) { 34 35 var defaults = {customClass: 'placeholder'}; 36 settings = $.extend({}, defaults, options); 37 38 var $this = this; 39 $this 40 .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]') 41 .not('.'+settings.customClass) 42 .bind({ 43 'focus.placeholder': clearPlaceholder, 44 'blur.placeholder': setPlaceholder 45 }) 46 .data('placeholder-enabled', true) 47 .trigger('blur.placeholder'); 48 return $this; 49 }; 50 51 placeholder.input = isInputSupported; 52 placeholder.textarea = isTextareaSupported; 53 54 hooks = { 55 'get': function(element) { 56 var $element = $(element); 57 58 var $passwordInput = $element.data('placeholder-password'); 59 if ($passwordInput) { 60 return $passwordInput[0].value; 61 } 62 63 return $element.data('placeholder-enabled') && $element.hasClass(settings.customClass) ? '' : element.value; 64 }, 65 'set': function(element, value) { 66 var $element = $(element); 67 68 var $passwordInput = $element.data('placeholder-password'); 69 if ($passwordInput) { 70 return $passwordInput[0].value = value; 71 } 72 73 if (!$element.data('placeholder-enabled')) { 74 return element.value = value; 75 } 76 if (value === '') { 77 element.value = value; 78 // Issue #56: Setting the placeholder causes problems if the element continues to have focus. 79 if (element != safeActiveElement()) { 80 // We can't use `triggerHandler` here because of dummy text/password inputs :( 81 setPlaceholder.call(element); 82 } 83 } else if ($element.hasClass(settings.customClass)) { 84 clearPlaceholder.call(element, true, value) || (element.value = value); 85 } else { 86 element.value = value; 87 } 88 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363 89 return $element; 90 } 91 }; 92 93 if (!isInputSupported) { 94 valHooks.input = hooks; 95 propHooks.value = hooks; 96 } 97 if (!isTextareaSupported) { 98 valHooks.textarea = hooks; 99 propHooks.value = hooks; 100 } 101 102 $(function() { 103 // Look for forms 104 $(document).delegate('form', 'submit.placeholder', function() { 105 // Clear the placeholder values so they don't get submitted 106 var $inputs = $('.'+settings.customClass, this).each(clearPlaceholder); 107 setTimeout(function() { 108 $inputs.each(setPlaceholder); 109 }, 10); 110 }); 111 }); 112 113 // Clear placeholder values upon page reload 114 $(window).bind('beforeunload.placeholder', function() { 115 $('.'+settings.customClass).each(function() { 116 this.value = ''; 117 }); 118 }); 119 120 } 121 122 function args(elem) { 123 // Return an object of element attributes 124 var newAttrs = {}; 125 var rinlinejQuery = /^jQuery\d+$/; 126 $.each(elem.attributes, function(i, attr) { 127 if (attr.specified && !rinlinejQuery.test(attr.name)) { 128 newAttrs[attr.name] = attr.value; 129 } 130 }); 131 return newAttrs; 132 } 133 134 function clearPlaceholder(event, value) { 135 var input = this; 136 var $input = $(input); 137 if (input.value == $input.attr('placeholder') && $input.hasClass(settings.customClass)) { 138 if ($input.data('placeholder-password')) { 139 $input = $input.hide().nextAll('input[type="password"]:first').show().attr('id', $input.removeAttr('id').data('placeholder-id')); 140 // If `clearPlaceholder` was called from `$.valHooks.input.set` 141 if (event === true) { 142 return $input[0].value = value; 143 } 144 $input.focus(); 145 } else { 146 input.value = ''; 147 $input.removeClass(settings.customClass); 148 input == safeActiveElement() && input.select(); 149 } 150 } 151 } 152 153 function setPlaceholder() { 154 var $replacement; 155 var input = this; 156 var $input = $(input); 157 var id = this.id; 158 if (input.value === '') { 159 if (input.type === 'password') { 160 if (!$input.data('placeholder-textinput')) { 161 try { 162 $replacement = $input.clone().attr({ 'type': 'text' }); 163 } catch(e) { 164 $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' })); 165 } 166 $replacement 167 .removeAttr('name') 168 .data({ 169 'placeholder-password': $input, 170 'placeholder-id': id 171 }) 172 .bind('focus.placeholder', clearPlaceholder); 173 $input 174 .data({ 175 'placeholder-textinput': $replacement, 176 'placeholder-id': id 177 }) 178 .before($replacement); 179 } 180 $input = $input.removeAttr('id').hide().prevAll('input[type="text"]:first').attr('id', id).show(); 181 // Note: `$input[0] != input` now! 182 } 183 $input.addClass(settings.customClass); 184 $input[0].value = $input.attr('placeholder'); 185 } else { 186 $input.removeClass(settings.customClass); 187 } 188 } 189 190 function safeActiveElement() { 191 // Avoid IE9 `document.activeElement` of death 192 // https://github.com/mathiasbynens/jquery-placeholder/pull/99 193 try { 194 return document.activeElement; 195 } catch (exception) {} 196 } 197 198}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.