Skip to content

Commit

Permalink
Simpler way to strip HTML in RTEField
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime RETY committed Feb 4, 2010
1 parent 0bf6e31 commit a78d984
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 77 deletions.
2 changes: 1 addition & 1 deletion build/inputex-min.js

Large diffs are not rendered by default.

67 changes: 29 additions & 38 deletions build/inputex.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,6 @@ lang.augmentObject(inputEx, {
replace(/[ç]/g,"c").
replace(/[œ]/g,"oe").
replace(/[æ]/g,"ae");
},

/**
* Returns a new string without whitespace characters at the beginning and end
* @static
* @param {String} str The string
* @return {String} The trimmed string
*/
trim: function(str) {
if (typeof String.prototype.trim === "function") {
return str.trim();
}

return str.replace(/^\s+|\s+$/g, "");
}

});
Expand Down Expand Up @@ -5318,6 +5304,32 @@ lang.extend(inputEx.RTEField, inputEx.Field, {
} else {
alert('Editor is not on the page');
}


/**
* Filters out msword html comments, classes, and other junk
* (complementary with YAHOO.widget.SimpleEditor.prototype.filter_msword, when filterWord option is true)
* @param {String} value The html string
* @return {String} The html string
*/
this.editor.filter_msword = function(html) {

html = editorType.prototype.filter_msword.call(this,html);

// if we don't filter ms word junk
if (!this.get('filterWord')) {
return html;
}

html = html.replace( /<!--[^>][\s\S]*-->/gi, ''); // strip (meta-)comments
html = html.replace( /<\/?meta[^>]*>/gi, ''); // strip meta tags
html = html.replace( /<\/?link[^>]*>/gi, ''); // strip link tags
html = html.replace( / class=('|")?MsoNormal('|")?/gi, ''); // strip MS office class
html = YAHOO.lang.trim(html); // trim spaces

return html;
};

},

/**
Expand Down Expand Up @@ -5354,36 +5366,15 @@ lang.extend(inputEx.RTEField, inputEx.Field, {
var html;

try {
// trigger HTML cleaning (strip MS word or internal junk)
// + save to hidden textarea (required for classic HTML 'submit')
html = this.editor.saveHTML();
html = this.filter_msword_extended(html);
return html;
}
catch(ex) { return null; }
},


/**
* Filters out msword html comments, classes, and other junk
* (complementary with YAHOO.widget.SimpleEditor.prototype.filter_msword, when filterWord option is true)
* @param {String} value The html string
* @return {String} The html string
*/
filter_msword_extended: function(html) {

// if we don't filter ms word junk
if (!this.editor.get('filterWord')) {
return html;
}

html = html.replace( /<!--[^>][\s\S]*-->/gi, ''); // strip (meta-)comments
html = html.replace( /<\/?meta[^>]*>/gi, ''); // strip meta tags
html = html.replace( /<\/?link[^>]*>/gi, ''); // strip link tags
html = html.replace( / class=('|")?MsoNormal('|")?/gi, ''); // strip MS office class
html = inputEx.trim(html); // trim spaces

return html;
}


});

// Register this class as "html" type
Expand Down
53 changes: 29 additions & 24 deletions js/fields/RTEField.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ lang.extend(inputEx.RTEField, inputEx.Field, {
} else {
alert('Editor is not on the page');
}


/**
* Filters out msword html comments, classes, and other junk
* (complementary with YAHOO.widget.SimpleEditor.prototype.filter_msword, when filterWord option is true)
* @param {String} value The html string
* @return {String} The html string
*/
this.editor.filter_msword = function(html) {

html = editorType.prototype.filter_msword.call(this,html);

// if we don't filter ms word junk
if (!this.get('filterWord')) {
return html;
}

html = html.replace( /<!--[^>][\s\S]*-->/gi, ''); // strip (meta-)comments
html = html.replace( /<\/?meta[^>]*>/gi, ''); // strip meta tags
html = html.replace( /<\/?link[^>]*>/gi, ''); // strip link tags
html = html.replace( / class=('|")?MsoNormal('|")?/gi, ''); // strip MS office class
html = YAHOO.lang.trim(html); // trim spaces

return html;
};

},

/**
Expand Down Expand Up @@ -104,36 +130,15 @@ lang.extend(inputEx.RTEField, inputEx.Field, {
var html;

try {
// trigger HTML cleaning (strip MS word or internal junk)
// + save to hidden textarea (required for classic HTML 'submit')
html = this.editor.saveHTML();
html = this.filter_msword_extended(html);
return html;
}
catch(ex) { return null; }
},


/**
* Filters out msword html comments, classes, and other junk
* (complementary with YAHOO.widget.SimpleEditor.prototype.filter_msword, when filterWord option is true)
* @param {String} value The html string
* @return {String} The html string
*/
filter_msword_extended: function(html) {

// if we don't filter ms word junk
if (!this.editor.get('filterWord')) {
return html;
}

html = html.replace( /<!--[^>][\s\S]*-->/gi, ''); // strip (meta-)comments
html = html.replace( /<\/?meta[^>]*>/gi, ''); // strip meta tags
html = html.replace( /<\/?link[^>]*>/gi, ''); // strip link tags
html = html.replace( / class=('|")?MsoNormal('|")?/gi, ''); // strip MS office class
html = inputEx.trim(html); // trim spaces

return html;
}


});

// Register this class as "html" type
Expand Down
14 changes: 0 additions & 14 deletions js/inputex.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,6 @@ lang.augmentObject(inputEx, {
replace(/[ç]/g,"c").
replace(/[œ]/g,"oe").
replace(/[æ]/g,"ae");
},

/**
* Returns a new string without whitespace characters at the beginning and end
* @static
* @param {String} str The string
* @return {String} The trimmed string
*/
trim: function(str) {
if (typeof String.prototype.trim === "function") {
return str.trim();
}

return str.replace(/^\s+|\s+$/g, "");
}

});
Expand Down
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ version 0.4.1 :
* id, className, and disabled options on buttons
* event facade for form submit : form.submitEvent
* event facades for buttons : button.clickEvent, button.submitEvent
* destroy method on group and form fields

- Bug fixes
* fixing setValue bug on checkboxes
Expand Down

0 comments on commit a78d984

Please sign in to comment.