jQuery.webshims.register('form-number-date-api', function($, webshims, window, document, undefined){ "use strict"; //ToDo if(!webshims.getStep){ webshims.getStep = function(elem, type){ var step = $.attr(elem, 'step'); if(step === 'any'){ return step; } type = type || getType(elem); if(!typeModels[type] || !typeModels[type].step){ return step; } step = typeProtos.number.asNumber(step); return ((!isNaN(step) && step > 0) ? step : typeModels[type].step) * typeModels[type].stepScaleFactor; }; } if(!webshims.addMinMaxNumberToCache){ webshims.addMinMaxNumberToCache = function(attr, elem, cache){ if (!(attr+'AsNumber' in cache)) { cache[attr+'AsNumber'] = typeModels[cache.type].asNumber(elem.attr(attr)); if(isNaN(cache[attr+'AsNumber']) && (attr+'Default' in typeModels[cache.type])){ cache[attr+'AsNumber'] = typeModels[cache.type][attr+'Default']; } } }; } var nan = parseInt('NaN', 10), doc = document, typeModels = webshims.inputTypes, isNumber = function(string){ return (typeof string == 'number' || (string && string == string * 1)); }, supportsType = function(type){ return ($('').prop('type') === type); }, getType = function(elem){ return (elem.getAttribute('type') || '').toLowerCase(); }, isDateTimePart = function(string){ return (isNumber(string) || (string && string == '0' + (string * 1))); }, addMinMaxNumberToCache = webshims.addMinMaxNumberToCache, addleadingZero = function(val, len){ val = ''+val; len = len - val.length; for(var i = 0; i < len; i++){ val = '0'+val; } return val; }, EPS = 1e-7, typeBugs = webshims.bugs.valueAsNumberSet || webshims.bugs.bustedValidity ; webshims.addValidityRule('stepMismatch', function(input, val, cache, validityState){ if(val === ''){return false;} if(!('type' in cache)){ cache.type = getType(input[0]); } //stepmismatch with date is computable, but it would be a typeMismatch (performance) if(cache.type == 'date'){ return false; } var ret = (validityState || {}).stepMismatch, base; if(typeModels[cache.type] && typeModels[cache.type].step){ if( !('step' in cache) ){ cache.step = webshims.getStep(input[0], cache.type); } if(cache.step == 'any'){return false;} if(!('valueAsNumber' in cache)){ cache.valueAsNumber = typeModels[cache.type].asNumber( val ); } if(isNaN(cache.valueAsNumber)){return false;} addMinMaxNumberToCache('min', input, cache); base = cache.minAsNumber; if(isNaN(base)){ base = typeModels[cache.type].stepBase || 0; } ret = Math.abs((cache.valueAsNumber - base) % cache.step); ret = !( ret <= EPS || Math.abs(ret - cache.step) <= EPS ); } return ret; }); [{name: 'rangeOverflow', attr: 'max', factor: 1}, {name: 'rangeUnderflow', attr: 'min', factor: -1}].forEach(function(data, i){ webshims.addValidityRule(data.name, function(input, val, cache, validityState) { var ret = (validityState || {})[data.name] || false; if(val === ''){return ret;} if (!('type' in cache)) { cache.type = getType(input[0]); } if (typeModels[cache.type] && typeModels[cache.type].asNumber) { if(!('valueAsNumber' in cache)){ cache.valueAsNumber = typeModels[cache.type].asNumber( val ); } if(isNaN(cache.valueAsNumber)){ return false; } addMinMaxNumberToCache(data.attr, input, cache); if(isNaN(cache[data.attr+'AsNumber'])){ return ret; } ret = ( cache[data.attr+'AsNumber'] * data.factor < cache.valueAsNumber * data.factor - EPS ); } return ret; }); }); webshims.reflectProperties(['input'], ['max', 'min', 'step']); //IDLs and methods, that aren't part of constrain validation, but strongly tight to it var valueAsNumberDescriptor = webshims.defineNodeNameProperty('input', 'valueAsNumber', { prop: { get: function(){ var elem = this; var type = getType(elem); var ret = (typeModels[type] && typeModels[type].asNumber) ? typeModels[type].asNumber($.prop(elem, 'value')) : (valueAsNumberDescriptor.prop._supget && valueAsNumberDescriptor.prop._supget.apply(elem, arguments)); if(ret == null){ ret = nan; } return ret; }, set: function(val){ var elem = this; var type = getType(elem); if(typeModels[type] && typeModels[type].numberToString){ //is NaN a number? if(isNaN(val)){ $.prop(elem, 'value', ''); return; } var set = typeModels[type].numberToString(val); if(set !== false){ $.prop(elem, 'value', set); } else { webshims.warn('INVALID_STATE_ERR: DOM Exception 11'); } } else { valueAsNumberDescriptor.prop._supset && valueAsNumberDescriptor.prop._supset.apply(elem, arguments); } } } }); var valueAsDateDescriptor = webshims.defineNodeNameProperty('input', 'valueAsDate', { prop: { get: function(){ var elem = this; var type = getType(elem); return (typeModels[type] && typeModels[type].asDate && !typeModels[type].noAsDate) ? typeModels[type].asDate($.prop(elem, 'value')) : valueAsDateDescriptor.prop._supget && valueAsDateDescriptor.prop._supget.call(elem) || null; }, set: function(value){ var elem = this; var type = getType(elem); if(typeModels[type] && typeModels[type].dateToString && !typeModels[type].noAsDate){ if(value === null){ $.prop(elem, 'value', ''); return ''; } var set = typeModels[type].dateToString(value); if(set !== false){ $.prop(elem, 'value', set); return set; } else { webshims.warn('INVALID_STATE_ERR: DOM Exception 11'); } } else { return valueAsDateDescriptor.prop._supset && valueAsDateDescriptor.prop._supset.apply(elem, arguments) || null; } } } }); var typeProtos = { number: { mismatch: function(val){ return !(isNumber(val)); }, step: 1, //stepBase: 0, 0 = default stepScaleFactor: 1, asNumber: function(str){ return (isNumber(str)) ? str * 1 : nan; }, numberToString: function(num){ return (isNumber(num)) ? num : false; } }, range: { minDefault: 0, maxDefault: 100 }, date: { mismatch: function(val){ if(!val || !val.split || !(/\d$/.test(val))){return true;} var valA = val.split(/\u002D/); if(valA.length !== 3){return true;} var ret = false; $.each(valA, function(i, part){ if(!isDateTimePart(part)){ ret = true; return false; } }); if(ret){return ret;} if(valA[0].length !== 4 || valA[1].length != 2 || valA[1] > 12 || valA[2].length != 2 || valA[2] > 33){ ret = true; } return (val !== this.dateToString( this.asDate(val, true) ) ); }, step: 1, //stepBase: 0, 0 = default stepScaleFactor: 86400000, asDate: function(val, _noMismatch){ if(!_noMismatch && this.mismatch(val)){ return null; } return new Date(this.asNumber(val, true)); }, asNumber: function(str, _noMismatch){ var ret = nan; if(_noMismatch || !this.mismatch(str)){ str = str.split(/\u002D/); ret = Date.UTC(str[0], str[1] - 1, str[2]); } return ret; }, numberToString: function(num){ return (isNumber(num)) ? this.dateToString(new Date( num * 1)) : false; }, dateToString: function(date){ return (date && date.getFullYear) ? date.getUTCFullYear() +'-'+ addleadingZero(date.getUTCMonth()+1, 2) +'-'+ addleadingZero(date.getUTCDate(), 2) : false; } } // ,time: { // mismatch: function(val, _getParsed){ // if(!val || !val.split || !(/\d$/.test(val))){return true;} // val = val.split(/\u003A/); // if(val.length < 2 || val.length > 3){return true;} // var ret = false, // sFraction; // if(val[2]){ // val[2] = val[2].split(/\u002E/); // sFraction = parseInt(val[2][1], 10); // val[2] = val[2][0]; // } // $.each(val, function(i, part){ // if(!isDateTimePart(part) || part.length !== 2){ // ret = true; // return false; // } // }); // if(ret){return true;} // if(val[0] > 23 || val[0] < 0 || val[1] > 59 || val[1] < 0){ // return true; // } // if(val[2] && (val[2] > 59 || val[2] < 0 )){ // return true; // } // if(sFraction && isNaN(sFraction)){ // return true; // } // if(sFraction){ // if(sFraction < 100){ // sFraction *= 100; // } else if(sFraction < 10){ // sFraction *= 10; // } // } // return (_getParsed === true) ? [val, sFraction] : false; // }, // step: 60, // stepBase: 0, // stepScaleFactor: 1000, // asDate: function(val){ // val = new Date(this.asNumber(val)); // return (isNaN(val)) ? null : val; // }, // asNumber: function(val){ // var ret = nan; // val = this.mismatch(val, true); // if(val !== true){ // ret = Date.UTC('1970', 0, 1, val[0][0], val[0][1], val[0][2] || 0); // if(val[1]){ // ret += val[1]; // } // } // return ret; // }, // dateToString: function(date){ // if(date && date.getUTCHours){ // var str = addleadingZero(date.getUTCHours(), 2) +':'+ addleadingZero(date.getUTCMinutes(), 2), // tmp = date.getSeconds() // ; // if(tmp != "0"){ // str += ':'+ addleadingZero(tmp, 2); // } // tmp = date.getUTCMilliseconds(); // if(tmp != "0"){ // str += '.'+ addleadingZero(tmp, 3); // } // return str; // } else { // return false; // } // } // } // ,'datetime-local': { // mismatch: function(val, _getParsed){ // if(!val || !val.split || (val+'special').split(/\u0054/).length !== 2){return true;} // val = val.split(/\u0054/); // return ( typeProtos.date.mismatch(val[0]) || typeProtos.time.mismatch(val[1], _getParsed) ); // }, // noAsDate: true, // asDate: function(val){ // val = new Date(this.asNumber(val)); // // return (isNaN(val)) ? null : val; // }, // asNumber: function(val){ // var ret = nan; // var time = this.mismatch(val, true); // if(time !== true){ // val = val.split(/\u0054/)[0].split(/\u002D/); // // ret = Date.UTC(val[0], val[1] - 1, val[2], time[0][0], time[0][1], time[0][2] || 0); // if(time[1]){ // ret += time[1]; // } // } // return ret; // }, // dateToString: function(date, _getParsed){ // return typeProtos.date.dateToString(date) +'T'+ typeProtos.time.dateToString(date, _getParsed); // } // } }; if(typeBugs || !supportsType('range') || !supportsType('time')){ typeProtos.range = $.extend({}, typeProtos.number, typeProtos.range); // typeProtos.time = $.extend({}, typeProtos.date, typeProtos.time); // typeProtos['datetime-local'] = $.extend({}, typeProtos.date, typeProtos.time, typeProtos['datetime-local']); } if(typeBugs || !supportsType('number')){ webshims.addInputType('number', typeProtos.number); } if(typeBugs || !supportsType('range')){ webshims.addInputType('range', typeProtos.range); } if(typeBugs || !supportsType('date')){ webshims.addInputType('date', typeProtos.date); } // if(typeBugs || !supportsType('time')){ // webshims.addInputType('time', typeProtos.time); // } // if(typeBugs || !supportsType('datetime-local')){ // webshims.addInputType('datetime-local', typeProtos['datetime-local']); // } });