Skip to content

Commit

Permalink
Enhancement(factories): Better removal of style junk which Chome inse…
Browse files Browse the repository at this point in the history
…rts. Also one bug fix in the old existing code (a missing /").
  • Loading branch information
JoelParke committed Jul 12, 2016
1 parent b432e74 commit 7d5d37c
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,32 @@ angular.module('textAngular.factories', [])
var taFixChrome = function(html){
if(!html || !angular.isString(html) || html.length <= 0) return html;
// grab all elements with a style attibute
var spanMatch = /<([^>\/]+?)style=("([^"]+)"|'([^']+)')([^>]*)>/ig;
var match, styleVal, newTag, finalHtml = '', lastIndex = 0;
var spanMatch = /<([^>\/]+?)style=("([^\"]+)"|'([^']+)')([^>]*)>/ig;
var appleConvertedSpaceMatch = /<span class="Apple-converted-space">([^<]+)<\/span>/ig;
var match, styleVal, appleSpaceVal, newTag, finalHtml = '', lastIndex = 0;
// remove all the Apple-converted-space spans and replace with the content of the span
/* istanbul ignore next: apple-contereted-space span match */
while(match = appleConvertedSpaceMatch.exec(html)){
appleSpaceVal = match[1];
appleSpaceVal = appleSpaceVal.replace(/&nbsp;/ig, ' ');
finalHtml += html.substring(lastIndex, match.index) + appleSpaceVal;
lastIndex = match.index + match[0].length;
}
/* istanbul ignore next: apple-contereted-space span has matched */
if (lastIndex) {
// modified....
html=finalHtml;
finalHtml='';
lastIndex=0;
}
while(match = spanMatch.exec(html)){
// one of the quoted values ' or "
/* istanbul ignore next: quotations match */
styleVal = match[3] || match[4];
// test for chrome inserted junk
if(styleVal && styleVal.match(/line-height: 1.[0-9]{3,12};|color: inherit; line-height: 1.1;/i)){
if(styleVal && styleVal.match(/line-height: 1.[0-9]{3,12};|color: inherit; line-height: 1.1;|color: rgb\(\d{1,3}, \d{1,3}, \d{1,3}\);|background-color: rgb\(\d{1,3}, \d{1,3}, \d{1,3}\);/i)){
// replace original tag with new tag
styleVal = styleVal.replace(/( |)font-family: inherit;|( |)line-height: 1.[0-9]{3,12};|( |)color: inherit;/ig, '');
styleVal = styleVal.replace(/( |)font-family: inherit;|( |)line-height: 1.[0-9]{3,12};|( |)color: inherit;|( |)color: rgb\(\d{1,3}, \d{1,3}, \d{1,3}\);|( |)background-color: rgb\(\d{1,3}, \d{1,3}, \d{1,3}\);/ig, '');
newTag = '<' + match[1].trim();
if(styleVal.trim().length > 0) newTag += ' style=' + match[2].substring(0,1) + styleVal + match[2].substring(0,1);
newTag += match[5].trim() + ">";
Expand Down Expand Up @@ -75,7 +91,7 @@ angular.module('textAngular.factories', [])
tag: 'i'
}
];

var styleMatch = [];
for(var i = 0; i < convert_infos.length; i++){
var _partialStyle = '(' + convert_infos[i].property + ':\\s*(';
Expand All @@ -88,7 +104,7 @@ angular.module('textAngular.factories', [])
styleMatch.push(_partialStyle);
}
var styleRegexString = '(' + styleMatch.join('|') + ')';

function wrapNested(html, wrapTag) {
var depth = 0;
var lastIndex = 0;
Expand All @@ -107,7 +123,7 @@ angular.module('textAngular.factories', [])
angular.element(wrapTag)[0].outerHTML.substring(wrapTag.length) +
html.substring(lastIndex);
}

function transformLegacyStyles(html){
if(!html || !angular.isString(html) || html.length <= 0) return html;
var i;
Expand Down Expand Up @@ -155,7 +171,7 @@ angular.module('textAngular.factories', [])
else finalHtml += html.substring(lastIndex);
return finalHtml;
}

function transformLegacyAttributes(html){
if(!html || !angular.isString(html) || html.length <= 0) return html;
// replace all align='...' tags with text-align attributes
Expand Down Expand Up @@ -184,7 +200,7 @@ angular.module('textAngular.factories', [])
// return with remaining html
return finalHtml + html.substring(lastIndex);
}

return function taSanitize(unsafe, oldsafe, ignore){
// unsafe html should NEVER built into a DOM object via angular.element. This allows XSS to be inserted and run.
if ( !ignore ) {
Expand All @@ -198,7 +214,7 @@ angular.module('textAngular.factories', [])
// any exceptions (lets say, color for example) should be made here but with great care
// setup unsafe element for modification
unsafe = transformLegacyAttributes(unsafe);

var safe;
try {
safe = $sanitize(unsafe);
Expand All @@ -207,9 +223,9 @@ angular.module('textAngular.factories', [])
} catch (e){
safe = oldsafe || '';
}

// Do processing for <pre> tags, removing tabs and return carriages outside of them

var _preTags = safe.match(/(<pre[^>]*>.*?<\/pre[^>]*>)/ig);
var processedSafe = safe.replace(/(&#(9|10);)*/ig, '');
var re = /<pre[^>]*>.*?<\/pre[^>]*>/ig;
Expand Down Expand Up @@ -247,4 +263,4 @@ angular.module('textAngular.factories', [])
deferred.resolve();
}
};
}]);
}]);

0 comments on commit 7d5d37c

Please sign in to comment.