Skip to content

Commit

Permalink
update JSONSelect for ie6 compat. thanks @rwldrn !
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed May 23, 2011
1 parent 62b4793 commit f440b93
Showing 1 changed file with 99 additions and 55 deletions.
154 changes: 99 additions & 55 deletions test/js/jsonselect.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/*! Copyright (c) 2011, Lloyd Hilaiel, ISC License */
// http://jsonselect.org/
/*
* This is the JSONSelect reference implementation, in javascript.
*/
;(function(exports) {
var jp = (typeof JSON !== 'undefined' ? JSON.parse : eval);
function jsonParse(s) { try { return jp(s); } catch(e) { te("ijs"); }; }

// emitted error codes. Strip this table for an, uh, "optimized build"
var _es = {}; // overshadow any globals when the table is stripped
/** --->>> **/
var _es = {
(function(exports) {

var // localize references
toString = Object.prototype.toString;

function jsonParse(str) {
try {
if(JSON && JSON.parse){
return JSON.parse(str);
}
return (new Function("return " + str))();
} catch(e) {
te("ijs");
}
}

// emitted error codes.
var errorCodes = {
"ijs": "invalid json string",
"mpc": "multiple pseudo classes (:xxx) not allowed",
"mepf": "malformed expression in pseudo-function",
Expand All @@ -21,21 +30,22 @@
"ujs": "unclosed json string",
"upc": "unrecognized pseudo class"
};
/** <<<--- **/
// throw a full or abbreviated error message depending on the existence of the
// _es table
var te = function(ec) { throw (_es[ec] ? _es[ec] : "jsonselect error: "+ec) }

// throw an error message
function te(ec) {
throw new Error(errorCodes[ec]);
}

// THE LEXER
var toks = {
psc: 1, // pseudo class
psf: 2, // pseudo class function
typ: 3, // type
str: 4, // string
str: 4 // string
};

var pat = /^(?:([\r\n\t\ ]+)|([*.,>])|(string|boolean|null|array|object|number)|(:(?:root|first-child|last-child|only-child))|(:(?:nth-child|nth-last-child))|(:\w+)|(\"(?:[^\\]|\\[^\"])*\")|(\")|((?:[_a-zA-Z]|[^\0-\0177]|\\[^\r\n\f0-9a-fA-F])(?:[_a-zA-Z0-9-]|[^\u0000-\u0177]|(?:\\[^\r\n\f0-9a-fA-F]))*))/;
var exprPat = /^\s*\(\s*(?:([+-]?)([0-9]*)n\s*(?:([+-])\s*([0-9]))?|(odd|even)|([+-]?[0-9]+))\s*\)/;
var pat = /^(?:([\r\n\t\ ]+)|([*.,>])|(string|boolean|null|array|object|number)|(:(?:root|first-child|last-child|only-child))|(:(?:nth-child|nth-last-child))|(:\w+)|(\"(?:[^\\]|\\[^\"])*\")|(\")|((?:[_a-zA-Z]|[^\0-\0177]|\\[^\r\n\f0-9a-fA-F])(?:[_a-zA-Z0-9\-]|[^\u0000-\u0177]|(?:\\[^\r\n\f0-9a-fA-F]))*))/;
var exprPat = /^\s*\(\s*(?:([+\-]?)([0-9]*)n\s*(?:([+\-])\s*([0-9]))?|(odd|even)|([+\-]?[0-9]+))\s*\)/;
var lex = function (str, off) {
if (!off) off = 0;
var m = pat.exec(str.substr(off));
Expand All @@ -57,19 +67,20 @@
// THE PARSER

var parse = function (str) {
var am = undefined, a = [], off = 0;
var a = [], off = 0, am;

while (true) {
var s = parse_selector(str, off);
a.push(s[1]);
s = lex(str, off = s[0]);
if (s && s[1] === ' ') s = lex(str, off = s[0]);
if (s && s[1] === " ") s = lex(str, off = s[0]);
if (!s) break;
// now we've parsed a selector, and have something else...
if (s[1] === ">") {
a.push(">");
off = s[0];
} else if (s[1] === ",") {
if (am == undefined) am = [ ",", a ];
if (am === undefined) am = [ ",", a ];
else am.push(a);
a = [];
off = s[0];
Expand Down Expand Up @@ -98,19 +109,19 @@
while (true) {
if (l === undefined) {
break;
} else if (l[1] === '.') {
} else if (l[1] === ".") {
l = lex(str, (off = l[0]));
if (!l || l[1] !== toks.str) te("sra");
if (s.id) te("nmi");
s.id = l[2];
} else if (l[1] === toks.psc) {
if (s.pc || s.pf) te("mpc");
// collapse first-child and last-child into nth-child expressions
if (l[2] === ':first-child') {
if (l[2] === ":first-child") {
s.pf = ":nth-child";
s.a = 0;
s.b = 1;
} else if (l[2] === ':last-child') {
} else if (l[2] === ":last-child") {
s.pf = ":nth-last-child";
s.a = 0;
s.b = 1;
Expand All @@ -124,12 +135,12 @@
if (!m) te("mepf");
if (m[5]) {
s.a = 2;
s.b = (m[5] === 'odd') ? 1 : 0;
s.b = (m[5] === "odd") ? 1 : 0;
} else if (m[6]) {
s.a = 0;
s.b = parseInt(m[6], 10);
} else {
s.a = parseInt((m[1] ? m[1] : "+") + (m[2] ? m[2] : "1"),10)
s.a = parseInt((m[1] ? m[1] : "+") + (m[2] ? m[2] : "1"),10);
s.b = m[3] ? parseInt(m[3] + m[4],10) : 0;
}
l[0] += m[0].length;
Expand All @@ -148,20 +159,21 @@
// THE EVALUATOR

function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
return Array.isArray ? Array.isArray(o) :
toString.call(o) === "[object Array]";
}

function mytypeof(o) {
if (o === null) return 'null';
if (o === null) return "null";
var to = typeof o;
if (to === 'object' && isArray(o)) to = 'array';
if (to === "object" && isArray(o)) to = "array";
return to;
}

function mn(node, sel, id, num, tot) {
var sels = [];
var cs = (sel[0] === '>') ? sel[1] : sel[0];
var m = true;
var cs = (sel[0] === ">") ? sel[1] : sel[0];
var m = true, mod;
if (cs.type) m = m && (cs.type === mytypeof(node));
if (cs.id) m = m && (cs.id === id);
if (m && cs.pf) {
Expand All @@ -170,66 +182,98 @@
if (cs.a === 0) {
m = cs.b === num;
} else {
m = (!((num - cs.b) % cs.a) && ((num*cs.a + cs.b) >= 0));
mod = ((num - cs.b) % cs.a);

m = (!mod && ((num*cs.a + cs.b) >= 0));
}
}

// should we repeat this selector for descendants?
if (sel[0] !== '>' && sel[0].pc !== ":root") sels.push(sel);
if (sel[0] !== ">" && sel[0].pc !== ":root") sels.push(sel);

if (m) {
// is there a fragment that we should pass down?
if (sel[0] === '>') { if (sel.length > 2) { m = false; sels.push(sel.slice(2)); } }
if (sel[0] === ">") { if (sel.length > 2) { m = false; sels.push(sel.slice(2)); } }
else if (sel.length > 1) { m = false; sels.push(sel.slice(1)); }
}

return [m, sels];
}

function forEach(sel, obj, fun, id, num, tot) {
var a = (sel[0] === ',') ? sel.slice(1) : [sel];
var a0 = [];
var call = false;
for (var i = 0; i < a.length; i++) {
var x = mn(obj, a[i], id, num, tot);
if (x[0]) call = true;
for (var j = 0; j < x[1].length; j++) a0.push(x[1][j]);
var a = (sel[0] === ",") ? sel.slice(1) : [sel],
a0 = [],
call = false,
i = 0, j = 0, l = 0, k, x;
for (i = 0; i < a.length; i++) {
x = mn(obj, a[i], id, num, tot);
if (x[0]) {
call = true;
}
for (j = 0; j < x[1].length; j++) {
a0.push(x[1][j]);
}
}
if (a0.length && typeof obj === 'object') {
if (a0.length >= 1) a0.unshift(",");
if (a0.length && typeof obj === "object") {
if (a0.length >= 1) {
a0.unshift(",");
}
if (isArray(obj)) {
for (var i = 0; i < obj.length; i++) forEach(a0, obj[i], fun, undefined, i, obj.length);
for (i = 0; i < obj.length; i++) {
forEach(a0, obj[i], fun, undefined, i, obj.length);
}
} else {
// it's a shame to do this for :last-child and other
// properties which count from the end when we don't
// even know if they're present. Also, the stream
// parser is going to be pissed.
var l = 0;
for (var k in obj) if (obj.hasOwnProperty(k)) l++;
var i = 0;
for (var k in obj) if (obj.hasOwnProperty(k)) forEach(a0, obj[k], fun, k, i++, l);
l = 0;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
l++;
}
}
i = 0;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
forEach(a0, obj[k], fun, k, i++, l);
}
}
}
}
if (call && fun) fun(obj);
};
if (call && fun) {
fun(obj);
}
}

function match(sel, obj) {
var a = [];
forEach(sel, obj, function(x) { a.push(x); });
forEach(sel, obj, function(x) {
a.push(x);
});
return a;
};
}

function compile(sel) {
return {
sel: parse(sel),
match: function(obj){return match(this.sel, obj)},
forEach: function(obj, fun) { return forEach(this.sel, obj, fun) }
match: function(obj){
return match(this.sel, obj);
},
forEach: function(obj, fun) {
return forEach(this.sel, obj, fun);
}
};
}

exports._lex = lex;
exports._parse = parse;
exports.match = function (sel, obj) { return compile(sel).match(obj) };
exports.forEach = function(sel, obj, fun) { return compile(sel).forEach(obj, fun) };
exports.match = function (sel, obj) {
return compile(sel).match(obj);
};
exports.forEach = function(sel, obj, fun) {
return compile(sel).forEach(obj, fun);
};
exports.compile = compile;
})(typeof exports === "undefined" ? (window.JSONSelect = {}) : exports);
})(typeof exports === "undefined" ? (window.JSONSelect = {}) : exports);

0 comments on commit f440b93

Please sign in to comment.