Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update qwery

  • Loading branch information...
commit 0cd8926610aa44b3f48e147ecbdf250cc82ec689 1 parent bd33785
@rvagg authored
Showing with 434 additions and 340 deletions.
  1. +2 −0  .gitignore
  2. +432 −340 test/integration/ender_qwery.js
View
2  .gitignore
@@ -1 +1,3 @@
node_modules
+npm-debug.log
+ender*min.js
View
772 test/integration/ender_qwery.js
@@ -2,16 +2,17 @@
* =============================================================
* Ender: open module JavaScript framework (https://ender.no.de)
* Build: ender build domready qwery bonzo ../.. --output ender_qwery
+ * Packages: ender-js@0.4.3-dev domready@0.2.11 qwery@3.3.8 bonzo@1.0.6 traversty@0.0.7
* =============================================================
*/
/*!
* Ender: open module JavaScript framework (client-lib)
- * copyright Dustin Diaz & Jacob Thornton 2011 (@ded @fat)
+ * copyright Dustin Diaz & Jacob Thornton 2011-2012 (@ded @fat)
* http://ender.no.de
* License MIT
*/
-!function (context) {
+(function (context) {
// a global object for node.js module compatiblity
// ============================================
@@ -23,17 +24,19 @@
// ============================================
var modules = {}
- , old = context.$
+ , old = context['$']
+ , oldRequire = context['require']
+ , oldProvide = context['provide']
function require (identifier) {
// modules can be required from ender's build system, or found on the window
- var module = modules[identifier] || window[identifier]
- if (!module) throw new Error("Requested module '" + identifier + "' has not been defined.")
+ var module = modules['$' + identifier] || window[identifier]
+ if (!module) throw new Error("Ender Error: Requested module '" + identifier + "' has not been defined.")
return module
}
function provide (name, what) {
- return (modules[name] = what)
+ return (modules['$' + name] = what)
}
context['provide'] = provide
@@ -44,43 +47,74 @@
return o
}
- function boosh(s, r, els) {
+ /**
+ * main Ender return object
+ * @constructor
+ * @param {Array|Node|string} s a CSS selector or DOM node(s)
+ * @param {Array.|Node} r a root node(s)
+ */
+ function Ender(s, r) {
+ var elements
+ , i
+
+ this.selector = s
// string || node || nodelist || window
- if (typeof s == 'string' || s.nodeName || (s.length && 'item' in s) || s == window) {
- els = ender._select(s, r)
- els.selector = s
- } else els = isFinite(s.length) ? s : [s]
- return aug(els, boosh)
+ if (typeof s == 'undefined') {
+ elements = []
+ this.selector = ''
+ } else if (typeof s == 'string' || s.nodeName || (s.length && 'item' in s) || s == window) {
+ elements = ender._select(s, r)
+ } else {
+ elements = isFinite(s.length) ? s : [s]
+ }
+ this.length = elements.length
+ for (i = this.length; i--;) this[i] = elements[i]
+ }
+
+ /**
+ * @param {function(el, i, inst)} fn
+ * @param {Object} opt_scope
+ * @returns {Ender}
+ */
+ Ender.prototype['forEach'] = function (fn, opt_scope) {
+ var i, l
+ // opt out of native forEach so we can intentionally call our own scope
+ // defaulting to the current item and be able to return self
+ for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(opt_scope || this[i], this[i], i, this)
+ // return self for chaining
+ return this
}
+ Ender.prototype.$ = ender // handy reference to self
+
+
function ender(s, r) {
- return boosh(s, r)
+ return new Ender(s, r)
}
- aug(ender, {
- _VERSION: '0.3.6'
- , fn: boosh // for easy compat to jQuery plugins
- , ender: function (o, chain) {
- aug(chain ? boosh : ender, o)
- }
- , _select: function (s, r) {
- return (r || document).querySelectorAll(s)
- }
- })
+ ender['_VERSION'] = '0.4.3-dev'
- aug(boosh, {
- forEach: function (fn, scope, i) {
- // opt out of native forEach so we can intentionally call our own scope
- // defaulting to the current item and be able to return self
- for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(scope || this[i], this[i], i, this)
- // return self for chaining
- return this
- },
- $: ender // handy reference to self
- })
+ ender.fn = Ender.prototype // for easy compat to jQuery plugins
+
+ ender.ender = function (o, chain) {
+ aug(chain ? Ender.prototype : ender, o)
+ }
+
+ ender._select = function (s, r) {
+ if (typeof s == 'string') return (r || document).querySelectorAll(s)
+ if (s.nodeName) return [s]
+ return s
+ }
- ender.noConflict = function () {
- context.$ = old
+
+ // use callback to receive Ender's require & provide
+ ender.noConflict = function (callback) {
+ context['$'] = old
+ if (callback) {
+ context['provide'] = oldProvide
+ context['require'] = oldRequire
+ callback(require, provide, this)
+ }
return this
}
@@ -88,18 +122,22 @@
// use subscript notation as extern for Closure compilation
context['ender'] = context['$'] = context['ender'] || ender
-}(this);
+}(this));
-!function () {
+
+(function () {
var module = { exports: {} }, exports = module.exports;
+ /*!
+ * domready (c) Dustin Diaz 2012 - License MIT
+ */
!function (name, definition) {
- if (typeof define == 'function') define(definition)
- else if (typeof module != 'undefined') module.exports = definition()
- else this[name] = this['domReady'] = definition()
+ if (typeof module != 'undefined') module.exports = definition()
+ else if (typeof define == 'function' && typeof define.amd == 'object') define(definition)
+ else this[name] = definition()
}('domready', function (ready) {
-
+
var fns = [], fn, f = false
, doc = document
, testEl = doc.documentElement
@@ -107,26 +145,27 @@
, domContentLoaded = 'DOMContentLoaded'
, addEventListener = 'addEventListener'
, onreadystatechange = 'onreadystatechange'
- , loaded = /^loade|c/.test(doc.readyState)
-
+ , readyState = 'readyState'
+ , loaded = /^loade|c/.test(doc[readyState])
+
function flush(f) {
loaded = 1
while (f = fns.shift()) f()
}
-
+
doc[addEventListener] && doc[addEventListener](domContentLoaded, fn = function () {
doc.removeEventListener(domContentLoaded, fn, f)
flush()
}, f)
-
-
- hack && doc.attachEvent(onreadystatechange, (fn = function () {
- if (/^c/.test(doc.readyState)) {
+
+
+ hack && doc.attachEvent(onreadystatechange, fn = function () {
+ if (/^c/.test(doc[readyState])) {
doc.detachEvent(onreadystatechange, fn)
flush()
}
- }))
-
+ })
+
return (ready = hack ?
function (fn) {
self != top ?
@@ -144,7 +183,6 @@
loaded ? fn() : fns.push(fn)
})
})
-
provide("domready", module.exports);
!function ($) {
@@ -157,25 +195,24 @@
}
}, true)
}(ender);
+}());
-}();
-
-!function () {
+(function () {
var module = { exports: {} }, exports = module.exports;
/*!
* Qwery - A Blazing Fast query selector engine
* https://github.com/ded/qwery
- * copyright Dustin Diaz & Jacob Thornton 2011
+ * copyright Dustin Diaz & Jacob Thornton 2012
* MIT License
*/
-
- !function (name, definition) {
- if (typeof module != 'undefined') module.exports = definition()
- else if (typeof define == 'function' && typeof define.amd == 'object') define(definition)
- else this[name] = definition()
- }('qwery', function () {
+
+ (function (name, definition, context) {
+ if (typeof module != 'undefined' && module.exports) module.exports = definition()
+ else if (typeof context['define'] != 'undefined' && context['define'] == 'function' && context['define']['amd']) define(name, definition)
+ else context[name] = definition()
+ })('qwery', function () {
var doc = document
, html = doc.documentElement
, byClass = 'getElementsByClassName'
@@ -185,7 +222,7 @@
, tagName = 'tagName'
, nodeType = 'nodeType'
, select // main select() method, assign later
-
+
// OOOOOOOOOOOOH HERE COME THE ESSSXXSSPRESSSIONSSSSSSSS!!!!!
, id = /#([\w\-]+)/
, clas = /\.[\w\-]+/g
@@ -220,7 +257,7 @@
return (p1 = previous(node)) && (p2 = previous(contestant)) && p1 == p2 && p1
}
}
-
+
function cache() {
this.c = {}
}
@@ -233,42 +270,42 @@
return (this.c[k] = v)
}
}
-
+
var classCache = new cache()
, cleanCache = new cache()
, attrCache = new cache()
, tokenCache = new cache()
-
+
function classRegex(c) {
return classCache.g(c) || classCache.s(c, '(^|\\s+)' + c + '(\\s+|$)', 1)
}
-
+
// not quite as fast as inline loops in older browsers so don't use liberally
function each(a, fn) {
var i = 0, l = a.length
for (; i < l; i++) fn(a[i])
}
-
+
function flatten(ar) {
for (var r = [], i = 0, l = ar.length; i < l; ++i) arrayLike(ar[i]) ? (r = r.concat(ar[i])) : (r[r.length] = ar[i])
return r
}
-
+
function arrayify(ar) {
var i = 0, l = ar.length, r = []
for (; i < l; i++) r[i] = ar[i]
return r
}
-
+
function previous(n) {
while (n = n.previousSibling) if (n[nodeType] == 1) break;
return n
}
-
+
function q(query) {
return query.match(chunker)
}
-
+
// called using `this` as element and arguments from regex group results.
// given => div.hello[title="world"]:foo('bar')
// div.hello[title="world"]:foo('bar'), div, .hello, [title="world"], title, =, world, :foo('bar'), foo, ('bar'), bar]
@@ -295,11 +332,11 @@
}
return this
}
-
+
function clean(s) {
return cleanCache.g(s) || cleanCache.s(s, s.replace(specialChars, '\\$1'))
}
-
+
function checkAttr(qualify, actual, val) {
switch (qualify) {
case '=':
@@ -317,19 +354,19 @@
}
return 0
}
-
+
// given a selector, first check for simple cases then collect all base candidate matches and filter
function _qwery(selector, _root) {
var r = [], ret = [], i, l, m, token, tag, els, intr, item, root = _root
, tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr))
, dividedTokens = selector.match(dividers)
-
+
if (!tokens.length) return r
-
+
token = (tokens = tokens.slice(0)).pop() // copy cached tokens, take the last one
if (tokens.length && (m = tokens[tokens.length - 1].match(idOnly))) root = byId(_root, m[1])
if (!root) return r
-
+
intr = q(token)
// collect base candidates to filter
els = root !== _root && root[nodeType] !== 9 && dividedTokens && /^[+~]$/.test(dividedTokens[dividedTokens.length - 1]) ?
@@ -345,17 +382,17 @@
if (item = interpret.apply(els[i], intr)) r[r.length] = item
}
if (!tokens.length) return r
-
+
// filter further according to the rest of the selector (the left side)
each(r, function(e) { if (ancestorMatch(e, tokens, dividedTokens)) ret[ret.length] = e })
return ret
}
-
+
// compare element to a selector
function is(el, selector, root) {
if (isNode(selector)) return el == selector
if (arrayLike(selector)) return !!~flatten(selector).indexOf(el) // if selector is an array, is el a member?
-
+
var selectors = selector.split(','), tokens, dividedTokens
while (selector = selectors.pop()) {
tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr))
@@ -367,7 +404,7 @@
}
return false
}
-
+
// given elements matching the right-most part of a selector, filter out any that don't match the rest
function ancestorMatch(el, tokens, dividedTokens, root) {
var cand
@@ -383,11 +420,11 @@
}
return (cand = crawl(el, tokens.length - 1, el)) && (!root || isAncestor(cand, root))
}
-
+
function isNode(el, t) {
return el && typeof el === 'object' && (t = el[nodeType]) && (t == 1 || t == 9)
}
-
+
function uniq(ar) {
var a = [], i, j
o: for (i = 0; i < ar.length; ++i) {
@@ -396,18 +433,18 @@
}
return a
}
-
+
function arrayLike(o) {
return (typeof o === 'object' && isFinite(o.length))
}
-
+
function normalizeRoot(root) {
if (!root) return doc
if (typeof root == 'string') return qwery(root)[0]
if (!root[nodeType] && arrayLike(root)) return root[0]
return root
}
-
+
function byId(root, id, el) {
// if doc, query on it, else query the parent doc or if a detached fragment rewrite the query and run on the fragment
return root[nodeType] === 9 ? root.getElementById(id) :
@@ -415,10 +452,10 @@
(((el = root.ownerDocument.getElementById(id)) && isAncestor(el, root) && el) ||
(!isAncestor(root, root.ownerDocument) && select('[id="' + id + '"]', root)[0]))
}
-
+
function qwery(selector, _root) {
var m, el, root = normalizeRoot(_root)
-
+
// easy, fast cases that we can dispatch with simple DOM calls
if (!root || !selector) return []
if (selector === window || isNode(selector)) {
@@ -430,10 +467,10 @@
if (m[2]) return arrayify(root[byTag](m[2]))
if (hasByClass && m[3]) return arrayify(root[byClass](m[3]))
}
-
+
return select(selector, root)
}
-
+
// where the root is not document and a relationship selector is first we have to
// do some awkward adjustments to get it to work, even with qSA
function collectSelector(root, collector) {
@@ -452,7 +489,7 @@
s.length && collector(root, s, false)
}
}
-
+
var isAncestor = 'compareDocumentPosition' in html ?
function (element, container) {
return (container.compareDocumentPosition(element) & 16) == 16
@@ -522,31 +559,38 @@
if (typeof options[useNativeQSA] !== 'undefined')
select = !options[useNativeQSA] ? selectNonNative : hasQSA ? selectQSA : selectNonNative
}
-
+
configure({ useNativeQSA: true })
-
+
qwery.configure = configure
qwery.uniq = uniq
qwery.is = is
qwery.pseudos = {}
-
+
return qwery
- })
-
+ }, this);
provide("qwery", module.exports);
- !function (doc, $) {
- var q = require('qwery')
-
+ (function ($) {
+ var q = function (r) {
+ try {
+ r = require('qwery')
+ } catch (ex) {
+ r = require('qwery-mobile')
+ } finally {
+ return r
+ }
+ }()
+
$.pseudos = q.pseudos
-
+
$._select = function (s, r) {
// detect if sibling module 'bonzo' is available at run-time
// rather than load-time since technically it's not a dependency and
// can be loaded in any order
// hence the lazy function re-definition
- return ($._select = (function(b) {
+ return ($._select = (function (b) {
try {
b = require('bonzo')
return function (s, r) {
@@ -556,7 +600,7 @@
return q
})())(s, r)
}
-
+
$.ender({
find: function (s) {
var r = [], i, l, j, k, els
@@ -571,6 +615,7 @@
for (var i = this.length, j = 0, l = this.length + plus.length; i < l; i++, j++) {
this[i] = plus[j]
}
+ this.length += plus.length
return this
}
, is: function(s, r) {
@@ -583,46 +628,47 @@
return false
}
}, true)
- }(document, ender);
-
+ }(ender));
-}();
+}());
-!function () {
+(function () {
var module = { exports: {} }, exports = module.exports;
/*!
- * Bonzo: DOM Utility (c) Dustin Diaz 2011
+ * Bonzo: DOM Utility (c) Dustin Diaz 2012
* https://github.com/ded/bonzo
* License MIT
*/
- !function (name, definition) {
- if (typeof module != 'undefined') module.exports = definition()
- else if (typeof define == 'function' && define.amd) define(name, definition)
- else this[name] = definition()
- }('bonzo', function() {
+ (function (name, definition, context) {
+ if (typeof module != 'undefined' && module.exports) module.exports = definition()
+ else if (typeof context['define'] != 'undefined' && context['define'] == 'function' && context['define']['amd']) define(name, definition)
+ else context[name] = definition()
+ })('bonzo', function() {
var context = this
, win = window
, doc = win.document
, html = doc.documentElement
, parentNode = 'parentNode'
, query = null
- , specialAttributes = /^checked|value|selected$/
- , specialTags = /select|fieldset|table|tbody|tfoot|td|tr|colgroup/i
- , table = [ '<table>', '</table>', 1 ]
- , td = [ '<table><tbody><tr>', '</tr></tbody></table>', 3 ]
- , option = [ '<select>', '</select>', 1 ]
- , tagMap = {
- thead: table, tbody: table, tfoot: table, colgroup: table, caption: table
- , tr: [ '<table><tbody>', '</tbody></table>', 2 ]
+ , specialAttributes = /^(checked|value|selected)$/i
+ , specialTags = /^(select|fieldset|table|tbody|tfoot|td|tr|colgroup)$/i // tags that we have trouble inserting *into*
+ , table = ['<table>', '</table>', 1]
+ , td = ['<table><tbody><tr>', '</tr></tbody></table>', 3]
+ , option = ['<select>', '</select>', 1]
+ , noscope = ['_', '', 0, 1]
+ , tagMap = { // tags that we have trouble *inserting*
+ thead: table, tbody: table, tfoot: table, colgroup: table, caption: table
+ , tr: ['<table><tbody>', '</tbody></table>', 2]
, th: td , td: td
- , col: [ '<table><colgroup>', '</colgroup></table>', 2 ]
- , fieldset: [ '<form>', '</form>', 1 ]
- , legend: [ '<form><fieldset>', '</fieldset></form>', 2 ]
- , option: option
- , optgroup: option }
- , stateAttributes = /^checked|selected$/
+ , col: ['<table><colgroup>', '</colgroup></table>', 2]
+ , fieldset: ['<form>', '</form>', 1]
+ , legend: ['<form><fieldset>', '</fieldset></form>', 2]
+ , option: option, optgroup: option
+ , script: noscope, style: noscope, link: noscope, param: noscope, base: noscope
+ }
+ , stateAttributes = /^(checked|selected)$/
, ie = /msie/i.test(navigator.userAgent)
, hasClass, addClass, removeClass
, uidMap = {}
@@ -651,7 +697,9 @@
}
}()
, trimReplace = /(^\s*|\s*$)/g
- , unitless = { lineHeight: 1, zoom: 1, zIndex: 1, opacity: 1 }
+ , whitespaceRegex = /\s+/
+ , toString = String.prototype.toString
+ , unitless = { lineHeight: 1, zoom: 1, zIndex: 1, opacity: 1, boxFlex: 1, WebkitBoxFlex: 1, MozBoxFlex: 1 }
, trim = String.prototype.trim ?
function (s) {
return s.trim()
@@ -659,16 +707,16 @@
function (s) {
return s.replace(trimReplace, '')
}
-
+
function classReg(c) {
return new RegExp("(^|\\s+)" + c + "(\\s+|$)")
}
-
+
function each(ar, fn, scope) {
for (var i = 0, l = ar.length; i < l; i++) fn.call(scope || ar[i], ar[i], i, ar)
return ar
}
-
+
function deepEach(ar, fn, scope) {
for (var i = 0, l = ar.length; i < l; i++) {
if (isNode(ar[i])) {
@@ -678,51 +726,55 @@
}
return ar
}
-
+
function camelize(s) {
return s.replace(/-(.)/g, function (m, m1) {
return m1.toUpperCase()
})
}
-
+
function decamelize(s) {
return s ? s.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() : s
}
-
+
function data(el) {
el[getAttribute]('data-node-uid') || el[setAttribute]('data-node-uid', ++uuids)
- uid = el[getAttribute]('data-node-uid')
+ var uid = el[getAttribute]('data-node-uid')
return uidMap[uid] || (uidMap[uid] = {})
}
-
+
function clearData(el) {
- uid = el[getAttribute]('data-node-uid')
- uid && (delete uidMap[uid])
+ var uid = el[getAttribute]('data-node-uid')
+ if (uid) delete uidMap[uid]
}
-
- function dataValue(d) {
+
+ function dataValue(d, f) {
try {
- return d === 'true' ? true : d === 'false' ? false : d === 'null' ? null : !isNaN(d) ? parseFloat(d) : d;
+ return (d === null || d === undefined) ? undefined :
+ d === 'true' ? true :
+ d === 'false' ? false :
+ d === 'null' ? null :
+ (f = parseFloat(d)) == d ? f : d;
} catch(e) {}
return undefined
}
-
+
function isNode(node) {
return node && node.nodeName && node.nodeType == 1
}
-
- function some(ar, fn, scope, i) {
+
+ function some(ar, fn, scope, i, j) {
for (i = 0, j = ar.length; i < j; ++i) if (fn.call(scope, ar[i], i, ar)) return true
return false
}
-
+
function styleProperty(p) {
(p == 'transform' && (p = features.transform)) ||
(/^transform-?[Oo]rigin$/.test(p) && (p = features.transform + "Origin")) ||
(p == 'float' && (p = features.cssFloat))
return p ? camelize(p) : null
}
-
+
var getStyle = features.computedStyle ?
function (el, property) {
var value = null
@@ -730,9 +782,9 @@
computed && (value = computed[property])
return el.style[property] || value
} :
-
+
(ie && html.currentStyle) ?
-
+
function (el, property) {
if (property == 'opacity') {
var val = 100
@@ -748,11 +800,11 @@
var value = el.currentStyle ? el.currentStyle[property] : null
return el.style[property] || value
} :
-
+
function (el, property) {
return el.style[property]
}
-
+
// this insert method is intense
function insert(target, host, fn) {
var i = 0, self = host || this, r = []
@@ -765,10 +817,22 @@
var n = !el[parentNode] || (el[parentNode] && !el[parentNode][parentNode]) ?
function () {
var c = el.cloneNode(true)
+ , cloneElems
+ , elElems
+
// check for existence of an event cloner
// preferably https://github.com/fat/bean
// otherwise Bonzo won't do this for you
- self.$ && self.cloneEvents && self.$(c).cloneEvents(el)
+ if (self.$ && self.cloneEvents) {
+ self.$(c).cloneEvents(el)
+
+ // clone events from every child node
+ cloneElems = self.$(c).find('*')
+ elElems = self.$(el).find('*')
+
+ for (var i = 0; i < elElems.length; i++)
+ self.$(cloneElems[i]).cloneEvents(elElems[i])
+ }
return c
}() : el
fn(t, n)
@@ -782,7 +846,7 @@
self.length = i
return self
}
-
+
function xy(el, x, y) {
var $el = bonzo(el)
, style = $el.css('position')
@@ -790,43 +854,47 @@
, rel = 'relative'
, isRel = style == rel
, delta = [parseInt($el.css('left'), 10), parseInt($el.css('top'), 10)]
-
+
if (style == 'static') {
$el.css('position', rel)
style = rel
}
-
+
isNaN(delta[0]) && (delta[0] = isRel ? 0 : el.offsetLeft)
isNaN(delta[1]) && (delta[1] = isRel ? 0 : el.offsetTop)
-
+
x != null && (el.style.left = x - offset.left + delta[0] + px)
y != null && (el.style.top = y - offset.top + delta[1] + px)
-
+
}
-
+
// classList support for class management
- // altho to be fair, the api sucks because it won't accept multiple classes at once,
- // so we have to iterate. bullshit
+ // altho to be fair, the api sucks because it won't accept multiple classes at once
+ // so we iterate down below
if (features.classList) {
hasClass = function (el, c) {
- return some(c.toString().split(' '), function (c) {
- return el.classList.contains(c)
- })
+ return el.classList.contains(c)
}
addClass = function (el, c) {
- each(c.toString().split(' '), function (c) {
- el.classList.add(c)
- })
+ el.classList.add(c)
+ }
+ removeClass = function (el, c) {
+ el.classList.remove(c)
}
- removeClass = function (el, c) { el.classList.remove(c) }
}
else {
- hasClass = function (el, c) { return classReg(c).test(el.className) }
- addClass = function (el, c) { el.className = trim(el.className + ' ' + c) }
- removeClass = function (el, c) { el.className = trim(el.className.replace(classReg(c), ' ')) }
+ hasClass = function (el, c) {
+ return classReg(c).test(el.className)
+ }
+ addClass = function (el, c) {
+ el.className = trim(el.className + ' ' + c)
+ }
+ removeClass = function (el, c) {
+ el.className = trim(el.className.replace(classReg(c), ' '))
+ }
}
-
-
+
+
// this allows method calling for setting values
// example:
// bonzo(elements).css('color', function (el) {
@@ -835,7 +903,7 @@
function setter(el, v) {
return typeof v == 'function' ? v(el) : v
}
-
+
function Bonzo(elements) {
this.length = 0
if (elements) {
@@ -845,28 +913,26 @@
elements :
[elements]
this.length = elements.length
- for (var i = 0; i < elements.length; i++) {
- this[i] = elements[i]
- }
+ for (var i = 0; i < elements.length; i++) this[i] = elements[i]
}
}
-
+
Bonzo.prototype = {
-
- // indexr method, because jQueriers want this method
+
+ // indexr method, because jQueriers want this method. Jerks
get: function (index) {
return this[index] || null
}
-
+
// itetators
, each: function (fn, scope) {
return each(this, fn, scope)
}
-
+
, deepEach: function (fn, scope) {
return deepEach(this, fn, scope)
}
-
+
, map: function (fn, reject) {
var m = [], n, i
for (i = 0; i < this.length; i++) {
@@ -875,14 +941,14 @@
}
return m
}
-
+
// text and html inserters!
, html: function (h, text) {
var method = text ?
html.textContent === undefined ?
'innerText' :
'textContent' :
- 'innerHTML', m;
+ 'innerHTML';
function append(el) {
each(normalize(h), function (node) {
el.appendChild(node)
@@ -890,20 +956,20 @@
}
return typeof h !== 'undefined' ?
this.empty().each(function (el) {
- !text && (m = el.tagName.match(specialTags)) ?
- append(el, m[0]) :
- !function() {
+ !text && specialTags.test(el.tagName) ?
+ append(el) :
+ (function () {
try { (el[method] = h) }
catch(e) { append(el) }
- }();
+ }())
}) :
this[0] ? this[0][method] : ''
}
-
+
, text: function (text) {
return this.html(text, 1)
}
-
+
// more related insertion methods
, append: function (node) {
return this.each(function (el) {
@@ -912,7 +978,7 @@
})
})
}
-
+
, prepend: function (node) {
return this.each(function (el) {
var first = el.firstChild
@@ -921,19 +987,19 @@
})
})
}
-
+
, appendTo: function (target, host) {
return insert.call(this, target, host, function (t, el) {
t.appendChild(el)
})
}
-
+
, prependTo: function (target, host) {
return insert.call(this, target, host, function (t, el) {
t.insertBefore(el, t.firstChild)
})
}
-
+
, before: function (node) {
return this.each(function (el) {
each(bonzo.create(node), function (i) {
@@ -941,7 +1007,7 @@
})
})
}
-
+
, after: function (node) {
return this.each(function (el) {
each(bonzo.create(node), function (i) {
@@ -949,13 +1015,13 @@
})
})
}
-
+
, insertBefore: function (target, host) {
return insert.call(this, target, host, function (t, el) {
t[parentNode].insertBefore(el, t)
})
}
-
+
, insertAfter: function (target, host) {
return insert.call(this, target, host, function (t, el) {
var sibling = t.nextSibling
@@ -967,55 +1033,72 @@
}
})
}
-
+
, replaceWith: function(html) {
this.deepEach(clearData)
-
+
return this.each(function (el) {
el.parentNode.replaceChild(bonzo.create(html)[0], el)
})
}
-
+
// class management
, addClass: function (c) {
+ c = toString.call(c).split(whitespaceRegex)
return this.each(function (el) {
- hasClass(el, setter(el, c)) || addClass(el, setter(el, c))
+ // we `each` here so you can do $el.addClass('foo bar')
+ each(c, function (c) {
+ if (c && !hasClass(el, setter(el, c)))
+ addClass(el, setter(el, c))
+ })
})
}
-
+
, removeClass: function (c) {
+ c = toString.call(c).split(whitespaceRegex)
return this.each(function (el) {
- hasClass(el, setter(el, c)) && removeClass(el, setter(el, c))
+ each(c, function (c) {
+ if (c && hasClass(el, setter(el, c)))
+ removeClass(el, setter(el, c))
+ })
})
}
-
+
, hasClass: function (c) {
+ c = toString.call(c).split(whitespaceRegex)
return some(this, function (el) {
- return hasClass(el, c)
+ return some(c, function (c) {
+ return c && hasClass(el, c)
+ })
})
}
-
+
, toggleClass: function (c, condition) {
+ c = toString.call(c).split(whitespaceRegex)
return this.each(function (el) {
- typeof condition !== 'undefined' ?
- condition ? addClass(el, c) : removeClass(el, c) :
- hasClass(el, c) ? removeClass(el, c) : addClass(el, c)
+ each(c, function (c) {
+ if (c) {
+ typeof condition !== 'undefined' ?
+ condition ? addClass(el, c) : removeClass(el, c) :
+ hasClass(el, c) ? removeClass(el, c) : addClass(el, c)
+ }
+ })
})
}
-
+
// display togglers
, show: function (type) {
return this.each(function (el) {
el.style.display = type || ''
})
}
-
+
, hide: function () {
return this.each(function (el) {
el.style.display = 'none'
})
}
-
+
, toggle: function (callback, type) {
this.each(function (el) {
el.style.display = (el.offsetWidth || el.offsetHeight) ? 'none' : type || ''
@@ -1023,28 +1106,28 @@
callback && callback()
return this
}
-
+
// DOM Walkers & getters
, first: function () {
return bonzo(this.length ? this[0] : [])
}
-
+
, last: function () {
return bonzo(this.length ? this[this.length - 1] : [])
}
-
+
, next: function () {
return this.related('nextSibling')
}
-
+
, previous: function () {
return this.related('previousSibling')
}
-
+
, parent: function() {
- return this.related('parentNode')
- }
-
+ return this.related(parentNode)
+ }
+
, related: function (method) {
return this.map(
function (el) {
@@ -1059,18 +1142,19 @@
}
)
}
-
+
// meh. use with care. the ones in Bean are better
, focus: function () {
- return this.length > 0 ? this[0].focus() : null
+ this.length && this[0].focus()
+ return this
}
-
+
, blur: function () {
return this.each(function (el) {
el.blur()
})
}
-
+
// style getter setter & related methods
, css: function (o, v, p) {
// is this a request for just getting a style?
@@ -1091,7 +1175,7 @@
iter = {}
iter[o] = v
}
-
+
if (ie && iter.opacity) {
// oh this 'ol gamut
iter.filter = 'alpha(opacity=' + (iter.opacity * 100) + ')'
@@ -1099,7 +1183,7 @@
iter.zoom = o.zoom || 1;
delete iter.opacity;
}
-
+
function fn(el, p, v) {
for (var k in iter) {
if (iter.hasOwnProperty(k)) {
@@ -1112,7 +1196,7 @@
}
return this.each(fn)
}
-
+
, offset: function (x, y) {
if (typeof x == 'number' || typeof y == 'number') {
return this.each(function (el) {
@@ -1133,8 +1217,13 @@
while (el = el.offsetParent) {
top = top + el.offsetTop
left = left + el.offsetLeft
+
+ if (el != document.body) {
+ top -= el.scrollTop
+ left -= el.scrollLeft
+ }
}
-
+
return {
top: top
, left: left
@@ -1142,8 +1231,9 @@
, width: width
}
}
-
+
, dim: function () {
+ if (!this.length) return { height: 0, width: 0 }
var el = this[0]
, orig = !el.offsetWidth && !el.offsetHeight ?
// el isn't visible, can't be measured properly, so fix that
@@ -1162,14 +1252,14 @@
}(this) : null
, width = el.offsetWidth
, height = el.offsetHeight
-
+
orig && this.first().css(orig)
return {
height: height
, width: width
}
}
-
+
// attributes are hard. go shopping
, attr: function (k, v) {
var el = this[0]
@@ -1180,7 +1270,7 @@
return this
}
return typeof v == 'undefined' ?
- specialAttributes.test(k) ?
+ !el ? null : specialAttributes.test(k) ?
stateAttributes.test(k) && typeof el[k] == 'string' ?
true : el[k] : (k == 'href' || k =='src') && features.hrefExtended ?
el[getAttribute](k, 2) : el[getAttribute](k) :
@@ -1188,79 +1278,84 @@
specialAttributes.test(k) ? (el[k] = setter(el, v)) : el[setAttribute](k, setter(el, v))
})
}
-
+
, removeAttr: function (k) {
return this.each(function (el) {
stateAttributes.test(k) ? (el[k] = false) : el.removeAttribute(k)
})
}
-
+
, val: function (s) {
- return (typeof s == 'string') ? this.attr('value', s) : this[0].value
+ return (typeof s == 'string') ?
+ this.attr('value', s) :
+ this.length ? this[0].value : null
}
-
+
// use with care and knowledge. this data() method uses data attributes on the DOM nodes
// to do this differently costs a lot more code. c'est la vie
, data: function (k, v) {
var el = this[0], uid, o, m
if (typeof v === 'undefined') {
+ if (!el) return null
o = data(el)
if (typeof k === 'undefined') {
each(el.attributes, function(a) {
- (m = (''+a.name).match(dattr)) && (o[camelize(m[1])] = dataValue(a.value))
+ (m = ('' + a.name).match(dattr)) && (o[camelize(m[1])] = dataValue(a.value))
})
return o
} else {
- return typeof o[k] === 'undefined' ?
- (o[k] = dataValue(this.attr('data-' + decamelize(k)))) : o[k]
+ if (typeof o[k] === 'undefined')
+ o[k] = dataValue(this.attr('data-' + decamelize(k)))
+ return o[k]
}
} else {
return this.each(function (el) { data(el)[k] = v })
}
}
-
+
// DOM detachment & related
, remove: function () {
this.deepEach(clearData)
-
+
return this.each(function (el) {
el[parentNode] && el[parentNode].removeChild(el)
})
}
-
+
, empty: function () {
return this.each(function (el) {
deepEach(el.childNodes, clearData)
-
+
while (el.firstChild) {
el.removeChild(el.firstChild)
}
})
}
-
+
, detach: function () {
return this.map(function (el) {
return el[parentNode].removeChild(el)
})
}
-
+
// who uses a mouse anyway? oh right.
, scrollTop: function (y) {
return scroll.call(this, null, y, 'y')
}
-
+
, scrollLeft: function (x) {
return scroll.call(this, x, null, 'x')
}
-
+
}
-
+
function normalize(node) {
return typeof node == 'string' ? bonzo.create(node) : isNode(node) ? [node] : node // assume [nodes]
}
-
+
function scroll(x, y, type) {
var el = this[0]
+ if (!el) return this
if (x == null && y == null) {
return (isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop })[type]
}
@@ -1272,31 +1367,31 @@
}
return this
}
-
+
function isBody(element) {
return element === win || (/^(?:body|html)$/i).test(element.tagName)
}
-
+
function getWindowScroll() {
return { x: win.pageXOffset || html.scrollLeft, y: win.pageYOffset || html.scrollTop }
}
-
+
function bonzo(els, host) {
return new Bonzo(els, host)
}
-
+
bonzo.setQueryEngine = function (q) {
query = q;
delete bonzo.setQueryEngine
}
-
+
bonzo.aug = function (o, target) {
// for those standalone bonzo users. this love is for you.
for (var k in o) {
o.hasOwnProperty(k) && ((target || Bonzo.prototype)[k] = o[k])
}
}
-
+
bonzo.create = function (node) {
// hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
return typeof node == 'string' && node !== '' ?
@@ -1306,11 +1401,14 @@
, els = []
, p = tag ? tagMap[tag[1].toLowerCase()] : null
, dep = p ? p[2] + 1 : 1
+ , ns = p && p[3]
, pn = parentNode
, tb = features.autoTbody && p && p[0] == '<table>' && !(/<tbody/i).test(node)
-
+
el.innerHTML = p ? (p[0] + node + p[1]) : node
while (dep--) el = el.firstChild
+ // for IE NoScope, we may insert cruft at the begining just to get it to work
+ if (ns && el && el.nodeType !== 1) el = el.nextSibling
do {
// tbody special case for IE<8, creates tbody on any empty table
// we don't want it if we're just after a <thead>, <caption>, etc.
@@ -1322,10 +1420,10 @@
// `dep` > 1 can also cause problems with the insert() check (must do this last)
each(els, function(el) { el[pn] && el[pn].removeChild(el) })
return els
-
+
}() : isNode(node) ? [node.cloneNode(true)] : []
}
-
+
bonzo.doc = function () {
var vp = bonzo.viewport()
return {
@@ -1333,21 +1431,21 @@
, height: Math.max(doc.body.scrollHeight, html.scrollHeight, vp.height)
}
}
-
+
bonzo.firstChild = function (el) {
for (var c = el.childNodes, i = 0, j = (c && c.length) || 0, e; i < j; i++) {
if (c[i].nodeType === 1) e = c[j = i]
}
return e
}
-
+
bonzo.viewport = function () {
return {
width: ie ? html.clientWidth : self.innerWidth
, height: ie ? html.clientHeight : self.innerHeight
}
}
-
+
bonzo.isAncestor = 'compareDocumentPosition' in html ?
function (container, element) {
return (container.compareDocumentPosition(element) & 16) == 16
@@ -1363,15 +1461,14 @@
}
return false
}
-
+
return bonzo
- })
-
+ }, this); // the only line we care about using a semi-colon. placed here for concatenation tools
provide("bonzo", module.exports);
- !function ($) {
-
+ (function ($) {
+
var b = require('bonzo')
b.setQueryEngine($)
$.ender(b)
@@ -1381,16 +1478,16 @@
return $(b.create(node))
}
})
-
+
$.id = function (id) {
return $([document.getElementById(id)])
}
-
+
function indexOf(ar, val) {
for (var i = 0; i < ar.length; i++) if (ar[i] === val) return i
return -1
}
-
+
function uniq(ar) {
var r = [], i = 0, j = 0, k, item, inIt
for (; item = ar[i]; ++i) {
@@ -1404,9 +1501,10 @@
}
return r
}
-
+
$.ender({
parents: function (selector, closest) {
+ if (!this.length) return this
var collection = $(selector), j, k, p, r = []
for (j = 0, k = this.length; j < k; j++) {
p = this[j]
@@ -1419,47 +1517,47 @@
}
return $(uniq(r))
}
-
+
, parent: function() {
return $(uniq(b(this).parent()))
}
-
+
, closest: function (selector) {
return this.parents(selector, true)
}
-
+
, first: function () {
return $(this.length ? this[0] : this)
}
-
+
, last: function () {
return $(this.length ? this[this.length - 1] : [])
}
-
+
, next: function () {
return $(b(this).next())
}
-
+
, previous: function () {
return $(b(this).previous())
}
-
+
, appendTo: function (t) {
return b(this.selector).appendTo(t, this)
}
-
+
, prependTo: function (t) {
return b(this.selector).prependTo(t, this)
}
-
+
, insertAfter: function (t) {
return b(this.selector).insertAfter(t, this)
}
-
+
, insertBefore: function (t) {
return b(this.selector).insertBefore(t, this)
}
-
+
, siblings: function () {
var i, l, p, r = []
for (i = 0, l = this.length; i < l; i++) {
@@ -1470,9 +1568,9 @@
}
return $(r)
}
-
+
, children: function () {
- var i, el, r = []
+ var i, l, el, r = []
for (i = 0, l = this.length; i < l; i++) {
if (!(el = b.firstChild(this[i]))) continue;
r.push(el)
@@ -1480,32 +1578,25 @@
}
return $(uniq(r))
}
-
+
, height: function (v) {
- return dimension(v, this, 'height')
+ return dimension.call(this, 'height', v)
}
-
+
, width: function (v) {
- return dimension(v, this, 'width')
+ return dimension.call(this, 'width', v)
}
}, true)
-
- function dimension(v, self, which) {
- return v ?
- self.css(which, v) :
- function (r) {
- if (!self[0]) return 0
- r = parseInt(self.css(which), 10);
- return isNaN(r) ? self[0]['offset' + which.replace(/^\w/, function (m) {return m.toUpperCase()})] : r
- }()
- }
-
- }(ender);
-
-}();
+ function dimension(type, v) {
+ return typeof v == 'undefined'
+ ? b(this).dim()[type]
+ : this.css(type, v)
+ }
+ }(ender));
+}());
-!function () {
+(function () {
var module = { exports: {} }, exports = module.exports;
@@ -1514,13 +1605,13 @@
* https://github.com/rvagg/traversty
* License: MIT
*/
-
+
!(function (name, definition) {
if (typeof module !== 'undefined') module.exports = definition()
else if (typeof define === 'function' && define.amd) define(name, definition)
else this[name] = definition()
}('traversty', function () {
-
+
var context = this
, old = context.traversty
, doc = window.document
@@ -1531,52 +1622,51 @@
, matchesSelector = (function (el, pfx, name, i, ms) {
while (i < pfx.length)
if (el[ms = pfx[i++] + name]) return ms
- if (el[name = 'm' + name.substring(1)]) return name
- }(html, [ 'ms', 'webkit', 'moz', 'o' ], 'MatchesSelector', 0))
-
+ }(html, [ 'msM', 'webkitM', 'mozM', 'oM', 'm' ], 'atchesSelector', 0))
+
, isNumber = function (o) {
return toString.call(o) === '[object Number]'
}
-
+
, isString = function (o) {
return toString.call(o) === '[object String]'
}
-
+
, isFunction = function (o) {
return toString.call(o) === '[object Function]'
}
-
+
, isUndefined = function (o) {
return o === void 0
}
-
+
, isElement = function (o) {
return o && o.nodeType === 1
}
-
+
// figure out which argument, if any, is our 'index'
, getIndex = function (selector, index) {
return isUndefined(selector) && !isNumber(index) ? 0 :
isNumber(selector) ? selector : isNumber(index) ? index : null
}
-
+
// figure out which argument, if any, is our 'selector'
, getSelector = function (selector) {
return isString(selector) ? selector : '*'
}
-
+
, nativeSelectorFind = function (selector, el) {
return slice.call(el.querySelectorAll(selector), 0)
}
-
+
, nativeSelectorMatches = function (selector, el) {
return selector === '*' || el[matchesSelector](selector)
}
-
+
, selectorFind = nativeSelectorFind
-
+
, selectorMatches = nativeSelectorMatches
-
+
// used in the case where our selector engine does out-of-order element returns for
// grouped selectors, e.g. '.class, tag', we need our elements in document-order
// so we do it ourselves if need be
@@ -1592,7 +1682,7 @@
return engineSelect(selector, el)
}
}
-
+
, unique = function (ar) {
var a = [], i = -1, j, has
while (++i < ar.length) {
@@ -1608,13 +1698,17 @@
}
return a
}
-
+
, collect = function (els, fn) {
- var ret = [], i = 0, l = els.length
- while (i < l) ret = ret.concat(fn(els[i++]))
+ var ret = [], res, i = 0, j, l = els.length, l2
+ while (i < l) {
+ j = 0
+ l2 = (res = fn(els[i++])).length
+ while (j < l2) ret.push(res[j++])
+ }
return ret
}
-
+
, move = function (els, method, selector, index) {
index = getIndex(selector, index)
selector = getSelector(selector)
@@ -1637,7 +1731,7 @@
}
)
}
-
+
, traversty = (function () {
function T(els) {
this.length = 0
@@ -1647,7 +1741,7 @@
while (i--) this[i] = els[i]
}
}
-
+
T.prototype = {
down: function (selector, index) {
index = getIndex(selector, index)
@@ -1659,24 +1753,24 @@
}
))
}
-
+
, up: function (selector, index) {
return traversty(move(this, 'parentNode', selector, index))
}
-
+
, previous: function (selector, index) {
return traversty(move(this, 'previousSibling', selector, index))
}
-
+
, next: function (selector, index) {
return traversty(move(this, 'nextSibling', selector, index))
}
}
-
+
function t(els) {
return new T(isString(els) ? selectorFind(els, doc) : els)
}
-
+
t.setSelectorEngine = function (s) {
// feature testing the selector engine like a boss
var ss, r, a, _selectorMatches, _selectorFind
@@ -1691,7 +1785,7 @@
isFunction(s.is) ? function (selector, el) { return s.is(el, selector) } :
isFunction(s.matchesSelector) ? function (selector, el) { return s.matchesSelector(el, selector) } :
isFunction(s.match) ? function (selector, el) { return s.match(el, selector) } : null
-
+
if (!_selectorMatches) {
// perhaps it's an selector(x).is(y) type selector?
ss = s('a', e)
@@ -1701,14 +1795,14 @@
isFunction(ss.matchesSelector) ? function (selector, el) { return s(el).matchesSelector(selector) } :
isFunction(ss.match) ? function (selector, el) { return s(el).match(selector) } : null
}
-
+
if (!_selectorMatches)
throw 'Traversty: couldn\'t find selector engine\'s `matchesSelector`'
-
+
// verify that we have a working `matchesSelector`
if (_selectorMatches('x,y', e) || !_selectorMatches('a,p', e))
throw 'Traversty: couldn\'t make selector engine\'s `matchesSelector` work'
-
+
// basic select
if ((r = select('b,a', e)).length !== 2) throw 'Traversty: don\'t know how to use this selector engine'
// check to see if the selector engine has given us the results in document-order
@@ -1717,7 +1811,7 @@
// have we done enough to get a working `selectorFind`?
if ((r = _selectorFind('b,a', e)).length !== 2 || r[0] !== a)
throw 'Traversty: couldn\'t make selector engine work'
-
+
selectorMatches = _selectorMatches
selectorFind = _selectorFind
} catch (ex) {
@@ -1727,18 +1821,17 @@
e = null
}
}
-
+
t.noConflict = function () {
context.traversty = old
return this
}
-
+
return t
}())
-
+
return traversty
}))
-
provide("traversty", module.exports);
@@ -1765,7 +1858,7 @@
, down = integrate('down')
, next = integrate('next')
, previous = integrate('previous')
-
+
$.ender(
{
// core
@@ -1780,6 +1873,5 @@
, true
)
}(ender))
-
-}();
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.