aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Redirector.csproj11
-rw-r--r--chrome/content/redirect.js155
-rw-r--r--chrome/content/redirect.xul22
-rw-r--r--chrome/content/unittests.html86
-rw-r--r--chrome/content/unittests.js12
-rw-r--r--chrome/locale/en-US/redirect.dtd1
-rw-r--r--components/redirector.js24
-rw-r--r--install.rdf2
8 files changed, 209 insertions, 104 deletions
diff --git a/Redirector.csproj b/Redirector.csproj
index 4c50a1a..558bb46 100644
--- a/Redirector.csproj
+++ b/Redirector.csproj
@@ -39,12 +39,7 @@
<None Include="chrome\locale\en-US\redirector.properties" />
<None Include="install.rdf" />
</ItemGroup>
- <Target Name="Build">
- </Target>
- <!--<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- <Target Name="BeforeBuild">
- </Target>
- <Target Name="AfterBuild">
- </Target>
- -->
+ <Target Name="Build" />
+ <Target Name="Rebuild" />
+ <Target Name="Clean" />
</Project> \ No newline at end of file
diff --git a/chrome/content/redirect.js b/chrome/content/redirect.js
index fe468f0..f51e7f2 100644
--- a/chrome/content/redirect.js
+++ b/chrome/content/redirect.js
@@ -1,83 +1,86 @@
-//// $Id$
-
-const kRedirectorWildcard = 'W';
-const kRedirectorRegex= 'R';
-var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
-
-function $(id) {
- return document.getElementById(id);
-}
-
-var Redirect = {
-
- onLoad : function() {
- var item = window.arguments[0];
- item.saved = false;
- $('txtExampleUrl').value = item.exampleUrl;
- $('txtPattern').value = item.pattern;
- $('txtRedirectUrl').value = item.redirectUrl || '';
- $('txtExcludePattern').value = item.excludePattern || '';
-
- $('txtPattern').focus();
- this.strings = document.getElementById("redirector-strings");
-
- if (item.patternType == kRedirectorRegex) {
- $('rdoRegex').setAttribute('selected', true);
- $('rdoWildcard').setAttribute('selected', false);
- }
- },
-
- onAccept : function() {
- var item = window.arguments[0];
-
- item.pattern = $('txtPattern').value;
- if ($('rdoRegex').selected) {
- item.patternType = kRedirectorRegex;
- } else {
- item.patternType = kRedirectorWildcard;
- }
- item.exampleUrl =$('txtExampleUrl').value;
- item.redirectUrl = $('txtRedirectUrl').value;
- item.excludePattern = $('txtExcludePattern').value;
- item.saved = true;
-
- return true;
- },
-
+//// $Id$
+
+const kRedirectorWildcard = 'W';
+const kRedirectorRegex= 'R';
+var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
+
+function $(id) {
+ return document.getElementById(id);
+}
+
+var Redirect = {
+
+ onLoad : function() {
+ var item = window.arguments[0];
+ item.saved = false;
+ $('txtExampleUrl').value = item.exampleUrl;
+ $('txtPattern').value = item.pattern;
+ $('txtRedirectUrl').value = item.redirectUrl || '';
+ $('txtExcludePattern').value = item.excludePattern || '';
+ $('chkUnescapeMatches').setAttribute('selected', !!item.unescapeMatches);
+
+ $('txtPattern').focus();
+ this.strings = document.getElementById("redirector-strings");
+
+ if (item.patternType == kRedirectorRegex) {
+ $('rdoRegex').setAttribute('selected', true);
+ $('rdoWildcard').setAttribute('selected', false);
+ }
+ },
+
+ onAccept : function() {
+ var item = window.arguments[0];
+
+ item.pattern = $('txtPattern').value;
+ if ($('rdoRegex').selected) {
+ item.patternType = kRedirectorRegex;
+ } else {
+ item.patternType = kRedirectorWildcard;
+ }
+ item.exampleUrl =$('txtExampleUrl').value;
+ item.redirectUrl = $('txtRedirectUrl').value;
+ item.excludePattern = $('txtExcludePattern').value;
+ item.unescapeMatches = $('chkUnescapeMatches').selected;
+ item.saved = true;
+
+ return true;
+ },
+
msgBox : function(title, text) {
Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService)
.alert(window, title, text);
},
- testPattern : function() {
- var redirectUrl, pattern, excludePattern, example, extName, isExcluded;
- redirectUrl = $('txtRedirectUrl').value;
- pattern = $('txtPattern').value;
- excludePattern = $('txtExcludePattern').value;
- example = $('txtExampleUrl').value;
-
- extName = this.strings.getString('extensionName');
-
- if ($('rdoRegex').selected) {
- redirectUrl = Redirector.regexMatch(pattern, example, redirectUrl);
- if (excludePattern) {
- isExcluded = Redirector.regexMatch(excludePattern, example, 'exclude');
- }
- } else {
- redirectUrl = Redirector.wildcardMatch(pattern, example, redirectUrl);
- if (excludePattern) {
- isExcluded = Redirector.wildcardMatch(excludePattern, example, 'exclude');
- }
- }
-
- var isRedirectMatch = redirectUrl || (redirectUrl === '' && $('txtRedirectUrl').value === '');
- if (isRedirectMatch && !isExcluded) {
- this.msgBox(extName, this.strings.getFormattedString('testPatternSuccess', [pattern, example, redirectUrl]));
- } else if (isExcluded) {
- this.msgBox(extName, this.strings.getFormattedString('testPatternExclude', [example, excludePattern]));
- } else {
- this.msgBox(extName, this.strings.getFormattedString('testPatternFailure', [pattern, example]));
- }
- }
+ testPattern : function() {
+ var redirectUrl, pattern, excludePattern, example, extName, isExcluded, unescapeMatches;
+ redirectUrl = $('txtRedirectUrl').value;
+ pattern = $('txtPattern').value;
+ excludePattern = $('txtExcludePattern').value;
+ example = $('txtExampleUrl').value;
+ unescapeMatches = $('chkUnescapeMatches').checked;
+
+ extName = this.strings.getString('extensionName');
+
+ if ($('rdoRegex').selected) {
+ redirectUrl = Redirector.regexMatch(pattern, example, redirectUrl, unescapeMatches);
+ if (excludePattern) {
+ isExcluded = Redirector.regexMatch(excludePattern, example, 'exclude');
+ }
+ } else {
+ redirectUrl = Redirector.wildcardMatch(pattern, example, redirectUrl, unescapeMatches);
+ if (excludePattern) {
+ isExcluded = Redirector.wildcardMatch(excludePattern, example, 'exclude');
+ }
+ }
+
+ var isRedirectMatch = redirectUrl || (redirectUrl === '' && $('txtRedirectUrl').value === '');
+ if (isRedirectMatch && !isExcluded) {
+ this.msgBox(extName, this.strings.getFormattedString('testPatternSuccess', [pattern, example, redirectUrl]));
+ } else if (isExcluded) {
+ this.msgBox(extName, this.strings.getFormattedString('testPatternExclude', [example, excludePattern]));
+ } else {
+ this.msgBox(extName, this.strings.getFormattedString('testPatternFailure', [pattern, example]));
+ }
+ }
}; \ No newline at end of file
diff --git a/chrome/content/redirect.xul b/chrome/content/redirect.xul
index aea930b..d3b0907 100644
--- a/chrome/content/redirect.xul
+++ b/chrome/content/redirect.xul
@@ -18,32 +18,36 @@
<grid>
<rows>
- <row>
+ <row align="center">
<text value="&txtExampleUrl.label;" style="margin-top:6px;"/>
<textbox id="txtExampleUrl" style="width:400px;"/>
</row>
- <row>
+ <row align="center">
<text value="&txtPattern.label;" style="margin-top:6px;"/>
<textbox id="txtPattern" taborder="1"/>
<button id="btnTestPattern" label="&btnTestPattern.label;" onclick="Redirect.testPattern();" taborder="2"/>
</row>
- <row>
+ <row align="center">
<text value="&txtExcludePattern.label;" style="margin-top:6px;"/>
<textbox id="txtExcludePattern" taborder="3"/>
</row>
- <row>
+ <row align="center">
<text value="&txtRedirectUrl.label;" style="margin-top:6px;"/>
<textbox id="txtRedirectUrl" taborder="4"/>
</row>
- <row>
+ <row align="center">
<text value="&rdoPatternTypes.label;"/>
<radiogroup>
- <radio id="rdoWildcard" label="&rdoWildcard.label;" accesskey="&rdoWildcard.accessKey;" selected="true" taborder="5"/>
- <radio id="rdoRegex" label="&rdoRegex.label;" accesskey="&rdoRegex.accessKey;" taborder="6"/>
+ <hbox>
+ <radio id="rdoWildcard" label="&rdoWildcard.label;" accesskey="&rdoWildcard.accessKey;" selected="true" taborder="5"/>
+ <radio id="rdoRegex" label="&rdoRegex.label;" accesskey="&rdoRegex.accessKey;" taborder="6"/>
+ </hbox>
</radiogroup>
</row>
+ <row align="center">
+ <text value="&chkUnescapeMatches.label;" />
+ <checkbox id="chkUnescapeMatches" label="" taborder="7"/>
+ </row>
</rows>
-
</grid>
-
</dialog>
diff --git a/chrome/content/unittests.html b/chrome/content/unittests.html
new file mode 100644
index 0000000..1f84455
--- /dev/null
+++ b/chrome/content/unittests.html
@@ -0,0 +1,86 @@
+<!-- $Id: help.html 210 2009-05-10 05:20:55Z einar@einaregilsson.com $ -->
+<html>
+ <head>
+ <title>Redirector Unit Tests</title>
+ <style type="text/css">
+ 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;}
+ td { border:solid 1px black; padding:3px; }
+ td.result { width:20px; height:20px; padding:0;}
+ td.result div { width:70%; height:70%; margin:auto; }
+ </style>
+ <script type="text/javascript">
+
+ //Global variables
+ var subscriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
+ var redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
+
+ function setupTest(name, testcase) {
+ var table = document.createElement('table');
+ var row = document.createElement('tr');
+ var cell = document.createElement('th');
+ var testdata;
+ cell.setAttribute('colspan', 2);
+ row.appendChild(cell);
+ table.appendChild(row);
+ cell.innerHTML = name;
+ document.getElementsByTagName('body')[0].appendChild(table);
+ for (var i = 0; i < testcase.tests.length; i++) {
+ var testdata = testcase.tests[i];
+ row = document.createElement('tr');
+ cell = document.createElement('td');
+ cell.setAttribute('class', 'result');
+ var dot = document.createElement('div');
+ dot.setAttribute('id', name + '_' + i);
+ cell.appendChild(dot);
+
+ row.appendChild(cell);
+ cell = document.createElement('td');
+ cell.innerHTML = testcase.describe(testdata);
+ row.appendChild(cell);
+ table.appendChild(row);
+ }
+ }
+
+ function setup() {
+ var tables = document.getElementsByTagName('table');
+ for (var i = 0; i < tables.length; i++) {
+ tables[i].parentNode.removeChild(tables[i]);
+ }
+
+ subscriptLoader.loadSubScript('chrome://redirector/content/unittests.js');
+ redirector.reload();
+
+ for (var name in tests) {
+ setupTest(name, tests[name]);
+ }
+ }
+
+ function runTests() {
+ for (var testcaseName in tests) {
+ var testcase = tests[testcaseName];
+ for (var i = 0; i < testcase.tests.length; i++) {
+ try {
+ var result = testcase.run(testcase.tests[i]);
+ if (result) {
+ document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'green';
+ } else {
+ document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'red';
+ }
+ } catch(e) {
+ document.getElementById(testcaseName + '_' + i).style.backgroundColor = 'red';
+ }
+ }
+ }
+ }
+
+ </script>
+ </head>
+ <body onload="setup();">
+ <h1>Redirector Unit Tests</h1>
+ <button onclick="runTests();">Run tests</button>
+ <button onclick="setup();">Reload tests</button>
+ </body>
+</html> \ No newline at end of file
diff --git a/chrome/content/unittests.js b/chrome/content/unittests.js
new file mode 100644
index 0000000..c532d3f
--- /dev/null
+++ b/chrome/content/unittests.js
@@ -0,0 +1,12 @@
+
+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]; },
+ tests : [
+ ['http://foo*', 'http://foobar.is'],
+ ['http://foo*', 'http://foo'],
+ ['*foo*', 'http://foo']
+ ]
+ }
+};
diff --git a/chrome/locale/en-US/redirect.dtd b/chrome/locale/en-US/redirect.dtd
index 30968fb..40098b8 100644
--- a/chrome/locale/en-US/redirect.dtd
+++ b/chrome/locale/en-US/redirect.dtd
@@ -10,4 +10,5 @@
<!ENTITY rdoRegex.label "Regular Expression">
<!ENTITY rdoPatternTypes.label "Pattern Type">
<!ENTITY rdoRegex.accessKey "R">
+<!ENTITY chkUnescapeMatches.label "Unescape matches">
diff --git a/components/redirector.js b/components/redirector.js
index 09ef35e..4e44c11 100644
--- a/components/redirector.js
+++ b/components/redirector.js
@@ -50,7 +50,8 @@ function RedirectorPolicy() {
pattern : arr[1],
redirectUrl : arr[2],
patternType : arr[3],
- excludePattern : arr[4]
+ excludePattern : arr[4],
+ unescapeMatches : !!arr[5] //This might be undefined for those upgrading from 1.7.1 but that's ok
});
}
}
@@ -117,6 +118,10 @@ RedirectorPolicy.prototype = {
this.prefBranch.setBoolPref('enabled', val);
},
+ reload : function() {
+ //TODO: Reload implementation
+ },
+
addRedirect : function(redirect) {
this.list.push(redirect);
this.save();
@@ -132,7 +137,7 @@ RedirectorPolicy.prototype = {
, tempList = [];
for each (r in this.list) {
- tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.patternType, r.excludePattern].join(',,,'));
+ tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.patternType, r.excludePattern, r.unescapeMatches].join(',,,'));
}
this.prefBranch.setCharPref('redirects', tempList.join(':::'));
},
@@ -141,7 +146,7 @@ RedirectorPolicy.prototype = {
return this.prefBranch.getBoolPref(name);
},
- regexMatch : function(pattern, text, redirectUrl) {
+ regexMatch : function(pattern, text, redirectUrl, unescapeMatches) {
if (!pattern) {
return null;
@@ -160,7 +165,7 @@ RedirectorPolicy.prototype = {
if (match) {
for (var i = 1; i < match.length; i++) {
rxrepl = new RegExp('\\$' + i, 'gi');
- redirectUrl = redirectUrl.replace(rxrepl, match[i]);
+ redirectUrl = redirectUrl.replace(rxrepl, unescapeMatches ? unescape(match[i]) : match[i]);
}
return redirectUrl;
}
@@ -182,13 +187,13 @@ RedirectorPolicy.prototype = {
this.debug(url + ' matches exclude pattern ' + redirect.excludePattern);
return null;
}
- return this.wildcardMatch(redirect.pattern, url, redirect.redirectUrl);
+ return this.wildcardMatch(redirect.pattern, url, redirect.redirectUrl, redirect.unescapeMatches);
} else if (redirect.patternType == kRedirectorRegex) {
if (this.regexMatch(redirect.excludePattern, url, 'whatever')) {
this.debug(url + ' matches exclude pattern ' + redirect.excludePattern);
return null;
}
- return this.regexMatch(redirect.pattern, url, redirect.redirectUrl);
+ return this.regexMatch(redirect.pattern, url, redirect.redirectUrl, redirect.unescapeMatches);
}
return null;
},
@@ -206,7 +211,7 @@ RedirectorPolicy.prototype = {
return uri.resolve(relativeUrl);
},
- wildcardMatch : function(pattern, text, redirectUrl) {
+ wildcardMatch : function(pattern, text, redirectUrl, unescapeMatches) {
var parts
, part
, i
@@ -239,15 +244,14 @@ RedirectorPolicy.prototype = {
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(text);
+ stars.push(unescapeMatches ? unescape(text) : text);
} else {
- stars.push(text.substr(0, pos));
+ stars.push(unescapeMatches ? unescape(text.substr(0, pos)) : text.substr(0, pos));
}
text = text.substr(pos + part.length);
diff --git a/install.rdf b/install.rdf
index 38f481b..f86b411 100644
--- a/install.rdf
+++ b/install.rdf
@@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>redirector@einaregilsson.com</em:id>
<em:name>Redirector</em:name>
- <em:version>1.7.1</em:version>
+ <em:version>1.7.2</em:version>
<em:creator>Einar Egilsson</em:creator>
<em:description>Automatically redirects to user-defined urls on certain pages</em:description>
<em:homepageURL>http://tech.einaregilsson.com/projects/redirector/</em:homepageURL>