* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* 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:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* 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
* THE SOFTWARE.
*/
// Defines the global Placeholders object along with various utility methods
(function(global){
"use strict";
// Cross-browser DOM event binding
functionaddEventListener(elem,event,fn){
if(elem.addEventListener){
returnelem.addEventListener(event,fn,false);
}
if(elem.attachEvent){
returnelem.attachEvent("on"+event,fn);
}
}
// 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)
functioninArray(arr,item){
vari,len;
for(i=0,len=arr.length;i<len;i++){
if(arr[i]===item){
returntrue;
}
}
returnfalse;
}
// Move the caret to the index position specified. Assumes that the element has focus
functionmoveCaret(elem,index){
varrange;
if(elem.createTextRange){
range=elem.createTextRange();
range.move("character",index);
range.select();
}elseif(elem.selectionStart){
elem.focus();
elem.setSelectionRange(index,index);
}
}
// Attempt to change the type property of an input element
functionchangeType(elem,type){
try{
elem.type=type;
returntrue;
}catch(e){
// You can't change input type in IE8 and below
returnfalse;
}
}
// Expose public methods
global.Placeholders={
Utils:{
addEventListener:addEventListener,
inArray:inArray,
moveCaret:moveCaret,
changeType:changeType
}
};
}(this));
(function(global){
"use strict";
varvalidTypes=[
"text",
"search",
"url",
"tel",
"email",
"password",
"number",
"textarea"
],
// The list of keycodes that are not allowed when the polyfill is configured to hide-on-input
badKeys=[
// The following keys all cause the caret to jump to the end of the input value
27,// Escape
33,// Page up
34,// Page down
35,// End
36,// Home
// Arrow keys allow you to move the caret manually, which should be prevented when the placeholder is visible
37,// Left
38,// Up
39,// Right
40,// Down
// The following keys allow you to modify the placeholder text by removing characters, which should be prevented when the placeholder is visible
// 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)
// 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)
// Create style element for placeholder styles (instead of directly setting style properties on elements - allows for better flexibility alongside user-defined styles)