Skip to content

Commit

Permalink
Fix regression in cleanPaste
Browse files Browse the repository at this point in the history
Port fix from #772
  • Loading branch information
j0k3r committed Aug 12, 2015
1 parent 4965f88 commit 98d7875
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
34 changes: 34 additions & 0 deletions spec/paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ describe('Pasting content', function () {

expect(this.el.innerHTML).toMatch(new RegExp('^Before(&nbsp;|\\s)(<span id="editor-inner">)?<sub>div one</sub><sub>div two</sub>(</span>)?(&nbsp;|\\s)after\\.$'));
});

it('should cleanup only pasted element on multi-line when nothing is selected', function () {
var editor = this.newMediumEditor('.editor', {
paste: {
forcePlainText: false,
cleanPastedHTML: true
}
});

this.el.innerHTML = '<div><img src="http://0.0.0.0/ohyeah.png" /></div>';

selectElementContents(this.el.firstChild, { collapse: true });

editor.cleanPaste('<table><tr><td>test</td><td><br/></td></tr></table>');

expect(this.el.innerHTML).toContain('<img src="http://0.0.0.0/ohyeah.png"></div>');
});
});

describe('using pasteHTML', function () {
Expand All @@ -321,6 +338,23 @@ describe('Pasting content', function () {
expect(editor.elements[0].innerHTML).toBe('<p>test</p>');
});

it('should not remove node with "empty" content', function () {
var editor = this.newMediumEditor('.editor', {
paste: {
forcePlainText: false,
cleanPastedHTML: true
}
});

this.el.innerHTML = '<div>this is a div</div><figure id="editor-inner">and this is a figure</figure>.';

selectElementContents(this.el.firstChild);

editor.pasteHTML('<table><tr><td>test</td><td><br/></td></tr></table>');

expect(this.el.innerHTML).toContain('<table><tbody><tr><td>test</td><td><br></td></tr></tbody></table>');
});

it('should accept a list of attrs to clean up', function () {
var editor = this.newMediumEditor('.editor');
selectElementContents(this.el.firstChild);
Expand Down
51 changes: 27 additions & 24 deletions src/js/extensions/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ var PasteHandler;
},

cleanPaste: function (text) {
var i, elList,
var i, elList, tmp, workEl,
multiline = /<p|<br|<div/.test(text),
replacements = createReplacements().concat(this.cleanReplacements || []);

Expand All @@ -152,10 +152,34 @@ var PasteHandler;
return this.pasteHTML(text);
}

// create a temporary div to cleanup block elements
tmp = this.document.createElement('div');

// double br's aren't converted to p tags, but we want paragraphs.
elList = text.split('<br><br>');
tmp.innerHTML = '<p>' + text.split('<br><br>').join('</p><p>') + '</p>';

// block element cleanup
elList = tmp.querySelectorAll('a,p,div,br');
for (i = 0; i < elList.length; i += 1) {
workEl = elList[i];

// Microsoft Word replaces some spaces with newlines.
// While newlines between block elements are meaningless, newlines within
// elements are sometimes actually spaces.
workEl.innerHTML = workEl.innerHTML.replace(/\n/gi, ' ');

switch (workEl.nodeName.toLowerCase()) {
case 'p':
case 'div':
this.filterCommonBlocks(workEl);
break;
case 'br':
this.filterLineBreak(workEl);
break;
}
}

this.pasteHTML('<p>' + elList.join('</p><p>') + '</p>');
this.pasteHTML(tmp.innerHTML);
},

pasteHTML: function (html, options) {
Expand Down Expand Up @@ -185,27 +209,6 @@ var PasteHandler;
Util.cleanupTags(workEl, options.cleanTags);
}

// block element cleanup
elList = fragmentBody.querySelectorAll('a,p,div,br');
for (i = 0; i < elList.length; i += 1) {
workEl = elList[i];

// Microsoft Word replaces some spaces with newlines.
// While newlines between block elements are meaningless, newlines within
// elements are sometimes actually spaces.
workEl.innerHTML = workEl.innerHTML.replace(/\n/gi, ' ');

switch (workEl.nodeName.toLowerCase()) {
case 'p':
case 'div':
this.filterCommonBlocks(workEl);
break;
case 'br':
this.filterLineBreak(workEl);
break;
}
}

Util.insertHTMLCommand(this.document, fragmentBody.innerHTML.replace(/&nbsp;/g, ' '));
},

Expand Down

0 comments on commit 98d7875

Please sign in to comment.