Skip to content

Commit 8ebde48

Browse files
committed
Added a forEach function to recursiveListFormat() to allow handling of
nodeList's even though they are not Arrays.
1 parent b700ac2 commit 8ebde48

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/taBind.js

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,29 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
183183
if (!_foundBlockElement) {
184184
value = "<" + attrs.taDefaultWrap + ">" + value + "</" + attrs.taDefaultWrap + ">";
185185
}
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-
// }
186+
else{
187+
value = "";
188+
for(i = 0; i < _children.length; i++){
189+
var node = _children[i];
190+
var nodeName = node.nodeName.toLowerCase();
191+
//console.log(nodeName);
192+
if(nodeName === '#comment') {
193+
value += '<!--' + node.nodeValue + '-->';
194+
} else if(nodeName === '#text') {
195+
value += node.textContent;
196+
} else if(!nodeName.match(BLOCKELEMENTS)){
197+
var _subVal = (node.outerHTML || node.nodeValue);
198+
/* istanbul ignore else: Doesn't seem to trigger on tests, is tested though */
199+
if(_subVal.trim() !== '')
200+
value += "<" + attrs.taDefaultWrap + ">" + _subVal + "</" + attrs.taDefaultWrap + ">";
201+
else value += _subVal;
202+
} else {
203+
value += node.outerHTML;
204+
}
205+
}
206+
}
200207
}
208+
console.log(value);
201209
return value;
202210
};
203211

@@ -375,16 +383,31 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
375383

376384
var recursiveListFormat = function(listNode, tablevel){
377385
var _html = '', _children = listNode.childNodes;
386+
var forEach = function (array, callback, scope) {
387+
for (var i= 0; i<array.length; i++) {
388+
callback.call(scope, i, array[i]);
389+
}
390+
};
378391
tablevel++;
379392
_html += _repeat('\t', tablevel-1) + listNode.outerHTML.substring(0, listNode.outerHTML.indexOf('<li'));
380-
for(var _i = 0; _i < _children.length; _i++){
393+
forEach(_children, function (index, node) {
381394
/* istanbul ignore next: browser catch */
382-
if(!_children[_i].outerHTML) continue;
383-
if(_children[_i].nodeName.toLowerCase() === 'ul' || _children[_i].nodeName.toLowerCase() === 'ol')
384-
_html += '\n' + recursiveListFormat(_children[_i], tablevel);
395+
var nodeName = node.nodeName.toLowerCase();
396+
console.log(node, nodeName);
397+
if (nodeName === '#comment') {
398+
_html += '<!--' + node.nodeValue + '-->';
399+
return;
400+
}
401+
if (nodeName === '#text') {
402+
_html += node.textContent;
403+
return;
404+
}
405+
if(!node.outerHTML) return;
406+
if(nodeName === 'ul' || nodeName === 'ol')
407+
_html += '\n' + recursiveListFormat(node, tablevel);
385408
else
386-
_html += '\n' + _repeat('\t', tablevel) + _children[_i].outerHTML;
387-
}
409+
_html += '\n' + _repeat('\t', tablevel) + node.outerHTML;
410+
});
388411
_html += '\n' + _repeat('\t', tablevel-1) + listNode.outerHTML.substring(listNode.outerHTML.lastIndexOf('<'));
389412
return _html;
390413
};
@@ -395,14 +418,23 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
395418
htmlValue = '';
396419
for(var i = 0; i < _children.length; i++){
397420
/* istanbul ignore next: browser catch */
421+
var node = _children[i];
422+
var nodeName = node.nodeName.toLowerCase();
423+
if (nodeName === '#comment') {
424+
htmlValue += '<!--' + node.nodeValue + '-->';
425+
continue;
426+
}
427+
if (nodeName === '#text') {
428+
htmlValue += node.textContent;
429+
continue;
430+
}
398431
if(!_children[i].outerHTML) continue;
399432
if(htmlValue.length > 0) htmlValue += '\n';
400433
if(_children[i].nodeName.toLowerCase() === 'ul' || _children[i].nodeName.toLowerCase() === 'ol')
401434
htmlValue += '' + recursiveListFormat(_children[i], 0);
402435
else htmlValue += '' + _children[i].outerHTML;
403436
}
404437
}
405-
406438
return htmlValue;
407439
});
408440
}else{

0 commit comments

Comments
 (0)