Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested divs in first_item_content #3348

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CodeSonia
Copy link
Contributor

@CodeSonia CodeSonia commented Sep 27, 2024

What

Trello card

This PR addresses an issue where the content list was not displayed on some publication pages due to the way nested content elements were handled in the show_contents_list? method.

Context:

The show_contents_list? method includes several conditions to determine whether a content list should display. These rules were introduced in PR #719.

Recently, a Zendesk ticket highlighted an issue on the DVSA Earned Recognition page, where the content list was missing, leaving an empty block of space on the left of the publication.

Debugging:

- first_item_content.length = 0
- first_item_has_long_content? = false
- first_item_content = ""

However, this is not accurate, because the content includes several h2s and h3s:

  • An h2 section for "Driver system providers"
  • Multiple h3 subsections like "A", "B", and "C", each containing p elements with contact details, all wrapped inside div tags.

The content in these sections was not being picked up by the method due to the nesting inside div elements. As a result, the condition in show_contents_list? were returning false, leading to the missing content list.

Solution:

I updated the first_item_content method in contents_list.rb to handle elements nested within div tags. This ensures that valid elements (p, ul, ol) inside divs are now considered when determining whether to display the content list.

Tests:

I also added two test cases to ensure this functionality works as expected:

  1. Test case for long content in nested elements: It shows the content list when the first item contains long content inside nested div elements.
  2. Test case for short content in nested elements: Ensures that the content list is not shown when the first item contains short content, even if it is nested inside divs.

Testing:

The page now correctly renders the content list:

  • first_item_content_length: 2817
  • first_item_has_long_content? true
  • first_item_content now captures the actual content.

Why

Without this fix, pages with only two content items and no long content could fail to render the content list, leaving an empty whitespace that negatively impacts user experience.

Visual Changes

Before After
image image

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

Follow these steps if you are doing a Rails upgrade.

@govuk-ci govuk-ci temporarily deployed to government-frontend-pr-3348 September 27, 2024 14:10 Inactive
@CodeSonia CodeSonia marked this pull request as ready for review September 27, 2024 15:05
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.

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

@unoduetre unoduetre Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

div is checked separately in line 53, so adding it here doesn't change anything and is confusing, so it can be removed from this line. So line 55 can be also changed to not exclude it.


until element.nil? || element.name == "h2"
if element.name == "div"
element.children.each do |child|
Copy link
Contributor

@unoduetre unoduetre Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first part of if only checks a single level of divs, so it won't find paragraphs inside nested divs e.g.:

<div>
  <div>
    <div>
      <p>
       text...
      </p>
    </div>
  </div>
</div>

It might solve the immediate problem with the particular document, but the problem will reappear when someone adds a document with more deeply nested divs.

If we want to find all nested paragraphs, a recursive solution would work, but is it what we want to do in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense - I'll change this so I look for more deeply nested divs as like you said I only considered the first level 😅

@CodeSonia CodeSonia marked this pull request as draft September 27, 2024 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants