true, "i" => true, "h1" => true, "h2" => true, "h3" => true, "h4" => true, "h5" => true, "h6" => true, "ul" => true, "ol" => true, "dl" => true, "li" => true, "dt" => true, "dd" => true, "table" => true, "caption" => true, "tr" => true, "td" => true, "th" => true, "hr" => true, // void element "br" => true, // void element "pre" => true, ]; /** * Do $parsoidData->tsr values span the entire DOM subtree rooted at $n? * * @param Element $n * @param DataParsoid $parsoidData * @return bool */ private function tsrSpansTagDOM( Element $n, DataParsoid $parsoidData ): bool { // - tags known to have tag-specific tsr // - html tags with 'stx' set // - tags with certain typeof properties (Parsoid-generated // constructs: placeholders, lang variants) $name = DOMCompat::nodeName( $n ); return !( isset( self::WT_TAGS_WITH_LIMITED_TSR[$name] ) || DOMUtils::matchTypeOf( $n, '/^mw:(Placeholder|LanguageVariant)$/D' ) || WTUtils::hasLiteralHTMLMarker( $parsoidData ) ); } /** * Is the inconsistency between two different ways of computing * start offset ($cs, $s) explainable and acceptable? * If so, we can suppress warnings. * * @param array $opts * @param Node $node * @param int $cs * @param int $s * @return bool */ private function acceptableInconsistency( array $opts, Node $node, int $cs, int $s ): bool { /** * 1. For wikitext URL links, suppress cs-s diff warnings because * the diffs can come about because of various reasons since the * canonicalized/decoded href will become the a-link text whose width * will not match the tsr width of source wikitext * * (a) urls with encoded chars (ex: 'http://example.com/?foo=bar') * (b) non-canonical spaces (ex: 'RFC 123' instead of 'RFC 123') * * 2. We currently don't have source offsets for attributes. * So, we get a lot of spurious complaints about cs/s mismatch * when DSR computation hit the
tag on this attribute. * $opts['attrExpansion'] tell us when we are processing an attribute * and let us suppress the mismatch warning on the tag. * * 3. Other scenarios .. to be added */ if ( $node instanceof Element && ( WTUtils::isATagFromURLLinkSyntax( $node ) || WTUtils::isATagFromMagicLinkSyntax( $node ) ) ) { return true; } elseif ( isset( $opts['attrExpansion'] ) && DOMUtils::atTheTop( $node ) ) { return true; } else { return false; } } /** * Compute wikitext string length that contributes to this * list item's open tag. Closing tag width is always 0 for lists. * * @param Element $li * @return int */ private function computeListEltWidth( Element $li ): int { if ( !$li->previousSibling && $li->firstChild ) { if ( DOMUtils::isList( $li->firstChild ) ) { // Special case!! // First child of a list that is on a chain // of nested lists doesn't get a width. return 0; } } // count nest listing depth and assign // that to the opening tag width. $depth = 0; // This is the crux of the algorithm in DOMHandler::getListBullets() while ( !DOMUtils::atTheTop( $li ) ) { $dp = DOMDataUtils::getDataParsoid( $li ); if ( DOMUtils::isListOrListItem( $li ) ) { if ( DOMUtils::isListItem( $li ) ) { $depth++; } } elseif ( !WTUtils::isLiteralHTMLNode( $li ) || empty( $dp->autoInsertedStart ) || empty( $dp->autoInsertedEnd ) ) { break; } $li = $li->parentNode; } return $depth; } /** * Compute wikitext string lengths that contribute to this * anchor's opening () and closing () tags. * * @param Element $node * @param ?DataParsoid $dp * @return int[]|null */ private function computeATagWidth( Element $node, ?DataParsoid $dp ): ?array { /* ------------------------------------------------------------- * Tag widths are computed as per this logic here: * * 1. [[Foo|bar]] <-- piped mw:WikiLink * -> start-tag: "[[Foo|" * -> content : "bar" * -> end-tag : "]]" * * 2. [[Foo]] <-- non-piped mw:WikiLink * -> start-tag: "[[" * -> content : "Foo" * -> end-tag : "]]" * * 3. [[{{1x|Foo}}|Foo]] <-- tpl-attr mw:WikiLink * Don't bother setting tag widths since dp->sa['href'] will be * the expanded target and won't correspond to original source. * * 4. [http://wp.org foo] <-- mw:ExtLink * -> start-tag: "[http://wp.org " * -> content : "foo" * -> end-tag : "]" * -------------------------------------------------------------- */ if ( !$dp ) { return null; } else { if ( WTUtils::isATagFromWikiLinkSyntax( $node ) && !WTUtils::hasExpandedAttrsType( $node ) ) { if ( isset( $dp->stx ) && $dp->stx === "piped" ) { // this seems like some kind of a phan bug $href = $dp->sa['href'] ?? null; if ( $href ) { return [ strlen( $href ) + 3, 2 ]; } else { return null; } } else { return [ 2, 2 ]; } } elseif ( isset( $dp->tsr ) && WTUtils::isATagFromExtLinkSyntax( $node ) ) { return [ $dp->tmp->extLinkContentOffsets->start - $dp->tsr->start, 1 ]; } elseif ( WTUtils::isATagFromURLLinkSyntax( $node ) || WTUtils::isATagFromMagicLinkSyntax( $node ) ) { return [ 0, 0 ]; } else { return null; } } } /** * Compute wikitext string lengths that contribute to this * node's opening and closing tags. * * @param int|null $stWidth Start tag width * @param int|null $etWidth End tag width * @param Element $node * @param DataParsoid $dp * @return int[] Start and end tag widths */ private function computeTagWidths( $stWidth, $etWidth, Element $node, DataParsoid $dp ): array { if ( isset( $dp->extTagOffsets ) ) { return [ $dp->extTagOffsets->openWidth, $dp->extTagOffsets->closeWidth ]; } if ( WTUtils::hasLiteralHTMLMarker( $dp ) ) { if ( !empty( $dp->selfClose ) ) { $etWidth = 0; } } elseif ( DOMUtils::hasTypeOf( $node, 'mw:LanguageVariant' ) ) { $stWidth = 2; // -{ $etWidth = 2; // }- } else { $nodeName = DOMCompat::nodeName( $node ); // 'tr' tags not in the original source have zero width if ( $nodeName === 'tr' && !isset( $dp->startTagSrc ) ) { $stWidth = 0; $etWidth = 0; } else { $wtTagWidth = Consts::$WtTagWidths[$nodeName] ?? null; if ( $stWidth === null ) { // we didn't have a tsr to tell us how wide this tag was. if ( $nodeName === 'a' ) { $wtTagWidth = $this->computeATagWidth( $node, $dp ); $stWidth = $wtTagWidth ? $wtTagWidth[0] : null; } elseif ( $nodeName === 'li' || $nodeName === 'dd' ) { $stWidth = $this->computeListEltWidth( $node ); } elseif ( $wtTagWidth ) { $stWidth = $wtTagWidth[0]; } } if ( $etWidth === null && $wtTagWidth ) { $etWidth = $wtTagWidth[1]; } } } return [ $stWidth, $etWidth ]; } /** * @param Env $env * @param mixed ...$args */ private function trace( Env $env, ...$args ): void { $env->log( "trace/dsr", static function () use ( $args ) { $buf = ''; foreach ( $args as $arg ) { $buf .= is_string( $arg ) ? $arg : PHPUtils::jsonEncode( $arg ); } return $buf; } ); } /** * TSR = "Tag Source Range". Start and end offsets giving the location * where the tag showed up in the original source. * * DSR = "DOM Source Range". dsr->start and dsr->end are open and end, * dsr->openWidth and dsr->closeWidth are widths of the container tag. * * TSR is set by the tokenizer. In most cases, it only applies to the * specific tag (opening or closing). However, for self-closing * tags that the tokenizer generates, the TSR values applies to the entire * DOM subtree (opening tag + content + closing tag). * * Ex: So [[foo]] will get tokenized to a SelfClosingTagTk(...) with a TSR * value of [0,7]. The DSR algorithm will then use that info and assign * the a-tag rooted at the foo DOM subtree a DSR value of * [0,7,2,2], where 2 and 2 refer to the opening and closing tag widths. * * [s,e) -- if defined, start/end position of wikitext source that generated * node's subtree * * @param Frame $frame * @param Node $node node to process * @param ?int $s start position, inclusive * @param ?int $e end position, exclusive * @param int $dsrCorrection * @param array $opts * @return array */ private function computeNodeDSR( Frame $frame, Node $node, ?int $s, ?int $e, int $dsrCorrection, array $opts ): array { $env = $frame->getEnv(); if ( $e === null && !$node->hasChildNodes() ) { $e = $s; } $this->trace( $env, "BEG: ", DOMCompat::nodeName( $node ), " with [s, e]=", [ $s, $e ] ); /** @var int|null $ce Child end */ $ce = $e; // Initialize $cs to $ce to handle the zero-children case properly // if this $node has no child content, then the start and end for // the child dom are indeed identical. Alternatively, we could // explicitly code this check before everything and bypass this. /** @var int|null $cs Child start */ $cs = $ce; $child = $node->lastChild; while ( $child !== null ) { $prevChild = $child->previousSibling; $origCE = $ce; $cType = $child->nodeType; $fosteredNode = false; $cs = null; if ( $child instanceof Element ) { $dp = DOMDataUtils::getDataParsoid( $child ); $endTSR = $dp->tmp->endTSR ?? null; if ( $endTSR ) { $ce = $endTSR->end; } } else { $endTSR = null; } // StrippedTag marker tags will be removed and won't // be around to fill in the missing gap. So, absorb its width into // the DSR of its previous sibling. Currently, this fix is only for // B and I tags where the fix is clear-cut and obvious. $next = $child->nextSibling; if ( $next instanceof Element ) { $ndp = DOMDataUtils::getDataParsoid( $next ); if ( isset( $ndp->src ) && DOMUtils::hasTypeOf( $next, 'mw:Placeholder/StrippedTag' ) && // NOTE: This inlist check matches the case in CleanUp where // the placeholders are not removed from the DOM. We don't want // to move the width into the sibling here and then leave around a // a zero width placeholder because serializeDOMNode only handles // a few cases of zero width nodes, so we'll end up duplicating // it from ->src. !DOMUtils::isNestedInListItem( $next ) ) { if ( isset( Consts::$WTQuoteTags[$ndp->name] ) && isset( Consts::$WTQuoteTags[DOMCompat::nodeName( $child )] ) ) { $correction = strlen( $ndp->src ); $ce += $correction; $dsrCorrection = $correction; if ( Utils::isValidDSR( $ndp->dsr ?? null ) ) { // Record original DSR for the meta tag // since it will now get corrected to zero width // since child acquires its width-> $ndp->getTemp()->origDSR = new DomSourceRange( $ndp->dsr->start, $ndp->dsr->end, null, null ); } } } } $env->log( "trace/dsr", static function () use ( $child, $cs, $ce ) { // slow, for debugging only $i = 0; foreach ( $child->parentNode->childNodes as $x ) { if ( $x === $child ) { break; } $i++; } return " CHILD: <" . DOMCompat::nodeName( $child->parentNode ) . ":" . $i . ">=" . ( $child instanceof Element ? '' : ( $child instanceof Text ? '#' : '!' ) ) . ( ( $child instanceof Element ) ? ( DOMCompat::nodeName( $child ) === 'meta' ? DOMCompat::getOuterHTML( $child ) : DOMCompat::nodeName( $child ) ) : PHPUtils::jsonEncode( $child->nodeValue ) ) . " with " . PHPUtils::jsonEncode( [ $cs, $ce ] ); } ); if ( $cType === XML_TEXT_NODE ) { if ( $ce !== null ) { $cs = $ce - strlen( $child->textContent ); } } elseif ( $cType === XML_COMMENT_NODE ) { '@phan-var Comment $child'; // @var Comment $child if ( $ce !== null ) { // Decode HTML entities & re-encode as wikitext to find length $cs = $ce - WTUtils::decodedCommentLength( $child ); } } elseif ( $cType === XML_ELEMENT_NODE ) { DOMUtils::assertElt( $child ); $dp = DOMDataUtils::getDataParsoid( $child ); $tsr = $dp->tsr ?? null; $oldCE = $tsr ? $tsr->end : null; $propagateRight = false; $stWidth = null; $etWidth = null; $fosteredNode = $dp->fostered ?? false; // We are making dsr corrections to account for // stripped tags (end tags usually). When stripping happens, // in most common use cases, a corresponding end tag is added // back elsewhere in the DOM. // // So, when an autoInsertedEnd tag is encountered and a matching // dsr-correction is found, make a 1-time correction in the // other direction. // // Currently, this fix is only for // B and I tags where the fix is clear-cut and obvious. if ( $ce !== null && !empty( $dp->autoInsertedEnd ) && DOMUtils::isQuoteElt( $child ) ) { $correction = 3 + strlen( DOMCompat::nodeName( $child ) ); if ( $correction === $dsrCorrection ) { $ce -= $correction; $dsrCorrection = 0; } } if ( DOMCompat::nodeName( $child ) === "meta" ) { if ( $tsr ) { if ( WTUtils::isTplMarkerMeta( $child ) ) { // If this is a meta-marker tag (for templates, extensions), // we have a new valid '$cs'. This marker also effectively resets tsr // back to the top-level wikitext source range from nested template // source range. $cs = $tsr->start; $ce = $tsr->end; $propagateRight = true; } else { // All other meta-tags: