Skip to content

Commit

Permalink
6.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Jan 1, 2019
1 parent f838900 commit 40a9504
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 789 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "roosterjs",
"version": "6.19.1",
"version": "6.19.2",
"description": "Framework-independent javascript editor",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import attachDomEvent from '../../coreAPI/attachDomEvent';
import createEditorCore from '../../editor/createEditorCore';
import Editor from '../../editor/Editor';
import EditorCore from '../../editor/EditorCore';
import EditorPlugin from '../../editor/EditorPlugin';
import { PluginEvent, PluginEventType, PluginKeyboardEvent } from 'roosterjs-editor-types';

class MockPlugin implements EditorPlugin {
lastEvent: PluginEvent = null;
initialize(editor: Editor) {}
dispose() {}
onPluginEvent(e: PluginEvent) {
this.lastEvent = e;
}
}

describe('attachDomEvent', () => {
let div: HTMLDivElement;
let core: EditorCore;
beforeEach(() => {
div = document.createElement('div');
document.body.appendChild(div);
core = createEditorCore(div, {});
(<any>core).plugins = core.plugins.filter(
plugin => plugin != core.undo && plugin != core.corePlugin
);
});

afterEach(() => {
document.body.removeChild(div);
core = null;
div = null;
});

it('Check return value to be a function', () => {
let disposer = attachDomEvent(core, 'click');
expect(typeof disposer).toBe('function');

disposer();
});

it('Check event is fired', () => {
let called = false;
let disposer = attachDomEvent(core, 'keydown', null, () => {
called = true;
});

let event = document.createEvent('KeyboardEvent');
event.initEvent('keydown');
div.dispatchEvent(event);

expect(called).toBe(true);
disposer();
});

it('Check event dispatched to plugin', () => {
let mockPlugin = new MockPlugin();
core.plugins.push(mockPlugin);
expect(mockPlugin.lastEvent).toBeNull();

let disposer = attachDomEvent(core, 'keydown', PluginEventType.KeyDown);

let event = document.createEvent('KeyboardEvent');
event.initEvent('keydown');
div.dispatchEvent(event);

expect(mockPlugin.lastEvent).not.toBeNull();
expect(mockPlugin.lastEvent.eventType).toBe(PluginEventType.KeyDown);
expect((<PluginKeyboardEvent>mockPlugin.lastEvent).rawEvent).toBe(event);
disposer();
});

it('Check event not dispatched to plugin after dispose', () => {
let mockPlugin = new MockPlugin();
core.plugins.push(mockPlugin);
expect(mockPlugin.lastEvent).toBeNull();

let disposer = attachDomEvent(core, 'keydown', PluginEventType.KeyDown);

let event = document.createEvent('KeyboardEvent');
event.initEvent('keydown');

disposer();

div.dispatchEvent(event);

expect(mockPlugin.lastEvent).toBeNull();
});
});
50 changes: 50 additions & 0 deletions packages/roosterjs-editor-core/lib/test/coreApi/hasFocusTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import createEditorCore from '../../editor/createEditorCore';
import EditorCore from '../../editor/EditorCore';
import hasFocus from '../../coreAPI/hasFocus';

describe('hasFocus', () => {
let div: HTMLDivElement;
let core: EditorCore;
beforeEach(() => {
div = document.createElement('div');
div.contentEditable = 'true';
document.body.appendChild(div);
core = createEditorCore(div, {});
(<any>core).plugins = core.plugins.filter(
plugin => plugin != core.undo && plugin != core.corePlugin
);
});

afterEach(() => {
document.body.removeChild(div);
core = null;
div = null;
});

it('Check editor has focus after set focus', () => {
core.api.focus(core);
let result = hasFocus(core);
expect(result).toBe(true);
});

it('Check editor does not have focus after set focus away', () => {
let button = document.createElement('button');
document.body.appendChild(button);
core.api.focus(core);
expect(hasFocus(core)).toBe(true);

button.focus();
expect(hasFocus(core)).toBe(false);

document.body.removeChild(button);
});

it('Check editor has focus after select', () => {
core.contentDiv.innerHTML = '<span>test</span>';
expect(hasFocus(core)).toBe(false);

let span = core.contentDiv.querySelector('span');
core.api.select(core, span.firstChild, 1);
expect(hasFocus(core)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class StartEndBlockElement implements BlockElement {
nodes[0] &&
nodes[0] != blockContext &&
nodes[0].parentNode != this.rootNode &&
!isStructureNode(nodes[0].parentNode)
STRUCTURE_NODE_TAGS.indexOf(getTagOfNode(nodes[0].parentNode)) < 0
) {
nodes = [splitBalancedNodeRange(nodes)];
}
Expand Down Expand Up @@ -171,7 +171,3 @@ export default class StartEndBlockElement implements BlockElement {
return this.contains(inlineElement.getContainerNode());
}
}

function isStructureNode(node: Node) {
return STRUCTURE_NODE_TAGS.indexOf(getTagOfNode(node)) >= 0;
}
11 changes: 3 additions & 8 deletions packages/roosterjs-editor-dom/lib/test/DomTestHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createRange from '../selection/createRange';
import getInlineElementAtNode from '../inlineElements/getInlineElementAtNode';
import NodeBlockElement from '../blockElements/NodeBlockElement';
import StartEndBlockElement from '../blockElements/StartEndBlockElement';
Expand Down Expand Up @@ -87,16 +88,10 @@ export function createInlineElementFromNode(node: Node, rootNode: Node): InlineE

// Create range from child nodes of given node
export function createRangeFromChildNodes(node: Node): Range {
let selectionRange = new Range();
selectionRange.setStartBefore(node.firstChild);
selectionRange.setEndAfter(node.lastChild);
return selectionRange;
return createRange(node.firstChild, node.lastChild);
}

// Create range from given HTMLElement
export function createRangeWithDiv(testDiv: HTMLElement): Range {
let selectionRange = new Range();
selectionRange.setStartBefore(testDiv);
selectionRange.setEndAfter(testDiv);
return selectionRange;
return createRange(testDiv);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import * as DomTestHelper from '../DomTestHelper';
import NodeBlockElement from '../../blockElements/NodeBlockElement';
import NodeInlineElement from '../../inlineElements/NodeInlineElement';
import Position from '../../selection/Position';
import { InlineElement } from 'roosterjs-editor-types';

let testID = 'NodeBlockElement';

Expand All @@ -12,39 +9,6 @@ function createNodeBlockElementWithContent(content: string): [NodeBlockElement,
return [nodeBlockElement, testDiv];
}

describe('NodeBlockElement getTextContent()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
});

function runTest(input: string, output: string) {
// Arrange
let [nodeBlockElement] = createNodeBlockElementWithContent(input);

// Act
let textContent = nodeBlockElement.getTextContent();

// Assert
expect(textContent).toBe(output);
}

it('input = <span>www.example.com</span>', () => {
runTest('<span>www.example.com</span>', 'www.example.com');
});

it('input = <p>hello world</p><span>www.example.com</span>', () => {
runTest('<p>hello world</p><span>www.example.com</span>', 'hello worldwww.example.com');
});

it('input = <a>link</a><span>www.example.com</span>', () => {
runTest('<a>link</a><span>www.example.com</span>', 'linkwww.example.com');
});

it('input = <img>', () => {
runTest('<img>', '');
});
});

describe('NodeBlockElement getStartNode()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
Expand Down Expand Up @@ -109,77 +73,6 @@ describe('NodeBlockElement getEndNode()', () => {
});
});

describe('NodeBlockElement getContentNodes()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
});

it('input = <span>www.example.com</span>', () => {
// Arrange
let [nodeBlockElement, testDiv] = createNodeBlockElementWithContent(
'<span>www.example.com</span>'
);

// Act
let contents = nodeBlockElement.getContentNodes();

// Assert
expect(contents).toEqual([testDiv]);
});

it('input = <span>part1</span><span>part2</span>', () => {
// Arrange
let [nodeBlockElement, testDiv] = createNodeBlockElementWithContent(
'<span>part1</span><span>part2</span>'
);

// Act
let contents = nodeBlockElement.getContentNodes();

// Assert
expect(contents).toEqual([testDiv]);
});
});

describe('NodeBlockElement getInlineElements()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
});

function runTest(
inlineElement: InlineElement,
startOffset: number,
endOffset: number,
node: Node
) {
let startPosition = new Position(node, startOffset);
let endPosition = new Position(node, endOffset);
expect(
DomTestHelper.isInlineElementEqual(
inlineElement,
startPosition,
endPosition,
node.textContent
)
).toBe(true);
}

it('input = <img><span>part1</span><a>part2</a>', () => {
// Arrange
let [nodeBlockElement] = createNodeBlockElementWithContent(
'<img><span>part1</span><a>part2</a>'
);

// Act
let inlineElements = nodeBlockElement.getInlineElements();

// Assert
runTest(inlineElements[0], 0, 1, document.createElement('img'));
runTest(inlineElements[1], 0, 5, document.createTextNode('part1'));
runTest(inlineElements[2], 0, 5, document.createTextNode('part2'));
});
});

describe('NodeBlockElement equals()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
Expand Down Expand Up @@ -261,40 +154,6 @@ describe('NodeBlockElement isAfter()', () => {
});
});

describe('NodeBlockElement isInBlock()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
});

function createNodeInlineElement(inlineElementContent: string): InlineElement {
let testDiv = DomTestHelper.createElementFromContent(testID, inlineElementContent);
let parentBlock = new NodeBlockElement(testDiv);
let inlineElement = new NodeInlineElement(testDiv.firstChild, parentBlock);
return inlineElement;
}

it('input = <img><span>part1</span><a>part2</a>', () => {
// Arrange
let [nodeBlockElement] = createNodeBlockElementWithContent(
'<img><span>part1</span><a>part2</a>'
);
let inlineElements = nodeBlockElement.getInlineElements();
let myInlineElement = createNodeInlineElement('<span>www.example.com</span>');

// Act
let isElement1InBlock = nodeBlockElement.isInBlock(inlineElements[0]);
let isElement2InBlock = nodeBlockElement.isInBlock(inlineElements[1]);
let isElement3InBlock = nodeBlockElement.isInBlock(inlineElements[2]);
let isElement4InBlock = nodeBlockElement.isInBlock(myInlineElement);

// Assert
expect(isElement1InBlock).toEqual(true);
expect(isElement2InBlock).toEqual(true);
expect(isElement3InBlock).toEqual(true);
expect(isElement4InBlock).toEqual(false);
});
});

describe('NodeBlockElement contains()', () => {
afterEach(() => {
DomTestHelper.removeElement(testID);
Expand Down
Loading

0 comments on commit 40a9504

Please sign in to comment.