|null */ private ?array $tagsWithChangedMisnestingBehavior = null; /** * We are trying to find HTML5 tags that have different behavior compared to HTML4 * in some misnesting scenarios around wikitext paragraphs. * * Ex: Input:

a

b

* Tidy output:

a

b

* HTML5 output:

a

b

* * So, all good here. * But, see how output changes when we use instead * * Ex: Input:

a

b

* Tidy output:

a

b

* HTML5 output:

a

b

* * The source wikitext is "a\n\nb". The difference persists even * when you have "a\n\n
b
" or "a\n\n{|\n|x\n|}\nbar". * * This is because Tidy seems to be doing the equivalent of HTM5-treebuilder's * active formatting element reconstruction step on all *inline* elements. * However, HTML5 parsers only do that on formatting elements. So, we need * to compute which HTML5 tags are subject to this differential behavior. * * We compute that by excluding the following tags from the list of all HTML5 tags * - If our sanitizer doesn't allow them, they will be escaped => ignore them * - HTML4 block tags are excluded (obviously) * - Void tags don't matter since they cannot wrap anything (obviously) * - Active formatting elements have special handling in the HTML5 tree building * algorithm where they are reconstructed to wrap all originally intended content. * (ex: above) * * Here is the list of 22 HTML5 tags that are affected: * ABBR, BDI, BDO, CITE, DATA, DEL, DFN, INS, KBD, MARK, * Q, RB, RP, RT, RTC, RUBY, SAMP, SPAN, SUB, SUP, TIME, VAR * * https://phabricator.wikimedia.org/T176363#3628173 verifies that this list of * tags all demonstrate this behavior. * * @return array * @phan-return array */ private function getTagsWithChangedMisnestingBehavior(): array { if ( $this->tagsWithChangedMisnestingBehavior === null ) { // This set is frozen in time. It gets us down to the requisite // 22 HTML5 tags above, but shouldn't be used for anything other // than that. $HTML4TidyBlockTags = PHPUtils::makeSet( [ 'div', 'p', # tables 'table', 'tbody', 'thead', 'tfoot', 'caption', 'th', 'tr', 'td', # lists 'ul', 'ol', 'li', 'dl', 'dt', 'dd', # HTML5 heading content 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', # HTML5 sectioning content 'article', 'aside', 'nav', 'section', 'footer', 'header', 'figure', 'figcaption', 'fieldset', 'details', 'blockquote', # other 'hr', 'button', 'canvas', 'center', 'col', 'colgroup', 'embed', 'map', 'object', 'pre', 'progress', 'video', ] ); $this->tagsWithChangedMisnestingBehavior = []; foreach ( Consts::$HTML['HTML5Tags'] as $tag => $dummy ) { if ( isset( Consts::$Sanitizer['AllowedLiteralTags'][$tag] ) && !isset( $HTML4TidyBlockTags[$tag] ) && !isset( Consts::$HTML['FormattingTags'][$tag] ) && !isset( Consts::$HTML['VoidTags'][$tag] ) ) { $this->tagsWithChangedMisnestingBehavior[$tag] = true; } } } return $this->tagsWithChangedMisnestingBehavior; } /** * Finds a matching node at the "start" of this node. */ private function leftMostMisnestedDescendent( ?Node $node, Element $match ): ?Element { if ( !$node instanceof Element ) { return null; } if ( DOMUtils::isMarkerMeta( $node, 'mw:Placeholder/StrippedTag' ) ) { $name = DOMDataUtils::getDataParsoid( $node )->name ?? null; return $name === DOMCompat::nodeName( $match ) ? $node : null; } if ( DOMCompat::nodeName( $node ) === DOMCompat::nodeName( $match ) ) { $dp = DOMDataUtils::getDataParsoid( $node ); if ( ( DOMDataUtils::getDataParsoid( $match )->stx ?? null ) === ( $dp->stx ?? null ) && !empty( $dp->autoInsertedStart ) ) { if ( !empty( $dp->autoInsertedEnd ) ) { return $this->getMatchingMisnestedNode( $node, $match ); } else { return $node; } } } return $this->leftMostMisnestedDescendent( $node->firstChild, $match ); } /** * $node has an 'autoInsertedEnd' flag set on it. We are looking for * its matching node that has an 'autoInsertedStart' flag set on it. * This happens when the tree-builder fixes up misnested tags. * This "adjacency" is wrt the HTML string. In a DOM, this can either * be the next sibling OR, it might be the left-most-descendent of * of $node's parent's sibling (and so on up the ancestor chain). */ private function getMatchingMisnestedNode( Node $node, Element $match ): ?Element { if ( DOMUtils::atTheTop( $node ) ) { return null; } if ( DiffDOMUtils::nextNonSepSibling( $node ) ) { return $this->leftMostMisnestedDescendent( DiffDOMUtils::nextNonSepSibling( $node ), $match ); } return $this->getMatchingMisnestedNode( $node->parentNode, $match ); } /** * Given a tplInfo object, determine whether we are: * - Not processing template content (could be extension or top level page) * - Processing encapsulated content that is produced by a single template. * If so, return the name of that template. * - Processing encapsulated content that comes from multiple templates. * If so, return a flag indicating this. * * FIXME: We might potentially be computing this information redundantly * for every lint we find within this template's content. It could probably * be cached in tplInfo after it is computed once. */ public static function findEnclosingTemplateName( Env $env, ?stdClass $tplInfo ): ?array { if ( !$tplInfo ) { return null; } if ( !DOMUtils::hasTypeOf( $tplInfo->first, 'mw:Transclusion' ) ) { return null; } $dmw = DOMDataUtils::getDataMw( $tplInfo->first ); // This count check is conservative in that link suffixes and prefixes // could artifically add an extra element to the parts array but we // don't have a good way of distinguishing that right now. It will require // a non-string representation for them and a change in spec along with // a version bump and all that song and dance. If linting accuracy in these // scenarios become a problem, we can revisit this. if ( !empty( $dmw->parts ) && count( $dmw->parts ) === 1 ) { $p0 = $dmw->parts[0]; if ( !( $p0 instanceof TemplateInfo ) ) { throw new UnreachableException( "a single part will always be a TemplateInfo not a string" ); } $name = null; if ( !empty( $p0->href ) ) { // Could be "function" // PORT-FIXME: Should that be SiteConfig::relativeLinkPrefix() rather than './'? $name = PHPUtils::stripPrefix( $p0->href, './' ); } else { // type === 'templatearg' or 'template' $name = trim( $p0->targetWt ); } return [ 'name' => $name ]; } else { return [ 'multiPartTemplateBlock' => true ]; } } /** * Compute the DSR information for the lint object. * - In the common case, this is simply the DSR value of the node * that generated the lint. But, occasionally, for some lints, * we might have to post-process the node's DSR. * - If the lint is found in template content, then the DSR spans * the transclusion markup in the toplevel page source. */ public static function findLintDSR( ?array $tplLintInfo, ?stdClass $tplInfo, ?DomSourceRange $nodeDSR, ?callable $updateNodeDSR = null ): ?DomSourceRange { if ( $tplLintInfo !== null || ( $tplInfo && !Utils::isValidDSR( $nodeDSR ) ) ) { return DOMDataUtils::getDataParsoid( $tplInfo->first )->dsr ?? null; } else { return $updateNodeDSR ? $updateNodeDSR( $nodeDSR ) : $nodeDSR; } } /** * Determine if a node has an identical nested tag (?) */ private function hasIdenticalNestedTag( Element $node, string $name ): bool { $c = $node->firstChild; while ( $c ) { if ( $c instanceof Element ) { if ( DOMCompat::nodeName( $c ) === $name && empty( DOMDataUtils::getDataParsoid( $c )->autoInsertedEnd ) ) { return true; } return $this->hasIdenticalNestedTag( $c, $name ); } $c = $c->nextSibling; } return false; } /** * Determine if a node has misnestable content */ private function hasMisnestableContent( Node $node, string $name ): bool { // For A, TD, TH, H* tags, Tidy doesn't seem to propagate // the unclosed tag outside these tags. // No need to check for tr/table since content cannot show up there if ( DOMUtils::atTheTop( $node ) || preg_match( '/^(?:a|td|th|h\d)$/D', DOMCompat::nodeName( $node ) ) ) { return false; } $next = DiffDOMUtils::nextNonSepSibling( $node ); if ( !$next ) { return $this->hasMisnestableContent( $node->parentNode, $name ); } $contentNode = null; if ( DOMCompat::nodeName( $next ) === 'p' && !WTUtils::isLiteralHTMLNode( $next ) ) { $contentNode = DiffDOMUtils::firstNonSepChild( $next ); } else { $contentNode = $next; } // If the first "content" node we find is a matching // stripped tag, we have nothing that can get misnested return $contentNode && !( $contentNode instanceof Element && DOMUtils::isMarkerMeta( $contentNode, 'mw:Placeholder/StrippedTag' ) && isset( DOMDataUtils::getDataParsoid( $contentNode )->name ) && DOMDataUtils::getDataParsoid( $contentNode )->name === $name ); } /** * Indicate whether an end tag is optional for this node * * See https://www.w3.org/TR/html5/syntax.html#optional-tags * * End tags for tr/td/th/li are entirely optional since they * require a parent container and can only be followed by like * kind. * * Caveat:
  • foo
    1. ..
    and
  • foo
      ..
    * generate different DOM trees, so explicit
  • tag * is required to specify which of the two was intended. * * With that one caveat around nesting, the parse with/without * the end tag is identical. For now, ignoring that caveat * since they aren't like to show up in our corpus much. * * For the other tags in that w3c spec section, I haven't reasoned * through when exactly they are optional. Not handling that complexity * for now since those are likely uncommon use cases in our corpus. */ private function endTagOptional( Node $node ): bool { static $tagNames = [ 'tr', 'td', 'th', 'li' ]; return in_array( DOMCompat::nodeName( $node ), $tagNames, true ); } /** * Find the nearest ancestor heading tag */ private function getHeadingAncestor( Node $node ): ?Node { while ( $node && !DOMUtils::isHeading( $node ) ) { $node = $node->parentNode; } return $node; } /** * For formatting tags, Tidy seems to be doing this "smart" fixup of * unclosed tags by looking for matching unclosed pairs of identical tags * and if the content ends in non-whitespace text, it treats the second * unclosed opening tag as a closing tag. But, a HTML5 parser won't do this. * So, detect this pattern and flag for linter fixup. */ private function matchedOpenTagPairExists( Node $c, DataParsoid $dp ): bool { $lc = $c->lastChild; if ( !$lc instanceof Element || DOMCompat::nodeName( $lc ) !== DOMCompat::nodeName( $c ) ) { return false; } $lcDP = DOMDataUtils::getDataParsoid( $lc ); if ( empty( $lcDP->autoInsertedEnd ) || ( $lcDP->stx ?? null ) !== ( $dp->stx ?? null ) ) { return false; } $prev = $lc->previousSibling; // PORT-FIXME: Do we care about non-ASCII whitespace here? if ( $prev instanceof Text && !preg_match( '/\s$/D', $prev->nodeValue ) ) { return true; } return false; } /** * Lint Treebuilder fixups marked by ProcessTreeBuilderFixups * * It handles the following scenarios: * * 1. Unclosed end tags (`missing-end-tag`, `missing-end-tag-in-heading`) * 2. Invalid self-closed tags (`self-closed-tag`) * 3. Stripped tags (`stripped-tag`) * * In addition, we have specialized categories for some patterns * where we encounter unclosed end tags. * * 4. misnested-tag * 5. html5-misnesting * 6. multiple-unclosed-formatting-tags * 7. unclosed-quotes-in-heading */ private function lintTreeBuilderFixup( Env $env, Element $c, DataParsoid $dp, ?stdClass $tplInfo ): void { // This might have been processed as part of // misnested-tag category identification. if ( $dp->getTempFlag( TempData::LINTED ) ) { return; } $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); // During DSR computation, stripped meta tags // surrender their width to its previous sibling. // We record the original DSR in the tmp attribute // for that reason. $dsr = self::findLintDSR( $tplLintInfo, $tplInfo, $dp->tmp->origDSR ?? $dp->dsr ?? null ); $lintObj = null; if ( DOMUtils::isMarkerMeta( $c, 'mw:Placeholder/StrippedTag' ) ) { $lintObj = [ 'dsr' => $dsr, 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => $dp->name ?? null ], ]; $env->recordLint( 'stripped-tag', $lintObj ); } // Dont bother linting for auto-inserted start/end or self-closing-tag if: // 1. c is a void element // Void elements won't have auto-inserted start/end tags // and self-closing versions are valid for them. // // 2. c is tbody (FIXME: don't remember why we have this exception) // // 3. c is not an HTML element (unless they are i/b quotes or tables) // // 4. c doesn't have DSR info and doesn't come from a template either $cNodeName = DOMCompat::nodeName( $c ); $ancestor = null; $isHtmlElement = WTUtils::hasLiteralHTMLMarker( $dp ); if ( !Utils::isVoidElement( $cNodeName ) && $cNodeName !== 'tbody' && ( $isHtmlElement || DOMUtils::isQuoteElt( $c ) || $cNodeName === 'table' ) && ( $tplInfo !== null || $dsr !== null ) ) { if ( !empty( $dp->selfClose ) && $cNodeName !== 'meta' ) { $lintObj = [ 'dsr' => $dsr, 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => $cNodeName ], ]; $env->recordLint( 'self-closed-tag', $lintObj ); // The other checks won't pass - no need to test them. return; } if ( ( $dp->autoInsertedEnd ?? null ) === true && ( $tplInfo || ( $dsr->openWidth ?? 0 ) > 0 ) ) { $lintObj = [ 'dsr' => $dsr, 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => $cNodeName ], ]; // FIXME: This literal html marker check is strictly not required // (a) we've already checked that above and know that isQuoteElt is // not one of our tags. // (b) none of the tags in the list have native wikitext syntax => // they will show up as literal html tags. // But, in the interest of long-term maintenance in the face of // changes (to wikitext or html specs), let us make it explicit. if ( $isHtmlElement && isset( $this->getTagsWithChangedMisnestingBehavior()[DOMCompat::nodeName( $c )] ) && $this->hasMisnestableContent( $c, DOMCompat::nodeName( $c ) ) && // Tidy WTF moment here! // I don't know why Tidy does something very different // when there is an identical nested tag here. // //

    aX

    b

    // vs. //

    a

    b

    OR //

    aX

    b

    // // For the first snippet, Tidy only wraps "a" with the id='1' span // For the second and third snippets, Tidy wraps "b" with the id='1' span as well. // // For the corresponding wikitext that generates the above token stream, // Parsoid (and Remex) won't wrap 'b' with the id=1' span at all. !$this->hasIdenticalNestedTag( $c, DOMCompat::nodeName( $c ) ) ) { $env->recordLint( 'html5-misnesting', $lintObj ); } elseif ( !$isHtmlElement && DOMUtils::isQuoteElt( $c ) && // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found ( $ancestor = $this->getHeadingAncestor( $c->parentNode ) ) ) { $lintObj['params']['ancestorName'] = DOMCompat::nodeName( $ancestor ); $env->recordLint( 'unclosed-quotes-in-heading', $lintObj ); } else { $adjNode = $this->getMatchingMisnestedNode( $c, $c ); if ( $adjNode ) { $adjDp = DOMDataUtils::getDataParsoid( $adjNode ); $adjDp->setTempFlag( TempData::LINTED ); $env->recordLint( 'misnested-tag', $lintObj ); } elseif ( !$this->endTagOptional( $c ) && empty( $dp->autoInsertedStart ) ) { $lintObj['params']['inTable'] = DOMUtils::hasNameOrHasAncestorOfName( $c, 'table' ); $category = $this->getHeadingAncestor( $c ) ? 'missing-end-tag-in-heading' : 'missing-end-tag'; $next = DiffDOMUtils::nextNonSepSibling( $c ); if ( // Skip if covered by deletable-table-tag !( $cNodeName === 'table' && $next && ( DOMCompat::nodeName( $c ) === 'table' ) ) ) { $env->recordLint( $category, $lintObj ); } if ( isset( Consts::$HTML['FormattingTags'][DOMCompat::nodeName( $c )] ) && $this->matchedOpenTagPairExists( $c, $dp ) ) { $env->recordLint( 'multiple-unclosed-formatting-tags', $lintObj ); } } } } } } /** * Lint fostered content marked by MarkFosteredContent. * * Lint category: `fostered`, `fostered-transparent` * * This will log cases like: * * {| * foo * |- * | bar * |} * * Here 'foo' gets fostered out. */ private function lintFostered( Env $env, Element $node, DataParsoid $dp, ?stdClass $tplInfo ): void { if ( DOMCompat::nodeName( $node ) !== 'table' ) { return; } // The top-level nodes in the foster box are span/p wrapped // and so, if we have fostered content, previous siblings to // the table are expected to be elements. $maybeFostered = $node->previousSibling; // Emit "fostered" or "fostered-transparent" depending on if the fostered // content is entirely transparent or not. // // We're trying to find a balance between creating noise for wikignomes // and avoiding dirty-diffs from DiscussionTools. DiscussionTools // expects to know when pages have fostered content otherwise it can // lead to corruption on edit. However, rendering transparent nodes // often end up in fosterable positions, like category links from // templates or include directives on template pages. $fosteredRenderingTransparent = false; while ( $maybeFostered instanceof Element && !empty( DOMDataUtils::getDataParsoid( $maybeFostered )->fostered ) && ( WTUtils::isRenderingTransparentNode( $maybeFostered ) || // TODO: Section tags are rendering transparent but not sol transparent, // and that method only considers WTUtils::isSolTransparentLink, though // there is a FIXME to consider all link nodes. ( DOMCompat::nodeName( $maybeFostered ) === 'link' && DOMUtils::hasTypeOf( $maybeFostered, 'mw:Extension/section' ) ) ) ) { // Skip rendering-transparent nodes if they come from a template, // since they'll roundtrip cleanly regardless $fosteredRenderingTransparent = $fosteredRenderingTransparent || !$tplInfo; $maybeFostered = $maybeFostered->previousSibling; } if ( $maybeFostered instanceof Element && !empty( DOMDataUtils::getDataParsoid( $maybeFostered )->fostered ) ) { $type = 'fostered'; } elseif ( $fosteredRenderingTransparent ) { $type = 'fostered-transparent'; } else { return; } $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); $lintObj = [ 'dsr' => self::findLintDSR( $tplLintInfo, $tplInfo, $dp->dsr ?? null ), 'templateInfo' => $tplLintInfo, ]; $env->recordLint( $type, $lintObj ); } /** * Lint obsolete HTML tags. * * Lint category: `obsolete-tag`, `tidy-font-bug` */ private function lintObsoleteTag( Env $env, Element $c, DataParsoid $dp, ?stdClass $tplInfo ): void { if ( !$this->obsoleteTagsRE ) { $elts = []; foreach ( Consts::$HTML['OlderHTMLTags'] as $tag => $dummy ) { // Looks like all existing editors let editors add the tag. // VE has a button to add , it seems so does the WikiEditor // and JS wikitext editor. So, don't flag BIG as an obsolete tag. if ( $tag !== 'big' ) { $elts[] = preg_quote( $tag, '/' ); } } $this->obsoleteTagsRE = '/^(?:' . implode( '|', $elts ) . ')$/D'; } $tplLintInfo = null; if ( ( empty( $dp->autoInsertedStart ) || empty( $dp->autoInsertedEnd ) ) && preg_match( $this->obsoleteTagsRE, DOMCompat::nodeName( $c ) ) ) { $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); $lintObj = [ 'dsr' => self::findLintDSR( $tplLintInfo, $tplInfo, $dp->dsr ?? null ), 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => DOMCompat::nodeName( $c ) ], ]; $env->recordLint( 'obsolete-tag', $lintObj ); } if ( DOMCompat::nodeName( $c ) === 'font' && $c->hasAttribute( 'color' ) ) { /* ---------------------------------------------------------- * Tidy migrates into the link in these cases * [[Foo]] * [[Foo]]l (link-trail) * [[Foo]] * __NOTOC__[[Foo]] * [[Category:Foo]][[Foo]] * {{1x|[[Foo]]}} * * Tidy does not migrate into the link in these cases * [[Foo]] * [[Foo]] * [[Foo]]L (not a link-trail) * [[Foo]][[Bar]] * [[Foo]][[Bar]] * * is special. * This behavior is not seen with other formatting tags. * * Remex/parsoid won't do any of this. * This difference in behavior only matters when the font tag * specifies a link colour because the link no longer renders * as blue/red but in the font-specified colour. * ---------------------------------------------------------- */ $tidyFontBug = $c->firstChild !== null; $haveLink = false; for ( $n = $c->firstChild; $n; $n = $n->nextSibling ) { $nodeName = DOMCompat::nodeName( $n ); if ( $nodeName !== 'a' && !WTUtils::isRenderingTransparentNode( $n ) && !WTUtils::isTplMarkerMeta( $n ) ) { $tidyFontBug = false; break; } if ( $nodeName === 'a' || $nodeName === 'figure' ) { if ( !$haveLink ) { $haveLink = true; } else { $tidyFontBug = false; break; } } } if ( $tidyFontBug ) { $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); $env->recordLint( 'tidy-font-bug', [ 'dsr' => self::findLintDSR( $tplLintInfo, $tplInfo, $dp->dsr ?? null ), 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => 'font' ] ] ); } } } /** * Log bogus (=unrecognized) media options. * * See - https://www.mediawiki.org/wiki/Help:Images#Syntax * * Lint category: `bogus-image-options` */ private function lintBogusImageOptions( Env $env, Node $c, DataParsoid $dp, ?stdClass $tplInfo ): void { // Despite the lint category name, this checks all media, not just images if ( WTUtils::isGeneratedFigure( $c ) && !empty( $dp->optList ) ) { $items = []; $bogusPx = $dp->getTempFlag( TempData::BOGUS_PX ); foreach ( $dp->optList as $item ) { if ( $item['ck'] === 'bogus' || ( $bogusPx && $item['ck'] === 'width' ) ) { $items[] = $item['ak']; } } if ( $items ) { $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); $env->recordLint( 'bogus-image-options', [ 'dsr' => self::findLintDSR( $tplLintInfo, $tplInfo, $dp->dsr ?? null ), 'templateInfo' => $tplLintInfo, 'params' => [ 'items' => $items ] ] ); } } } /** * Lint tables Tidy deletes. * * Lint category: `deletable-table-tag` * * In this example below, the second table is in a fosterable position * (inside a ). The tree builder closes the first table at that point * and starts a new table there. We are detecting this pattern because * Tidy does something very different here. It strips the inner table * and retains the outer table. So, for preserving rendering of pages * that are tailored for Tidy, editors have to fix up this wikitext * to strip the inner table (to mimic what Tidy does). * * {| style='border:1px solid red;' * |a * |- * {| style='border:1px solid blue;' * |b * |c * |} * |} */ private function lintDeletableTableTag( Env $env, Node $c, DataParsoid $dp, ?stdClass $tplInfo ): void { if ( DOMCompat::nodeName( $c ) === 'table' ) { $prev = DiffDOMUtils::previousNonSepSibling( $c ); if ( $prev instanceof Element && DOMCompat::nodeName( $prev ) === 'table' && !empty( DOMDataUtils::getDataParsoid( $prev )->autoInsertedEnd ) ) { $tplLintInfo = self::findEnclosingTemplateName( $env, $tplInfo ); $dsr = self::findLintDSR( $tplLintInfo, $tplInfo, $dp->dsr ?? null, static function ( ?DomSourceRange $nodeDSR ): ?DomSourceRange { // Identify the dsr-span of the opening tag // of the table that needs to be deleted $x = $nodeDSR === null ? null : ( clone $nodeDSR ); if ( !empty( $x->openWidth ) ) { $x->end = $x->innerStart(); $x->openWidth = 0; $x->closeWidth = 0; } return $x; } ); $lintObj = [ 'dsr' => $dsr, 'templateInfo' => $tplLintInfo, 'params' => [ 'name' => 'table' ], ]; $env->recordLint( 'deletable-table-tag', $lintObj ); } } } /** * Find the first child passing the filter. */ private function findMatchingChild( Node $node, callable $filter ): ?Node { $c = $node->firstChild; while ( $c && !$filter( $c ) ) { $c = $c->nextSibling; } return $c; } /** * Test if the node has a 'nowrap' CSS rule * * In the general case, this CSS can come from a class, * or from a