discourse/spec/system/post_selection_fast_edit_spec.rb
zogstrip aaec80413d FIX: fast edit with a typographic character
When a post containing an apostrophe (') is being cooked, the apostrophe is being converted to the "typographic" version (’) (because we enable markdown-it's **typographer** mode by default in Discourse)

When you select text that contains such apostrophe and then try to save your fast edit, it fails miserably without any error.

That's because when you select text from the DOM, it uses the cooked version which has the typographic apostrophe.

When you save your fast edit, we fetch the raw version of the post, which has the "regular" apostrophe. Thus doing `raw.replace(selectedText, editedText)` doesn't work because `raw` has the regular apostrophe but `selectedText` has the typographic apostrophe.

Since it's somewhat complicated to handle all typographic characters, we would basically have to reverse the process done in `custom-typographer-replacements.js`, we instead bail out and show the composer when we detect such character in the selection.

Internal ref - t/143836
2024-12-10 12:13:10 +01:00

123 lines
3.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
describe "Post selection | Fast edit", type: :system do
let(:topic_page) { PageObjects::Pages::Topic.new }
let(:fast_editor) { PageObjects::Components::FastEditor.new }
fab!(:topic)
fab!(:post) { Fabricate(:post, topic:) }
fab!(:post_2) { Fabricate(:post, topic:, raw: "It twas a great “time”!") }
fab!(:spanish_post) { Fabricate(:post, topic:, raw: "Hola Juan, ¿cómo estás?") }
fab!(:chinese_post) { Fabricate(:post, topic:, raw: "这是一个测试") }
fab!(:post_with_emoji) { Fabricate(:post, topic:, raw: "Good morning :wave:!") }
fab!(:post_with_quote) do
Fabricate(
:post,
topic:,
raw: "[quote]\n#{post_2.raw}\n[/quote]\n\nBelle journée, n'est-ce pas ?",
)
end
fab!(:current_user) { Fabricate(:admin) }
before { sign_in(current_user) }
def css(post) = "#{topic_page.post_by_number_selector(post.post_number)} .cooked p"
context "when text is selected" do
before do
topic_page.visit_topic(topic)
select_text_range(css(post), 0, 5)
end
it "opens context menu" do
expect(topic_page.fast_edit_button).to be_visible
end
context "when clicking the fast edit button" do
before { topic_page.click_fast_edit_button }
it "opens the fast editor" do
expect(topic_page.fast_edit_input).to be_visible
end
context "when entering some text and clicking the save button" do
before do
fast_editor.fill_content("Howdy")
fast_editor.save
end
it "saves changes" do
expect(page).to have_selector(css(post), text: "Howdy world")
end
end
end
end
context "when text selected is inside a quote" do
it "opens the composer directly" do
topic_page.visit_topic(topic)
select_text_range(css(post_with_quote), 5, 10)
topic_page.click_fast_edit_button
expect(topic_page).to have_expanded_composer
end
end
context "when editing text that has strange characters" do
it "saves when paragraph contains apostrophes" do
topic_page.visit_topic(topic)
select_text_range(css(post_2), 19, 4)
topic_page.click_fast_edit_button
fast_editor.fill_content("day")
fast_editor.save
expect(page).to have_selector(css(post_2), text: "It twas a great “day”!")
end
it "saves when text contains diacritics" do
topic_page.visit_topic(topic)
select_text_range(css(spanish_post), 11, 12)
topic_page.click_fast_edit_button
fast_editor.fill_content("¿está todo bien?")
fast_editor.save
expect(page).to have_selector(css(spanish_post), text: "Hola Juan, ¿está todo bien?")
end
it "saves when text contains CJK ranges" do
topic_page.visit_topic(topic)
select_text_range(css(chinese_post), 0, 2)
topic_page.click_fast_edit_button
fast_editor.fill_content("今天")
fast_editor.save
expect(page).to have_selector(css(chinese_post), text: "今天一个测试")
end
it "saves when text contains emoji" do
topic_page.visit_topic(topic)
select_text_range(css(post_with_emoji), 5, 7)
topic_page.click_fast_edit_button
fast_editor.fill_content("day")
fast_editor.save
# NOTE: the emoji isn't picked up by the "text:" selector
expect(page).to have_selector(css(post_with_emoji), text: "Good day !")
# So we also check the raw content to ensure it's been saved correctly
expect(post_with_emoji.reload.raw).to eq "Good day :wave:!"
end
end
end