Skip to content

Commit

Permalink
fix(taSanitize): Fix issue with last pre-tag overwriting all previous.
Browse files Browse the repository at this point in the history
Fixes #508
  • Loading branch information
SimeonC authored and SimeonC committed Jan 26, 2015
1 parent 97f3564 commit 230e779
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/textAngular.min.js

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions lib/factories.js
Expand Up @@ -132,17 +132,22 @@ 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);
safe = safe.replace(/(&#(9|10);)*/ig, '');
var re = /<pre[^>]*>.*?<\/pre[^>]*>/i;
processedSafe = safe.replace(/(&#(9|10);)*/ig, '');
var re = /<pre[^>]*>.*?<\/pre[^>]*>/ig;
var index = 0;
var lastIndex = 0;
var origTag;
while((origTag = re.exec(safe)) !== null && index < _preTags.length){
safe = safe.substring(0, origTag.index) + _preTags[index] + safe.substring(origTag.index + origTag[0].length);
re.lastIndex = Math.max(0, re.lastIndex + _preTags[index].length - origTag[0].length);
safe = '';
while((origTag = re.exec(processedSafe)) !== null && index < _preTags.length){
safe += processedSafe.substring(lastIndex, origTag.index) + _preTags[index];
lastIndex = origTag.index + origTag[0].length;
index++;
}
return safe;
return safe + processedSafe.substring(lastIndex);
};
}]).factory('taToolExecuteAction', ['$q', '$log', function($q, $log){
// this must be called on a toolScope or instance
Expand Down
17 changes: 11 additions & 6 deletions src/textAngular.js
Expand Up @@ -311,17 +311,22 @@ 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);
safe = safe.replace(/(&#(9|10);)*/ig, '');
var re = /<pre[^>]*>.*?<\/pre[^>]*>/i;
processedSafe = safe.replace(/(&#(9|10);)*/ig, '');
var re = /<pre[^>]*>.*?<\/pre[^>]*>/ig;
var index = 0;
var lastIndex = 0;
var origTag;
while((origTag = re.exec(safe)) !== null && index < _preTags.length){
safe = safe.substring(0, origTag.index) + _preTags[index] + safe.substring(origTag.index + origTag[0].length);
re.lastIndex = Math.max(0, re.lastIndex + _preTags[index].length - origTag[0].length);
safe = '';
while((origTag = re.exec(processedSafe)) !== null && index < _preTags.length){
safe += processedSafe.substring(lastIndex, origTag.index) + _preTags[index];
lastIndex = origTag.index + origTag[0].length;
index++;
}
return safe;
return safe + processedSafe.substring(lastIndex);
};
}]).factory('taToolExecuteAction', ['$q', '$log', function($q, $log){
// this must be called on a toolScope or instance
Expand Down
5 changes: 5 additions & 0 deletions test/taSanitize.spec.js
Expand Up @@ -97,6 +97,11 @@ describe('taSanitize', function(){
var result = taSanitize('<p>&#10;Test &#10; &#9;Test 2&#10;&#9;</p><pre>&#9;Test &#10; &#9;Test 2&#10;&#9;</pre>', 'safe');
expect(result).toBe('<p>Test Test 2</p><pre>&#9;Test &#10; &#9;Test 2&#10;&#9;</pre>');
}));

it('correctly handles more than one pre-tag', inject(function(taSanitize){
var result = taSanitize('<p>&#10;Test &#10; &#9;Test 2&#10;&#9;</p><pre>&#9;Test &#10; &#9;Test 1&#10;&#9;</pre><p>&#10;Test &#10; &#9;Test 2&#10;&#9;</p><pre>&#9;Test &#10; &#9;Test 2&#10;&#9;</pre>', 'safe');
expect(result).toBe('<p>Test Test 2</p><pre>&#9;Test &#10; &#9;Test 1&#10;&#9;</pre><p>Test Test 2</p><pre>&#9;Test &#10; &#9;Test 2&#10;&#9;</pre>');
}));
});

describe('only certain style attributes are allowed', function(){
Expand Down

0 comments on commit 230e779

Please sign in to comment.