diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/base-code-block-copied-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/base-code-block-copied-linux.png index 19d403ce..34f82346 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/base-code-block-copied-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/base-code-block-copied-linux.png differ diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/long-content-code-block-text-wrapping-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/long-content-code-block-text-wrapping-linux.png index afd3c840..fd553cb4 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/long-content-code-block-text-wrapping-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/chromium/long-content-code-block-text-wrapping-linux.png differ diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/base-code-block-copied-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/base-code-block-copied-linux.png index 122d5f51..a09497c8 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/base-code-block-copied-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/base-code-block-copied-linux.png differ diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/long-content-code-block-text-wrapping-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/long-content-code-block-text-wrapping-linux.png index c3597cc6..e11fc8ff 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/long-content-code-block-text-wrapping-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/firefox/long-content-code-block-text-wrapping-linux.png differ diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/base-code-block-copied-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/base-code-block-copied-linux.png index 7f54fb2a..0bf062cf 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/base-code-block-copied-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/base-code-block-copied-linux.png differ diff --git a/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/long-content-code-block-text-wrapping-linux.png b/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/long-content-code-block-text-wrapping-linux.png index 923b6c9b..5af166e2 100644 Binary files a/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/long-content-code-block-text-wrapping-linux.png and b/e2e/tests/__screenshots__/codeBlock.spec.ts/webkit/long-content-code-block-text-wrapping-linux.png differ diff --git a/src/js/code.ts b/src/js/code.ts index a67ef5ff..2d8d20a8 100644 --- a/src/js/code.ts +++ b/src/js/code.ts @@ -2,6 +2,7 @@ import {copyToClipboard, getEventTarget, isCustom} from './utils'; const COPY_BUTTON_SELECTOR = '.yfm-clipboard-button'; const WRAP_BUTTON_SELECTOR = '.yfm-wrapping-button'; +const FLOATING_CONTAINER_SELECTOR = '.yfm-code-floating-container'; function notifySuccess(svgButton: HTMLElement | null) { if (!svgButton) { @@ -21,9 +22,11 @@ function notifySuccess(svgButton: HTMLElement | null) { } function buttonCopyFn(target: HTMLElement) { - const container = target.parentNode?.parentNode; + const button = target.closest(COPY_BUTTON_SELECTOR); + const container = button?.closest(FLOATING_CONTAINER_SELECTOR); const code = container?.querySelector('pre code'); - if (!container || !code) { + + if (!button || !container || !code) { return; } @@ -47,13 +50,14 @@ function buttonCopyFn(target: HTMLElement) { } function buttonWrapFn(target: HTMLElement) { - const container = target.parentNode?.parentNode; + const button = target.closest(WRAP_BUTTON_SELECTOR); + const container = button?.closest(FLOATING_CONTAINER_SELECTOR); const code = container?.querySelector('pre code'); - if (!container || !code) { + if (!button || !container || !code) { return; } - code.classList.toggle('wrap'); + button.setAttribute('aria-pressed', String(code.classList.contains('wrap'))); setTimeout(() => target.blur(), 500); } @@ -66,9 +70,9 @@ if (typeof document !== 'undefined') { const target = getEventTarget(event) as HTMLElement; - if (target.matches(COPY_BUTTON_SELECTOR)) { + if (target.closest(COPY_BUTTON_SELECTOR)) { buttonCopyFn(target); - } else if (target.matches(WRAP_BUTTON_SELECTOR)) { + } else if (target.closest(WRAP_BUTTON_SELECTOR)) { buttonWrapFn(target); } });