Skip to content

Instantly share code, notes, and snippets.

@arian
Created October 9, 2015 14:11
Show Gist options
  • Save arian/600f3913db18bfb2a7cc to your computer and use it in GitHub Desktop.
Save arian/600f3913db18bfb2a7cc to your computer and use it in GitHub Desktop.
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var slick = require('slick');
var sMyString = "<foo><bar></bar></foo>";
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(sMyString, "text/xml");
console.log(slick('bar', oDOM));
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var parse=require("./parser");var index=0,counter=document.__counter=(parseInt(document.__counter||-1,36)+1).toString(36),key="uid:"+counter;var uniqueID=function(n,xml){if(n===window)return"window";if(n===document)return"document";if(n===document.documentElement)return"html";if(xml){var uid=n.getAttribute(key);if(!uid){uid=(index++).toString(36);n.setAttribute(key,uid)}return uid}else{return n[key]||(n[key]=(index++).toString(36))}};var uniqueIDXML=function(n){return uniqueID(n,true)};var isArray=Array.isArray||function(object){return Object.prototype.toString.call(object)==="[object Array]"};var uniqueIndex=0;var HAS={GET_ELEMENT_BY_ID:function(test,id){id="slick_"+uniqueIndex++;test.innerHTML='<a id="'+id+'"></a>';return!!this.getElementById(id)},QUERY_SELECTOR:function(test){test.innerHTML="_<style>:nth-child(2){}</style>";test.innerHTML='<a class="MiX"></a>';return test.querySelectorAll(".MiX").length===1},EXPANDOS:function(test,id){id="slick_"+uniqueIndex++;test._custom_property_=id;return test._custom_property_===id},MATCHES_SELECTOR:function(test){test.className="MiX";var matches=test.matchesSelector||test.mozMatchesSelector||test.webkitMatchesSelector;if(matches)try{matches.call(test,":slick")}catch(e){return matches.call(test,".MiX")?matches:false}return false},GET_ELEMENTS_BY_CLASS_NAME:function(test){test.innerHTML='<a class="f"></a><a class="b"></a>';if(test.getElementsByClassName("b").length!==1)return false;test.firstChild.className="b";if(test.getElementsByClassName("b").length!==2)return false;test.innerHTML='<a class="a"></a><a class="f b a"></a>';if(test.getElementsByClassName("a").length!==2)return false;return true},GET_ATTRIBUTE:function(test){var shout="fus ro dah";test.innerHTML='<a class="'+shout+'"></a>';return test.firstChild.getAttribute("class")===shout}};var Finder=function Finder(document){this.document=document;var root=this.root=document.documentElement;this.tested={};this.uniqueID=this.has("EXPANDOS")?uniqueID:uniqueIDXML;this.getAttribute=this.has("GET_ATTRIBUTE")?function(node,name){return node.getAttribute(name)}:function(node,name){node=node.getAttributeNode(name);return node&&node.specified?node.value:null};this.hasAttribute=root.hasAttribute?function(node,attribute){return node.hasAttribute(attribute)}:function(node,attribute){node=node.getAttributeNode(attribute);return!!(node&&node.specified)};this.contains=document.contains&&root.contains?function(context,node){return context.contains(node)}:root.compareDocumentPosition?function(context,node){return context===node||!!(context.compareDocumentPosition(node)&16)}:function(context,node){do{if(node===context)return true}while(node=node.parentNode);return false};this.sorter=root.compareDocumentPosition?function(a,b){if(!a.compareDocumentPosition||!b.compareDocumentPosition)return 0;return a.compareDocumentPosition(b)&4?-1:a===b?0:1}:"sourceIndex"in root?function(a,b){if(!a.sourceIndex||!b.sourceIndex)return 0;return a.sourceIndex-b.sourceIndex}:document.createRange?function(a,b){if(!a.ownerDocument||!b.ownerDocument)return 0;var aRange=a.ownerDocument.createRange(),bRange=b.ownerDocument.createRange();aRange.setStart(a,0);aRange.setEnd(a,0);bRange.setStart(b,0);bRange.setEnd(b,0);return aRange.compareBoundaryPoints(Range.START_TO_END,bRange)}:null;this.failed={};var nativeMatches=this.has("MATCHES_SELECTOR");if(nativeMatches)this.matchesSelector=function(node,expression){if(this.failed[expression])return null;try{return nativeMatches.call(node,expression)}catch(e){if(slick.debug)console.warn("matchesSelector failed on "+expression);this.failed[expression]=true;return null}};if(this.has("QUERY_SELECTOR")){this.querySelectorAll=function(node,expression){if(this.failed[expression])return true;var result,_id,_expression,_combinator,_node;if(node!==this.document){_combinator=expression[0].combinator;_id=node.getAttribute("id");_expression=expression;if(!_id){_node=node;_id="__slick__";_node.setAttribute("id",_id)}expression="#"+_id+" "+_expression;if(_combinator.indexOf("~")>-1||_combinator.indexOf("+")>-1){node=node.parentNode;if(!node)result=true}}if(!result)try{result=node.querySelectorAll(expression.toString())}catch(e){if(slick.debug)console.warn("querySelectorAll failed on "+(_expression||expression));result=this.failed[_expression||expression]=true}if(_node)_node.removeAttribute("id");return result}}};Finder.prototype.has=function(FEATURE){var tested=this.tested,testedFEATURE=tested[FEATURE];if(testedFEATURE!=null)return testedFEATURE;var root=this.root,document=this.document,testNode=document.createElement("div");testNode.setAttribute("style","display: none;");root.appendChild(testNode);var TEST=HAS[FEATURE],result=false;if(TEST)try{result=TEST.call(document,testNode)}catch(e){}if(slick.debug&&!result)console.warn("document has no "+FEATURE);root.removeChild(testNode);return tested[FEATURE]=result};var combinators={" ":function(node,part,push){var item,items;var noId=!part.id,noTag=!part.tag,noClass=!part.classes;if(part.id&&node.getElementById&&this.has("GET_ELEMENT_BY_ID")){item=node.getElementById(part.id);if(item&&item.getAttribute("id")===part.id){items=[item];noId=true;if(part.tag==="*")noTag=true}}if(!items){if(part.classes&&node.getElementsByClassName&&this.has("GET_ELEMENTS_BY_CLASS_NAME")){items=node.getElementsByClassName(part.classList);noClass=true;if(part.tag==="*")noTag=true}else{items=node.getElementsByTagName(part.tag);if(part.tag!=="*")noTag=true}if(!items||!items.length)return false}for(var i=0;item=items[i++];)if(noTag&&noId&&noClass&&!part.attributes&&!part.pseudos||this.match(item,part,noTag,noId,noClass))push(item);return true},">":function(node,part,push){if(node=node.firstChild)do{if(node.nodeType==1&&this.match(node,part))push(node)}while(node=node.nextSibling)},"+":function(node,part,push){while(node=node.nextSibling)if(node.nodeType==1){if(this.match(node,part))push(node);break}},"^":function(node,part,push){node=node.firstChild;if(node){if(node.nodeType===1){if(this.match(node,part))push(node)}else{combinators["+"].call(this,node,part,push)}}},"~":function(node,part,push){while(node=node.nextSibling){if(node.nodeType===1&&this.match(node,part))push(node)}},"++":function(node,part,push){combinators["+"].call(this,node,part,push);combinators["!+"].call(this,node,part,push)},"~~":function(node,part,push){combinators["~"].call(this,node,part,push);combinators["!~"].call(this,node,part,push)},"!":function(node,part,push){while(node=node.parentNode)if(node!==this.document&&this.match(node,part))push(node)},"!>":function(node,part,push){node=node.parentNode;if(node!==this.document&&this.match(node,part))push(node)},"!+":function(node,part,push){while(node=node.previousSibling)if(node.nodeType==1){if(this.match(node,part))push(node);break}},"!^":function(node,part,push){node=node.lastChild;if(node){if(node.nodeType==1){if(this.match(node,part))push(node)}else{combinators["!+"].call(this,node,part,push)}}},"!~":function(node,part,push){while(node=node.previousSibling){if(node.nodeType===1&&this.match(node,part))push(node)}}};Finder.prototype.search=function(context,expression,found){if(!context)context=this.document;else if(!context.nodeType&&context.document)context=context.document;var expressions=parse(expression);if(!expressions||!expressions.length)throw new Error("invalid expression");if(!found)found=[];var uniques,push=isArray(found)?function(node){found[found.length]=node}:function(node){found[found.length++]=node};if(expressions.length>1){uniques={};var plush=push;push=function(node){var uid=uniqueID(node);if(!uniques[uid]){uniques[uid]=true;plush(node)}}}var node,nodes,part;main:for(var i=0;expression=expressions[i++];){if(!slick.noQSA&&this.querySelectorAll){nodes=this.querySelectorAll(context,expression);if(nodes!==true){if(nodes&&nodes.length)for(var j=0;node=nodes[j++];)if(node.nodeName>"@"){push(node)}continue main}}if(expression.length===1){part=expression[0];combinators[part.combinator].call(this,context,part,push)}else{var cs=[context],c,f,u,p=function(node){var uid=uniqueID(node);if(!u[uid]){u[uid]=true;f[f.length]=node}};for(var j=0;part=expression[j++];){f=[];u={};for(var k=0;c=cs[k++];)combinators[part.combinator].call(this,c,part,p);if(!f.length)continue main;cs=f}if(i===0)found=f;else for(var l=0;l<f.length;l++)push(f[l])}}if(uniques&&found&&found.length>1)this.sort(found);return found};Finder.prototype.sort=function(nodes){return this.sorter?Array.prototype.sort.call(nodes,this.sorter):nodes};var pseudos={empty:function(){return!(this&&this.nodeType===1)&&!(this.innerText||this.textContent||"").length},not:function(expression){return!slick.matches(this,expression)},contains:function(text){return(this.innerText||this.textContent||"").indexOf(text)>-1},"first-child":function(){var node=this;while(node=node.previousSibling)if(node.nodeType==1)return false;return true},"last-child":function(){var node=this;while(node=node.nextSibling)if(node.nodeType==1)return false;return true},"only-child":function(){var prev=this;while(prev=prev.previousSibling)if(prev.nodeType==1)return false;var next=this;while(next=next.nextSibling)if(next.nodeType==1)return false;return true},"first-of-type":function(){var node=this,nodeName=node.nodeName;while(node=node.previousSibling)if(node.nodeName==nodeName)return false;return true},"last-of-type":function(){var node=this,nodeName=node.nodeName;while(node=node.nextSibling)if(node.nodeName==nodeName)return false;return true},"only-of-type":function(){var prev=this,nodeName=this.nodeName;while(prev=prev.previousSibling)if(prev.nodeName==nodeName)return false;var next=this;while(next=next.nextSibling)if(next.nodeName==nodeName)return false;return true},enabled:function(){return!this.disabled},disabled:function(){return this.disabled},checked:function(){return this.checked||this.selected},selected:function(){return this.selected},focus:function(){var doc=this.ownerDocument;return doc.activeElement===this&&(this.href||this.type||slick.hasAttribute(this,"tabindex"))},root:function(){return this===this.ownerDocument.documentElement}};Finder.prototype.match=function(node,bit,noTag,noId,noClass){if(!slick.noQSA&&this.matchesSelector){var matches=this.matchesSelector(node,bit);if(matches!==null)return matches}if(!noTag&&bit.tag){var nodeName=node.nodeName.toLowerCase();if(bit.tag==="*"){if(nodeName<"@")return false}else if(nodeName!=bit.tag){return false}}if(!noId&&bit.id&&node.getAttribute("id")!==bit.id)return false;var i,part;if(!noClass&&bit.classes){var className=this.getAttribute(node,"class");if(!className)return false;for(part in bit.classes)if(!RegExp("(^|\\s)"+bit.classes[part]+"(\\s|$)").test(className))return false}var name,value;if(bit.attributes)for(i=0;part=bit.attributes[i++];){var operator=part.operator,escaped=part.escapedValue;name=part.name;value=part.value;if(!operator){if(!this.hasAttribute(node,name))return false}else{var actual=this.getAttribute(node,name);if(actual==null)return false;switch(operator){case"^=":if(!RegExp("^"+escaped).test(actual))return false;break;case"$=":if(!RegExp(escaped+"$").test(actual))return false;break;case"~=":if(!RegExp("(^|\\s)"+escaped+"(\\s|$)").test(actual))return false;break;case"|=":if(!RegExp("^"+escaped+"(-|$)").test(actual))return false;break;case"=":if(actual!==value)return false;break;case"*=":if(actual.indexOf(value)===-1)return false;break;default:return false}}}if(bit.pseudos)for(i=0;part=bit.pseudos[i++];){name=part.name;value=part.value;if(pseudos[name])return pseudos[name].call(node,value);if(value!=null){if(this.getAttribute(node,name)!==value)return false}else{if(!this.hasAttribute(node,name))return false}}return true};Finder.prototype.matches=function(node,expression){var expressions=parse(expression);if(expressions.length===1&&expressions[0].length===1){return this.match(node,expressions[0][0])}if(!slick.noQSA&&this.matchesSelector){var matches=this.matchesSelector(node,expressions);if(matches!==null)return matches}var nodes=this.search(this.document,expression,{length:0});for(var i=0,res;res=nodes[i++];)if(node===res)return true;return false};var finders={};var finder=function(context){var doc=context||document;if(doc.ownerDocument)doc=doc.ownerDocument;else if(doc.document)doc=doc.document;if(doc.nodeType!==9)throw new TypeError("invalid document");var uid=uniqueID(doc);return finders[uid]||(finders[uid]=new Finder(doc))};var slick=function(expression,context){return slick.search(expression,context)};slick.search=function(expression,context,found){return finder(context).search(context,expression,found)};slick.find=function(expression,context){return finder(context).search(context,expression)[0]||null};slick.getAttribute=function(node,name){return finder(node).getAttribute(node,name)};slick.hasAttribute=function(node,name){return finder(node).hasAttribute(node,name)};slick.contains=function(context,node){return finder(context).contains(context,node)};slick.matches=function(node,expression){return finder(node).matches(node,expression)};slick.sort=function(nodes){if(nodes&&nodes.length>1)finder(nodes[0]).sort(nodes);return nodes};slick.parse=parse;module.exports=slick},{"./parser":2}],2:[function(require,module,exports){"use strict";var escapeRe=/([-.*+?^${}()|[\]\/\\])/g,unescapeRe=/\\/g;var escape=function(string){return(string+"").replace(escapeRe,"\\$1")};var unescape=function(string){return(string+"").replace(unescapeRe,"")};var slickRe=RegExp("^(?:\\s*(,)\\s*|\\s*(<combinator>+)\\s*|(\\s+)|(<unicode>+|\\*)|\\#(<unicode>+)|\\.(<unicode>+)|\\[\\s*(<unicode1>+)(?:\\s*([*^$!~|]?=)(?:\\s*(?:([\"']?)(.*?)\\9)))?\\s*\\](?!\\])|(:+)(<unicode>+)(?:\\((?:(?:([\"'])([^\\13]*)\\13)|((?:\\([^)]+\\)|[^()]*)+))\\))?)".replace(/<combinator>/,"["+escape(">+~`!@$%^&={}\\;</")+"]").replace(/<unicode>/g,"(?:[\\w\\u00a1-\\uFFFF-]|\\\\[^\\s0-9a-f])").replace(/<unicode1>/g,"(?:[:\\w\\u00a1-\\uFFFF-]|\\\\[^\\s0-9a-f])"));var Part=function Part(combinator){this.combinator=combinator||" ";this.tag="*"};Part.prototype.toString=function(){if(!this.raw){var xpr="",k,part;xpr+=this.tag||"*";if(this.id)xpr+="#"+this.id;if(this.classes)xpr+="."+this.classList.join(".");if(this.attributes)for(k=0;part=this.attributes[k++];){xpr+="["+part.name+(part.operator?part.operator+'"'+part.value+'"':"")+"]"}if(this.pseudos)for(k=0;part=this.pseudos[k++];){xpr+=":"+part.name;if(part.value)xpr+="("+part.value+")"}this.raw=xpr}return this.raw};var Expression=function Expression(){this.length=0};Expression.prototype.toString=function(){if(!this.raw){var xpr="";for(var j=0,bit;bit=this[j++];){if(j!==1)xpr+=" ";if(bit.combinator!==" ")xpr+=bit.combinator+" ";xpr+=bit}this.raw=xpr}return this.raw};var replacer=function(rawMatch,separator,combinator,combinatorChildren,tagName,id,className,attributeKey,attributeOperator,attributeQuote,attributeValue,pseudoMarker,pseudoClass,pseudoQuote,pseudoClassQuotedValue,pseudoClassValue){var expression,current;if(separator||!this.length){expression=this[this.length++]=new Expression;if(separator)return""}if(!expression)expression=this[this.length-1];if(combinator||combinatorChildren||!expression.length){current=expression[expression.length++]=new Part(combinator)}if(!current)current=expression[expression.length-1];if(tagName){current.tag=unescape(tagName)}else if(id){current.id=unescape(id)}else if(className){var unescaped=unescape(className);var classes=current.classes||(current.classes={});if(!classes[unescaped]){classes[unescaped]=escape(className);var classList=current.classList||(current.classList=[]);classList.push(unescaped);classList.sort()}}else if(pseudoClass){pseudoClassValue=pseudoClassValue||pseudoClassQuotedValue;(current.pseudos||(current.pseudos=[])).push({type:pseudoMarker.length==1?"class":"element",name:unescape(pseudoClass),escapedName:escape(pseudoClass),value:pseudoClassValue?unescape(pseudoClassValue):null,escapedValue:pseudoClassValue?escape(pseudoClassValue):null})}else if(attributeKey){attributeValue=attributeValue?escape(attributeValue):null;(current.attributes||(current.attributes=[])).push({operator:attributeOperator,name:unescape(attributeKey),escapedName:escape(attributeKey),value:attributeValue?unescape(attributeValue):null,escapedValue:attributeValue?escape(attributeValue):null})}return""};var Expressions=function Expressions(expression){this.length=0;var self=this;var original=expression,replaced;while(expression){replaced=expression.replace(slickRe,function(){return replacer.apply(self,arguments)});if(replaced===expression)throw new Error(original+" is an invalid expression");expression=replaced}};Expressions.prototype.toString=function(){if(!this.raw){var expressions=[];for(var i=0,expression;expression=this[i++];)expressions.push(expression);this.raw=expressions.join(", ")}return this.raw};var cache={};var parse=function(expression){if(expression==null)return null;expression=(""+expression).replace(/^\s+|\s+$/g,"");return cache[expression]||(cache[expression]=new Expressions(expression))};module.exports=parse},{}],slick:[function(require,module,exports){(function(global){"use strict";module.exports="document"in global?require("./finder"):{parse:require("./parser")}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./finder":1,"./parser":2}]},{},[]);var slick=require("slick");var sMyString="<foo><bar></bar></foo>";var oParser=new DOMParser;var oDOM=oParser.parseFromString(sMyString,"text/xml");console.log(slick("bar",oDOM));
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"slick": "1.12.2"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment