@@ -7,6 +7,7 @@ var defaults = {
7
7
"filterMethod" : 0 , // ["Censor", "Substitute", "Remove"];
8
8
"globalMatchMethod" : 3 , // ["Exact", "Partial", "Whole", "Per-Word"]
9
9
"preserveFirst" : false ,
10
+ "preserveLast" : false ,
10
11
"showCounter" : true ,
11
12
"substitutionMark" : true ,
12
13
"words" : { }
@@ -26,7 +27,7 @@ var defaultWords = {
26
27
"tits" : { "matchMethod" : 1 , "words" : [ "explative" ] } ,
27
28
"whore" : { "matchMethod" : 1 , "words" : [ "harlot" , "tramp" ] }
28
29
} ;
29
- var censorCharacter , censorFixedLength , defaultSubstitutions , disabledDomains , filterMethod , globalMatchMethod , matchMethod , preserveFirst , showCounter , substitutionMark , words , wordList ;
30
+ var censorCharacter , censorFixedLength , defaultSubstitutions , disabledDomains , filterMethod , globalMatchMethod , matchMethod , preserveFirst , preserveLast , showCounter , substitutionMark , words , wordList ;
30
31
var wordRegExps = [ ] ;
31
32
var whitespaceRegExp = new RegExp ( '\\s' ) ;
32
33
var xpathDocText = '//*[not(self::script or self::style)]/text()[normalize-space(.) != ""]' ;
@@ -35,25 +36,25 @@ var xpathNodeText = './/*[not(self::script or self::style)]/text()[normalize-spa
35
36
// Word must match exactly (not sub-string)
36
37
// /\b(w)ord\b/gi
37
38
function buildExactRegexp ( word ) {
38
- wordRegExps . push ( new RegExp ( '\\b(' + word [ 0 ] + ')' + word . substring ( 1 ) + '\\b' , 'gi' ) ) ;
39
+ wordRegExps . push ( new RegExp ( '\\b(' + word [ 0 ] + ')' + word . slice ( 1 ) + '\\b' , 'gi' ) ) ;
39
40
}
40
41
41
42
// Match any part of a word (sub-string)
42
43
// /(w)ord/gi
43
44
function buildPartRegexp ( word ) {
44
- wordRegExps . push ( new RegExp ( '(' + word [ 0 ] + ')' + word . substring ( 1 ) , 'gi' ) ) ;
45
+ wordRegExps . push ( new RegExp ( '(' + word [ 0 ] + ')' + word . slice ( 1 ) , 'gi' ) ) ;
45
46
}
46
47
47
48
// Match entire word that contains sub-string
48
49
// /\b[\w-]*(w)ord[\w-]*\b/gi
49
50
function buildWholeRegexp ( word ) {
50
- wordRegExps . push ( new RegExp ( '\\b([\\w-]*' + word [ 0 ] + ')' + word . substring ( 1 ) + '[\\w-]*\\b' , 'gi' ) ) ;
51
+ wordRegExps . push ( new RegExp ( '\\b([\\w-]*' + word [ 0 ] + ')' + word . slice ( 1 ) + '[\\w-]*\\b' , 'gi' ) )
51
52
}
52
53
53
54
// Match entire word that contains sub-string and surrounding whitespace
54
55
// /\s?\b[\w-]*(w)ord[\w-]*\b\s?/gi
55
56
function buildWholeRegexpForRemove ( word ) {
56
- wordRegExps . push ( new RegExp ( '\\s?\\b([\\w-]*' + word [ 0 ] + ')' + word . substring ( 1 ) + '[\\w-]*\\b\\s?' , 'gi' ) ) ;
57
+ wordRegExps . push ( new RegExp ( '\\s?\\b([\\w-]*' + word [ 0 ] + ')' + word . slice ( 1 ) + '[\\w-]*\\b\\s?' , 'gi' ) ) ;
57
58
}
58
59
59
60
function checkNodeForProfanity ( mutation ) {
@@ -70,14 +71,22 @@ function censorReplace(strMatchingString, strFirstLetter) {
70
71
var censoredString = '' ;
71
72
72
73
if ( censorFixedLength > 0 ) {
73
- if ( preserveFirst ) {
74
- censoredString = strFirstLetter [ 0 ] + censorCharacter . repeat ( ( censorFixedLength - 1 ) ) ;
74
+ if ( preserveFirst && preserveLast ) {
75
+ censoredString = strFirstLetter + censorCharacter . repeat ( ( censorFixedLength - 2 ) ) + strMatchingString . slice ( - 1 ) ;
76
+ } else if ( preserveFirst ) {
77
+ censoredString = strFirstLetter + censorCharacter . repeat ( ( censorFixedLength - 1 ) ) ;
78
+ } else if ( preserveLast ) {
79
+ censoredString = censorCharacter . repeat ( ( censorFixedLength - 1 ) ) + strMatchingString . slice ( - 1 ) ;
75
80
} else {
76
81
censoredString = censorCharacter . repeat ( censorFixedLength ) ;
77
82
}
78
83
} else {
79
- if ( preserveFirst ) {
80
- censoredString = strFirstLetter [ 0 ] + censorCharacter . repeat ( ( strMatchingString . length - 1 ) ) ;
84
+ if ( preserveFirst && preserveLast ) {
85
+ censoredString = strFirstLetter + censorCharacter . repeat ( ( strMatchingString . length - 2 ) ) + strMatchingString . slice ( - 1 ) ;
86
+ } else if ( preserveFirst ) {
87
+ censoredString = strFirstLetter + censorCharacter . repeat ( ( strMatchingString . length - 1 ) ) ;
88
+ } else if ( preserveLast ) {
89
+ censoredString = censorCharacter . repeat ( ( strMatchingString . length - 1 ) ) + strMatchingString . slice ( - 1 ) ;
81
90
} else {
82
91
censoredString = censorCharacter . repeat ( strMatchingString . length ) ;
83
92
}
@@ -110,6 +119,7 @@ function cleanPage() {
110
119
globalMatchMethod = storage . globalMatchMethod ;
111
120
matchMethod = storage . matchMethod ;
112
121
preserveFirst = storage . preserveFirst ;
122
+ preserveLast = storage . preserveLast ;
113
123
showCounter = storage . showCounter ;
114
124
substitutionMark = storage . substitutionMark ;
115
125
words = storage . words ;
0 commit comments