Reduce down singly nested containers when returning final content from the readability algorithm

Open
#343 1 comment 0 reactions 1 assignee View on GitHub

@evanxd is already working on this.

Since Jan 23, 2017.

Assessment

This issue has not been assessed yet.

Description

maintenance reader-mode-has-issues

In #337 we started replacing candidate container elements with their parents if they had no siblings. This helped in cases where major blocks of text were deeply nested and we were therefore unable to detect their siblings.

However, there are 2 downsides:

  1. we end up adding needless <div> and similar nesting to elements that, given the lack of styling for them, adds no value to the readability result.
  2. we no longer correctly detect dir attributes on the container element, because the container element has changed.

We should fix this by having an algorithm (after all the cleanup etc., once we're sure we're returning a particular candidate element) that does 3 things:

  1. while the candidate has exactly 1 element child node and no non-whitespace text children, make its child the top candidate
  2. in that case, if the parent has a dir attribute and the child does not, set the dir on the child. (If the child has a dir attribute, that direction should "win", ie persist.)
  3. if the final candidate we land on this way is not <p> or <div> or <article> or <section>, create a <div> and put all the child nodes (not just elements!) in that <div> and return that <div>. This will fix cases where we'd otherwise return <table> or <td> elements.

@evanxd, can you take this on?

Dominant language
JavaScript
Stars
11.5k
Forks
732
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mozilla/readability

All issues in mozilla/readability

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.