Skip to content

Commit 7a8ca17

Browse files
committed
- textAngular-sanitize now keeps whitespace and comments.
- taBind.js does not automatically wrap things when this is not necessary.
1 parent 8677c63 commit 7a8ca17

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

src/taBind.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,35 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
168168

169169
var _blankTest = _taBlankTest(_defaultTest);
170170

171-
var _ensureContentWrapped = function(value){
172-
if(_blankTest(value)) return value;
171+
var _ensureContentWrapped = function(value) {
172+
if (_blankTest(value)) return value;
173173
var domTest = angular.element("<div>" + value + "</div>");
174-
if(domTest.children().length === 0){
174+
if (domTest.children().length === 0) {
175175
value = "<" + attrs.taDefaultWrap + ">" + value + "</" + attrs.taDefaultWrap + ">";
176-
}else{
176+
} else {
177177
var _children = domTest[0].childNodes;
178178
var i;
179179
var _foundBlockElement = false;
180-
for(i = 0; i < _children.length; i++){
181-
if(_foundBlockElement = _children[i].nodeName.toLowerCase().match(BLOCKELEMENTS)) break;
180+
for (i = 0; i < _children.length; i++) {
181+
if (_foundBlockElement = _children[i].nodeName.toLowerCase().match(BLOCKELEMENTS)) break;
182182
}
183-
if(!_foundBlockElement){
183+
if (!_foundBlockElement) {
184184
value = "<" + attrs.taDefaultWrap + ">" + value + "</" + attrs.taDefaultWrap + ">";
185-
}else{
186-
value = "";
187-
for(i = 0; i < _children.length; i++){
188-
if(!_children[i].nodeName.toLowerCase().match(BLOCKELEMENTS)){
189-
var _subVal = (_children[i].outerHTML || _children[i].nodeValue);
190-
/* istanbul ignore else: Doesn't seem to trigger on tests, is tested though */
191-
if(_subVal.trim() !== '')
192-
value += "<" + attrs.taDefaultWrap + ">" + _subVal + "</" + attrs.taDefaultWrap + ">";
193-
else value += _subVal;
194-
}else{
195-
value += _children[i].outerHTML;
196-
}
197-
}
198185
}
186+
// else{
187+
// value = "";
188+
// for(i = 0; i < _children.length; i++){
189+
// if(!_children[i].nodeName.toLowerCase().match(BLOCKELEMENTS)){
190+
// var _subVal = (_children[i].outerHTML || _children[i].nodeValue);
191+
// /* istanbul ignore else: Doesn't seem to trigger on tests, is tested though */
192+
// if(_subVal.trim() !== '')
193+
// value += "<" + attrs.taDefaultWrap + ">" + _subVal + "</" + attrs.taDefaultWrap + ">";
194+
// else value += _subVal;
195+
// }else{
196+
// value += _children[i].outerHTML;
197+
// }
198+
// }
199+
// }
199200
}
200201
return value;
201202
};
@@ -389,18 +390,18 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
389390
};
390391
ngModel.$formatters.unshift(function(htmlValue){
391392
// tabulate the HTML so it looks nicer
392-
var _children = angular.element('<div>' + htmlValue + '</div>')[0].childNodes;
393-
if(_children.length > 0){
394-
htmlValue = '';
395-
for(var i = 0; i < _children.length; i++){
396-
/* istanbul ignore next: browser catch */
397-
if(!_children[i].outerHTML) continue;
398-
if(htmlValue.length > 0) htmlValue += '\n';
399-
if(_children[i].nodeName.toLowerCase() === 'ul' || _children[i].nodeName.toLowerCase() === 'ol')
400-
htmlValue += '' + recursiveListFormat(_children[i], 0);
401-
else htmlValue += '' + _children[i].outerHTML;
402-
}
403-
}
393+
// var _children = angular.element('<div>' + htmlValue + '</div>')[0].childNodes;
394+
// if(_children.length > 0){
395+
// htmlValue = '';
396+
// for(var i = 0; i < _children.length; i++){
397+
// /* istanbul ignore next: browser catch */
398+
// if(!_children[i].outerHTML) continue;
399+
// if(htmlValue.length > 0) htmlValue += '\n';
400+
// if(_children[i].nodeName.toLowerCase() === 'ul' || _children[i].nodeName.toLowerCase() === 'ol')
401+
// htmlValue += '' + recursiveListFormat(_children[i], 0);
402+
// else htmlValue += '' + _children[i].outerHTML;
403+
// }
404+
// }
404405

405406
return htmlValue;
406407
});
@@ -713,11 +714,13 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
713714
_setInnerHTML(_defaultVal);
714715
taSelection.setSelectionToElementStart(element.children()[0]);
715716
}else if(val.substring(0, 1) !== '<' && attrs.taDefaultWrap !== ''){
717+
/* we no longer do this, since there can be comments here and white space
716718
var _savedSelection = $window.rangy.saveSelection();
717719
val = _compileHtml();
718720
val = "<" + attrs.taDefaultWrap + ">" + val + "</" + attrs.taDefaultWrap + ">";
719721
_setInnerHTML(val);
720722
$window.rangy.restoreSelection(_savedSelection);
723+
*/
721724
}
722725
var triggerUndo = _lastKey !== event.keyCode && UNDO_TRIGGER_KEYS.test(event.keyCode);
723726
if(_keyupTimeout) $timeout.cancel(_keyupTimeout);

src/textAngular-sanitize.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,13 @@ var START_TAG_REGEXP =
168168
BEGIN_TAG_REGEXP = /^</,
169169
BEGING_END_TAGE_REGEXP = /^<\//,
170170
COMMENT_REGEXP = /<!--(.*?)-->/g,
171+
SINGLE_COMMENT_REGEXP = /(^<!--.*?-->)/,
171172
DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
172173
CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
173174
SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
174175
// Match everything outside of normal chars and " (quote character)
175-
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
176+
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g,
177+
WHITE_SPACE_REGEXP = /^(\s+)/;
176178

177179

178180
// Good source of info about elements and attributes
@@ -288,14 +290,23 @@ function htmlParser(html, handler) {
288290
// Make sure we're not in a script or style element
289291
if (!stack.last() || !specialElements[ stack.last() ]) {
290292

291-
// Comment
292-
if (html.indexOf("<!--") === 0) {
293-
// comments containing -- are not allowed unless they terminate the comment
294-
index = html.indexOf("--", 4);
293+
// White space
294+
if (WHITE_SPACE_REGEXP.test(html)) {
295+
match = html.match(WHITE_SPACE_REGEXP);
295296

296-
if (index >= 0 && html.lastIndexOf("-->", index) === index) {
297-
if (handler.comment) handler.comment(html.substring(4, index));
298-
html = html.substring(index + 3);
297+
if (match) {
298+
var mat = match[0];
299+
if (handler.whitespace) handler.whitespace(match[0]);
300+
html = html.replace(match[0], '');
301+
chars = false;
302+
}
303+
//Comment
304+
} else if (SINGLE_COMMENT_REGEXP.test(html)) {
305+
match = html.match(SINGLE_COMMENT_REGEXP);
306+
307+
if (match) {
308+
if (handler.comment) handler.comment(match[1]);
309+
html = html.replace(match[0], '');
299310
chars = false;
300311
}
301312
// DOCTYPE
@@ -587,6 +598,12 @@ function htmlSanitizeWriter(buf, uriValidator) {
587598
out(unary ? '/>' : '>');
588599
}
589600
},
601+
comment: function (com) {
602+
out(com);
603+
},
604+
whitespace: function (ws) {
605+
out(ws);
606+
},
590607
end: function(tag) {
591608
tag = angular.lowercase(tag);
592609
if (!ignore && validElements[tag] === true) {

0 commit comments

Comments
 (0)