Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function Selector(sel) {
- this.valid = true;
- this.header = "@a";
- this.args = {};
- {
- if(typeof sel != "string") {this.valid = false;return null};
- var validHeaders = ["@a","@p","@e","@r"];
- if(validHeaders.indexOf(sel.substring(0,2)) > -1) {
- this.header = sel.substring(0,2);
- } else {this.valid = false;return null;}
- sel = sel.substring(2);
- var tempArgs = {};
- var token = "";
- var key = "";
- var argsCount = 0;
- var action = "GET_KEY";
- iteration: for(var i = 0; i < sel.length; i++) {
- var c = sel.charAt(i);
- if(i == 0) {
- if(c != "[") break; else continue;
- }
- if("]=, ".indexOf(c) > -1) {
- switch(c) {
- case "]":
- case ",": {
- if(action == "GET_KEY" && token.length > 0) {
- key = ["x","y","z","r"][argsCount];
- if(key == undefined) {
- break iteration;
- }
- tempArgs[key] = token;
- token = key = "";
- argsCount++;
- break;
- } else if(action == "GET_VALUE") {
- tempArgs[key] = token;
- token = key = "";
- action = "GET_KEY";
- argsCount++;
- break;
- }
- if(c == "]") {
- break iteration;
- }
- }
- case "=": {
- if(action == "GET_KEY") {
- key = token;
- token = "";
- action = "GET_VALUE";
- break;
- } else if(action == "GET_VALUE") {
- break iteration;
- }
- } case " ": {
- break iteration;
- }
- }
- } else {
- token += c;
- }
- }
- this.args = tempArgs;
- }
- this.clone = function() {
- var ns = new Selector();
- ns.valid = this.valid;
- ns.header = this.header;
- ns.args = {};
- for(var i in this.args) {
- ns.args[i] = this.args[i];
- }
- return ns;
- }
- }
- Selector.prototype.toString = function() {
- if(!this.valid) return null;
- var s = this.header;
- var hasArgs = false;
- var argsRaw = "";
- for(var i in this.args) {
- hasArgs = true;
- argsRaw += i + "=" + this.args[i] + ",";
- }
- if(argsRaw.charAt(argsRaw.length-1) == ",") argsRaw = argsRaw.substring(0,argsRaw.length-1);
- s += (hasArgs) ? ("[" + argsRaw + "]") : "";
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment