fitnesse.resources.wysiwyg.pasting-spec.js Maven / Gradle / Ivy
describe("Copying and pasting", function () {
var editor, contentDocument, contentBody;
beforeEach(function() {
this.addMatchers({
// Sometimes Firefox and Chrome react differently
toBeEither: function(expected1, expected2) {
var actual = this.actual;
return actual === expected1 || actual === expected2;
}
});
});
beforeEach(function() {
document.getElementById("editor").innerHTML = '';
jasmine.Clock.useMock();
Wysiwyg.paths = { base: ".", stylesheets: ["../css/fitnesse_wiki.css", "editor.css"] };
var options = Wysiwyg.getOptions();
editor = Wysiwyg.newInstance(document.getElementById("pageContent"), options);
jasmine.Clock.tick(1000);
contentDocument = editor.contentDocument;
contentBody = contentDocument.getElementsByTagName("body")[0];
// Ensure the wysiwyg editor is visible
$('#editor-wysiwyg-1').click();
});
function clearContent() {
while (contentBody.firstChild) {
contentBody.removeChild(contentBody.firstChild);
}
}
function givenHtml(html) {
contentBody.innerHTML = html;
}
function select(startQuery, startOffset, endQuery, endOffset) {
var startContainer = contentDocument.querySelector(startQuery);
var endContainer = contentDocument.querySelector(endQuery);
editor.selectRange(startContainer.firstChild, startOffset, endContainer.firstChild, endOffset);
}
function paste(something) {
$(contentBody).trigger("paste");
editor.insertHTML(something);
jasmine.Clock.tick(20);
}
it("should inset a single word before", function () {
givenHtml("page contains text
");
select("p", 0, "p", 0);
paste("test-");
expect(contentBody.innerHTML).toBe("test-page contains text
");
});
it("should insert paragraph before current content", function () {
givenHtml("page contains text
");
select("p", 0, "p", 0);
paste("test-
");
expect(contentBody.innerHTML).toBeEither(
"test-page contains text
",
"test-
page contains text
");
});
it("should replace text when selected", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("test");
expect(contentBody.innerHTML).toBe("test contains text
");
});
it("should replace all content if selected", function () {
givenHtml("Page contains text
Another paragraph
");
select("p", 0, "p", 18);
paste("blah");
expect(contentBody.innerHTML).toBe("blah
Another paragraph
");
});
it("should merge two paragraphs if text is selected over paragraph boundries", function () {
givenHtml('first
second
');
select("#first", 1, "#second", 5);
paste("oo");
expect(contentBody.innerHTML).toBeEither(
'food
',
'food
');
});
describe("Pasting in tables", function () {
it("should paste only one line of content (plain text)", function () {
givenHtml('cell content
');
select("#first", 0, "#first", 4);
paste("new");
expect(contentBody.innerHTML).toBe('new content
');
});
it("should paste only one line of content (markup text)", function () {
givenHtml('cell content
');
select("#first", 0, "#first", 4);
paste("new
");
expect(contentBody.innerHTML).toBeEither(
'
new content
',
'new content
');
});
it("should add pasted tables to the parent table", function () {
givenHtml('cell content
');
select('#first', 0, '#first', 0);
paste('nested content
');
expect(contentBody.innerHTML).toBe(
'cell content nested content
');
});
});
describe("Pasting wiki text", function () {
it("should format multi-line text", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("test\n\n");
expect(contentBody.innerHTML).toBeEither(
'test
contains text
',
'test
contains text
');
});
it("should format multi-line text formatted with br's (firefox)", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("test
");
expect(contentBody.innerHTML).toBeEither(
'test
contains text
',
'test
contains text
');
});
it("should format as rich text", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("''test''");
expect(contentBody.innerHTML).toBe("test contains text
");
});
it("should format as rich text", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("''test'' content\n\n'''test'''");
expect(contentBody.innerHTML).toBe('test content
test contains text
');
});
it("should construct tables", function () {
givenHtml("page contains text
");
select("p", 0, "p", 4);
paste("| cell content |");
expect(contentBody.innerHTML).toBe(' cell content contains text
');
});
it("should add table if table markup is pasted", function () {
givenHtml("page contains text
");
select("p", 0, "p", 0);
paste("| cell content |\n" +
"| more content |\n");
expect(contentBody.innerHTML).toBe(' cell content more content
page contains text
');
});
});
});