0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/spec/system/page_objects/modals/upsert_hyperlink.rb
Régis Hanol 7bb363b4cb
FIX: preserve selection formatting in link-insertion modal (#39374)
When CMD+K is pressed (or the link toolbar button clicked) with a
non-empty selection, the modal now only asks for the URL — the existing
selection is wrapped as the link's display content, keeping every inline
mark and block context under it.

Previously the modal pre-filled a "Link text" field from the selection.
In the rich-text editor this serialized the selected slice to markdown
and leaked syntax the user never typed: selecting a word inside an H2
produced `## word`, selecting text inside an inline code span produced
backticks, selecting inside a list produced `- word`, and so on.

The new behavior wraps the selection directly instead of replacing it:

- Rich-text editor: adds a `link` mark over the selection range,
preserving every underlying mark (bold, italic, code, emoji, mentions,
…) and every surrounding block (heading, list, blockquote, …).
- Markdown editor: inserts `[` at the selection start and `](url)` at
the end, preserving any literal characters in between.

Both editors now behave identically for the common "select then link"
flow. The link-text field is still shown when:

- Inserting a link with no selection (the user needs to provide display
text).
- Editing an existing link via the link-toolbar (the user may want to
change the displayed text).

A small `applyLink(url)` method is exposed on the text-manipulation API
of each editor, and `toolbarEvent.applyLink` lets the modal pick the
right path without knowing which editor is active. The chat composer is
wired through the same path.

Ref - t/182095
2026-05-20 11:22:35 +02:00

39 lines
951 B
Ruby
Vendored

# frozen_string_literal: true
module PageObjects
module Modals
class UpsertHyperlink < PageObjects::Modals::Base
BODY_SELECTOR = ".upsert-hyperlink-modal"
MODAL_SELECTOR = ".upsert-hyperlink-modal"
LINK_TEXT_SELECTOR = ".d-modal__body input.link-text"
LINK_URL_SELECTOR = ".d-modal__body input.link-url"
def fill_in_link_text(text)
find(LINK_TEXT_SELECTOR).fill_in(with: text)
end
def send_enter_link_text
find(LINK_TEXT_SELECTOR).send_keys(:enter)
end
def fill_in_link_url(url)
find(LINK_URL_SELECTOR).fill_in(with: url)
end
def link_text_value
find(LINK_TEXT_SELECTOR).value
end
def link_url_value
find(LINK_URL_SELECTOR).value
end
def has_link_text_field?
has_css?(LINK_TEXT_SELECTOR)
end
def has_no_link_text_field?
has_no_css?(LINK_TEXT_SELECTOR)
end
end
end
end