MDL-49686 atto: Process style and class attributes in sub-functions

To ensure we only clean style and classes, first we select the inside
of those attributes and "replace" them with handler functions. Those
functions scan the actual attribute values for class or styles that
we want to exclude.

The first level regex has 3 groups. group1 selects everything in the
tag leading up to the attribute values, group2 has the attributes,
group3 has the trailing quote mark. We work on group2 then return
the combination of group1, group2, and group3.
This commit is contained in:
Eric Merrill
2015-03-27 11:36:05 -04:00
parent 20d38830ae
commit 8e202bd80a
4 changed files with 45 additions and 21 deletions
@@ -1323,12 +1323,20 @@ EditorClean.prototype = {
// Run some more rules that care about quotes and whitespace.
rules = [
// Remove MSO-blah, MSO:blah in style attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?style\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[-:][^>;"]*;?)+/gi, replace: "$1"},
// Remove MSO classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Remove Apple- classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*Apple-[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Get all style attributes so we can work on them.
{regex: /(<[^>]*?style\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO-blah, MSO:blah style attributes.
group2 = group2.replace(/(?:^|;)[\s]*MSO[-:](?:&[\w]*;|[^;"])*/gi,"");
return group1 + group2 + group3;
}},
// Get all class attributes so we can work on them.
{regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO classes.
group2 = group2.replace(/(?:^|[\s])[\s]*MSO[_a-zA-Z0-9\-]*/gi,"");
// Remove Apple- classes.
group2 = group2.replace(/(?:^|[\s])[\s]*Apple-[_a-zA-Z0-9\-]*/gi,"");
return group1 + group2 + group3;
}},
// Remove OLE_LINK# anchors that may litter the code.
{regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""},
// Remove empty spans, but not ones from Rangy.
File diff suppressed because one or more lines are too long
@@ -1312,12 +1312,20 @@ EditorClean.prototype = {
// Run some more rules that care about quotes and whitespace.
rules = [
// Remove MSO-blah, MSO:blah in style attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?style\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[-:][^>;"]*;?)+/gi, replace: "$1"},
// Remove MSO classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Remove Apple- classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*Apple-[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Get all style attributes so we can work on them.
{regex: /(<[^>]*?style\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO-blah, MSO:blah style attributes.
group2 = group2.replace(/(?:^|;)[\s]*MSO[-:](?:&[\w]*;|[^;"])*/gi,"");
return group1 + group2 + group3;
}},
// Get all class attributes so we can work on them.
{regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO classes.
group2 = group2.replace(/(?:^|[\s])[\s]*MSO[_a-zA-Z0-9\-]*/gi,"");
// Remove Apple- classes.
group2 = group2.replace(/(?:^|[\s])[\s]*Apple-[_a-zA-Z0-9\-]*/gi,"");
return group1 + group2 + group3;
}},
// Remove OLE_LINK# anchors that may litter the code.
{regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""},
// Remove empty spans, but not ones from Rangy.
+14 -6
View File
@@ -298,12 +298,20 @@ EditorClean.prototype = {
// Run some more rules that care about quotes and whitespace.
rules = [
// Remove MSO-blah, MSO:blah in style attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?style\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[-:][^>;"]*;?)+/gi, replace: "$1"},
// Remove MSO classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*MSO[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Remove Apple- classes in class attributes. Only removes one or more that appear in succession.
{regex: /(<[^>]*?class\s*?=\s*?"[^>"]*?)(?:[\s]*Apple-[_a-zA-Z0-9\-]*)+/gi, replace: "$1"},
// Get all style attributes so we can work on them.
{regex: /(<[^>]*?style\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO-blah, MSO:blah style attributes.
group2 = group2.replace(/(?:^|;)[\s]*MSO[-:](?:&[\w]*;|[^;"])*/gi,"");
return group1 + group2 + group3;
}},
// Get all class attributes so we can work on them.
{regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
// Remove MSO classes.
group2 = group2.replace(/(?:^|[\s])[\s]*MSO[_a-zA-Z0-9\-]*/gi,"");
// Remove Apple- classes.
group2 = group2.replace(/(?:^|[\s])[\s]*Apple-[_a-zA-Z0-9\-]*/gi,"");
return group1 + group2 + group3;
}},
// Remove OLE_LINK# anchors that may litter the code.
{regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""},
// Remove empty spans, but not ones from Rangy.