0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/serializers/post_item_excerpt_spec.rb
David Battersby 1150994e42
FIX: draft excerpts with deeply nested html (#35678)
Posts with deeply nested html structures (ie. greater than Nokogiri's
max tree depth) is causing a 500 error when viewing the drafts list
route.

When we can't process the excerpt for overly complex posts, we can just
return a blank value which will still allows us to view and edit the
draft as usual.

Internal ref: /t/-/166042
2025-11-03 13:37:15 +04:00

36 lines
1.2 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe PostItemExcerpt do
fab!(:regular_post) { Fabricate(:post, raw: "abc " * 100) }
fab!(:complex_post) { Fabricate(:post, raw: "<div>" * 10 + "Hello" + "</div>" * 10) }
let!(:max_excerpt_length) { 300 + "&hellip;".size }
class ExcerptSerializer < PostSerializer
include PostItemExcerpt
end
context "with regular post structure" do
it "includes excerpt and truncated attributes" do
item = ExcerptSerializer.new(regular_post, scope: Guardian.new, root: false)
expect(item.excerpt.size).to eq(max_excerpt_length)
expect(item.truncated).to eq(true)
end
end
context "with complex post structure" do
it "works when HTML depth is within limits" do
stub_const(Nokogiri::Gumbo, "DEFAULT_MAX_TREE_DEPTH", 20) do
item = ExcerptSerializer.new(complex_post, scope: Guardian.new, root: false)
expect(item.excerpt).to eq("Hello")
end
end
it "returns nil when HTML depth exceeds limits" do
stub_const(Nokogiri::Gumbo, "DEFAULT_MAX_TREE_DEPTH", 5) do
item = ExcerptSerializer.new(complex_post, scope: Guardian.new, root: false)
expect(item.excerpt).to eq("")
end
end
end
end