Skip to content

Commit

Permalink
Handle nested divs in first_item_content
Browse files Browse the repository at this point in the history
Amended the first_item_content method so that it accounts for nested div elements and processes their child elements, so that there is sufficient content to consider rendering a list.
  • Loading branch information
CodeSonia committed Sep 27, 2024
1 parent 8113059 commit 07c7fd6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/presenters/content_item/contents_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ def first_item_has_long_content?
def first_item_content
element = first_item
first_item_text = ""
allowed_elements = %w[p ul ol]

until element.name == "h2"
first_item_text += element.text if element.name.in?(allowed_elements)
allowed_elements = %w[p ul ol div]

until element.nil? || element.name == "h2"
if element.name == "div"
element.children.each do |child|
first_item_text += child.text if child.name.in?(allowed_elements - %w[div])
end
elsif element.name.in?(allowed_elements)
first_item_text += element.text
end
element = element.next_element
break if element.nil?
end
first_item_text
first_item_text.strip
end

def first_item_character_count
Expand Down
36 changes: 36 additions & 0 deletions test/presenters/content_item/contents_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,42 @@ def body
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if the first item contains long content from nested elements" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<div>
<p>#{Faker::Lorem.characters(number: 200)}</p>
<ul>
<li>#{Faker::Lorem.characters(number: 150)}</li>
<li>#{Faker::Lorem.characters(number: 150)}</li>
</ul>
</div>
<h2 id='two'>Two</h2>
"
end
end
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns false if the first item does not contain long content from nested elements" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<div>
<p>#{Faker::Lorem.characters(number: 50)}</p>
<ul>
<li>#{Faker::Lorem.characters(number: 15)}</li>
<li>#{Faker::Lorem.characters(number: 15)}</li>
</ul>
</div>
<h2 id='two'>Two</h2>
"
end
end
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if number of table rows in the first item is more than 13" do
class << @contents_list
def body
Expand Down

0 comments on commit 07c7fd6

Please sign in to comment.