-
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.
Create getSafeIdSelector and replace all Id selectors with it. (#2747)
* init * Add other occurence
- Loading branch information
1 parent
1bf1e77
commit 9546b4e
Showing
11 changed files
with
309 additions
and
17 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
packages/roosterjs-content-model-dom/lib/domUtils/getSafeIdSelector.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 @@ | ||
const StartsWithUnsupportedCharacter = /^[.|\-|_|\d]/; | ||
|
||
/** | ||
* Returns a safe Id to use in Native APIs. | ||
* IDs that start with number or hyphen can throw errors if used. | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id | ||
* @param id | ||
* @returns | ||
*/ | ||
export function getSafeIdSelector(id: string) { | ||
if (!id) { | ||
return id; | ||
} | ||
|
||
if (id.match(StartsWithUnsupportedCharacter)) { | ||
return `[id="${id}"]`; | ||
} else { | ||
return `#${id}`; | ||
} | ||
} |
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
Oops, something went wrong.