aboutsummaryrefslogtreecommitdiff
path: root/chrome
diff options
context:
space:
mode:
Diffstat (limited to 'chrome')
-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
5 files changed, 191 insertions, 85 deletions
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">