Skip to content

Commit

Permalink
[#65872] replace querySelector with getElementById
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Sep 19, 2024
1 parent 4d44712 commit bf6622c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/hooks/markdownMermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const markdownItMermaid = (md, { preview, lineMap, parent }) => {
cached = contentCache.get(new IMurMurHash(code, hashSeed).result());
}
const id = Math.random().toString().replace(".", "");
token.attrSet("data-mermaid-id", id);
token.attrSet("id", `mermaid-${id}`);

if (cached) {
token.attrSet("class", "mermaid");
Expand All @@ -49,7 +49,7 @@ const markdownItMermaid = (md, { preview, lineMap, parent }) => {
container.style.visibility = "none";
document.body.appendChild(container);

waitForElement(preview.current, `[data-mermaid-id="${id}"]`).then((el) => {
waitForElement(preview.current, `mermaid-${id}`).then((el) => {
mermaid
.render(`mermaid-${id}`, code, container)
.then(({ svg }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/markdownReplacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PreviewWrapper {
const placeholderId = "placeholder-" + Math.random().toString().slice(2);

promise
.then(waitForElement(this.preview, `#${placeholderId}`))
.then(waitForElement(this.preview, placeholderId))
.then((result) => {
setCached(input, result);
this.fillPlaceholder(placeholderId, result);
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export async function waitForElement(parent, selector) {
export async function waitForElement(parent, id) {
return new Promise((resolve) => {
const elem = parent.querySelector(selector);
const elem = parent.getElementById(id);
if (elem) {
resolve(elem);
return;
}

const observer = new MutationObserver(() => {
const elem = parent.querySelector(selector);
const elem = parent.getElementById(id);
if (elem) {
observer.disconnect();
resolve(elem);
Expand Down

0 comments on commit bf6622c

Please sign in to comment.