Skip to content

Commit b11f2a6

Browse files
committed
Address code review feedback
Narrow closest('a') result with instanceof HTMLAnchorElement for type safety. Skip non-web protocols (mailto:, tel:, etc.) in isExternalLinkCandidate to match body-level controller behavior. Pass element reference to paste_links JS instead of global querySelector.
1 parent c003950 commit b11f2a6

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

frontend/src/react/extensions/external-link-capture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const ExternalLinkCaptureExtension = Extension.create({
6363
? event.target
6464
: (event.target as Node)?.parentElement;
6565
const link = target?.closest('a');
66-
if (!link) return false;
66+
if (!(link instanceof HTMLAnchorElement)) return false;
6767
if (!view.dom.contains(link)) return false;
6868
if (!isExternalLinkCandidate(link)) return false;
6969
if (!isLinkExternal(link)) return false;

frontend/src/stimulus/helpers/external-link-helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ export function isLinkExternal(link:HTMLAnchorElement) {
5858

5959
/**
6060
* Returns true when the link is eligible for external-link processing.
61-
* Links with empty hrefs (e.g. anchor-only) and download links are never
62-
* rewritten or intercepted.
61+
* Links with empty hrefs (e.g. anchor-only), download links, and non-web
62+
* protocols (mailto:, tel:, javascript:, etc.) are never rewritten or intercepted.
6363
*/
6464
export function isExternalLinkCandidate(link:HTMLAnchorElement) {
6565
const href = link.href || '';
6666
if (href === '' || link.hasAttribute('download')) return false;
67+
if (!link.protocol.startsWith('http')) return false;
6768
return true;
6869
}
6970

spec/support/form_fields/primerized/block_note_editor_input.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ def shadow_root
5959
# { text: "Two", url: "https://two.com" }
6060
# )
6161
def paste_links(*links)
62-
element.click
62+
el = element
63+
el.click
6364

6465
html = links.map { |l| %(<a href="#{l[:url]}">#{l[:text]}</a>) }.join(" ")
6566
plain = links.pluck(:text).join(" ")
6667

67-
page.execute_script(<<~JS, html, plain)
68-
const el = document.querySelector('op-block-note').shadowRoot.querySelector('div[role="textbox"]');
68+
page.execute_script(<<~JS, el.native, html, plain)
69+
const el = arguments[0];
6970
const dt = new DataTransfer();
70-
dt.setData('text/html', arguments[0]);
71-
dt.setData('text/plain', arguments[1]);
71+
dt.setData('text/html', arguments[1]);
72+
dt.setData('text/plain', arguments[2]);
7273
el.dispatchEvent(new ClipboardEvent('paste', { clipboardData: dt, bubbles: true, cancelable: true }));
7374
JS
7475
end

0 commit comments

Comments
 (0)