Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Fix list buttons stacking markers on selections with blank lines.
sandeep36e24af4fdcb
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.tsindex 7bbe7ac053a..b6270a4a57c 100644--- a/web/src/compose_ui.ts+++ b/web/src/compose_ui.ts@@ -962,8 +962,13 @@ export let format_text = (const sections = section_off_selected_lines();let {before_lines, selected_lines, after_lines} = sections;const {separating_new_line_before, separating_new_line_after} = sections;- // If there is even a single unmarked line selected, we mark all.- const should_mark = selected_lines.split("\n").some((line) => !is_marked(line));+ // Blank lines never have markers, so exclude them when deciding whether+ // to mark or unmark. Otherwise a selection containing blank lines would+ // always be detected as "needs marking", causing the marker to be+ // prepended again on every click instead of toggling off.+ const non_blank_lines = selected_lines.split("\n").filter((line) => line.trim() !== "");+ const should_mark =+ non_blank_lines.length === 0 || non_blank_lines.some((line) => !is_marked(line));if (should_mark) {const lines = selected_lines.split("\n");// Only skip blank lines for multi-line selections, where blanksdiff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjsindex 30334fc96ab..3b1896d791d 100644--- a/web/tests/compose_ui.test.cjs+++ b/web/tests/compose_ui.test.cjs@@ -850,6 +850,12 @@ run_test("format_text - bulleted and numbered lists", ({override_rewire}) => {compose_ui.format_text($textarea, "bulleted");assert.equal(get_textarea_state(), "<\n- first_item\n\n- second_item\n\n- third_item>");+ // Toggling off a bulleted list that contains blank lines between items+ // should strip markers, not prepend new ones.+ init_textarea_state("<- first_item\n\n- second_item\n\n- third_item>");+ compose_ui.format_text($textarea, "bulleted");+ assert.equal(get_textarea_state(), "<first_item\n\nsecond_item\n\nthird_item>");+// Toggling off bulleted listinit_textarea_state("<- first_item\n- second_item>");compose_ui.format_text($textarea, "bulleted");@@ -896,6 +902,12 @@ run_test("format_text - bulleted and numbered lists", ({override_rewire}) => {compose_ui.format_text($textarea, "numbered");assert.equal(get_textarea_state(), "<\n1. first_item\n\n2. second_item\n\n3. third_item>");+ // Toggling off a numbered list that contains blank lines between items+ // should strip markers, not prepend new ones.+ init_textarea_state("<1. first_item\n\n2. second_item\n\n3. third_item>");+ compose_ui.format_text($textarea, "numbered");+ assert.equal(get_textarea_state(), "<first_item\n\nsecond_item\n\nthird_item>");+// Toggling off numbered listinit_textarea_state("<1. first_item\n2. second_item>");compose_ui.format_text($textarea, "numbered");