From 01a229011a9318cb1c7b39db73ac8ee0d946ac5a Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Wed, 11 Nov 2009 17:32:19 -0500 Subject: [PATCH] Avoid repeating declaration statements. --- CHANGELOG | 2 ++ src/dom/dom.js | 72 ++++++++++++++++++++++---------------------- src/dom/selector.js | 16 +++++----- src/lang/class.js | 4 +-- src/lang/string.js | 9 +++--- src/lang/template.js | 5 +-- src/prototype.js | 6 ++-- 7 files changed, 60 insertions(+), 54 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2d4e595..10548fe 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Avoid repeating declaration statements where it makes sense, for slightly better runtime performance and minification. (kangax) + * Make `Event.stopObserving` return element in all cases. [#810 state:resolved] (Yaffle, Tobie Langel) * String#startsWith, String#endsWith performance optimization (Yaffle, Tobie Langel, kangax) diff --git a/src/dom/dom.js b/src/dom/dom.js index 0f0e247..ae9bc21 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -115,9 +115,9 @@ if (!Node.ELEMENT_NODE) { // setAttribute is broken in IE (particularly when setting name attribute) // see: http://msdn.microsoft.com/en-us/library/ms536389.aspx var SETATTRIBUTE_IGNORES_NAME = (function(){ - var elForm = document.createElement("form"); - var elInput = document.createElement("input"); - var root = document.documentElement; + var elForm = document.createElement("form"), + elInput = document.createElement("input"), + root = document.documentElement; elInput.setAttribute("name", "test"); elForm.appendChild(elInput); root.appendChild(elForm); @@ -443,8 +443,9 @@ Element.Methods = { element = $(element); var result = '<' + element.tagName.toLowerCase(); $H({'id': 'id', 'className': 'class'}).each(function(pair) { - var property = pair.first(), attribute = pair.last(); - var value = (element[property] || '').toString(); + var property = pair.first(), + attribute = pair.last(), + value = (element[property] || '').toString(); if (value) result += ' ' + attribute + '=' + value.inspect(true); }); return result + '>'; @@ -1055,16 +1056,16 @@ Element.Methods = { // All *Width and *Height properties give 0 on elements with display none, // so enable the element temporarily - var els = element.style; - var originalVisibility = els.visibility; - var originalPosition = els.position; - var originalDisplay = els.display; + var els = element.style, + originalVisibility = els.visibility, + originalPosition = els.position, + originalDisplay = els.display; els.visibility = 'hidden'; if (originalPosition != 'fixed') // Switching fixed to absolute causes issues in Safari els.position = 'absolute'; els.display = 'block'; - var originalWidth = element.clientWidth; - var originalHeight = element.clientHeight; + var originalWidth = element.clientWidth, + originalHeight = element.clientHeight; els.display = originalDisplay; els.position = originalPosition; els.visibility = originalVisibility; @@ -1209,11 +1210,11 @@ Element.Methods = { element = $(element); if (Element.getStyle(element, 'position') == 'absolute') return element; - var offsets = Element.positionedOffset(element); - var top = offsets[1]; - var left = offsets[0]; - var width = element.clientWidth; - var height = element.clientHeight; + var offsets = Element.positionedOffset(element), + top = offsets[1], + left = offsets[0], + width = element.clientWidth, + height = element.clientHeight; element._originalLeft = left - parseFloat(element.style.left || 0); element._originalTop = top - parseFloat(element.style.top || 0); @@ -1241,8 +1242,8 @@ Element.Methods = { if (Element.getStyle(element, 'position') == 'relative') return element; element.style.position = 'relative'; - var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); - var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); + var top = parseFloat(element.style.top || 0) - (element._originalTop || 0), + left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); element.style.top = top + 'px'; element.style.left = left + 'px'; @@ -1310,9 +1311,10 @@ Element.Methods = { * as properties: `{ left: leftValue, top: topValue }`. **/ viewportOffset: function(forElement) { - var valueT = 0, valueL = 0; - - var element = forElement; + var valueT = 0, + valueL = 0, + element = forElement; + do { valueT += element.offsetTop || 0; valueL += element.offsetLeft || 0; @@ -1403,12 +1405,11 @@ Element.Methods = { // find page position of source source = $(source); - var p = Element.viewportOffset(source); + var p = Element.viewportOffset(source), delta = [0, 0], parent = null; // find coordinate system to use element = $(element); - var delta = [0, 0]; - var parent = null; + // delta [0,0] will do fine with position: fixed elements, // position:absolute needs offsetParent deltas if (Element.getStyle(element, 'position') == 'absolute') { @@ -1618,10 +1619,9 @@ else if (Prototype.Browser.IE) { Element._attributeTranslations = (function(){ - var classProp = 'className'; - var forProp = 'for'; - - var el = document.createElement('div'); + var classProp = 'className', + forProp = 'for', + el = document.createElement('div'); // try "className" first (IE <8) el.setAttribute(classProp, 'x'); @@ -1666,10 +1666,9 @@ else if (Prototype.Browser.IE) { }, _getEv: (function(){ - var el = document.createElement('div'); + var el = document.createElement('div'), f; el.onclick = Prototype.emptyFunction; var value = el.getAttribute('onclick'); - var f; // IE<8 if (String(value).indexOf('{') > -1) { @@ -1846,8 +1845,8 @@ if ('outerHTML' in document.documentElement) { var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); if (Element._insertionTranslations.tags[tagName]) { - var nextSibling = element.next(); - var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); + var nextSibling = element.next(), + fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); parent.removeChild(element); if (nextSibling) fragments.each(function(node) { parent.insertBefore(node, nextSibling) }); @@ -1962,8 +1961,8 @@ Element.extend = (function() { if (typeof window.Element != 'undefined') { var proto = window.Element.prototype; if (proto) { - var id = '_' + (Math.random()+'').slice(2); - var el = document.createElement(tagName); + var id = '_' + (Math.random()+'').slice(2), + el = document.createElement(tagName); proto[id] = 'x'; var isBuggy = (el[id] !== 'x'); delete proto[id]; @@ -2228,8 +2227,9 @@ Element.addMethods = function(methods) { klass = 'HTML' + tagName.capitalize() + 'Element'; if (window[klass]) return window[klass]; - var element = document.createElement(tagName); - var proto = element['__proto__'] || element.constructor.prototype; + var element = document.createElement(tagName), + proto = element['__proto__'] || element.constructor.prototype; + element = null; return proto; } diff --git a/src/dom/selector.js b/src/dom/selector.js index 7692a62..7ba2942 100644 --- a/src/dom/selector.js +++ b/src/dom/selector.js @@ -349,8 +349,8 @@ Object.extend(Selector, { return '[' + fragment + "= " + mm[1] + ']'; if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b if (mm[1] == "-") mm[1] = -1; - var a = mm[1] ? Number(mm[1]) : 1; - var b = mm[2] ? Number(mm[2]) : 0; + var a = mm[1] ? Number(mm[1]) : 1, + b = mm[2] ? Number(mm[2]) : 0; predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + "((#{fragment} - #{b}) div #{a} >= 0)]"; return new Template(predicate).evaluate({ @@ -716,9 +716,11 @@ Object.extend(Selector, { if (node.nodeIndex == formula) results.push(node); } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b if (m[1] == "-") m[1] = -1; - var a = m[1] ? Number(m[1]) : 1; - var b = m[2] ? Number(m[2]) : 0; - var indices = Selector.pseudos.getIndices(a, b, nodes.length); + + var a = m[1] ? Number(m[1]) : 1, + b = m[2] ? Number(m[2]) : 0, + indices = Selector.pseudos.getIndices(a, b, nodes.length); + for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { for (var j = 0; j < l; j++) if (node.nodeIndex == indices[j]) results.push(node); @@ -739,8 +741,8 @@ Object.extend(Selector, { }, 'not': function(nodes, selector, root) { - var h = Selector.handlers, selectorType, m; - var exclusions = new Selector(selector).findElements(root); + var h = Selector.handlers, selectorType, m, + exclusions = new Selector(selector).findElements(root); h.mark(exclusions); for (var i = 0, results = [], node; node = nodes[i]; i++) if (!node._countedByPrototype) results.push(node); diff --git a/src/lang/class.js b/src/lang/class.js index 68b1fe6..1ac0750 100644 --- a/src/lang/class.js +++ b/src/lang/class.js @@ -135,8 +135,8 @@ var Class = (function() { * //-> alerts "You should probably run. He looks really mad." **/ function addMethods(source) { - var ancestor = this.superclass && this.superclass.prototype; - var properties = Object.keys(source); + var ancestor = this.superclass && this.superclass.prototype, + properties = Object.keys(source); // IE6 doesn't enumerate toString and valueOf properties, // Force copy if they're not coming from Object.prototype. diff --git a/src/lang/string.js b/src/lang/string.js index 26c3cf2..28b3f22 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -167,8 +167,8 @@ Object.extend(String.prototype, (function() { * returns them as an array of strings. **/ function extractScripts() { - var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); - var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); + var matchAll = new RegExp(Prototype.ScriptFragment, 'img'), + matchOne = new RegExp(Prototype.ScriptFragment, 'im'); return (this.match(matchAll) || []).map(function(scriptTag) { return (scriptTag.match(matchOne) || ['', ''])[1]; }); @@ -251,8 +251,9 @@ Object.extend(String.prototype, (function() { return match[1].split(separator || '&').inject({ }, function(hash, pair) { if ((pair = pair.split('='))[0]) { - var key = decodeURIComponent(pair.shift()); - var value = pair.length > 1 ? pair.join('=') : pair[0]; + var key = decodeURIComponent(pair.shift()), + value = pair.length > 1 ? pair.join('=') : pair[0]; + if (value != undefined) value = decodeURIComponent(value); if (key in hash) { diff --git a/src/lang/template.js b/src/lang/template.js index cb9020d..8b396a8 100644 --- a/src/lang/template.js +++ b/src/lang/template.js @@ -133,8 +133,9 @@ var Template = Class.create({ var before = match[1] || ''; if (before == '\\') return match[2]; - var ctx = object, expr = match[3]; - var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; + var ctx = object, expr = match[3], + pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; + match = pattern.exec(expr); if (match == null) return before; diff --git a/src/prototype.js b/src/prototype.js index 21089a4..5f78f78 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -35,9 +35,9 @@ var Prototype = { if (typeof window.HTMLDivElement !== 'undefined') return true; - var div = document.createElement('div'); - var form = document.createElement('form'); - var isSupported = false; + var div = document.createElement('div'), + form = document.createElement('form'), + isSupported = false; if (div['__proto__'] && (div['__proto__'] !== form['__proto__'])) { isSupported = true;