JS implement CSS style selector (2KB after compression) _javascript tips

Source: Internet
Author: User
Recently in doing some OA front-end interface, in order to better manage the page code to write a JS selector, write written to find very laborious, simply looking for a look on the Internet, Kung Fu, find a mini CSS selector, and performance extraordinary: The following code is compressed, only 2KB.
Copy Code code as follows:

var $= (function () {var b=/(?: [\w\-\\.#]+) + (?: \ [\w+?= ([\]])? (?:\ \\1|.) +?\1\])? | \*|>/ig,g=/^ (?: [\w\-_]+)? \. ([\w\-_]+)/,f=/^ (?: [\w\-_]+) # ([\w\-_]+)/,j=/^ ([\w\*\-_]+)/,h=[null,null];function D (o,m) {m=m| | Document;var k=/^[\w\-_#]+$/.test (o); if (!k&&m.queryselectorall) {return C (M.queryselectorall (O))}if ( O.indexof (",") >-1) {var v=o.split (/,/g), t=[],s=0,r=v.length;for (; s<r;++s) {T=t.concat (d (v[s],m))}return E (t) }var P=o.match (b), N=p.pop (), l= (N.match (f) | | h) [1],u=!l&& (N.match (g) | | h) [1],w=!l&& (N.match (j) | | h) [1],q;if (U&&!w&&m.getelementsbyclassname) {q=c (M.getelementsbyclassname (u))}else{q=!l& &c (M.getelementsbytagname (w| |) * ")); if (u) {q=i (q," ClassName ", RegExp (" (^|\\s) "+u+" (\\s|$) "))}if (l) {var x=m.getelementbyid (l); return x?[ X]:[]}}return P[0]&&q[0]?a (p,q): q}function C (o) {try{return Array.prototype.slice.call (o)}catch (n) {var l=[] , m=0,k=o.length;for (; m<k;++m) {L[m]=o[m]}return l}}function A (w,p,n) {var q=w.pop (); if (q=== ">") {return a (w,p, true)}var s=[],k=-1,l= (Q.match (f) | | h) [1],t=!l&& (Q.match (g) | | h) [1],v=!l&& (Q.match (j) | | h) [1],u=-1,m,x,o;v=v&&v.tolowercase (); while ((M=p[++u])) {x=m.parentnode;do{o=!v| | v=== "*" | | V===x.nodename.tolowercase ();o=o&& (!l| | x.id===l);o=o&& (!t| | RegExp ("(^|\\s)" +t+ "(\\s|$)"). Test (X.classname); if (n| | O) {break}}while ((x=x.parentnode)); if (o) {S[++k]=m}}return w[0]&&s[0]?a (w,s): S}var e= (function () {var k=+new Date (); var l= (function () {var m=1;return function (p) {var o=p[k],n=m++;if (!o) {P[k]=n;return True}return false}}) (); return function (m) {var s=m.length,n=[],q=-1,o=0,p;for (; o<s;++o) {p=m[o];if (L (p)) {N[++q]=p}}k+=1;return n}}) (); function I (q,k,p) {var m=-1,o,n=-1,l=[];while ((o=q[++m)) {if (P.test (o[k))) {L[++n]=o}}return L}return D}) ();

Share the original:
Copy Code code as follows:

/**
* "Mini" Selector Engine
* Copyright (c) 2009 James Padolsey
* -------------------------------------------------------
* Dual licensed under the MIT and GPL licenses.
*-http://www.opensource.org/licenses/mit-license.php
*-http://www.gnu.org/copyleft/gpl.html
* -------------------------------------------------------
* version:0.01 (BETA)
*/
var mini = (function () {
var snack =/(?: [\w\-\\.#]+) + (?: \ [\w+?= ([\]])? (?:\ \\1|.) +?\1\])? | \*|>/ig,
Exprclassname =/^ (?: [\w\-_]+)? \. ([\w\-_]+)/,
Exprid =/^ (?: [\w\-_]+)? # ([\w\-_]+)/,
Exprnodename =/^ ([\w\*\-_]+)/,
NA = [Null,null];
function _find (selector, context) {
/**
* This is what your call via X () it's your so-called X
* Starts everything off ... The beginning of all the
*/
Context = Context | | Document
var simple =/^[\w\-_#]+$/.test (selector);
if (!simple && context.queryselectorall) {
Return Realarray (Context.queryselectorall (selector));
}
if (Selector.indexof (', ') >-1) {
var split = Selector.split (/,/g), ret = [], Sindex = 0, len = split.length;
for (; Sindex < Len; ++sindex) {
ret = Ret.concat (_find (Split[sindex), context);
}
Return unique (ret);
}
var parts = Selector.match (snack),
Part = Parts.pop (),
id = (Part.match (exprid) | | na) [1],
ClassName =!id && (part.match (exprclassname) | | na) [1],
NodeName =!id && (part.match (exprnodename) | | na) [1],
Collection
if (className &&!nodename && context.getelementsbyclassname) {
Collection = Realarray (Context.getelementsbyclassname (className));
} else {
Collection =!id && realarray (context.getelementsbytagname nodename | | '*'));
if (className) {
Collection = Filterbyattr (collection, ' ClassName ', RegExp (' (^|\\s) ' + ClassName + ' (\\s|$) '));
}
if (ID) {
var Byid = Context.getelementbyid (ID);
Return Byid? [byid]:[];
}
}
return parts[0] && collection[0]? Filterparents (Parts, collection): collection;
}
function Realarray (c) {
/**
* Transforms a node collection into converting a collection of nodes
* A real array
*/
try {
return Array.prototype.slice.call (c);
catch (e) {
var ret = [], i = 0, len = c.length;
for (; i < Len; ++i) {
Ret[i] = C[i];
}
return ret;
}
}
function Filterparents (Selectorparts, collection, direct) {
/**
* This is where the magic happens. This is how magic happens.
* Parents are stepped through (upwards) to parents stepped up through the
* The If they comply with the selector. See if they fit the selector.
*/
var parentselector = Selectorparts.pop ();
if (Parentselector = = ' > ') {
Return Filterparents (Selectorparts, collection, true);
}
var ret = [],
r =-1,
id = (Parentselector.match (exprid) | | na) [1],
ClassName =!id && (parentselector.match (exprclassname) | | na) [1],
NodeName =!id && (parentselector.match (exprnodename) | | na) [1],
CIndex =-1,
node, parent,
Matches;
nodename = nodename && nodename.tolowercase ();
while ((node = Collection[++cindex])) {
parent = Node.parentnode;
do {
Matches =!nodename | | NodeName = = ' * ' | | NodeName = = Parent.nodeName.toLowerCase ();
matches = Matches && (!id | | parent.id = = ID);
matches = Matches && (!classname | | RegExp (' (^|\\s) ' + ClassName + ' (\\s|$) '). Test (Parent.classname));
if (Direct | | matches) {break;}
while (parent = Parent.parentnode);
if (matches) {
RET[++R] = node;
}
}
return selectorparts[0] && ret[0]? Filterparents (Selectorparts, ret): ret;
}
var unique = (function () {
var uid = +new Date ();
var data = (function () {
var n = 1;
return function (Elem) {
var cacheindex = Elem[uid],
Nextcacheindex = n++;
if (!cacheindex) {
Elem[uid] = Nextcacheindex;
return true;
}
return false;
};
})();
return function (arr) {
/**
* Returns A unique array returns a distinct pattern
*/
var length = Arr.length,
ret = [],
r =-1,
i = 0,
Item
for (; i < length; ++i) {
item = Arr[i];
if (data (item)) {
RET[++R] = Item;
}
}
UID + 1;
return ret;
};
})();
function Filterbyattr (collection, attr, regex) {
/**
* Filters A collection by an attribute. A collection filter an attribute
*/
var i =-1, node, r =-1, ret = [];
while ((node = collection[++i])) {
if (Regex.test (Node[attr])) {
RET[++R] = node;
}
}
return ret;
}
return _find;
})();

The above code supports CSS style style writing including:
Copy Code code as follows:

Div
. Example
Body Div
Div, p
Div, p,. Example
Div p
div > P
Div.example
UL. Example
#title
H1#title
Div #title
Ul.foo > * span
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.