From 10e637b427e5cdd9b5b6660469391cac525da637 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Mon, 14 Sep 2009 14:06:50 +0000 Subject: Unescape matches option + some unit tests git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@245 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/redirect.js | 155 +++++++++++++++++++++--------------------- chrome/content/redirect.xul | 22 +++--- chrome/content/unittests.html | 86 +++++++++++++++++++++++ chrome/content/unittests.js | 12 ++++ 4 files changed, 190 insertions(+), 85 deletions(-) create mode 100644 chrome/content/unittests.html create mode 100644 chrome/content/unittests.js (limited to 'chrome/content') 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 @@ - + - + + + + \ 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'] + ] + } +}; -- cgit v1.2.3-70-g09d2