-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone Editor: Provide a DOMHelper to allow access DOM tree (#2363)
- Loading branch information
1 parent
c16ba1a
commit 9c69ea0
Showing
10 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages-content-model/roosterjs-content-model-core/lib/editor/DOMHelperImpl.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,17 @@ | ||
import { toArray } from 'roosterjs-content-model-dom'; | ||
import type { DOMHelper } from 'roosterjs-content-model-types'; | ||
|
||
class DOMHelperImpl implements DOMHelper { | ||
constructor(private contentDiv: HTMLElement) {} | ||
|
||
queryElements(selector: string): HTMLElement[] { | ||
return toArray(this.contentDiv.querySelectorAll(selector)) as HTMLElement[]; | ||
} | ||
} | ||
|
||
/** | ||
* @internal Create new instance of DOMHelper | ||
*/ | ||
export function createDOMHelper(contentDiv: HTMLElement): DOMHelper { | ||
return new DOMHelperImpl(contentDiv); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
packages-content-model/roosterjs-content-model-core/test/editor/DOMHelperImplTest.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,20 @@ | ||
import { createDOMHelper } from '../../lib/editor/DOMHelperImpl'; | ||
|
||
describe('DOMHelperImpl', () => { | ||
it('queryElements', () => { | ||
const mockedResult = ['RESULT'] as any; | ||
const querySelectorAllSpy = jasmine | ||
.createSpy('querySelectorAll') | ||
.and.returnValue(mockedResult); | ||
const mockedDiv: HTMLElement = { | ||
querySelectorAll: querySelectorAllSpy, | ||
} as any; | ||
const mockedSelector = 'SELECTOR'; | ||
const domHelper = createDOMHelper(mockedDiv); | ||
|
||
const result = domHelper.queryElements(mockedSelector); | ||
|
||
expect(result).toEqual(mockedResult); | ||
expect(querySelectorAllSpy).toHaveBeenCalledWith(mockedSelector); | ||
}); | ||
}); |
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
22 changes: 22 additions & 0 deletions
22
packages-content-model/roosterjs-content-model-types/lib/parameter/DOMHelper.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,22 @@ | ||
/** | ||
* A helper class to provide DOM access APIs | ||
*/ | ||
export interface DOMHelper { | ||
/** | ||
* Query HTML elements in editor by tag name. | ||
* Be careful of this function since it will also return element under entity. | ||
* @param tag Tag name of the element to query | ||
* @returns HTML Element array of the query result | ||
*/ | ||
queryElements<TTag extends keyof HTMLElementTagNameMap>( | ||
tag: TTag | ||
): HTMLElementTagNameMap[TTag][]; | ||
|
||
/** | ||
* Query HTML elements in editor by a selector string | ||
* Be careful of this function since it will also return element under entity. | ||
* @param selector Selector string to query | ||
* @returns HTML Element array of the query result | ||
*/ | ||
queryElements(selector: string): HTMLElement[]; | ||
} |