Permalink
Browse files

new pseudos package + proper pseudos&mobile builds

  • Loading branch information...
1 parent 832e641 commit 452a183f5589ca5fa403ac225d97e56ab7e5c154 @rvagg rvagg committed Feb 6, 2012
View
@@ -5,9 +5,13 @@
"./src/copyright.js"
, "./src/qwery.js"
]
- , "qwery-mobile": [
+ , "mobile/qwery-mobile": [
"./src/copyright.js"
- , "./mobile/qwery-mobile.js"
+ , "./mobile/src/mobile.js"
+ ]
+ , "pseudos/qwery-pseudos": [
+ "./src/copyright.js"
+ , "./pseudos/src/pseudos.js"
]
}
, "JSHINT_OPTS": {
View
@@ -1,3 +1,10 @@
+/*!
+ * Qwery - A Blazing Fast query selector engine
+ * https://github.com/ded/qwery
+ * copyright Dustin Diaz & Jacob Thornton 2011
+ * MIT License
+ */
+
!function (context, doc, win) {
var classOnly = /^\.([\w\-]+)$/,
File renamed without changes.
@@ -1,10 +1,3 @@
-/*!
- * Qwery - A Blazing Fast query selector engine
- * https://github.com/ded/qwery
- * copyright Dustin Diaz & Jacob Thornton 2011
- * MIT License
- */
-
!function (context, doc, win) {
var classOnly = /^\.([\w\-]+)$/,
View
@@ -0,0 +1,17 @@
+{
+ "name": "qwery-pseudos",
+ "description": "Pseudo-selector extensions for Qwery",
+ "version": "1.0.0",
+ "homepage": "https://github.com/ded/qwery",
+ "authors": ["Dustin Diaz, @ded", "Jacob Thornton, @fat"],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/ded/qwery.git"
+ },
+ "dependencies": {
+ "qwery": "*"
+ },
+ "main": "./qwery-pseudos.js",
+ "keywords": ["ender", "query", "css", "pseudos", "pseudo", "selector engine"],
+ "ender": "noop"
+}
View
@@ -0,0 +1,106 @@
+/*!
+ * Qwery - A Blazing Fast query selector engine
+ * https://github.com/ded/qwery
+ * copyright Dustin Diaz & Jacob Thornton 2011
+ * MIT License
+ */
+
+!function () {
+ var q, pseudos, i, l, p, r, nodes, m, nthPattern = /\s*((?:\+|\-)?(\d*))n\s*((?:\+|\-)\s*\d+)?\s*/
+ if (typeof module != 'undefined' && typeof 'require' != 'undefined')
+ q = require('qwery')
+ else if (typeof qwery != 'undefined')
+ q = qwery
+ else
+ return
+
+ pseudos = q.pseudos
+
+ function children(node, ofType) {
+ var r = []
+ nodes = node.childNodes
+
+ for (i = 0, l = nodes.length; i < l; i++) {
+ if (nodes[i].nodeType == 1 && (!ofType || nodes[i].nodeName == ofType)) r.push(nodes[i])
+ }
+ return r
+ }
+
+ function checkNthExpr(el, nodes, a, b) {
+ if (!a) return (nodes[b-1] == el)
+ for (i = b, l = nodes.length; ((a > 0) ? (i <= l) : (i >= 1)); i += a) if (el == nodes[i-1]) return true
+ return false
+ }
+
+ function checkNth(el, nodes, val) {
+ if (isFinite(val)) return nodes[val - 1] == el
+ else if (val == 'odd') return checkNthExpr(el, nodes, 2, 1)
+ else if (val == 'even') return checkNthExpr(el, nodes, 2, 0)
+ else if (m = nthPattern.exec(val))
+ return checkNthExpr(el, nodes,
+ (m[2] ? parseInt(m[1], 10) : parseInt(m[1] + '1', 10)), // Check case where coefficient is omitted
+ (m[3] ? parseInt(m[3].replace(/\s*/, ''), 10) : 0)) // Check case where constant is omitted
+
+ return false
+ }
+
+ function text(el, s) {
+ if (el.nodeType === 3 || el.nodeType === 4) return el.nodeValue;
+ if (el.nodeType !== 1 && el.nodeType !== 9) return '';
+ for (s = '', el = el.firstChild; el; el = el.nextSibling) {
+ if (el.nodeType !== 8) s += el.textContent || el.innerText || text(el)
+ }
+ return s
+ }
+
+ // *was* going to be in CSS3, didn't quite make it
+ pseudos.contains = function(el, val) { return text(el).indexOf(val) != -1 }
+
+ pseudos.not = function(el, val) { return !q.is(el, val) }
+
+ pseudos['nth-child'] = function (el, val) {
+ if (!val || !(p = el.parentNode)) return false
+ return checkNth(el, children(p), val)
+ }
+
+ pseudos['nth-last-child'] = function (el, val) {
+ if (!val || !(p = el.parentNode)) return false
+ return checkNth(el, children(p).reverse(), val)
+ }
+
+ pseudos['nth-of-type'] = function (el, val) {
+ if (!val || !(p = el.parentNode)) return false
+ return checkNth(el, children(p, el.nodeName), val)
+ }
+
+ pseudos['nth-last-of-type'] = function (el, val) {
+ if (!val || !(p = el.parentNode)) return false
+ return checkNth(el, children(p, el.nodeName).reverse(), val)
+ }
+
+ pseudos['first-child'] = function (el) { return pseudos['nth-child'](el, 1) }
+ pseudos['last-child'] = function (el) { return pseudos['nth-last-child'](el, 1) }
+ pseudos['first-of-type'] = function (el) { return pseudos['nth-of-type'](el, 1) }
+ pseudos['last-of-type'] = function (el) { return pseudos['nth-last-of-type'](el, 1) }
+
+ pseudos['only-child'] = function (el) {
+ return (p = el.parentNode) && (nodes = children(p)) && (nodes.length == 1) && (el == nodes[0])
+ };
+
+ pseudos['only-of-type'] = function (el) {
+ return (p = el.parentNode) && (nodes = children(p, el.nodeName)) && (nodes.length == 1) && (el == nodes[0])
+ }
+
+ pseudos.target = function (el) {
+ return (el.getAttribute('id') == location.hash.substr(1))
+ }
+
+ pseudos.checked = function (el) { return el.checked }
+
+ pseudos.enabled = function (el) { return !el.disabled }
+
+ pseudos.disabled = function (el) { return el.disabled }
+
+ pseudos.empty = function (el) { return !el.childNodes.length }
+
+}();

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -1,8 +1,17 @@
!function () {
- var q = qwery, pseudos = q.pseudos, i, l, p, r, nodes, m, nthPattern = /\s*((?:\+|\-)?(\d*))n\s*((?:\+|\-)\s*\d+)?\s*/
+ var q, pseudos, i, l, p, r, nodes, m, nthPattern = /\s*((?:\+|\-)?(\d*))n\s*((?:\+|\-)\s*\d+)?\s*/
+ if (typeof module != 'undefined' && typeof 'require' != 'undefined')
+ q = require('qwery')
+ else if (typeof qwery != 'undefined')
+ q = qwery
+ else
+ return
+
+ pseudos = q.pseudos
function children(node, ofType) {
- nodes = node.childNodes, r = []
+ var r = []
+ nodes = node.childNodes
for (i = 0, l = nodes.length; i < l; i++) {
if (nodes[i].nodeType == 1 && (!ofType || nodes[i].nodeName == ofType)) r.push(nodes[i])
@@ -22,8 +31,8 @@
else if (val == 'even') return checkNthExpr(el, nodes, 2, 0)
else if (m = nthPattern.exec(val))
return checkNthExpr(el, nodes,
- (m[2] ? parseInt(m[1]) : parseInt(m[1] + '1')), // Check case where coefficient is omitted
- (m[3] ? parseInt(m[3].replace(/\s*/, '')) : 0)) // Check case where constant is omitted
+ (m[2] ? parseInt(m[1], 10) : parseInt(m[1] + '1', 10)), // Check case where coefficient is omitted
+ (m[3] ? parseInt(m[3].replace(/\s*/, ''), 10) : 0)) // Check case where constant is omitted
return false
}
View
@@ -30,11 +30,7 @@
, specialChars = /([.*+?\^=!:${}()|\[\]\/\\])/g
, simple = /^(\*|[a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/
, attr = /\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/
- , pseudo = /:([\w\-]+)(\(['"]?([\s\w\+\-]+)['"]?\))?/
- // check if we can pass a selector to a non-CSS3 compatible qSA.
- // *not* suitable for validating a selector, it's too lose; it's the users' responsibility to pass valid selectors
- // this regex must be kept in sync with the one in tests.js
- , css2 = /^(([\w\-]*[#\.]?[\w\-]+|\*)?(\[[\w\-]+([\~\|]?=['"][ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+["'])?\])?(\:(link|visited|active|hover))?([\s>+~\.,]|(?:$)))+$/
+ , pseudo = /:([\w\-]+)(\(['"]?([^()]+)['"]?\))?/
, easy = new RegExp(idOnly.source + '|' + tagOnly.source + '|' + classOnly.source)
, dividers = new RegExp('(' + splitters.source + ')' + splittersMore.source, 'g')
, tokenizr = new RegExp(splitters.source + splittersMore.source)
@@ -54,6 +50,8 @@
return (p1 = previous(node)) && (p2 = previous(contestant)) && p1 == p2 && p1
}
}
+ , useNativeQSA = 'useNativeQSA'
+ , select // main select() method, assign later
function cache() {
this.c = {}
@@ -266,7 +264,7 @@
if (m = selector.match(easy)) {
if (m[1]) return (el = byId(root, m[1])) ? [el] : []
if (m[2]) return arrayify(root[byTag](m[2]))
- if (supportsCSS3 && m[3]) return arrayify(root[byClass](m[3]))
+ if (hasByClass && m[3]) return arrayify(root[byClass](m[3]))
}
return select(selector, root)
@@ -313,19 +311,11 @@
} :
function(e, a) { return e.getAttribute(a) }
}()
- // does native qSA support CSS3 level selectors
- , supportsCSS3 = function () {
- if (doc[byClass] && doc.querySelector && doc[qSA]) {
- try {
- var p = doc.createElement('p')
- p.innerHTML = '<a/>'
- return p[qSA](':nth-of-type(1)').length
- } catch (e) { }
- }
- return false
- }()
- // native support for CSS3 selectors
- , selectCSS3 = function (selector, root) {
+ , hasByClass = !!doc[byClass]
+ // has native qSA support
+ , hasQSA = doc.querySelector && doc[qSA]
+ // use native qSA
+ , selectQSA = function (selector, root) {
var result = [], ss, e
try {
if (root.nodeType === 9 || !splittable.test(selector)) {
@@ -342,21 +332,6 @@
} catch(ex) { }
return selectNonNative(selector, root)
}
- // native support for CSS2 selectors only
- , selectCSS2qSA = function (selector, root) {
- var i, r, l, ss, result = []
- selector = selector.replace(normalizr, '$1')
- // safe to pass whole selector to qSA
- if (!splittable.test(selector) && css2.test(selector)) return arrayify(root[qSA](selector))
- each(ss = selector.split(','), collectSelector(root, function(ctx, s, rewrite) {
- // use native qSA if selector is compatile, otherwise use _qwery()
- r = css2.test(s) ? ctx[qSA](s) : _qwery(s, ctx)
- for (i = 0, l = r.length; i < l; i++) {
- if (ctx.nodeType === 9 || rewrite || isAncestor(r[i], root)) result[result.length] = r[i]
- }
- }))
- return ss.length > 1 && result.length > 1 ? uniq(result) : result
- }
// no native selector support
, selectNonNative = function (selector, root) {
var result = [], items, m, i, l, r, ss
@@ -378,15 +353,18 @@
}))
return ss.length > 1 && result.length > 1 ? uniq(result) : result
}
- , select = function () {
- var q = qwery.nonStandardEngine ? selectNonNative : supportsCSS3 ? selectCSS3 : doc[qSA] ? selectCSS2qSA : selectNonNative
- return q.apply(q, arguments)
+ , configure = function (options) {
+ // configNativeQSA: use fully-internal selector or native qSA where present
+ 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 = {}
- qwery.nonStandardEngine = false
return qwery
})
Oops, something went wrong.

0 comments on commit 452a183

Please sign in to comment.