From 50d488f4c2137206e63f291ff99c4f09d177d31d Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Tue, 15 Sep 2009 08:00:03 +0000 Subject: Fixed the wildcard match and added a bunch of unit tests. git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@247 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/redirector.prototype.js | 87 +++++++++++++++++----------------- chrome/content/unittests.html | 10 ++-- chrome/content/unittests.js | 31 ++++++++++-- 3 files changed, 75 insertions(+), 53 deletions(-) (limited to 'chrome/content') diff --git a/chrome/content/redirector.prototype.js b/chrome/content/redirector.prototype.js index 6d8d3d2..d064764 100644 --- a/chrome/content/redirector.prototype.js +++ b/chrome/content/redirector.prototype.js @@ -8,7 +8,7 @@ kRedirectorRegex= 'R'; nsIContentPolicy = Ci.nsIContentPolicy; -RedirectorPolicy.prototype = { +Redirector.prototype = { prefBranch : null, list : null, strings : null, @@ -109,6 +109,7 @@ RedirectorPolicy.prototype = { return nsIContentPolicy.ACCEPT; }, + // nsIContentPolicy interface implementation shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) { return nsIContentPolicy.ACCEPT; }, @@ -123,8 +124,8 @@ RedirectorPolicy.prototype = { .getService(Ci.mozIJSSubScriptLoader) .loadSubScript('chrome://redirector/content/redirector.prototype.js'); - for (var key in RedirectorPolicy.prototype) { - this[key] = RedirectorPolicy.prototype[key]; + for (var key in Redirector.prototype) { + this[key] = Redirector.prototype[key]; } this.init(); }, @@ -217,55 +218,53 @@ RedirectorPolicy.prototype = { return uri.resolve(relativeUrl); }, - + wildcardMatch : function(pattern, text, redirectUrl, unescapeMatches) { - var parts - , part - , i - , pos - , originalText - , stars; - - if (!pattern) { - return null; - } - parts = pattern.split('*'); - - stars = []; - originalText = text; - var starStart = -1; - - for (i in parts) { - part = parts[i]; + if (!pattern || !text) { + return null; + } + if (pattern.indexOf('*') == -1) { + return (pattern == text) ? redirectUrl : null; + } + + var parts = pattern.split('*'); + var first = parts[0], + last = parts[parts.length-1]; - pos = text.lastIndexOf(part); + if (first) { + if (text.substr(0, first.length) != first) { + return null; + } + text = text.substr(first.length); + } + if (last) { + if (text.substr(text.length-last.length) != last) { + return null; + } + text = text.substr(0, text.length-last.length); + } + + if ((first || last) && parts.length == 2) { + return redirectUrl.replace('$1', text); + } + parts.splice(0,1); + parts.splice(parts.length-1,1); + var pos = 0, lastPos = 0; + var matches = []; + for each(part in parts) { + pos = text.indexOf(part, lastPos); if (pos == -1) { return null; } - - if (i == 0 && pos != 0) { - return null; - } - - if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) { - return null; - } - - if (i == 0) { - //Do nothing, part will be added on next run - } else if (i == parts.length-1 && parts[i] == '') { - stars.push(unescapeMatches ? unescape(text) : text); - } else { - stars.push(unescapeMatches ? unescape(text.substr(0, pos)) : text.substr(0, pos)); - } - - text = text.substr(pos + part.length); + var match = text.substr(lastPos, pos-lastPos); + matches.push(match); + lastPos = pos + part.length; } - - for (var i = 1; i <= stars.length; i++) { - redirectUrl = redirectUrl.replace(new RegExp('\\$' + i, 'gi'), stars[i-1]); + matches.push(text.substr(lastPos)); + for (var i = 1; i <= matches.length; i++) { + redirectUrl = redirectUrl.replace(new RegExp('\\$' + i, 'gi'), unescapeMatches ? unescape(matches[i-1]) : matches[i-1]); } return redirectUrl; diff --git a/chrome/content/unittests.html b/chrome/content/unittests.html index 1f84455..cee8488 100644 --- a/chrome/content/unittests.html +++ b/chrome/content/unittests.html @@ -6,7 +6,7 @@ body { font-family: Verdana, Arial; color:black; background-color:white; font-size:0.8em; width:800px; margin:auto; text-align:center;} a { color:blue; } h1 { text-align:center; margin:20px 0px; } - table { margin:auto; border:solid 1px black; width:500px; border-collapse:collapse;} + table { margin:auto; border:solid 1px black; width:700px; border-collapse:collapse;} td { border:solid 1px black; padding:3px; } td.result { width:20px; height:20px; padding:0;} td.result div { width:70%; height:70%; margin:auto; } @@ -45,6 +45,7 @@ } function setup() { + try{ var tables = document.getElementsByTagName('table'); for (var i = 0; i < tables.length; i++) { tables[i].parentNode.removeChild(tables[i]); @@ -56,6 +57,7 @@ for (var name in tests) { setupTest(name, tests[name]); } + }catch(e){alert(e);} } function runTests() { @@ -65,12 +67,12 @@ try { var result = testcase.run(testcase.tests[i]); if (result) { - document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'green'; + document.getElementById(testcaseName + '_' + i).style.backgroundColor = '#17f816'; } else { - document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'red'; + document.getElementById(testcaseName + '_' + i).style.backgroundColor = '#ff0000'; } } catch(e) { - document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'red'; + document.getElementById(testcaseName + '_' + i).style.backgroundColor = '#ff0000'; } } } diff --git a/chrome/content/unittests.js b/chrome/content/unittests.js index c532d3f..095b13b 100644 --- a/chrome/content/unittests.js +++ b/chrome/content/unittests.js @@ -1,12 +1,33 @@ var tests = { "Wildcard matches" : { - run : function(data) { return redirector.wildcardMatch(data[0], data[1], 'abc', false); }, - describe : function(data) { return data[0] + ' matches ' + data[1]; }, + run : function(data) { + var pattern = data[0], + url = data[1], + expected = data[2]; + var parts = expected.split(','); + var redirectUrl = ''; + if (!(parts.length == 1 && parts[0] == '')) { + for (var i in parts) { + redirectUrl += '$' + (parseFloat(i)+1) + ','; + } + redirectUrl = redirectUrl.substr(0, redirectUrl.length-1); + } + var result = redirector.wildcardMatch(pattern, url, redirectUrl, false); + return result == expected; + }, + + describe : function(data) { return data[0] + ' matches ' + data[1] + ', matches=' + data[2]; }, tests : [ - ['http://foo*', 'http://foobar.is'], - ['http://foo*', 'http://foo'], - ['*foo*', 'http://foo'] + ['http://foo*', 'http://foobar.is', 'bar.is'], + ['http://foo*', 'http://foo', ''], + ['*foo*', 'http://foo', 'http://,'], + ['*foo*', 'http://foobar.is', 'http://,bar.is'], + ['http://foo.is', 'http://foo.is', ''], + ['*', 'http://foo.is', 'http://foo.is'], + ['*://foo.is', 'http://foo.is', 'http'], + ['*://foo.is*', 'http://foo.is/bar/baz', 'http,/bar/baz'], + ['*://*oo*bar*', 'http://foo.is/bar/baz', 'http,f,.is/,/baz'] ] } }; -- cgit v1.2.3-70-g09d2