Skip to content
Browse files

Merge branch 'master' of git://github.com/adamsanderson/selectorgadge…

…t into adamsanderson/master
  • Loading branch information...
2 parents 455b528 + ace0479 commit de0c3eea1c400101ec7d39d7a931505de5299310 @cantino cantino committed Apr 19, 2009
Showing with 71 additions and 7 deletions.
  1. +13 −7 lib/dom.js
  2. +58 −0 sites/test/testCSSTokenizing.html
View
20 lib/dom.js
@@ -94,22 +94,28 @@ DomPredictionHelper.prototype.tokenizeCss = function(css_string) {
var skip = false;
var word = '';
var tokens = [];
-// if (css_string.substring(0,1) != ' ') css_string = " " + css_string;
- jQuery.each(css_string.replace(/,/, ' , ').replace(/\s+/g, ' ').split(''), function() {
+
+ var css_string = css_string.replace(/,/, ' , ').replace(/\s+/g, ' ');
+ var length = css_string.length;
+ var c = '';
+
+ for (var i = 0; i < length; i++){
+ c = css_string[i];
+
if (skip) {
skip = false;
- } else if (this == '\\') {
+ } else if (c == '\\') {
skip = true;
- } else if (this == '.' || this == ' ' || this == '#' || this == '>' || this == ':' || this == ',') {
+ } else if (c == '.' || c == ' ' || c == '#' || c == '>' || c == ':' || c == ',') {
if (word.length > 0) tokens.push(word);
word = '';
}
- word += this;
- if (this == ' ' || this == ',') {
+ word += c;
+ if (c == ' ' || c == ',') {
tokens.push(word);
word = '';
}
- });
+ }
if (word.length > 0) tokens.push(word);
return tokens;
};
View
58 sites/test/testCSSTokenizing.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+
+ <title>CSS Path Tokenizing Test</title>
+ <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
+ <script src="../../lib/dom.js" type="text/javascript" charset="utf-8"></script>
+
+ <script type="text/javascript" charset="utf-8">
+ $(document).ready(function () {
+ $("#start").click(function(){
+ runTests();
+ });
+ });
+
+ function runTests(){
+ var domPred = new DomPredictionHelper();
+ var out = $('#results').html('');
+
+ $('#paths .test').each(function(i,e){
+ var text = $(e).text();
+ var start = new Date();
+ var n = 100; while(n-- > 0){
+ var tokens = domPred.tokenizeCss(text);
+ }
+ var finish = new Date();
+ var time = finish.getTime() - start.getTime();
+
+ out.append($('<dt>').text(text));
+ out.append($('<dd>').text(time+'ms'));
+ });
+
+ return false;
+ }
+ </script>
+</head>
+
+<body>
+ <p>
+ Below are a variety of css paths, and the time it takes to parse each of them.
+ <a href='#' id='start'>Run Tests</a>
+ </p>
+ <h1>Tests</h1>
+ <div id='paths'>
+ <div class='test'> a </div>
+ <div class='test'> .videos a </div>
+ <div class='test'> .videos a, img, li:nth-child(10) a </div>
+ <div class='test'> img , .offsite a, li:nth-child(10) a, #cnnT2Col li:nth-child(7) a </div>
+ </div>
+
+ <h2>Results</h2>
+ <p>Milliseconds for 100 calls</p>
+ <dl id='results'></dl>
+</body>
+</html>

0 comments on commit de0c3ee

Please sign in to comment.
Something went wrong with that request. Please try again.