-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone editor: Remove more dependencies (#2127)
* Standalone editor: decouple utilities * Standalone editor: Remove more dependencies * fix build * remove unnecessary code
- Loading branch information
1 parent
bee10b7
commit 9dfda12
Showing
37 changed files
with
733 additions
and
186 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages-content-model/roosterjs-content-model-dom/lib/domUtils/moveChildNodes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Replace all child nodes of the given target node to the child nodes of source node. | ||
* @param target Target node, all child nodes of this node will be removed if keepExistingChildren is not set to true | ||
* @param source (Optional) source node, all child nodes of this node will be move to target node | ||
* @param keepExistingChildren (Optional) When set to true, all existing child nodes of target will be kept | ||
*/ | ||
export function moveChildNodes(target: Node, source?: Node, keepExistingChildren?: boolean) { | ||
if (!target) { | ||
return; | ||
} | ||
|
||
while (!keepExistingChildren && target.firstChild) { | ||
target.removeChild(target.firstChild); | ||
} | ||
|
||
while (source?.firstChild) { | ||
target.appendChild(source.firstChild); | ||
} | ||
} | ||
|
||
/** | ||
* Wrap all child nodes of the given parent element using a new element with the given tag name | ||
* @param parent The parent element | ||
* @param tagName The tag name of new wrapper | ||
* @returns New wrapper element | ||
*/ | ||
export function wrapAllChildNodes<T extends keyof HTMLElementTagNameMap>( | ||
parent: HTMLElement, | ||
tagName: T | ||
): HTMLElementTagNameMap[T] { | ||
const newElement = parent.ownerDocument.createElement(tagName); | ||
|
||
moveChildNodes(newElement, parent); | ||
parent.appendChild(newElement); | ||
|
||
return newElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages-content-model/roosterjs-content-model-dom/lib/modelToDom/handlers/handleTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages-content-model/roosterjs-content-model-dom/test/domUtils/moveChildNodesTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { moveChildNodes, wrapAllChildNodes } from '../../lib/domUtils/moveChildNodes'; | ||
|
||
describe('moveChildNodes', () => { | ||
function htmlToDom(html: string) { | ||
let element = document.createElement('DIV'); | ||
element.innerHTML = html; | ||
|
||
return element.firstChild as HTMLElement; | ||
} | ||
|
||
function runTest( | ||
targetHtml: string, | ||
sourceHtml: string, | ||
keepExisting: boolean, | ||
expectedTargetHtml: string | ||
) { | ||
const source = htmlToDom(sourceHtml); | ||
const target = htmlToDom(targetHtml); | ||
|
||
moveChildNodes(target, source, keepExisting); | ||
|
||
const targetResult = target ? target.outerHTML : ''; | ||
|
||
expect(targetResult).toBe(expectedTargetHtml); | ||
} | ||
|
||
it('null input', () => { | ||
runTest(null!, null!, true, ''); | ||
runTest(null!, null!, false, ''); | ||
}); | ||
|
||
it('null target input', () => { | ||
runTest(null!, '<div><span>test</span></div>', true, ''); | ||
runTest(null!, '<div><span>test</span></div>', false, ''); | ||
}); | ||
|
||
it('null source input', () => { | ||
runTest('<div><span>test</span></div>', null!, true, '<div><span>test</span></div>'); | ||
runTest('<div><span>test</span></div>', null!, false, '<div></div>'); | ||
}); | ||
|
||
it('null source input', () => { | ||
runTest('<div><span>test</span></div>', null!, true, '<div><span>test</span></div>'); | ||
runTest('<div><span>test</span></div>', null!, false, '<div></div>'); | ||
}); | ||
|
||
it('regular case', () => { | ||
runTest( | ||
'<div><span>test1</span><span>test2</span></div>', | ||
'<div><span>test3</span><span>test4</span></div>', | ||
false, | ||
'<div><span>test3</span><span>test4</span></div>' | ||
); | ||
runTest( | ||
'<div><span>test1</span><span>test2</span></div>', | ||
'<div><span>test3</span><span>test4</span></div>', | ||
true, | ||
'<div><span>test1</span><span>test2</span><span>test3</span><span>test4</span></div>' | ||
); | ||
}); | ||
}); | ||
|
||
describe('wrapAllChildNodes', () => { | ||
it('Single element, no child', () => { | ||
const div = document.createElement('div'); | ||
const result = wrapAllChildNodes(div, 'span'); | ||
|
||
expect(div.innerHTML).toBe('<span></span>'); | ||
expect(result).toBe(div.firstChild as HTMLElement); | ||
}); | ||
|
||
it('Single element, with child nodes', () => { | ||
const div = document.createElement('div'); | ||
|
||
div.innerHTML = 'test<b>test2</b>test3'; | ||
const result = wrapAllChildNodes(div, 'span'); | ||
|
||
expect(div.innerHTML).toBe('<span>test<b>test2</b>test3</span>'); | ||
expect(result).toBe(div.firstChild as HTMLElement); | ||
}); | ||
}); |
Oops, something went wrong.