From 5c3f1202cf8f78e2c4063cf53551879cb32c9572 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 6 Oct 2020 13:24:38 +0200 Subject: [PATCH] FIX: Handle poll titles when headings are present (#10832) Poll markdown processing failed when there were any heading elements preceding a poll. (Issue originally reported in https://github.com/discourse/discourse/commit/babbebfb3571b09bfe6600bbe0921ded3c29fb3e#commitcomment-42983768) --- .../lib/discourse-markdown/poll.js.es6 | 19 +++++--- plugins/poll/spec/lib/pretty_text_spec.rb | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 index b098a0b65bd..da28392b1ec 100644 --- a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 +++ b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 @@ -81,18 +81,25 @@ function invalidPoll(state, tag) { token.content = "[/" + tag + "]"; } -function getTitle(tokens) { - const open = tokens.findIndex((token) => token.type === "heading_open"); - const close = tokens.findIndex((token) => token.type === "heading_close"); +function getTitle(tokens, startToken) { + const startIndex = tokens.indexOf(startToken); + + if (startIndex === -1) { + return; + } + + const pollTokens = tokens.slice(startIndex); + const open = pollTokens.findIndex((token) => token.type === "heading_open"); + const close = pollTokens.findIndex((token) => token.type === "heading_close"); if (open === -1 || close === -1) { return; } - const titleTokens = tokens.slice(open + 1, close); + const titleTokens = pollTokens.slice(open + 1, close); // Remove the heading element - tokens.splice(open, close - open + 1); + tokens.splice(startIndex + open, close - open + 1); return titleTokens; } @@ -108,7 +115,7 @@ const rule = { }, after: function (state, openToken, raw) { - const titleTokens = getTitle(state.tokens); + const titleTokens = getTitle(state.tokens, openToken); let items = getListItems(state.tokens, openToken); if (!items) { diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb index b76d9c320df..d887b10e753 100644 --- a/plugins/poll/spec/lib/pretty_text_spec.rb +++ b/plugins/poll/spec/lib/pretty_text_spec.rb @@ -169,4 +169,49 @@ describe PrettyText do HTML end + + it "does not break when there are headings before/after a poll with a title" do + cooked = PrettyText.cook <<~MD + # Pre-heading + + [poll] + # What's your favorite *berry*? :wink: https://google.com/ + * Strawberry + * Raspberry + * Blueberry + [/poll] + + # Post-heading + MD + + expect(cooked).to include(<<~HTML) +
What’s your favorite berry? :wink: https://google.com/ +
+ HTML + + expect(cooked).to include("

Pre-heading

") + expect(cooked).to include("

Post-heading

") + end + + it "does not break when there are headings before/after a poll without a title" do + cooked = PrettyText.cook <<~MD + # Pre-heading + + [poll] + * Strawberry + * Raspberry + * Blueberry + [/poll] + + # Post-heading + MD + + expect(cooked).to_not include('
') + expect(cooked).to include(<<~HTML) +
+ HTML + + expect(cooked).to include("

Pre-heading

") + expect(cooked).to include("

Post-heading

") + end end