Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#61255 closed feature request (fixed)

HTML API: Report depth of currently-matched element.

Reported by: dmsnell Owned by: dmsnell
Priority: normal Milestone: 6.6
Component: HTML API Version: 6.6
Severity: normal Keywords: has-patch has-unit-tests needs-dev-note
Cc: Focuses:

Description (last modified by dmsnell)

The HTML Processor maintains a stack of open elements, where every element, every #text node, every HTML comment, and other node is pushed and popped while traversing the document. The "depth" of each of these nodes represents how deep that stack is where the node appears. Unfortunately this information isn't exposed to calling code, which has led different projects to attempt to calculate this value externally. This isn't always trivial, but the HTML Processor could make it so by exposing the internal knowledge in a new method.

In the attached patch, the get_current_depth() method returns just that. Since the processor always exists within a context, the depth includes nesting from the always-present html element and also the body, since currently the HTML Processor only supports parsing in the IN BODY context.

This means that the depth reported for the DIV in <div> is 3, not 1, because its breadcrumbs path is HTML > BODY > DIV.

Change History (6)

This ticket was mentioned in ​PR #6589 on ​WordPress/wordpress-develop by ​@dmsnell.


2 years ago
#1

  • Keywords has-patch has-unit-tests added

Trac ticket: Core-61255.

#2 @dmsnell
2 years ago

  • Description modified (diff)

#3 @dmsnell
2 years ago

  • Owner set to dmsnell
  • Resolution → fixed
  • Status new → closed

In 58191:

HTML API: Add method to report depth of currently-matched node.

The HTML Processor maintains a stack of open elements, where every element,
every #text node, every HTML comment, and other node is pushed and popped while
traversing the document. The "depth" of each of these nodes represents how deep
that stack is where the node appears. Unfortunately this information isn't
exposed to calling code, which has led different projects to attempt to
calculate this value externally. This isn't always trivial, but the HTML
Processor could make it so by exposing the internal knowledge in a new method.

In this patch the get_current_depth() method returns just that. Since the
processor always exists within a context, the depth includes nesting from the
always-present html element and also the body, since currently the HTML
Processor only supports parsing in the IN BODY context.

This means that the depth reported for the DIV in <div> is 3, not 1, because
its breadcrumbs path is HTML > BODY > DIV.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/6589
Discussed in https://core.trac.wordpress.org/ticket/61255

Fixes #61255.
Props dmsnell, jonsurrell.

#5 @dmsnell
2 years ago

  • Keywords needs-dev-note added
Note: See TracTickets for help on using tickets.