Skip to content

Commit

Permalink
Add tests for copy-pasting on IE11.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoran Brondsema committed Feb 16, 2016
1 parent b4c46c3 commit 2a743fa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
28 changes: 20 additions & 8 deletions tests/acceptance/editor-copy-paste-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Editor } from 'mobiledoc-kit';
import Helpers from '../test-helpers';
import Range from 'mobiledoc-kit/utils/cursor/range';
import { supportsStandardClipboardAPI } from '../helpers/browsers';
import {
MIME_TEXT_PLAIN,
MIME_TEXT_HTML
Expand Down Expand Up @@ -83,9 +84,14 @@ test('paste plain text with line breaks', (assert) => {
Helpers.dom.setCopyData(MIME_TEXT_PLAIN, ['abc', 'def'].join('\n'));
Helpers.dom.triggerPasteEvent(editor);

assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
assert.hasElement('#editor p:contains(def)', 'second section is pasted');
assert.equal($('#editor p').length, 2, 'adds a second section');
if (supportsStandardClipboardAPI()) {
assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
assert.hasElement('#editor p:contains(def)', 'second section is pasted');
assert.equal($('#editor p').length, 2, 'adds a second section');
} else {
assert.hasElement('#editor p:contains(abcabc\ndef)', 'pastes the text');
assert.equal($('#editor p').length, 1, 'adds a second section');
}
});

test('paste plain text with list items', (assert) => {
Expand All @@ -103,8 +109,12 @@ test('paste plain text with list items', (assert) => {
Helpers.dom.setCopyData(MIME_TEXT_PLAIN, ['* abc', '* def'].join('\n'));
Helpers.dom.triggerPasteEvent(editor);

assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
assert.hasElement('#editor ul li:contains(def)', 'list item is pasted');
if (supportsStandardClipboardAPI()) {
assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
assert.hasElement('#editor ul li:contains(def)', 'list item is pasted');
} else {
assert.hasElement('#editor p:contains(abc* abc\n* def)', 'pastes the text');
}
});

test('can cut and then paste content', (assert) => {
Expand Down Expand Up @@ -300,10 +310,12 @@ test('copy sets html & text for pasting externally', (assert) => {

Helpers.dom.triggerCopyEvent(editor);

let text = Helpers.dom.getCopyData(MIME_TEXT_PLAIN);
let html = Helpers.dom.getCopyData(MIME_TEXT_HTML);
assert.equal(text, ["heading", "h2 subheader", "The text" ].join('\n'),
'gets plain text');
if (supportsStandardClipboardAPI()) {
let text = Helpers.dom.getCopyData(MIME_TEXT_PLAIN);
assert.equal(text, ["heading", "h2 subheader", "The text" ].join('\n'),
'gets plain text');
}

assert.ok(html.indexOf("<h1>heading") !== -1, 'html has h1');
assert.ok(html.indexOf("<h2>h2 subheader") !== -1, 'html has h2');
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/browsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export function supportsSelectionExtend() {
let selection = window.getSelection();
return !!selection.extend;
}

// See http://caniuse.com/#feat=clipboard
// This rules out the Internet Explorers.
export function supportsStandardClipboardAPI() {
return !window.clipboardData;
}
45 changes: 33 additions & 12 deletions tests/helpers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import KEY_CODES from 'mobiledoc-kit/utils/keycodes';
import { DIRECTION, MODIFIERS } from 'mobiledoc-kit/utils/key';
import { isTextNode } from 'mobiledoc-kit/utils/dom-utils';
import { merge } from 'mobiledoc-kit/utils/merge';
import { supportsStandardClipboardAPI } from './browsers';

// walks DOWN the dom from node to childNodes, returning the element
// for which `conditionFn(element)` is true
Expand Down Expand Up @@ -270,11 +271,17 @@ function triggerLeftArrowKey(editor, modifier) {
// Allows our fake copy and paste events to communicate with each other.
const lastCopyData = {};
function triggerCopyEvent(editor) {
let event = createMockEvent('copy', editor.element, {
clipboardData: {
setData(type, value) { lastCopyData[type] = value; }
}
});
let eventData = {};

if (supportsStandardClipboardAPI()) {
eventData = {
clipboardData: {
setData(type, value) { lastCopyData[type] = value; }
}
};
}

let event = createMockEvent('copy', editor.element, eventData);
editor.triggerEvent(editor.element, 'copy', event);
}

Expand All @@ -288,20 +295,34 @@ function triggerCutEvent(editor) {
}

function triggerPasteEvent(editor) {
let event = createMockEvent('copy', editor.element, {
clipboardData: {
getData(type) { return lastCopyData[type]; }
}
});
let eventData = {};

if (supportsStandardClipboardAPI()) {
eventData = {
clipboardData: {
getData(type) { return lastCopyData[type]; }
}
};
}

let event = createMockEvent('copy', editor.element, eventData);
editor.triggerEvent(editor.element, 'paste', event);
}

function getCopyData(type) {
return lastCopyData[type];
if (supportsStandardClipboardAPI()) {
return lastCopyData[type];
} else {
return window.clipboardData.getData('Text');
}
}

function setCopyData(type, value) {
lastCopyData[type] = value;
if (supportsStandardClipboardAPI()) {
lastCopyData[type] = value;
} else {
window.clipboardData.setData('Text', value);
}
}

function clearCopyData() {
Expand Down

0 comments on commit 2a743fa

Please sign in to comment.