placeholder.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2012 James Allardice
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  15. * THE SOFTWARE.
  16. */
  17. // Defines the global Placeholders object along with various utility methods
  18. (function (global) {
  19. "use strict";
  20. // Cross-browser DOM event binding
  21. function addEventListener(elem, event, fn) {
  22. if (elem.addEventListener) {
  23. return elem.addEventListener(event, fn, false);
  24. }
  25. if (elem.attachEvent) {
  26. return elem.attachEvent("on" + event, fn);
  27. }
  28. }
  29. // Check whether an item is in an array (we don't use Array.prototype.indexOf so we don't clobber any existing polyfills - this is a really simple alternative)
  30. function inArray(arr, item) {
  31. var i, len;
  32. for (i = 0, len = arr.length; i < len; i++) {
  33. if (arr[i] === item) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. // Move the caret to the index position specified. Assumes that the element has focus
  40. function moveCaret(elem, index) {
  41. var range;
  42. if (elem.createTextRange) {
  43. range = elem.createTextRange();
  44. range.move("character", index);
  45. range.select();
  46. } else if (elem.selectionStart) {
  47. elem.focus();
  48. elem.setSelectionRange(index, index);
  49. }
  50. }
  51. // Attempt to change the type property of an input element
  52. function changeType(elem, type) {
  53. try {
  54. elem.type = type;
  55. return true;
  56. } catch (e) {
  57. // You can't change input type in IE8 and below
  58. return false;
  59. }
  60. }
  61. // Expose public methods
  62. global.Placeholders = {
  63. Utils: {
  64. addEventListener: addEventListener,
  65. inArray: inArray,
  66. moveCaret: moveCaret,
  67. changeType: changeType
  68. }
  69. };
  70. }(this));
  71. (function (global) {
  72. "use strict";
  73. var validTypes = [
  74. "text",
  75. "search",
  76. "url",
  77. "tel",
  78. "email",
  79. "password",
  80. "number",
  81. "textarea"
  82. ],
  83. // The list of keycodes that are not allowed when the polyfill is configured to hide-on-input
  84. badKeys = [
  85. // The following keys all cause the caret to jump to the end of the input value
  86. 27, // Escape
  87. 33, // Page up
  88. 34, // Page down
  89. 35, // End
  90. 36, // Home
  91. // Arrow keys allow you to move the caret manually, which should be prevented when the placeholder is visible
  92. 37, // Left
  93. 38, // Up
  94. 39, // Right
  95. 40, // Down
  96. // The following keys allow you to modify the placeholder text by removing characters, which should be prevented when the placeholder is visible
  97. 8, // Backspace
  98. 46 // Delete
  99. ],
  100. // Styling variables
  101. placeholderStyleColor = "#ccc",
  102. placeholderClassName = "placeholdersjs",
  103. classNameRegExp = new RegExp("(?:^|\\s)" + placeholderClassName + "(?!\\S)"),
  104. // These will hold references to all elements that can be affected. NodeList objects are live, so we only need to get those references once
  105. inputs, textareas,
  106. // The various data-* attributes used by the polyfill
  107. ATTR_CURRENT_VAL = "data-placeholder-value",
  108. ATTR_ACTIVE = "data-placeholder-active",
  109. ATTR_INPUT_TYPE = "data-placeholder-type",
  110. ATTR_FORM_HANDLED = "data-placeholder-submit",
  111. ATTR_EVENTS_BOUND = "data-placeholder-bound",
  112. ATTR_OPTION_FOCUS = "data-placeholder-focus",
  113. ATTR_OPTION_LIVE = "data-placeholder-live",
  114. // Various other variables used throughout the rest of the script
  115. test = document.createElement("input"),
  116. head = document.getElementsByTagName("head")[0],
  117. root = document.documentElement,
  118. Placeholders = global.Placeholders,
  119. Utils = Placeholders.Utils,
  120. hideOnInput, liveUpdates, keydownVal, styleElem, styleRules, placeholder, timer, form, elem, len, i;
  121. // No-op (used in place of public methods when native support is detected)
  122. function noop() {}
  123. // Hide the placeholder value on a single element. Returns true if the placeholder was hidden and false if it was not (because it wasn't visible in the first place)
  124. function hidePlaceholder(elem) {
  125. var type;
  126. if (elem.value === elem.getAttribute(ATTR_CURRENT_VAL) && elem.getAttribute(ATTR_ACTIVE) === "true") {
  127. elem.setAttribute(ATTR_ACTIVE, "false");
  128. elem.value = "";
  129. elem.className = elem.className.replace(classNameRegExp, "");
  130. // If the polyfill has changed the type of the element we need to change it back
  131. type = elem.getAttribute(ATTR_INPUT_TYPE);
  132. if (type) {
  133. elem.type = type;
  134. }
  135. return true;
  136. }
  137. return false;
  138. }
  139. // Show the placeholder value on a single element. Returns true if the placeholder was shown and false if it was not (because it was already visible)
  140. function showPlaceholder(elem) {
  141. var type,
  142. val = elem.getAttribute(ATTR_CURRENT_VAL);
  143. if (elem.value === "" && val) {
  144. elem.setAttribute(ATTR_ACTIVE, "true");
  145. elem.value = val;
  146. elem.className += " " + placeholderClassName;
  147. // If the type of element needs to change, change it (e.g. password inputs)
  148. type = elem.getAttribute(ATTR_INPUT_TYPE);
  149. if (type) {
  150. elem.type = "text";
  151. } else if (elem.type === "password") {
  152. if (Utils.changeType(elem, "text")) {
  153. elem.setAttribute(ATTR_INPUT_TYPE, "password");
  154. }
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. function handleElem(node, callback) {
  161. var handleInputs, handleTextareas, elem, len, i;
  162. // Check if the passed in node is an input/textarea (in which case it can't have any affected descendants)
  163. if (node && node.getAttribute(ATTR_CURRENT_VAL)) {
  164. callback(node);
  165. } else {
  166. // If an element was passed in, get all affected descendants. Otherwise, get all affected elements in document
  167. handleInputs = node ? node.getElementsByTagName("input") : inputs;
  168. handleTextareas = node ? node.getElementsByTagName("textarea") : textareas;
  169. // Run the callback for each element
  170. for (i = 0, len = handleInputs.length + handleTextareas.length; i < len; i++) {
  171. elem = i < handleInputs.length ? handleInputs[i] : handleTextareas[i - handleInputs.length];
  172. callback(elem);
  173. }
  174. }
  175. }
  176. // Return all affected elements to their normal state (remove placeholder value if present)
  177. function disablePlaceholders(node) {
  178. handleElem(node, hidePlaceholder);
  179. }
  180. // Show the placeholder value on all appropriate elements
  181. function enablePlaceholders(node) {
  182. handleElem(node, showPlaceholder);
  183. }
  184. // Returns a function that is used as a focus event handler
  185. function makeFocusHandler(elem) {
  186. return function () {
  187. // Only hide the placeholder value if the (default) hide-on-focus behaviour is enabled
  188. if (hideOnInput && elem.value === elem.getAttribute(ATTR_CURRENT_VAL) && elem.getAttribute(ATTR_ACTIVE) === "true") {
  189. // Move the caret to the start of the input (this mimics the behaviour of all browsers that do not hide the placeholder on focus)
  190. Utils.moveCaret(elem, 0);
  191. } else {
  192. // Remove the placeholder
  193. hidePlaceholder(elem);
  194. }
  195. };
  196. }
  197. // Returns a function that is used as a blur event handler
  198. function makeBlurHandler(elem) {
  199. return function () {
  200. showPlaceholder(elem);
  201. };
  202. }
  203. // Functions that are used as a event handlers when the hide-on-input behaviour has been activated - very basic implementation of the "input" event
  204. function makeKeydownHandler(elem) {
  205. return function (e) {
  206. keydownVal = elem.value;
  207. //Prevent the use of the arrow keys (try to keep the cursor before the placeholder)
  208. if (elem.getAttribute(ATTR_ACTIVE) === "true") {
  209. if (keydownVal === elem.getAttribute(ATTR_CURRENT_VAL) && Utils.inArray(badKeys, e.keyCode)) {
  210. if (e.preventDefault) {
  211. e.preventDefault();
  212. }
  213. return false;
  214. }
  215. }
  216. };
  217. }
  218. function makeKeyupHandler(elem) {
  219. return function () {
  220. var type;
  221. if (elem.getAttribute(ATTR_ACTIVE) === "true" && elem.value !== keydownVal) {
  222. // Remove the placeholder
  223. elem.className = elem.className.replace(classNameRegExp, "");
  224. elem.value = elem.value.replace(elem.getAttribute(ATTR_CURRENT_VAL), "");
  225. elem.setAttribute(ATTR_ACTIVE, false);
  226. // If the type of element needs to change, change it (e.g. password inputs)
  227. type = elem.getAttribute(ATTR_INPUT_TYPE);
  228. if (type) {
  229. elem.type = type;
  230. }
  231. }
  232. // If the element is now empty we need to show the placeholder
  233. if (elem.value === "") {
  234. elem.blur();
  235. Utils.moveCaret(elem, 0);
  236. }
  237. };
  238. }
  239. function makeClickHandler(elem) {
  240. return function () {
  241. if (elem === document.activeElement && elem.value === elem.getAttribute(ATTR_CURRENT_VAL) && elem.getAttribute(ATTR_ACTIVE) === "true") {
  242. Utils.moveCaret(elem, 0);
  243. }
  244. };
  245. }
  246. // Returns a function that is used as a submit event handler on form elements that have children affected by this polyfill
  247. function makeSubmitHandler(form) {
  248. return function () {
  249. // Turn off placeholders on all appropriate descendant elements
  250. disablePlaceholders(form);
  251. };
  252. }
  253. // Bind event handlers to an element that we need to affect with the polyfill
  254. function newElement(elem) {
  255. // If the element is part of a form, make sure the placeholder string is not submitted as a value
  256. if (elem.form) {
  257. form = elem.form;
  258. // Set a flag on the form so we know it's been handled (forms can contain multiple inputs)
  259. if (!form.getAttribute(ATTR_FORM_HANDLED)) {
  260. Utils.addEventListener(form, "submit", makeSubmitHandler(form));
  261. form.setAttribute(ATTR_FORM_HANDLED, "true");
  262. }
  263. }
  264. // Bind event handlers to the element so we can hide/show the placeholder as appropriate
  265. Utils.addEventListener(elem, "focus", makeFocusHandler(elem));
  266. Utils.addEventListener(elem, "blur", makeBlurHandler(elem));
  267. // If the placeholder should hide on input rather than on focus we need additional event handlers
  268. if (hideOnInput) {
  269. Utils.addEventListener(elem, "keydown", makeKeydownHandler(elem));
  270. Utils.addEventListener(elem, "keyup", makeKeyupHandler(elem));
  271. Utils.addEventListener(elem, "click", makeClickHandler(elem));
  272. }
  273. // Remember that we've bound event handlers to this element
  274. elem.setAttribute(ATTR_EVENTS_BOUND, "true");
  275. elem.setAttribute(ATTR_CURRENT_VAL, placeholder);
  276. // If the element doesn't have a value, set it to the placeholder string
  277. showPlaceholder(elem);
  278. }
  279. Placeholders.nativeSupport = test.placeholder !== void 0;
  280. if (!Placeholders.nativeSupport) {
  281. // Get references to all the input and textarea elements currently in the DOM (live NodeList objects to we only need to do this once)
  282. inputs = document.getElementsByTagName("input");
  283. textareas = document.getElementsByTagName("textarea");
  284. // Get any settings declared as data-* attributes on the root element (currently the only options are whether to hide the placeholder on focus or input and whether to auto-update)
  285. hideOnInput = root.getAttribute(ATTR_OPTION_FOCUS) === "false";
  286. liveUpdates = root.getAttribute(ATTR_OPTION_LIVE) !== "false";
  287. // Create style element for placeholder styles (instead of directly setting style properties on elements - allows for better flexibility alongside user-defined styles)
  288. styleElem = document.createElement("style");
  289. styleElem.type = "text/css";
  290. // Create style rules as text node
  291. styleRules = document.createTextNode("." + placeholderClassName + " { color:" + placeholderStyleColor + "; }");
  292. // Append style rules to newly created stylesheet
  293. if (styleElem.styleSheet) {
  294. styleElem.styleSheet.cssText = styleRules.nodeValue;
  295. } else {
  296. styleElem.appendChild(styleRules);
  297. }
  298. // Prepend new style element to the head (before any existing stylesheets, so user-defined rules take precedence)
  299. head.insertBefore(styleElem, head.firstChild);
  300. // Set up the placeholders
  301. for (i = 0, len = inputs.length + textareas.length; i < len; i++) {
  302. elem = i < inputs.length ? inputs[i] : textareas[i - inputs.length];
  303. // Get the value of the placeholder attribute, if any. IE10 emulating IE7 fails with getAttribute, hence the use of the attributes node
  304. placeholder = elem.attributes.placeholder;
  305. if (placeholder) {
  306. // IE returns an empty object instead of undefined if the attribute is not present
  307. placeholder = placeholder.nodeValue;
  308. // Only apply the polyfill if this element is of a type that supports placeholders, and has a placeholder attribute with a non-empty value
  309. if (placeholder && Utils.inArray(validTypes, elem.type)) {
  310. newElement(elem);
  311. }
  312. }
  313. }
  314. // If enabled, the polyfill will repeatedly check for changed/added elements and apply to those as well
  315. timer = setInterval(function () {
  316. for (i = 0, len = inputs.length + textareas.length; i < len; i++) {
  317. elem = i < inputs.length ? inputs[i] : textareas[i - inputs.length];
  318. // Only apply the polyfill if this element is of a type that supports placeholders, and has a placeholder attribute with a non-empty value
  319. placeholder = elem.attributes.placeholder;
  320. if (placeholder) {
  321. placeholder = placeholder.nodeValue;
  322. if (placeholder && Utils.inArray(validTypes, elem.type)) {
  323. // If the element hasn't had event handlers bound to it then add them
  324. if (!elem.getAttribute(ATTR_EVENTS_BOUND)) {
  325. newElement(elem);
  326. }
  327. // If the placeholder value has changed or not been initialised yet we need to update the display
  328. if (placeholder !== elem.getAttribute(ATTR_CURRENT_VAL) || (elem.type === "password" && !elem.getAttribute(ATTR_INPUT_TYPE))) {
  329. // Attempt to change the type of password inputs (fails in IE < 9)
  330. if (elem.type === "password" && !elem.getAttribute(ATTR_INPUT_TYPE) && Utils.changeType(elem, "text")) {
  331. elem.setAttribute(ATTR_INPUT_TYPE, "password");
  332. }
  333. // If the placeholder value has changed and the placeholder is currently on display we need to change it
  334. if (elem.value === elem.getAttribute(ATTR_CURRENT_VAL)) {
  335. elem.value = placeholder;
  336. }
  337. // Keep a reference to the current placeholder value in case it changes via another script
  338. elem.setAttribute(ATTR_CURRENT_VAL, placeholder);
  339. }
  340. }
  341. }
  342. }
  343. // If live updates are not enabled cancel the timer
  344. if (!liveUpdates) {
  345. clearInterval(timer);
  346. }
  347. }, 100);
  348. }
  349. // Expose public methods
  350. Placeholders.disable = Placeholders.nativeSupport ? noop : disablePlaceholders;
  351. Placeholders.enable = Placeholders.nativeSupport ? noop : enablePlaceholders;
  352. }(this));