aboutsummaryrefslogtreecommitdiff
path: root/chrome/content/code/redirect.js
diff options
context:
space:
mode:
authorEinar Egilsson2009-10-20 13:04:15 +0000
committerEinar Egilsson2009-10-20 13:04:15 +0000
commit1059664ff4a3c92b2c6a4720f695e158aed215e4 (patch)
treea8313f1f52d9a10d13bd5e2a27e7a563ef3e8e3a /chrome/content/code/redirect.js
parent85e1989a00957f4f08ea6720e0b884b278f24714 (diff)
Changed to use actual interfaces! Seems to work, tiny bug left in unit tests.
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@285 119bf307-c92d-0410-89bd-8f53e6181181
Diffstat (limited to 'chrome/content/code/redirect.js')
-rw-r--r--chrome/content/code/redirect.js241
1 files changed, 131 insertions, 110 deletions
diff --git a/chrome/content/code/redirect.js b/chrome/content/code/redirect.js
index e65ca50..2be8fc9 100644
--- a/chrome/content/code/redirect.js
+++ b/chrome/content/code/redirect.js
@@ -1,5 +1,7 @@
//// $Id$
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
function Redirect(exampleUrl, includePattern, redirectUrl, patternType, excludePattern, unescapeMatches, disabled) {
this._init(exampleUrl, includePattern, redirectUrl, patternType, excludePattern, unescapeMatches, disabled);
}
@@ -9,33 +11,145 @@ Redirect.WILDCARD = 'W';
Redirect.REGEX = 'R';
Redirect.prototype = {
-
- //These are the only ones that are necessary to have as properties for now
- //The others can be changed to properties later as neccessary
- _includePattern : null,
- _excludePattern : null,
- _patternType : null,
- _rxInclude : null,
- _rxExclude : null,
-
- get patternType() { return this._patternType; },
- set patternType(value) {
- this._patternType = value;
- this.compile();
- },
+ // rdIRedirect implementation
+
+ //attributes
+ exampleUrl : null,
+
get includePattern() { return this._includePattern; },
set includePattern(value) {
this._includePattern = value;
this._rxInclude = this._compile(value);
},
-
+
get excludePattern() { return this._excludePattern; },
set excludePattern(value) {
this._excludePattern = value;
this._rxExclude = this._compile(value);
},
+ redirectTo : null,
+
+ get patternType() { return this._patternType; },
+ set patternType(value) {
+ this._patternType = value;
+ this.compile();
+ },
+
+ unescapeMatches : false,
+
+ disabled : false,
+
+ //Functions
+ clone : function() {
+ return new Redirect(this.exampleUrl, this.includePattern,
+ this.redirectUrl, this.patternType,
+ this.excludePattern, this.unescapeMatches,
+ this.disabled);
+ },
+
+ compile : function() {
+ this._rxInclude = this._compile(this._includePattern);
+ this._rxExclude = this._compile(this._excludePattern);
+ },
+
+ copyValues : function(other) {
+ this.exampleUrl = other.exampleUrl;
+ this.includePattern = other.includePattern;
+ this.excludePattern = other.excludePattern;
+ this.redirectUrl = other.redirectUrl;
+ this.patternType = other.patternType;
+ this.unescapeMatches = other.unescapeMatches;
+ this.disabled = other.disabled;
+ },
+
+ deserialize : function(str) {
+ if (!str || !str.split) {
+ throw Error("Invalid serialized redirect: " + str);
+ }
+ var parts = str.split(',,,');
+ if (parts.length < 5) {
+ throw Error("Invalid serialized redirect, too few fields: " + str);
+ }
+ this._init.apply(this, parts);
+ },
+
+ equals : function(redirect) {
+ return this.exampleUrl == redirect.exampleUrl
+ && this.includePattern == redirect.includePattern
+ && this.excludePattern == redirect.excludePattern
+ && this.redirectUrl == redirect.redirectUrl
+ && this.patternType == redirect.patternType
+ && this.unescapeMatches == redirect.unescapeMatches
+ ;
+ },
+
+ getMatch: function(url) {
+ var result = {
+ isMatch : false,
+ isExcludeMatch : false,
+ isDisabledMatch : false,
+ redirectTo : '',
+ toString : function() { return "{ isMatch : " + this.isMatch +
+ ", isExcludeMatch : " + this.isExcludeMatch +
+ ", isDisabledMatch : " + this.isDisabledMatch +
+ ", redirectTo : \"" + this.redirectTo + "\"" +
+ "}"; }
+ };
+ var redirectTo = null;
+
+ redirectTo = this._includeMatch(url);
+ if (redirectTo !== null) {
+ if (this.disabled) {
+ result.isDisabledMatch = true;
+ } else if (this._excludeMatch(url)) {
+ result.isExcludeMatch = true;
+ } else {
+ result.isMatch = true;
+ result.redirectTo = redirectTo;
+ }
+ }
+ return result;
+ },
+
+ isRegex: function() {
+ return this.patternType == Redirect.REGEX;
+ },
+
+ isWildcard : function() {
+ return this.patternType == Redirect.WILDCARD;
+ },
+
+ serialize : function() {
+ return [ this.exampleUrl
+ , this.includePattern
+ , this.redirectUrl
+ , this.patternType
+ , this.excludePattern
+ , this.unescapeMatches
+ , this.disabled ].join(',,,');
+ },
+
+ test : function() {
+ return this.getMatch(this.exampleUrl);
+ },
+
+ //end rdIRedirect
+
+ //nsISupports
+ QueryInterface : XPCOMUtils.generateQI([Components.interfaces.rdIRedirect]),
+
+ //end nsISupports
+
+ //Private functions below
+
+ _includePattern : null,
+ _excludePattern : null,
+ _patternType : null,
+ _rxInclude : null,
+ _rxExclude : null,
+
_preparePattern : function(pattern) {
if (this.patternType == Redirect.REGEX) {
return pattern;
@@ -55,12 +169,7 @@ Redirect.prototype = {
return converted;
}
},
-
- compile : function() {
- this._rxInclude = this._compile(this._includePattern);
- this._rxExclude = this._compile(this._excludePattern);
- },
-
+
_compile : function(pattern) {
if (!pattern) {
return null;
@@ -90,67 +199,6 @@ Redirect.prototype = {
+ '\n}\n';
},
- isWildcard : function() {
- return this.patternType == Redirect.WILDCARD;
- },
-
- isRegex: function() {
- return this.patternType == Redirect.REGEX;
- },
-
- test : function() {
- return this.getMatch(this.exampleUrl);
- },
-
- serialize : function() {
- return [ this.exampleUrl
- , this.includePattern
- , this.redirectUrl
- , this.patternType
- , this.excludePattern
- , this.unescapeMatches
- , this.disabled ].join(',,,');
- },
-
- deserialize : function(str) {
- if (!str || !str.split) {
- throw Error("Invalid serialized redirect: " + str);
- }
- var parts = str.split(',,,');
- if (parts.length < 5) {
- throw Error("Invalid serialized redirect, too few fields: " + str);
- }
- this._init.apply(this, parts);
- },
-
- getMatch: function(url) {
- var result = {
- isMatch : false,
- isExcludeMatch : false,
- isDisabledMatch : false,
- redirectTo : '',
- toString : function() { return "{ isMatch : " + this.isMatch +
- ", isExcludeMatch : " + this.isExcludeMatch +
- ", isDisabledMatch : " + this.isDisabledMatch +
- ", redirectTo : \"" + this.redirectTo + "\"" +
- "}"; }
- };
- var redirectTo = null;
-
- redirectTo = this._includeMatch(url);
- if (redirectTo !== null) {
- if (this.disabled) {
- result.isDisabledMatch = true;
- } else if (this._excludeMatch(url)) {
- result.isExcludeMatch = true;
- } else {
- result.isMatch = true;
- result.redirectTo = redirectTo;
- }
- }
- return result;
- },
-
_includeMatch : function(url) {
if (!this._rxInclude) {
return null;
@@ -174,32 +222,5 @@ Redirect.prototype = {
var shouldExclude = !!this._rxExclude.exec(url);
this._rxExclude.lastIndex = 0;
return shouldExclude;
- },
-
- clone : function() {
- return new Redirect(this.exampleUrl, this.includePattern,
- this.redirectUrl, this.patternType,
- this.excludePattern, this.unescapeMatches,
- this.disabled);
- },
-
- copyValues : function(other) {
- this.exampleUrl = other.exampleUrl;
- this.includePattern = other.includePattern;
- this.excludePattern = other.excludePattern;
- this.redirectUrl = other.redirectUrl;
- this.patternType = other.patternType;
- this.unescapeMatches = other.unescapeMatches;
- this.disabled = other.disabled;
- },
-
- equals : function(redirect) {
- return this.exampleUrl == redirect.exampleUrl
- && this.includePattern == redirect.includePattern
- && this.excludePattern == redirect.excludePattern
- && this.redirectUrl == redirect.redirectUrl
- && this.patternType == redirect.patternType
- && this.unescapeMatches == redirect.unescapeMatches
- ;
- }
+ }
}; \ No newline at end of file