FIX: proper details / summary excerpt (#30229)

It doesn't make much sense to have the content of a `<details>` in an excerpt so I replaced them with "▶ summary" instead.

That way, they can't be (ab)used in user cards for example.

Reference - https://meta.discourse.org/t/335094
This commit is contained in:
Régis Hanol
2024-12-12 09:09:49 +01:00
committed by GitHub
parent 7c00c52c36
commit 44cabc3569
3 changed files with 13 additions and 78 deletions

View File

@ -24,8 +24,6 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@start_excerpt = false
@start_hashtag_icon = false
@in_details_depth = 0
@summary_contents = +""
@detail_contents = +""
end
def self.get_excerpt(html, length, options)
@ -127,12 +125,11 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
include_tag(name, attributes)
end
when "details"
@detail_contents = +"" if @in_details_depth == 0
@in_details_depth += 1
when "summary"
if @in_details_depth == 1 && !@in_summary
@summary_contents = +""
@in_summary = true
characters("", truncate: false, count_it: false, encode: false)
end
when "svg"
attributes = Hash[*attributes.flatten]
@ -162,29 +159,6 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@in_quote = false
when "details"
@in_details_depth -= 1
if @in_details_depth == 0
@summary_contents = clean(@summary_contents)
@detail_contents = clean(@detail_contents)
if @current_length + @summary_contents.length >= @length
characters(
@summary_contents,
encode: false,
before_string: "<details class='disabled'><summary>",
after_string: "</summary></details>",
)
else
characters(
@summary_contents,
truncate: false,
encode: false,
before_string: "<details><summary>",
after_string: "</summary>",
)
characters(@detail_contents, encode: false, after_string: "</details>")
end
end
when "summary"
@in_summary = false if @in_details_depth == 1
when "div", "span"
@ -210,18 +184,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
before_string: nil,
after_string: nil
)
return if @in_quote
return if @in_quote || @in_details_depth > 1 || (@in_details_depth == 1 && !@in_summary)
# we call length on this so might as well ensure we have a string
string = string.to_s
if @in_details_depth > 0
if @in_summary
@summary_contents << string
else
@detail_contents << string
end
return
end
@excerpt << before_string if before_string