Repository: mozilla/pdf.js
File: test/integration/accessibility_spec.mjs (JavaScript)

Code before:
    it("must check that span hasn't an 'undefined' id", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          const id = await page.$eval("span.markedContent", span => span.id);
          expect(id).withContext(`In ${browserName}`).toBe("");
        })
      );
    });
  });

  describe("MathML in AF entry from LaTeX", () => {
    let pages;

    beforeEach(async () => {
      pages = await loadAndWait("bug1937438_af_from_latex.pdf", ".textLayer");
    });

    afterEach(async () => {
      await closePages(pages);
    });

    it("must check that the MathML is correctly inserted", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          const isSanitizerSupported = await page.evaluate(() =>
            (() => {
              try {
                // eslint-disable-next-line no-undef
                return typeof Sanitizer !== "undefined";
              } catch {
                return false;
              }
            })()
          );
          if (isSanitizerSupported) {
            const mathML = await page.$eval(
              "span.structTree span[aria-owns='p58R_mc13'] > math",
              el => el?.innerHTML ?? ""
            );
            expect(mathML)
              .withContext(`In ${browserName}`)
              .toEqual(
                " <msqrt><msup><mi>x</mi><mn>2</mn></msup></msqrt> <mo>=</mo> <mrow><mo>|</mo><mi>x</mi><mo>|</mo></mrow> "
              );
          }
        })
      );
    });
  });

  describe("MathML tags in the struct tree", () => {

Review comment (security):
Do we really need a closure here, or could we also simply run the try/catch inside the `evaluate` block like below (untested)?

```
const isSanitizerSupported = await page.evaluate(() => {
  try {
    // eslint-disable-next-line no-undef
    return typeof Sanitizer !== "undefined";
  } catch {
    return false;
  }
});

Code after:
    it("must check that span hasn't an 'undefined' id", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          const id = await page.$eval("span.markedContent", span => span.id);
          expect(id).withContext(`In ${browserName}`).toBe("");
        })
      );
    });
  });

  describe("MathML in AF entry from LaTeX", () => {
    let pages;

    beforeEach(async () => {
      pages = await loadAndWait("bug1937438_af_from_latex.pdf", ".textLayer");
    });

    afterEach(async () => {
      await closePages(pages);
    });

    it("must check that the MathML is correctly inserted", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          const isSanitizerSupported = await page.evaluate(() => {
            try {
              // eslint-disable-next-line no-undef
              return typeof Sanitizer !== "undefined";
            } catch {
              return false;
            }
          });
          if (isSanitizerSupported) {
            const mathML = await page.$eval(
              "span.structTree span[aria-owns='p58R_mc13'] > math",
              el => el?.innerHTML ?? ""
            );
            expect(mathML)
              .withContext(`In ${browserName}`)
              .toEqual(
                " <msqrt><msup><mi>x</mi><mn>2</mn></msup></msqrt> <mo>=</mo> <mrow><mo>|</mo><mi>x</mi><mo>|</mo></mrow> "
              );
          } else {
            pending(`Sanitizer API (in ${browserName}) is not supported`);
          }
        })
      );
    });
  });

  describe("MathML tags in the struct tree", () => {