Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

chunk of code was indented too far

  • Loading branch information...
commit 6b9250eeac0e92ebb5a6a032ab46dddcf769d013 1 parent 52185d2
@trevnorris trevnorris authored peol committed
Showing with 141 additions and 144 deletions.
  1. +141 −144 myQuery.js
View
285 myQuery.js
@@ -54,191 +54,188 @@
myQuery.last()
*/
var myQuery = function(selector) {
-
- // Initializes this instance of myQuery using
- // a provided selector
- this.init = function(selector) {
- // As per jQuery, handle myQuery('')
- // myQuery(null) or myQuery(undefined)
- // by returning the instance if no
- // valid selectors are provided
- if (!selector) {
- return this;
- }
-
- // Handle selections (ID, Class, all others, DOM nodes)
- // This is extremely simplified, but I want to avoid getting it too complex
- // It's not supposed to demonstrate Sizzle's level of interpretation by any
- // means. Just something understandable. Perhaps cover a few more cases?
- // DOM nodes
- if (selector.nodeType) {
- this.selection = selector;
+ // Initializes this instance of myQuery using
+ // a provided selector
+ this.init = function(selector) {
+ // As per jQuery, handle myQuery('')
+ // myQuery(null) or myQuery(undefined)
+ // by returning the instance if no
+ // valid selectors are provided
+ if (!selector) {
+ return this;
+ }
+
+ // Handle selections (ID, Class, all others, DOM nodes)
+ // This is extremely simplified, but I want to avoid getting it too complex
+ // It's not supposed to demonstrate Sizzle's level of interpretation by any
+ // means. Just something understandable. Perhaps cover a few more cases?
+ // DOM nodes
+ if (selector.nodeType) {
+ this.selection = selector;
+ } else {
+ // Class selector
+ if (selector[0] === '.') {
+ this.selection = document.getElementsByClassName(selector.slice(1, selector.length));
+ } else if (selector[0] === '#') {
+ // ID selector: only grab the portion after # if using getElementById (faster than qSA)
+ this.selection = document.getElementById(selector.slice(1, selector.length));
} else {
- // Class selector
- if (selector[0] === '.') {
- this.selection = document.getElementsByClassName(selector.slice(1, selector.length));
- } else if (selector[0] === '#') {
- // ID selector: only grab the portion after # if using getElementById (faster than qSA)
- this.selection = document.getElementById(selector.slice(1, selector.length));
- } else {
- // All others
- this.selection = document.querySelectorAll(selector);
- }
+ // All others
+ this.selection = document.querySelectorAll(selector);
}
+ }
- // Set the local length to the selection length
- this.length = this.selection.length || 0;
- };
+ // Set the local length to the selection length
+ this.length = this.selection.length || 0;
+ };
- // Initialize this internal instance
- this.init(selector);
+ // Initialize this internal instance
+ this.init(selector);
- // Use the load-time configuration pattern
- // to cache what events are best applicable for
- // event handling with the current browser
- this.addListener = handleListenerAssignment.addListener;
- this.removeListener = handleListenerAssignment.removeListener;
+ // Use the load-time configuration pattern
+ // to cache what events are best applicable for
+ // event handling with the current browser
+ this.addListener = handleListenerAssignment.addListener;
+ this.removeListener = handleListenerAssignment.removeListener;
- // Document ready handler
- // TODO: Could probably be a one-time thing and check if a private var `isReady`
- // With this, anything triggered after window `load` event won't be invoked
- this.ready = function(callback) {
- this.addListener(window, 'load', callback);
- };
+ // Document ready handler
+ // TODO: Could probably be a one-time thing and check if a private var `isReady`
+ // With this, anything triggered after window `load` event won't be invoked
+ this.ready = function(callback) {
+ this.addListener(window, 'load', callback);
+ };
- // Set attribute values
- // E.g.: el.attr(prop, val);
- // We call .access to access properties within a specified context
- // so if I pass in `attr`, that means this is an attribute operation
- // `this.selection` is the `context` and `prop` is the value to return.
- // Passing in `val` will make this behave as a setter.
- // TODO: A bit rough, needs more work (probably)
- this.attr = function(prop, val) {
- if (prop && this.length) {
- if (val === undefined) {
- //attribute getter
- return this.access('attr', this.selection, prop);
- } else {
- //attribute setter
- this.access('attr', this.selection, prop, val);
- }
+ // Set attribute values
+ // E.g.: el.attr(prop, val);
+ // We call .access to access properties within a specified context
+ // so if I pass in `attr`, that means this is an attribute operation
+ // `this.selection` is the `context` and `prop` is the value to return.
+ // Passing in `val` will make this behave as a setter.
+ // TODO: A bit rough, needs more work (probably)
+ this.attr = function(prop, val) {
+ if (prop && this.length) {
+ if (val === undefined) {
+ //attribute getter
+ return this.access('attr', this.selection, prop);
+ } else {
+ //attribute setter
+ this.access('attr', this.selection, prop, val);
}
+ }
- return this;
- };
-
-
- // Set CSS property values
- // E.g.: el.css(prop, val);
- this.css = function(prop, val) {
- if (prop) {
- // No value means getting CSS property value
- if (val === undefined) {
- return this.access('css', this.selection, prop);
+ return this;
+ };
- } else {
- // Check if setter arguments valid
- if (this.length) {
- // Handle multiple elements
- for (i = 0; i < this.length; i++) {
- this.access('css', this.selection[i].style, prop, val);
- }
- } else {
- // Handle single elements without the need to iterate
- this.access('css', this.selection.style, prop, val);
+ // Set CSS property values
+ // E.g.: el.css(prop, val);
+ this.css = function(prop, val) {
+ if (prop) {
+ // No value means getting CSS property value
+ if (val === undefined) {
+ return this.access('css', this.selection, prop);
+ } else {
+ // Check if setter arguments valid
+ if (this.length) {
+ // Handle multiple elements
+ for (i = 0; i < this.length; i++) {
+ this.access('css', this.selection[i].style, prop, val);
}
- return this;
+ } else {
+ // Handle single elements without the need to iterate
+ this.access('css', this.selection.style, prop, val);
+
}
+
+ return this;
}
- };
+ }
+ };
- // A generic property setter and getter
- this.access = function(mode, context, prop, val) {
- if (context && prop && val !== undefined) {
- context[prop] = val;
- } else {
+ // A generic property setter and getter
+ this.access = function(mode, context, prop, val) {
+ if (context && prop && val !== undefined) {
+ context[prop] = val;
+ } else {
- // Getter/setter modes for CSS or attributes
- if (mode === 'css') {
- var y = "";
+ // Getter/setter modes for CSS or attributes
+ if (mode === 'css') {
+ var y = "";
- if (context.currentStyle) {
- y = context.currentStyle[prop];
- } else {
- if (window.getComputedStyle) {
- y = document.defaultView.getComputedStyle(context, null).getPropertyValue(prop);
- }
+ if (context.currentStyle) {
+ y = context.currentStyle[prop];
+ } else {
+ if (window.getComputedStyle) {
+ y = document.defaultView.getComputedStyle(context, null).getPropertyValue(prop);
}
-
- return y;
- } else if (mode === 'attr') {
- // Isn't very compatible with the HTML5 data-* attributes
- // return context[prop];
- return context.getAttribute(prop);
}
+
+ return y;
+ } else if (mode === 'attr') {
+ // Isn't very compatible with the HTML5 data-* attributes
+ // return context[prop];
+ return context.getAttribute(prop);
}
- };
+ }
+ };
- // Return an element at a particular index in a selection
- this.eq = function(i) {
- var elem = this.selection[i];
- return elem ? new myQuery(elem) : this;
- };
+ // Return an element at a particular index in a selection
+ this.eq = function(i) {
+ var elem = this.selection[i];
+ return elem ? new myQuery(elem) : this;
+ };
- // Return the first element in a selection
- this.first = function() {
- return this.eq(0);
- };
+ // Return the first element in a selection
+ this.first = function() {
+ return this.eq(0);
+ };
- // Return the last element in a selection
- this.last = function() {
- return this.eq(this.selection.length - 1);
- };
+ // Return the last element in a selection
+ this.last = function() {
+ return this.eq(this.selection.length - 1);
+ };
- // Bind an eventName to a handler
- // el.bind(eventName, handler)
- this.bind = function(eventType, handler) {
- this.addListener(this.selection, eventType, handler);
- return this;
- };
+ // Bind an eventName to a handler
+ // el.bind(eventName, handler)
+ this.bind = function(eventType, handler) {
+ this.addListener(this.selection, eventType, handler);
+ return this;
+ };
- // Unbind an eventName
- // el.unbind(eventName, handler)
- this.unbind = function(eventType, handler) {
- this.removeListener(this.selection, eventType, handler);
- return this;
- };
+ // Unbind an eventName
+ // el.unbind(eventName, handler)
+ this.unbind = function(eventType, handler) {
+ this.removeListener(this.selection, eventType, handler);
+ return this;
+ };
- // Easy log fn
- this.log = (window.console && typeof console.log === 'function') ?
- function() {
- console.log(this.selection);
- } :
- function() {
- //TODO: create alternative logging
- };
+ // Easy log fn
+ this.log = function() {
+ if (window.console && typeof console.log === 'function') {
+ console.log(this.selection);
+ }
+ };
- return this instanceof myQuery ?
- this.myQuery :
- new myQuery(selector);
- }
+ return this instanceof myQuery ?
+ this.myQuery :
+ new myQuery(selector);
+ }
- // Expose myQuery to the global object
- window.myQuery = myQuery;
+ // Expose myQuery to the global object
+ window.myQuery = myQuery;
})(window, document);
Please sign in to comment.
Something went wrong with that request. Please try again.