Skip to content

Commit 7b873df

Browse files
SimeonCSimeonC
authored andcommitted
fix(taExecCommand): Attempt to wrap unwrapped content in list breaks.
Fix textAngular#362
1 parent 5ff572f commit 7b873df

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/textAngular.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,12 @@ See README.md or https://github.com/fraywing/textAngular/wiki for requirements a
865865
}else if(tagName.match(BLOCKELEMENTS)){
866866
// if we get here then all the contents of the ta-bind are selected
867867
_nodes = taSelection.getOnlySelectedElements();
868-
if(_nodes.length === 1 && (_nodes[0].tagName.toLowerCase() === 'ol' || _nodes[0].tagName.toLowerCase() === 'ul')){
868+
if(_nodes.length === 0){
869+
// here is if there is only text in ta-bind ie <div ta-bind>test content</div>
870+
$target = angular.element('<' + selfTag + '><li>' + selectedElement.innerHTML + '</li></' + selfTag + '>');
871+
$selected.html('');
872+
$selected.append($target);
873+
}else if(_nodes.length === 1 && (_nodes[0].tagName.toLowerCase() === 'ol' || _nodes[0].tagName.toLowerCase() === 'ul')){
869874
if(_nodes[0].tagName.toLowerCase() === selfTag){
870875
// remove
871876
return listToDefault(angular.element(_nodes[0]), taDefaultWrap);
@@ -884,13 +889,8 @@ See README.md or https://github.com/fraywing/textAngular/wiki for requirements a
884889
}
885890
}
886891
$target = angular.element('<' + selfTag + '>' + html + '</' + selfTag + '>');
887-
if($nodes.length){
888-
$nodes.pop().replaceWith($target);
889-
angular.forEach($nodes, function($node){ $node.remove(); });
890-
}else{
891-
// selection was empty, insert html (cursor moved automatically)
892-
return taSelection.insertHtml('<' + selfTag + '>' + html + '</' + selfTag + '>');
893-
}
892+
$nodes.pop().replaceWith($target);
893+
angular.forEach($nodes, function($node){ $node.remove(); });
894894
}
895895
taSelection.setSelectionToElementEnd($target[0]);
896896
return;

test/taExecCommand.spec.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,28 @@ describe('taExecCommand', function(){
376376
}));
377377
it('to ol', inject(function(taSelection, taExecCommand){
378378
taExecCommand()('insertorderedlist', false, null);
379-
expect(insertedHtml).toBe('<ol></ol>');
379+
expect(insertedHtml).toBe('');
380380
}));
381381

382382
it('to ul', inject(function(taSelection, taExecCommand){
383383
taExecCommand()('insertunorderedlist', false, null);
384-
expect(insertedHtml).toBe('<ul></ul>');
384+
expect(insertedHtml).toBe('');
385+
}));
386+
});
387+
describe('text-only ta-bind', function(){
388+
beforeEach(inject(function(taSelection){
389+
element = angular.element('<div class="ta-bind">testsomecontent</div>');
390+
taSelection.element = element[0];
391+
taSelection.getOnlySelectedElements = function(){ return []; };
392+
}));
393+
it('to ol', inject(function(taSelection, taExecCommand){
394+
taExecCommand()('insertorderedlist', false, null);
395+
expect(element.html()).toBe('<ol><li>testsomecontent</li></ol>');
396+
}));
397+
398+
it('to ul', inject(function(taSelection, taExecCommand){
399+
taExecCommand()('insertunorderedlist', false, null);
400+
expect(element.html()).toBe('<ul><li>testsomecontent</li></ul>');
385401
}));
386402
});
387403
describe('single element selected', function(){

0 commit comments

Comments
 (0)