Skip to content

Commit 30c2703

Browse files
committed
Fixed text manipulation order of applied actions.
1 parent 8ac99cd commit 30c2703

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

extension/scripts/Selector.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var Selector = (function () {
1010
* Manipulates return data from selector.
1111
* @param data
1212
*/
13-
manipulateData: function (data) {
13+
manipulateData: function (data) {
1414

1515
var regex = function (content, regex, regexgroup) {
1616
try {
@@ -42,8 +42,8 @@ var Selector = (function () {
4242
var replace;
4343
try {
4444
var regex = new RegExp(replaceText, 'gm');
45-
replace = regex.test(content) ? regex : replaceText;
46-
} catch (e) { replace = replaceText;}
45+
replace = regex.test(content) ? regex : replaceText;
46+
} catch (e) { replace = replaceText; }
4747

4848
return content.replace(replace, replacementText);
4949
}
@@ -63,44 +63,62 @@ var Selector = (function () {
6363
isArray = Array.isArray(content),
6464
isTextmManipulationDefined = typeof this.textmanipulation != 'undefined' && this.textmanipulation !== "",
6565
textManipulationAvailable = (isString || isUnderlyingString) && isTextmManipulationDefined;
66-
66+
6767
if (textManipulationAvailable) {
6868
content = isString ? content : $(content).text();
6969

7070
// use key in object since unit tests might not define each property
71+
var keys = []
7172
for (var key in this.textmanipulation) {
72-
if (this.textmanipulation.hasOwnProperty(key)) {
73-
var value = this.textmanipulation[key];
74-
switch (key) {
75-
case "regex":
76-
var group = this.textmanipulation.regexgroup;
77-
group = typeof group != 'undefined' ? group : "";
78-
if (value !== '') { content = regex(content, value, group); }
79-
break;
80-
case "removeHtml":
81-
if (value) { content = removeHtml(content); }
82-
break;
83-
case "trimText":
84-
if (value) { content = trimText(content); }
85-
break;
86-
case "replaceText":
87-
var replacement =this.textmanipulation.replacementText;
88-
replacement = typeof replacement != 'undefined' ? replacement : "";
89-
content = replaceText(content, value, replacement);
90-
break;
91-
case "textPrefix":
92-
if (value !== '') { content = textPrefix(content, value) };
93-
break;
94-
case "textSuffix":
95-
if (value !== '') { content = textSuffix(content, value) };
96-
break;
97-
}
73+
if (!this.textmanipulation.hasOwnProperty(key)) { continue; }
74+
keys.push(key)
75+
}
76+
77+
function propertyIsAvailable(key) {
78+
return keys.indexOf(key) >= 0;
79+
}
80+
81+
if (propertyIsAvailable("regex")) {
82+
var group = this.textmanipulation.regexgroup;
83+
var value = this.textmanipulation.regex;
84+
group = typeof group != 'undefined' ? group : "";
85+
if (value !== '') { content = regex(content, value, group); }
86+
}
87+
88+
if (propertyIsAvailable("removeHtml")) {
89+
if (this.textmanipulation.removeHtml) {
90+
content = removeHtml(content);
9891
}
9992
}
93+
94+
if (propertyIsAvailable("trimText")) {
95+
if (this.textmanipulation.trimText) {
96+
content = trimText(content);
97+
}
98+
}
99+
100+
if (propertyIsAvailable("replaceText")) {
101+
var replacement = this.textmanipulation.replacementText;
102+
replacement = typeof replacement != 'undefined' ? replacement : "";
103+
content = replaceText(content, this.textmanipulation.replaceText, replacement);
104+
}
105+
106+
if (propertyIsAvailable("textPrefix")) {
107+
if (this.textmanipulation.textPrefix !== '') {
108+
content = textPrefix(content, this.textmanipulation.textPrefix)
109+
};
110+
}
111+
112+
if (propertyIsAvailable("textSuffix")) {
113+
if (this.textmanipulation.textSuffix !== '') {
114+
content = textSuffix(content, this.textmanipulation.textSuffix)
115+
};
116+
}
117+
100118
element[this.id] = content;
101119
} else if (isArray && isTextmManipulationDefined) {
102120
element[this.id] = JSON.stringify(content);
103-
this.manipulateData(element);
121+
this.manipulateData(element);
104122
}
105123

106124
}.bind(this));

0 commit comments

Comments
 (0)