From 55e0d3d69c1383a31fd61ec6c93d2cdd531fa3a9 Mon Sep 17 00:00:00 2001 From: "SOUTHAMERICA\\bvalverde" Date: Wed, 10 Jul 2024 09:40:59 -0600 Subject: [PATCH 1/2] init --- .../coreApi/setEditorStyle/ensureUniqueId.ts | 2 +- .../setDOMSelection/setDOMSelectionTest.ts | 2 +- .../setEditorStyle/ensureUniqueIdTest.ts | 45 ++++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/roosterjs-content-model-core/lib/coreApi/setEditorStyle/ensureUniqueId.ts b/packages/roosterjs-content-model-core/lib/coreApi/setEditorStyle/ensureUniqueId.ts index c3cb20c5363..c52e9f0bc47 100644 --- a/packages/roosterjs-content-model-core/lib/coreApi/setEditorStyle/ensureUniqueId.ts +++ b/packages/roosterjs-content-model-core/lib/coreApi/setEditorStyle/ensureUniqueId.ts @@ -7,7 +7,7 @@ export function ensureUniqueId(element: HTMLElement, idPrefix: string): string { const doc = element.ownerDocument; let i = 0; - while (!element.id || doc.querySelectorAll('#' + element.id).length > 1) { + while (!element.id || doc.querySelectorAll(`[id="${element.id}"]`).length > 1) { element.id = idPrefix + '_' + i++; } diff --git a/packages/roosterjs-content-model-core/test/coreApi/setDOMSelection/setDOMSelectionTest.ts b/packages/roosterjs-content-model-core/test/coreApi/setDOMSelection/setDOMSelectionTest.ts index 5ecbc2ffb25..26f8537f3c9 100644 --- a/packages/roosterjs-content-model-core/test/coreApi/setDOMSelection/setDOMSelectionTest.ts +++ b/packages/roosterjs-content-model-core/test/coreApi/setDOMSelection/setDOMSelectionTest.ts @@ -529,7 +529,7 @@ describe('setDOMSelection', () => { createRangeSpy.and.returnValue(mockedRange); querySelectorAllSpy.and.callFake(selector => { - return selector == '#image_0' ? ['', ''] : ['']; + return selector == '[id="image_0"]' ? ['', ''] : ['']; }); hasFocusSpy.and.returnValue(false); diff --git a/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts b/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts index d45aeeb931e..3b8aa583416 100644 --- a/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts +++ b/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts @@ -1,3 +1,4 @@ +import * as ensureUniqueIdFile from '../../../lib/coreApi/setEditorStyle/ensureUniqueId'; import { ensureUniqueId } from '../../../lib/coreApi/setEditorStyle/ensureUniqueId'; describe('ensureUniqueId', () => { @@ -38,10 +39,52 @@ describe('ensureUniqueId', () => { id: 'dup', } as any; querySelectorAllSpy.and.callFake((selector: string) => - selector == '#dup' ? [{}, {}] : [] + selector == '[id="dup"]' ? [{}, {}] : [] ); const result = ensureUniqueId(element, 'prefix'); expect(result).toBe('dup_0'); }); + + it('Should not throw when element id starts with number', () => { + const element = { + ownerDocument: doc, + id: '0', + } as any; + + let isFirst = true; + querySelectorAllSpy.and.callFake((_selector: string) => { + if (isFirst) { + isFirst = false; + return [{}, {}]; + } + return [{}]; + }); + + ensureUniqueId(element, 'prefix'); + + expect(querySelectorAllSpy).toHaveBeenCalledWith('[id="0"]'); + expect(element.id).toEqual('0_0'); + }); + + it('Should not throw when element id starts with hyphen', () => { + const element = { + ownerDocument: doc, + id: '-', + } as any; + + let isFirst = true; + querySelectorAllSpy.and.callFake((_selector: string) => { + if (isFirst) { + isFirst = false; + return [{}, {}]; + } + return [{}]; + }); + + ensureUniqueId(element, 'prefix'); + + expect(querySelectorAllSpy).toHaveBeenCalledWith('[id="-"]'); + expect(element.id).toEqual('-_0'); + }); }); From ff632e3336dfade24c2a425f75c717136d3a2b62 Mon Sep 17 00:00:00 2001 From: "SOUTHAMERICA\\bvalverde" Date: Wed, 10 Jul 2024 09:47:37 -0600 Subject: [PATCH 2/2] Fix build --- .../test/coreApi/setEditorStyle/ensureUniqueIdTest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts b/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts index 3b8aa583416..0237af7ef8e 100644 --- a/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts +++ b/packages/roosterjs-content-model-core/test/coreApi/setEditorStyle/ensureUniqueIdTest.ts @@ -1,4 +1,3 @@ -import * as ensureUniqueIdFile from '../../../lib/coreApi/setEditorStyle/ensureUniqueId'; import { ensureUniqueId } from '../../../lib/coreApi/setEditorStyle/ensureUniqueId'; describe('ensureUniqueId', () => {