FIX: Ignore max excerpt length for div excerpts too (#13058)
We support two types of custom excerpts. It can be <div class="excerpt"> or <span class="excerpt">: b21f74060e/lib/excerpt_parser.rb (L120)
We also ignore max excerpt length for custom excerpts. But we forgot to process div when ignoring max length.
This commit is contained in:
parent
332ae97555
commit
c62efc0f0f
|
@ -4,7 +4,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
|
|
||||||
attr_reader :excerpt
|
attr_reader :excerpt
|
||||||
|
|
||||||
SPAN_REGEX = /<\s*span[^>]*class\s*=\s*['|"]excerpt['|"][^>]*>/
|
CUSTOM_EXCERPT_REGEX = /<\s*(span|div)[^>]*class\s*=\s*['"]excerpt['"][^>]*>/
|
||||||
|
|
||||||
def initialize(length, options = nil)
|
def initialize(length, options = nil)
|
||||||
@length = length
|
@length = length
|
||||||
|
@ -29,7 +29,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
|
|
||||||
def self.get_excerpt(html, length, options)
|
def self.get_excerpt(html, length, options)
|
||||||
html ||= ''
|
html ||= ''
|
||||||
length = html.length if html.include?('excerpt') && SPAN_REGEX === html
|
length = html.length if html.include?('excerpt') && CUSTOM_EXCERPT_REGEX === html
|
||||||
me = self.new(length, options)
|
me = self.new(length, options)
|
||||||
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
||||||
catch(:done) do
|
catch(:done) do
|
||||||
|
|
|
@ -798,18 +798,24 @@ describe PrettyText do
|
||||||
expect(post.excerpt).to eq("hello <a href=\"https://site.com\" rel=\"noopener nofollow ugc\">site</a>")
|
expect(post.excerpt).to eq("hello <a href=\"https://site.com\" rel=\"noopener nofollow ugc\">site</a>")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "handles div excerpt at the beginning of a post" do
|
||||||
|
expect(PrettyText.excerpt("<div class='excerpt'>hi</div> test", 100)).to eq('hi')
|
||||||
|
end
|
||||||
|
|
||||||
it "handles span excerpt at the beginning of a post" do
|
it "handles span excerpt at the beginning of a post" do
|
||||||
expect(PrettyText.excerpt("<span class='excerpt'>hi</span> test", 100)).to eq('hi')
|
expect(PrettyText.excerpt("<span class='excerpt'>hi</span> test", 100)).to eq('hi')
|
||||||
post = Fabricate(:post, raw: "<span class='excerpt'>hi</span> test")
|
end
|
||||||
expect(post.excerpt).to eq("hi")
|
|
||||||
|
it "ignores max excerpt length if a div excerpt is specified" do
|
||||||
|
two_hundred = "123456789 " * 20 + "."
|
||||||
|
text = two_hundred + "<div class='excerpt'>#{two_hundred}</div>" + two_hundred
|
||||||
|
expect(PrettyText.excerpt(text, 100)).to eq(two_hundred)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "ignores max excerpt length if a span excerpt is specified" do
|
it "ignores max excerpt length if a span excerpt is specified" do
|
||||||
two_hundred = "123456789 " * 20 + "."
|
two_hundred = "123456789 " * 20 + "."
|
||||||
text = two_hundred + "<span class='excerpt'>#{two_hundred}</span>" + two_hundred
|
text = two_hundred + "<span class='excerpt'>#{two_hundred}</span>" + two_hundred
|
||||||
expect(PrettyText.excerpt(text, 100)).to eq(two_hundred)
|
expect(PrettyText.excerpt(text, 100)).to eq(two_hundred)
|
||||||
post = Fabricate(:post, raw: text)
|
|
||||||
expect(post.excerpt).to eq(two_hundred)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "unescapes html entities when we want text entities" do
|
it "unescapes html entities when we want text entities" do
|
||||||
|
|
Loading…
Reference in New Issue