Skip to content

Commit

Permalink
Merge pull request textAngular#967 from Benew/fix_paste_headings_from…
Browse files Browse the repository at this point in the history
…_word

Fix pasting headings from word
  • Loading branch information
pbassut committed Dec 13, 2015
2 parents 77efc23 + d5a6a73 commit aa35c02
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/taBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
/* istanbul ignore else: don't care if nothing pasted */
if(text && text.trim().length){
// test paste from word/microsoft product
if(text.match(/class=["']*Mso(Normal|List)/i)){
if(text.match(/class=["']*Mso(Normal|List)/i) || text.match(/content=["']*Word.Document/i)){
var textFragment = text.match(/<!--StartFragment-->([\s\S]*?)<!--EndFragment-->/i);
if(!textFragment) textFragment = text;
else textFragment = textFragment[1];
Expand Down Expand Up @@ -523,7 +523,14 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
_list.lastLevelMatch = null;
};
for(var i = 0; i <= dom[0].childNodes.length; i++){
if(!dom[0].childNodes[i] || dom[0].childNodes[i].nodeName === "#text" || dom[0].childNodes[i].tagName.toLowerCase() !== "p") continue;
if(!dom[0].childNodes[i] || dom[0].childNodes[i].nodeName === "#text"){
continue;
} else {
var tagName = dom[0].childNodes[i].tagName.toLowerCase();
if(tagName !== "p" && tagName !== "h1" && tagName !== "h2" && tagName !== "h3" && tagName !== "h4" && tagName !== "h5" && tagName !== "h6"){
continue;
}
}
var el = angular.element(dom[0].childNodes[i]);
var _listMatch = (el.attr('class') || '').match(/MsoList(Bullet|Number|Paragraph)(CxSp(First|Middle|Last)|)/i);

Expand Down
32 changes: 32 additions & 0 deletions test/taBind/taBind.wordPaste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ describe('taBind.wordPaste', function () {
$rootScope.$digest();
expect(pasted).toBe('<h1>Header 1</h1>');
}));
it('Header Element inside fragment with MsoNormal class', inject(function($timeout, taSelection){
element.triggerHandler('paste', {clipboardData: {types: ['text/html'], getData: function(){
return '<div><!--StartFragment--><h1 class="MsoNormal">Header 1<o:p></o:p></h1><!--EndFragment--></div>';// jshint ignore:line
}}});
$timeout.flush();
$rootScope.$digest();
expect(pasted).toBe('<h1>Header 1</h1>');
}));
it('Header Element #2 inside fragment with MsoNormal class', inject(function($timeout, taSelection){
element.triggerHandler('paste', {clipboardData: {types: ['text/html'], getData: function(){
return '<div><!--StartFragment--><h2 class="MsoNormal">Header 2<o:p></o:p></h2><!--EndFragment--></div>';// jshint ignore:line
}}});
$timeout.flush();
$rootScope.$digest();
expect(pasted).toBe('<h2>Header 2</h2>');
}));
it('Header Element inside fragment without MsoNormal class but with MSWord document syntax', inject(function($timeout, taSelection){
element.triggerHandler('paste', {clipboardData: {types: ['text/html'], getData: function(){
return '<html xmlns:o="urn:schemas-microsoft-com:office:office">' +
'<head>' +
'<meta name=ProgId content=Word.Document>' +
'</head>' +
'<body>' +
'<div><!--StartFragment--><h1>Header 1<o:p></o:p></h1><!--EndFragment--></div>' +// jshint ignore:line
'</body>' +
'</html>';
}}});
$timeout.flush();
$rootScope.$digest();
expect(pasted).toBe('<h1>Header 1</h1>');
}));

// bold/italics
it('handle bold/italics/underline', inject(function($timeout, taSelection){
element.triggerHandler('paste', {clipboardData: {types: ['text/html'], getData: function(){
Expand Down

0 comments on commit aa35c02

Please sign in to comment.