X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=modules%2Fluci-base%2Fhtdocs%2Fluci-static%2Fresources%2Fcbi.js;h=6c35372cddd7560b6d76690f8dc1e0ac04b6e999;hp=4b7227f1a4e959c225dfa0fecac9e78a64d58c19;hb=9dcf73b323fa97534a931e3acfbab8b03ce3961f;hpb=f25c4e07bc9642e0491d3a4ab3942a69545891e9 diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index 4b7227f1a..6c35372cd 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -13,28 +13,92 @@ var cbi_d = []; var cbi_t = []; -var cbi_c = []; +var cbi_strings = { path: {}, label: {} }; + +function Int(x) { + return (/^-?\d+$/.test(x) ? +x : NaN); +} + +function Dec(x) { + return (/^-?\d+(?:\.\d+)?$/.test(x) ? +x : NaN); +} + +function IPv4(x) { + if (!x.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) + return null; + + if (RegExp.$1 > 255 || RegExp.$2 > 255 || RegExp.$3 > 255 || RegExp.$4 > 255) + return null; + + return [ +RegExp.$1, +RegExp.$2, +RegExp.$3, +RegExp.$4 ]; +} + +function IPv6(x) { + if (x.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) { + var v6 = RegExp.$1, v4 = IPv4(RegExp.$2); + + if (!v4) + return null; + + x = v6 + ':' + (v4[0] * 256 + v4[1]).toString(16) + + ':' + (v4[2] * 256 + v4[3]).toString(16); + } + + if (!x.match(/^[a-fA-F0-9:]+$/)) + return null; + + var prefix_suffix = x.split(/::/); + + if (prefix_suffix.length > 2) + return null; + + var prefix = (prefix_suffix[0] || '0').split(/:/); + var suffix = prefix_suffix.length > 1 ? (prefix_suffix[1] || '0').split(/:/) : []; + + if (suffix.length ? (prefix.length + suffix.length > 7) : (prefix.length > 8)) + return null; + + var i, word; + var words = []; + + for (i = 0, word = parseInt(prefix[0], 16); i < prefix.length; word = parseInt(prefix[++i], 16)) + if (prefix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF) + words.push(word); + else + return null; + + for (i = 0; i < (8 - prefix.length - suffix.length); i++) + words.push(0); + + for (i = 0, word = parseInt(suffix[0], 16); i < suffix.length; word = parseInt(suffix[++i], 16)) + if (suffix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF) + words.push(word); + else + return null; + + return words; +} var cbi_validators = { 'integer': function() { - return (this.match(/^-?[0-9]+$/) != null); + return !!Int(this); }, 'uinteger': function() { - return (cbi_validators.integer.apply(this) && (this >= 0)); + return (Int(this) >= 0); }, 'float': function() { - return !isNaN(parseFloat(this)); + return !!Dec(this); }, 'ufloat': function() { - return (cbi_validators['float'].apply(this) && (this >= 0)); + return (Dec(this) >= 0); }, 'ipaddr': function() @@ -45,93 +109,101 @@ var cbi_validators = { 'ip4addr': function() { - if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/)) - { - return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) && - (RegExp.$2 >= 0) && (RegExp.$2 <= 255) && - (RegExp.$3 >= 0) && (RegExp.$3 <= 255) && - (RegExp.$4 >= 0) && (RegExp.$4 <= 255) && - ((RegExp.$6.indexOf('.') < 0) - ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)) - : (cbi_validators.ip4addr.apply(RegExp.$6))) - ; - } - - return false; + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\/(\d{1,2}))?$/); + return !!(m && IPv4(m[1]) && (m[2] ? IPv4(m[2]) : (m[3] ? cbi_validators.ip4prefix.apply(m[3]) : true))); }, 'ip6addr': function() { - if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) ) - { - if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) ) - { - var addr = RegExp.$1; + var m = this.match(/^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/); + return !!(m && IPv6(m[1]) && (m[2] ? cbi_validators.ip6prefix.apply(m[2]) : true)); + }, - if( addr == '::' ) - { - return true; - } + 'ip4prefix': function() + { + return !isNaN(this) && this >= 0 && this <= 32; + }, - if( addr.indexOf('.') > 0 ) - { - var off = addr.lastIndexOf(':'); + 'ip6prefix': function() + { + return !isNaN(this) && this >= 0 && this <= 128; + }, - if( !(off && cbi_validators.ip4addr.apply(addr.substr(off+1))) ) - return false; + 'cidr': function() + { + return cbi_validators.cidr4.apply(this) || + cbi_validators.cidr6.apply(this); + }, - addr = addr.substr(0, off) + ':0:0'; - } + 'cidr4': function() + { + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/); + return !!(m && IPv4(m[1]) && cbi_validators.ip4prefix.apply(m[2])); + }, - if( addr.indexOf('::') >= 0 ) - { - var colons = 0; - var fill = '0'; + 'cidr6': function() + { + var m = this.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/); + return !!(m && IPv6(m[1]) && cbi_validators.ip6prefix.apply(m[2])); + }, - for( var i = 1; i < (addr.length-1); i++ ) - if( addr.charAt(i) == ':' ) - colons++; + 'ipnet4': function() + { + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/); + return !!(m && IPv4(m[1]) && IPv4(m[2])); + }, - if( colons > 7 ) - return false; + 'ipnet6': function() + { + var m = this.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/); + return !!(m && IPv6(m[1]) && IPv6(m[2])); + }, - for( var i = 0; i < (7 - colons); i++ ) - fill += ':0'; + 'ip6hostid': function() + { + if (this == "eui64" || this == "random") + return true; - if (addr.match(/^(.*?)::(.*?)$/)) - addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill + - (RegExp.$2 ? ':' + RegExp.$2 : ''); - } + var v6 = IPv6(this); + return !(!v6 || v6[0] || v6[1] || v6[2] || v6[3]); + }, - return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null); - } - } + 'ipmask': function() + { + return cbi_validators.ipmask4.apply(this) || + cbi_validators.ipmask6.apply(this); + }, - return false; + 'ipmask4': function() + { + return cbi_validators.cidr4.apply(this) || + cbi_validators.ipnet4.apply(this) || + cbi_validators.ip4addr.apply(this); + }, + + 'ipmask6': function() + { + return cbi_validators.cidr6.apply(this) || + cbi_validators.ipnet6.apply(this) || + cbi_validators.ip6addr.apply(this); }, 'port': function() { - return cbi_validators.integer.apply(this) && - (this >= 0) && (this <= 65535); + var p = Int(this); + return (p >= 0 && p <= 65535); }, 'portrange': function() { if (this.match(/^(\d+)-(\d+)$/)) { - var p1 = RegExp.$1; - var p2 = RegExp.$2; - - return cbi_validators.port.apply(p1) && - cbi_validators.port.apply(p2) && - (parseInt(p1) <= parseInt(p2)) - ; - } - else - { - return cbi_validators.port.apply(this); + var p1 = +RegExp.$1; + var p2 = +RegExp.$2; + return (p1 <= p2 && p2 <= 65535); } + + return cbi_validators.port.apply(this); }, 'macaddr': function() @@ -139,18 +211,20 @@ var cbi_validators = { return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null); }, - 'host': function() + 'host': function(ipv4only) { return cbi_validators.hostname.apply(this) || - cbi_validators.ipaddr.apply(this); + ((ipv4only != 1) && cbi_validators.ipaddr.apply(this)) || + ((ipv4only == 1) && cbi_validators.ip4addr.apply(this)); }, - 'hostname': function() + 'hostname': function(strict) { if (this.length <= 253) - return (this.match(/^[a-zA-Z0-9]+$/) != null || + return (this.match(/^[a-zA-Z0-9_]+$/) != null || (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) && - this.match(/[^0-9.]/))); + this.match(/[^0-9.]/))) && + (!strict || !this.match(/^_/)); return false; }, @@ -161,28 +235,49 @@ var cbi_validators = { cbi_validators.host.apply(this); }, - 'hostport': function() + 'hostport': function(ipv4only) { var hp = this.split(/:/); if (hp.length == 2) - return (cbi_validators.host.apply(hp[0]) && + return (cbi_validators.host.apply(hp[0], ipv4only) && cbi_validators.port.apply(hp[1])); return false; }, - 'ipaddrport': function() + 'ip4addrport': function() { var hp = this.split(/:/); if (hp.length == 2) return (cbi_validators.ipaddr.apply(hp[0]) && cbi_validators.port.apply(hp[1])); - return false; }, + 'ipaddrport': function(bracket) + { + if (this.match(/^([^\[\]:]+):([^:]+)$/)) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip4addr.apply(addr) && + cbi_validators.port.apply(port)); + } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip6addr.apply(addr) && + cbi_validators.port.apply(port)); + } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip6addr.apply(addr) && + cbi_validators.port.apply(port)); + } else { + return false; + } + }, + 'wpakey': function() { var v = this; @@ -213,56 +308,34 @@ var cbi_validators = { 'range': function(min, max) { - var val = parseFloat(this); - if (!isNaN(min) && !isNaN(max) && !isNaN(val)) - return ((val >= min) && (val <= max)); - - return false; + var val = Dec(this); + return (val >= +min && val <= +max); }, 'min': function(min) { - var val = parseFloat(this); - if (!isNaN(min) && !isNaN(val)) - return (val >= min); - - return false; + return (Dec(this) >= +min); }, 'max': function(max) { - var val = parseFloat(this); - if (!isNaN(max) && !isNaN(val)) - return (val <= max); - - return false; + return (Dec(this) <= +max); }, 'rangelength': function(min, max) { var val = '' + this; - if (!isNaN(min) && !isNaN(max)) - return ((val.length >= min) && (val.length <= max)); - - return false; + return ((val.length >= +min) && (val.length <= +max)); }, 'minlength': function(min) { - var val = '' + this; - if (!isNaN(min)) - return (val.length >= min); - - return false; + return ((''+this).length >= +min); }, 'maxlength': function(max) { - var val = '' + this; - if (!isNaN(max)) - return (val.length <= max); - - return false; + return ((''+this).length <= +max); }, 'or': function() @@ -323,7 +396,7 @@ var cbi_validators = { { return (this.match(/^[0-9\*#!\.]+$/) != null); }, - 'timehhmmss': function() + 'timehhmmss': function() { return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null); }, @@ -367,12 +440,12 @@ var cbi_validators = { }; -function cbi_d_add(field, dep, next) { - var obj = document.getElementById(field); +function cbi_d_add(field, dep, index) { + var obj = (typeof(field) === 'string') ? document.getElementById(field) : field; if (obj) { var entry for (var i=0; i entry.index) { + break; + } + } + if (!next) { parent.appendChild(entry.node); } else { - next.parentNode.insertBefore(entry.node, next); + parent.insertBefore(entry.node, next); } + state = true; - if( entry.parent ) - cbi_c[entry.parent]++; } + + // hide optionals widget if no choices remaining + if (parent && parent.parentNode && parent.getAttribute('data-optionals')) + parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : ''; } if (entry && entry.parent) { @@ -477,6 +559,84 @@ function cbi_d_update() { } } +function cbi_init() { + var nodes; + + nodes = document.querySelectorAll('[data-strings]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + var str = JSON.parse(node.getAttribute('data-strings')); + for (var key in str) { + for (var key2 in str[key]) { + var dst = cbi_strings[key] || (cbi_strings[key] = { }); + dst[key2] = str[key][key2]; + } + } + } + + nodes = document.querySelectorAll('[data-depends]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + var index = parseInt(node.getAttribute('data-index'), 10); + var depends = JSON.parse(node.getAttribute('data-depends')); + if (!isNaN(index) && depends.length > 0) { + for (var alt = 0; alt < depends.length; alt++) { + cbi_d_add(node, depends[alt], index); + } + } + } + + nodes = document.querySelectorAll('[data-update]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + var events = node.getAttribute('data-update').split(' '); + for (var j = 0, event; (event = events[j]) !== undefined; j++) { + cbi_bind(node, event, cbi_d_update); + } + } + + nodes = document.querySelectorAll('[data-choices]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + var choices = JSON.parse(node.getAttribute('data-choices')); + var options = {}; + + for (var j = 0; j < choices[0].length; j++) + options[choices[0][j]] = choices[1][j]; + + var def = (node.getAttribute('data-optional') === 'true') + ? node.placeholder || '' : null; + + cbi_combobox_init(node, options, def, + node.getAttribute('data-manual')); + } + + nodes = document.querySelectorAll('[data-dynlist]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + var choices = JSON.parse(node.getAttribute('data-dynlist')); + var options = null; + + if (choices[0] && choices[0].length) { + options = {}; + + for (var j = 0; j < choices[0].length; j++) + options[choices[0][j]] = choices[1][j]; + } + + cbi_dynlist_init(node, choices[2], choices[3], options); + } + + nodes = document.querySelectorAll('[data-type]'); + + for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { + cbi_validate_field(node, node.getAttribute('data-optional') === 'true', + node.getAttribute('data-type')); + } + + cbi_d_update(); +} + function cbi_bind(obj, type, callback, mode) { if (!obj.addEventListener) { obj.attachEvent('on' + type, @@ -495,7 +655,7 @@ function cbi_bind(obj, type, callback, mode) { return obj; } -function cbi_combobox(id, values, def, man) { +function cbi_combobox(id, values, def, man, focus) { var selid = "cbi.combobox." + id; if (document.getElementById(selid)) { return @@ -504,6 +664,7 @@ function cbi_combobox(id, values, def, man) { var obj = document.getElementById(id) var sel = document.createElement("select"); sel.id = selid; + sel.index = obj.index; sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select'); if (obj.nextSibling) { @@ -515,14 +676,11 @@ function cbi_combobox(id, values, def, man) { var dt = obj.getAttribute('cbi_datatype'); var op = obj.getAttribute('cbi_optional'); - if (dt) - cbi_validate_field(sel, op == 'true', dt); - if (!values[obj.value]) { if (obj.value == "") { var optdef = document.createElement("option"); optdef.value = ""; - optdef.appendChild(document.createTextNode(def)); + optdef.appendChild(document.createTextNode(typeof(def) === 'string' ? def : cbi_strings.label.choose)); sel.appendChild(optdef); } else { var opt = document.createElement("option"); @@ -547,14 +705,18 @@ function cbi_combobox(id, values, def, man) { var optman = document.createElement("option"); optman.value = ""; - optman.appendChild(document.createTextNode(man)); + optman.appendChild(document.createTextNode(typeof(man) === 'string' ? man : cbi_strings.label.custom)); sel.appendChild(optman); obj.style.display = "none"; + if (dt) + cbi_validate_field(sel, op == 'true', dt); + cbi_bind(sel, "change", function() { if (sel.selectedIndex == sel.options.length - 1) { obj.style.display = "inline"; + sel.blur(); sel.parentNode.removeChild(sel); obj.focus(); } else { @@ -569,32 +731,34 @@ function cbi_combobox(id, values, def, man) { }) // Retrigger validation in select - sel.focus(); - sel.blur(); + if (focus) { + sel.focus(); + sel.blur(); + } } function cbi_combobox_init(id, values, def, man) { - var obj = document.getElementById(id); + var obj = (typeof(id) === 'string') ? document.getElementById(id) : id; cbi_bind(obj, "blur", function() { - cbi_combobox(id, values, def, man) + cbi_combobox(obj.id, values, def, man, true); }); - cbi_combobox(id, values, def, man); + cbi_combobox(obj.id, values, def, man, false); } -function cbi_filebrowser(id, url, defpath) { +function cbi_filebrowser(id, defpath) { var field = document.getElementById(id); var browser = window.open( - url + ( field.value || defpath || '' ) + '?field=' + id, + cbi_strings.path.browser + ( field.value || defpath || '' ) + '?field=' + id, "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes" ); browser.focus(); } -function cbi_browser_init(id, respath, url, defpath) +function cbi_browser_init(id, resource, defpath) { function cbi_browser_btnclick(e) { - cbi_filebrowser(id, url, defpath); + cbi_filebrowser(id, defpath); return false; } @@ -602,18 +766,16 @@ function cbi_browser_init(id, respath, url, defpath) var btn = document.createElement('img'); btn.className = 'cbi-image-button'; - btn.src = respath + '/cbi/folder.gif'; + btn.src = (resource || cbi_strings.path.resource) + '/cbi/folder.gif'; field.parentNode.insertBefore(btn, field.nextSibling); cbi_bind(btn, 'click', cbi_browser_btnclick); } -function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choices) +function cbi_dynlist_init(parent, datatype, optional, choices) { - var input0 = document.getElementsByName(name)[0]; - var prefix = input0.name; - var parent = input0.parentNode; - var holder = input0.placeholder; + var prefix = parent.getAttribute('data-prefix'); + var holder = parent.getAttribute('data-placeholder'); var values; @@ -624,7 +786,7 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic while (parent.firstChild) { var n = parent.firstChild; - var i = parseInt(n.index); + var i = +n.index; if (i != del) { @@ -664,14 +826,14 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic } var b = document.createElement('img'); - b.src = respath + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif'); + b.src = cbi_strings.path.resource + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif'); b.className = 'cbi-image-button'; parent.appendChild(t); parent.appendChild(b); if (datatype == 'file') { - cbi_browser_init(t.id, respath, url, defpath); + cbi_browser_init(t.id, null, parent.getAttribute('data-browser-path')); } parent.appendChild(document.createElement('br')); @@ -683,7 +845,7 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic if (choices) { - cbi_combobox_init(t.id, choices[0], '', choices[1]); + cbi_combobox_init(t.id, choices, '', cbi_strings.label.custom); b.index = i; cbi_bind(b, 'keydown', cbi_dynlist_keydown); @@ -763,15 +925,15 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic se = se.parentNode; var prev = se.previousSibling; - while (prev && prev.name != name) + while (prev && prev.name != prefix) prev = prev.previousSibling; var next = se.nextSibling; - while (next && next.name != name) + while (next && next.name != prefix) next = next.nextSibling; /* advance one further in combobox case */ - if (next && next.nextSibling.name == name) + if (next && next.nextSibling.name == prefix) next = next.nextSibling; switch (ev.keyCode) @@ -827,7 +989,7 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic var se = ev.target ? ev.target : ev.srcElement; var input = se.previousSibling; - while (input && input.name != name) { + while (input && input.name != prefix) { input = input.previousSibling; } @@ -854,27 +1016,6 @@ function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choic cbi_dynlist_redraw(NaN, -1, -1); } -//Hijacks the CBI form to send via XHR (requires Prototype) -function cbi_hijack_forms(layer, win, fail, load) { - var forms = layer.getElementsByTagName('form'); - for (var i=0; i 0 ) + if (hl_tabs.length > 0) window.setTimeout(function() { for( var i = 0; i < hl_tabs.length; i++ ) hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, ''); @@ -1230,6 +1372,9 @@ String.prototype.format = function() var quot_esc = [/"/g, '"', /'/g, ''']; function esc(s, r) { + if (typeof(s) !== 'string' && !(s instanceof String)) + return ''; + for( var i = 0; i < r.length; i += 2 ) s = s.replace(r[i], r[i+1]); return s; @@ -1240,7 +1385,7 @@ String.prototype.format = function() var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/; var a = b = [], numSubstitutions = 0, numMatches = 0; - while( a = re.exec(str) ) + while (a = re.exec(str)) { var m = a[1]; var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5]; @@ -1263,6 +1408,8 @@ String.prototype.format = function() pad = leftpart.substr(1,1); else if (pPad) pad = pPad; + else + pad = ' '; var justifyRight = true; if (pJustify && pJustify === "-") @@ -1270,40 +1417,40 @@ String.prototype.format = function() var minLength = -1; if (pMinLength) - minLength = parseInt(pMinLength); + minLength = +pMinLength; var precision = -1; if (pPrecision && pType == 'f') - precision = parseInt(pPrecision.substring(1)); + precision = +pPrecision.substring(1); var subst = param; switch(pType) { case 'b': - subst = (parseInt(param) || 0).toString(2); + subst = (+param || 0).toString(2); break; case 'c': - subst = String.fromCharCode(parseInt(param) || 0); + subst = String.fromCharCode(+param || 0); break; case 'd': - subst = (parseInt(param) || 0); + subst = ~~(+param || 0); break; case 'u': - subst = Math.abs(parseInt(param) || 0); + subst = ~~Math.abs(+param || 0); break; case 'f': subst = (precision > -1) - ? ((parseFloat(param) || 0.0)).toFixed(precision) - : (parseFloat(param) || 0.0); + ? ((+param || 0.0)).toFixed(precision) + : (+param || 0.0); break; case 'o': - subst = (parseInt(param) || 0).toString(8); + subst = (+param || 0).toString(8); break; case 's': @@ -1311,11 +1458,11 @@ String.prototype.format = function() break; case 'x': - subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase(); + subst = ('' + (+param || 0).toString(16)).toLowerCase(); break; case 'X': - subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase(); + subst = ('' + (+param || 0).toString(16)).toUpperCase(); break; case 'h': @@ -1358,22 +1505,32 @@ String.prototype.format = function() break; case 'm': - var mf = pMinLength ? parseInt(pMinLength) : 1000; - var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2; + var mf = pMinLength ? +pMinLength : 1000; + var pr = pPrecision ? ~~(10 * +('0' + pPrecision)) : 2; var i = 0; - var val = parseFloat(param || 0); - var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ]; + var val = (+param || 0); + var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ]; for (i = 0; (i < units.length) && (val > mf); i++) val /= mf; - subst = val.toFixed(pr) + ' ' + units[i]; + subst = (i ? val.toFixed(pr) : val) + units[i]; + pMinLength = null; break; } } } + if (pMinLength) { + subst = subst.toString(); + for (var i = subst.length; i < pMinLength; i++) + if (pJustify == '-') + subst = subst + ' '; + else + subst = pad + subst; + } + out += leftpart + subst; str = str.substr(m.length); }