[refactor] Parsoid: Rename and reorder some code for readability

Just some random variables I renamed. Mostly writing out what's
shortened. In some minor cases moving declarations a bit so they
are closer to the usage.

More stuff to follow.

Bug: T382232
Change-Id: I2b372f65e548074366652caf477b897fea2eeb0a
This commit is contained in:
WMDE-Fisch 2024-12-04 14:43:55 +01:00
parent c2dc9ca49d
commit b43b3546e8

View file

@ -120,7 +120,7 @@ class References extends ExtensionTagHandler {
} }
private function extractRefFromNode( private function extractRefFromNode(
ParsoidExtensionAPI $extApi, Element $node, ReferencesData $refsData ParsoidExtensionAPI $extApi, Element $node, ReferencesData $referencesData
): void { ): void {
$doc = $node->ownerDocument; $doc = $node->ownerDocument;
$errs = []; $errs = [];
@ -128,35 +128,35 @@ class References extends ExtensionTagHandler {
// This is data-parsoid from the dom fragment node that's gone through // This is data-parsoid from the dom fragment node that's gone through
// dsr computation and template wrapping. // dsr computation and template wrapping.
$nodeDp = DOMDataUtils::getDataParsoid( $node ); $nodeDp = DOMDataUtils::getDataParsoid( $node );
$isTplWrapper = DOMUtils::hasTypeOf( $node, 'mw:Transclusion' ); $isTemplateWrapper = DOMUtils::hasTypeOf( $node, 'mw:Transclusion' );
$contentId = $nodeDp->html; $contentId = $nodeDp->html;
$tplDmw = $isTplWrapper ? DOMDataUtils::getDataMw( $node ) : null; $templateDataMw = $isTemplateWrapper ? DOMDataUtils::getDataMw( $node ) : null;
// This is the <sup> that's the meat of the sealed fragment // This is the <sup> that's the meat of the sealed fragment
$c = $extApi->getContentDOM( $contentId )->firstChild; $refFragment = $extApi->getContentDOM( $contentId )->firstChild;
DOMUtils::assertElt( $c ); DOMUtils::assertElt( $refFragment );
$cDp = DOMDataUtils::getDataParsoid( $c ); $refFragmentDp = DOMDataUtils::getDataParsoid( $refFragment );
$refDmw = DOMDataUtils::getDataMw( $c ); $refDataMw = DOMDataUtils::getDataMw( $refFragment );
// Use the about attribute on the wrapper with priority, since it's // Use the about attribute on the wrapper with priority, since it's
// only added when the wrapper is a template sibling. // only added when the wrapper is a template sibling.
$about = DOMCompat::getAttribute( $node, 'about' ) ?? $about = DOMCompat::getAttribute( $node, 'about' ) ??
DOMCompat::getAttribute( $c, 'about' ); DOMCompat::getAttribute( $refFragment, 'about' );
'@phan-var string $about'; // assert that $about is non-null '@phan-var string $about'; // assert that $about is non-null
// FIXME(SSS): Need to clarify semantics here. // FIXME(SSS): Need to clarify semantics here.
// If both the containing <references> elt as well as the nested <ref> // If both the containing <references> elt as well as the nested <ref>
// elt has a group attribute, what takes precedence? // elt has a group attribute, what takes precedence?
$groupName = $refDmw->attrs->group ?? $refsData->referencesGroup; $groupName = $refDataMw->attrs->group ?? $referencesData->referencesGroup;
$group = $refsData->getRefGroup( $groupName ); $refGroup = $referencesData->getRefGroup( $groupName );
if ( if (
$refsData->inReferencesContent() && $referencesData->inReferencesContent() &&
$groupName !== $refsData->referencesGroup $groupName !== $referencesData->referencesGroup
) { ) {
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_references_group_mismatch', 'cite_error_references_group_mismatch',
[ $refDmw->attrs->group ] [ $refDataMw->attrs->group ]
); );
} }
@ -168,50 +168,45 @@ class References extends ExtensionTagHandler {
'dir' => true 'dir' => true
]; ];
if ( array_diff_key( (array)$refDmw->attrs, $validAttributes ) !== [] ) { if ( array_diff_key( (array)$refDataMw->attrs, $validAttributes ) !== [] ) {
$errs[] = new DataMwError( 'cite_error_ref_too_many_keys' ); $errs[] = new DataMwError( 'cite_error_ref_too_many_keys' );
} }
// NOTE: This will have been trimmed in Utils::getExtArgInfo()'s call // NOTE: This will have been trimmed in Utils::getExtArgInfo()'s call
// to TokenUtils::kvToHash() and ExtensionHandler::normalizeExtOptions() // to TokenUtils::kvToHash() and ExtensionHandler::normalizeExtOptions()
$refName = $refDmw->attrs->name ?? ''; $refName = $refDataMw->attrs->name ?? '';
$followName = $refDmw->attrs->follow ?? ''; $followName = $refDataMw->attrs->follow ?? '';
$refDir = strtolower( $refDmw->attrs->dir ?? '' ); $refDir = strtolower( $refDataMw->attrs->dir ?? '' );
$extendsRef = $refDmw->attrs->extends ?? null; $extendsRef = $refDataMw->attrs->extends ?? null;
// Add ref-index linkback $hasName = strlen( $refName ) > 0;
$linkBack = $doc->createElement( 'sup' );
$ref = null;
$hasRefName = strlen( $refName ) > 0;
$hasFollow = strlen( $followName ) > 0; $hasFollow = strlen( $followName ) > 0;
$validFollow = false;
if ( $hasFollow ) { if ( $hasFollow ) {
// Always wrap follows content so that there's no ambiguity // Always wrap follows content so that there's no ambiguity
// where to find it when roundtripping // where to find it when roundtripping
$span = $doc->createElement( 'span' ); $followSpan = $doc->createElement( 'span' );
DOMUtils::addTypeOf( $span, 'mw:Cite/Follow' ); DOMUtils::addTypeOf( $followSpan, 'mw:Cite/Follow' );
$span->setAttribute( 'about', $about ); $followSpan->setAttribute( 'about', $about );
$span->appendChild( $followSpan->appendChild(
$doc->createTextNode( ' ' ) $doc->createTextNode( ' ' )
); );
DOMUtils::migrateChildren( $c, $span ); DOMUtils::migrateChildren( $refFragment, $followSpan );
$c->appendChild( $span ); $refFragment->appendChild( $followSpan );
} }
$html = ''; $ref = null;
$contentDiffers = false; $refFragmentHtml = '';
$hasDifferingContent = false;
$hasValidFollow = false;
if ( $hasRefName ) { if ( $hasName ) {
if ( $hasFollow ) { if ( $hasFollow ) {
// Presumably, "name" has higher precedence // Presumably, "name" has higher precedence
$errs[] = new DataMwError( 'cite_error_ref_follow_conflicts' ); $errs[] = new DataMwError( 'cite_error_ref_follow_conflicts' );
} }
if ( isset( $group->indexByName[$refName] ) ) { if ( isset( $refGroup->indexByName[$refName] ) ) {
$ref = $group->indexByName[$refName]; $ref = $refGroup->indexByName[$refName];
// If there are multiple <ref>s with the same name, but different content, // If there are multiple <ref>s with the same name, but different content,
// the content of the first <ref> shows up in the <references> section. // the content of the first <ref> shows up in the <references> section.
// in order to ensure lossless RT-ing for later <refs>, we have to record // in order to ensure lossless RT-ing for later <refs>, we have to record
@ -222,14 +217,14 @@ class References extends ExtensionTagHandler {
$refContent = $extApi->getContentDOM( $ref->contentId )->firstChild; $refContent = $extApi->getContentDOM( $ref->contentId )->firstChild;
$ref->cachedHtml = $extApi->domToHtml( $refContent, true, false ); $ref->cachedHtml = $extApi->domToHtml( $refContent, true, false );
} }
$html = $extApi->domToHtml( $c, true, false ); $refFragmentHtml = $extApi->domToHtml( $refFragment, true, false );
$contentDiffers = ( $html !== $ref->cachedHtml ); $hasDifferingContent = ( $refFragmentHtml !== $ref->cachedHtml );
} }
} else { } else {
if ( $refsData->inReferencesContent() ) { if ( $referencesData->inReferencesContent() ) {
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_references_missing_key', 'cite_error_references_missing_key',
[ $refDmw->attrs->name ] [ $refDataMw->attrs->name ]
); );
} }
} }
@ -237,9 +232,9 @@ class References extends ExtensionTagHandler {
if ( $hasFollow ) { if ( $hasFollow ) {
// This is a follows ref, so check that a named ref has already // This is a follows ref, so check that a named ref has already
// been defined // been defined
if ( isset( $group->indexByName[$followName] ) ) { if ( isset( $refGroup->indexByName[$followName] ) ) {
$validFollow = true; $hasValidFollow = true;
$ref = $group->indexByName[$followName]; $ref = $refGroup->indexByName[$followName];
} else { } else {
// FIXME: This key isn't exactly appropriate since this // FIXME: This key isn't exactly appropriate since this
// is more general than just being in a <references> // is more general than just being in a <references>
@ -248,10 +243,10 @@ class References extends ExtensionTagHandler {
// equivalent key and just outputs something wacky. // equivalent key and just outputs something wacky.
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_references_missing_key', 'cite_error_references_missing_key',
[ $refDmw->attrs->follow ] [ $refDataMw->attrs->follow ]
); );
} }
} elseif ( $refsData->inReferencesContent() ) { } elseif ( $referencesData->inReferencesContent() ) {
$errs[] = new DataMwError( 'cite_error_references_no_key' ); $errs[] = new DataMwError( 'cite_error_references_no_key' );
} }
} }
@ -260,13 +255,13 @@ class References extends ExtensionTagHandler {
// //
// Do this before possibly adding the a ref below or // Do this before possibly adding the a ref below or
// migrating contents out of $c if we have a valid follow // migrating contents out of $c if we have a valid follow
if ( empty( $cDp->empty ) && self::hasRef( $c ) ) { if ( empty( $refFragmentDp->empty ) && self::hasRef( $refFragment ) ) {
if ( $contentDiffers ) { if ( $hasDifferingContent ) {
$refsData->pushEmbeddedContentFlag(); $referencesData->pushEmbeddedContentFlag();
} }
$this->processRefs( $extApi, $refsData, $c ); $this->processRefs( $extApi, $referencesData, $refFragment );
if ( $contentDiffers ) { if ( $hasDifferingContent ) {
$refsData->popEmbeddedContentFlag(); $referencesData->popEmbeddedContentFlag();
// If we have refs and the content differs, we need to // If we have refs and the content differs, we need to
// reserialize now that we processed the refs. Unfortunately, // reserialize now that we processed the refs. Unfortunately,
// the cachedHtml we compared against already had its refs // the cachedHtml we compared against already had its refs
@ -274,16 +269,19 @@ class References extends ExtensionTagHandler {
// always be considered a redefinition. The implementation for // always be considered a redefinition. The implementation for
// the legacy parser also considers this a redefinition so // the legacy parser also considers this a redefinition so
// there is likely little content out there like this :) // there is likely little content out there like this :)
$html = $extApi->domToHtml( $c, true, true ); $refFragmentHtml = $extApi->domToHtml( $refFragment, true, true );
} }
} }
if ( $validFollow ) { // Add ref-index linkback
$linkBackSup = $doc->createElement( 'sup' );
if ( $hasValidFollow ) {
// Migrate content from the follow to the ref // Migrate content from the follow to the ref
if ( $ref->contentId ) { if ( $ref->contentId ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
$refContent = $extApi->getContentDOM( $ref->contentId )->firstChild; $refContent = $extApi->getContentDOM( $ref->contentId )->firstChild;
DOMUtils::migrateChildren( $c, $refContent ); DOMUtils::migrateChildren( $refFragment, $refContent );
} else { } else {
// Otherwise, we have a follow that comes after a named // Otherwise, we have a follow that comes after a named
// ref without content so use the follow fragment as // ref without content so use the follow fragment as
@ -297,19 +295,19 @@ class References extends ExtensionTagHandler {
// Even worse would be if it tried to redefine itself! // Even worse would be if it tried to redefine itself!
if ( !$ref ) { if ( !$ref ) {
$ref = $refsData->add( $extApi, $groupName, $refName, $extendsRef, $refDir ); $ref = $referencesData->add( $extApi, $groupName, $refName, $extendsRef, $refDir );
} }
// Handle linkbacks // Handle linkbacks
if ( $refsData->inEmbeddedContent() ) { if ( $referencesData->inEmbeddedContent() ) {
$ref->embeddedNodes[] = $about; $ref->embeddedNodes[] = $about;
} else { } else {
$ref->nodes[] = $linkBack; $ref->nodes[] = $linkBackSup;
$ref->linkbacks[] = $ref->key . '-' . count( $ref->linkbacks ); $ref->linkbacks[] = $ref->key . '-' . count( $ref->linkbacks );
} }
} }
if ( isset( $refDmw->attrs->dir ) ) { if ( isset( $refDataMw->attrs->dir ) ) {
if ( $refDir !== 'rtl' && $refDir !== 'ltr' ) { if ( $refDir !== 'rtl' && $refDir !== 'ltr' ) {
$errs[] = new DataMwError( 'cite_error_ref_invalid_dir', [ $refDir ] ); $errs[] = new DataMwError( 'cite_error_ref_invalid_dir', [ $refDir ] );
} elseif ( $ref->dir !== '' && $ref->dir !== $refDir ) { } elseif ( $ref->dir !== '' && $ref->dir !== $refDir ) {
@ -326,71 +324,71 @@ class References extends ExtensionTagHandler {
// Check for missing content, added ?? '' to fix T259676 crasher // Check for missing content, added ?? '' to fix T259676 crasher
// FIXME: See T260082 for a more complete description of cause and deeper fix // FIXME: See T260082 for a more complete description of cause and deeper fix
$missingContent = ( !empty( $cDp->empty ) || trim( $refDmw->body->extsrc ?? '' ) === '' ); $hasMissingContent = ( !empty( $refFragmentDp->empty ) || trim( $refDataMw->body->extsrc ?? '' ) === '' );
if ( $missingContent ) { if ( $hasMissingContent ) {
// Check for missing name and content to generate error code // Check for missing name and content to generate error code
// //
// In references content, refs should be used for definition so missing content // In references content, refs should be used for definition so missing content
// is an error. It's possible that no name is present (!hasRefName), which also // is an error. It's possible that no name is present (!hasRefName), which also
// gets the error "cite_error_references_no_key" above, so protect against that. // gets the error "cite_error_references_no_key" above, so protect against that.
if ( $refsData->inReferencesContent() ) { if ( $referencesData->inReferencesContent() ) {
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_empty_references_define', 'cite_error_empty_references_define',
[ $refDmw->attrs->name ?? '', $refDmw->attrs->group ?? '' ] [ $refDataMw->attrs->name ?? '', $refDataMw->attrs->group ?? '' ]
); );
} elseif ( !$hasRefName ) { } elseif ( !$hasName ) {
if ( !empty( $cDp->selfClose ) ) { if ( !empty( $refFragmentDp->selfClose ) ) {
$errs[] = new DataMwError( 'cite_error_ref_no_key' ); $errs[] = new DataMwError( 'cite_error_ref_no_key' );
} else { } else {
$errs[] = new DataMwError( 'cite_error_ref_no_input' ); $errs[] = new DataMwError( 'cite_error_ref_no_input' );
} }
} }
if ( !empty( $cDp->selfClose ) ) { if ( !empty( $refFragmentDp->selfClose ) ) {
unset( $refDmw->body ); unset( $refDataMw->body );
} else { } else {
// Empty the <sup> since we've serialized its children and // Empty the <sup> since we've serialized its children and
// removing it below asserts everything has been migrated out // removing it below asserts everything has been migrated out
DOMCompat::replaceChildren( $c ); DOMCompat::replaceChildren( $refFragment );
$refDmw->body = (object)[ 'html' => $refDmw->body->extsrc ?? '' ]; $refDataMw->body = (object)[ 'html' => $refDataMw->body->extsrc ?? '' ];
} }
} else { } else {
if ( $ref->contentId && !$validFollow ) { if ( $ref->contentId && !$hasValidFollow ) {
// Empty the <sup> since we've serialized its children and // Empty the <sup> since we've serialized its children and
// removing it below asserts everything has been migrated out // removing it below asserts everything has been migrated out
DOMCompat::replaceChildren( $c ); DOMCompat::replaceChildren( $refFragment );
} }
if ( $contentDiffers ) { if ( $hasDifferingContent ) {
// TODO: Since this error is being placed on the ref, the // TODO: Since this error is being placed on the ref, the
// key should arguably be "cite_error_ref_duplicate_key" // key should arguably be "cite_error_ref_duplicate_key"
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_references_duplicate_key', 'cite_error_references_duplicate_key',
[ $refDmw->attrs->name ] [ $refDataMw->attrs->name ]
); );
$refDmw->body = (object)[ 'html' => $html ]; $refDataMw->body = (object)[ 'html' => $refFragmentHtml ];
} else { } else {
$refDmw->body = (object)[ 'id' => 'mw-reference-text-' . $ref->target ]; $refDataMw->body = (object)[ 'id' => 'mw-reference-text-' . $ref->target ];
} }
} }
$class = 'mw-ref reference'; $class = 'mw-ref reference';
if ( $validFollow ) { if ( $hasValidFollow ) {
$class .= ' mw-ref-follow'; $class .= ' mw-ref-follow';
} }
$lastLinkback = $ref->linkbacks[count( $ref->linkbacks ) - 1] ?? null; $lastlinkBack = $ref->linkbacks[count( $ref->linkbacks ) - 1] ?? null;
DOMUtils::addAttributes( $linkBack, [ DOMUtils::addAttributes( $linkBackSup, [
'about' => $about, 'about' => $about,
'class' => $class, 'class' => $class,
'id' => ( $refsData->inEmbeddedContent() || $validFollow ) ? 'id' => ( $referencesData->inEmbeddedContent() || $hasValidFollow ) ?
null : ( $ref->name ? $lastLinkback : $ref->id ), null : ( $ref->name ? $lastlinkBack : $ref->id ),
'rel' => 'dc:references', 'rel' => 'dc:references',
'typeof' => DOMCompat::getAttribute( $node, 'typeof' ), 'typeof' => DOMCompat::getAttribute( $node, 'typeof' ),
] ]
); );
DOMUtils::removeTypeOf( $linkBack, 'mw:DOMFragment/sealed/ref' ); DOMUtils::removeTypeOf( $linkBackSup, 'mw:DOMFragment/sealed/ref' );
DOMUtils::addTypeOf( $linkBack, 'mw:Extension/ref' ); DOMUtils::addTypeOf( $linkBackSup, 'mw:Extension/ref' );
$dataParsoid = new DataParsoid; $dataParsoid = new DataParsoid;
if ( isset( $nodeDp->src ) ) { if ( isset( $nodeDp->src ) ) {
@ -402,15 +400,17 @@ class References extends ExtensionTagHandler {
if ( isset( $nodeDp->pi ) ) { if ( isset( $nodeDp->pi ) ) {
$dataParsoid->pi = $nodeDp->pi; $dataParsoid->pi = $nodeDp->pi;
} }
DOMDataUtils::setDataParsoid( $linkBack, $dataParsoid ); DOMDataUtils::setDataParsoid( $linkBackSup, $dataParsoid );
$dmw = $isTplWrapper ? $tplDmw : $refDmw; DOMDataUtils::setDataMw(
DOMDataUtils::setDataMw( $linkBack, $dmw ); $linkBackSup,
$isTemplateWrapper ? $templateDataMw : $refDataMw
);
// FIXME(T214241): Should the errors be added to data-mw if // FIXME(T214241): Should the errors be added to data-mw if
// $isTplWrapper? Here and other calls to addErrorsToNode. // $isTplWrapper? Here and other calls to addErrorsToNode.
if ( $errs ) { if ( $errs ) {
self::addErrorsToNode( $linkBack, $errs ); self::addErrorsToNode( $linkBackSup, $errs );
} }
// refLink is the link to the citation // refLink is the link to the citation
@ -432,7 +432,7 @@ class References extends ExtensionTagHandler {
) ); ) );
$refLink->appendChild( $refLinkSpan ); $refLink->appendChild( $refLinkSpan );
$linkBack->appendChild( $refLink ); $linkBackSup->appendChild( $refLink );
// Checking if the <ref> is nested in a link // Checking if the <ref> is nested in a link
$aParent = DOMUtils::findAncestorOfName( $node, 'a' ); $aParent = DOMUtils::findAncestorOfName( $node, 'a' );
@ -447,31 +447,31 @@ class References extends ExtensionTagHandler {
) { ) {
$insertionPoint = $insertionPoint->nextSibling; $insertionPoint = $insertionPoint->nextSibling;
} }
$aParent->parentNode->insertBefore( $linkBack, $insertionPoint ); $aParent->parentNode->insertBefore( $linkBackSup, $insertionPoint );
// set misnested to true and DSR to zero-sized to avoid round-tripping issues // set misnested to true and DSR to zero-sized to avoid round-tripping issues
$dsrOffset = DOMDataUtils::getDataParsoid( $aParent )->dsr->end ?? null; $dsrOffset = DOMDataUtils::getDataParsoid( $aParent )->dsr->end ?? null;
// we created that node hierarchy above, so we know that it only contains these nodes, // we created that node hierarchy above, so we know that it only contains these nodes,
// hence there's no need for a visitor // hence there's no need for a visitor
self::setMisnested( $linkBack, $dsrOffset ); self::setMisnested( $linkBackSup, $dsrOffset );
self::setMisnested( $refLink, $dsrOffset ); self::setMisnested( $refLink, $dsrOffset );
self::setMisnested( $refLinkSpan, $dsrOffset ); self::setMisnested( $refLinkSpan, $dsrOffset );
$parentAbout = DOMCompat::getAttribute( $aParent, 'about' ); $parentAbout = DOMCompat::getAttribute( $aParent, 'about' );
if ( $parentAbout !== null ) { if ( $parentAbout !== null ) {
$linkBack->setAttribute( 'about', $parentAbout ); $linkBackSup->setAttribute( 'about', $parentAbout );
} }
$node->parentNode->removeChild( $node ); $node->parentNode->removeChild( $node );
} else { } else {
// if not, we insert it where we planned in the first place // if not, we insert it where we planned in the first place
$node->parentNode->replaceChild( $linkBack, $node ); $node->parentNode->replaceChild( $linkBackSup, $node );
} }
// Keep the first content to compare multiple <ref>s with the same name. // Keep the first content to compare multiple <ref>s with the same name.
if ( $ref->contentId === null && !$missingContent ) { if ( $ref->contentId === null && !$hasMissingContent ) {
$ref->contentId = $contentId; $ref->contentId = $contentId;
// Use the dir parameter only from the full definition of a named ref tag // Use the dir parameter only from the full definition of a named ref tag
$ref->dir = $refDir; $ref->dir = $refDir;
} else { } else {
DOMCompat::remove( $c ); DOMCompat::remove( $refFragment );
$extApi->clearContentDOM( $contentId ); $extApi->clearContentDOM( $contentId );
} }
} }
@ -500,10 +500,10 @@ class References extends ExtensionTagHandler {
ParsoidExtensionAPI $extApi, Element $refsNode, ParsoidExtensionAPI $extApi, Element $refsNode,
ReferencesData $refsData, bool $autoGenerated = false ReferencesData $refsData, bool $autoGenerated = false
): void { ): void {
$isTplWrapper = DOMUtils::hasTypeOf( $refsNode, 'mw:Transclusion' ); $isTemplateWrapper = DOMUtils::hasTypeOf( $refsNode, 'mw:Transclusion' );
$dp = DOMDataUtils::getDataParsoid( $refsNode ); $nodeDp = DOMDataUtils::getDataParsoid( $refsNode );
$group = $dp->group ?? ''; $groupName = $nodeDp->group ?? '';
$refGroup = $refsData->getRefGroup( $group ); $refGroup = $refsData->getRefGroup( $groupName );
// Iterate through the ref list to back-patch typeof and data-mw error // Iterate through the ref list to back-patch typeof and data-mw error
// information into ref for errors only known at time of references // information into ref for errors only known at time of references
@ -511,14 +511,14 @@ class References extends ExtensionTagHandler {
// whereas embedded refs will be gathered for batch processing, since // whereas embedded refs will be gathered for batch processing, since
// we need to parse embedded content to find them. // we need to parse embedded content to find them.
if ( $refGroup ) { if ( $refGroup ) {
$autoGeneratedWithGroup = ( $autoGenerated && $group !== '' ); $autoGeneratedWithGroup = ( $autoGenerated && $groupName !== '' );
foreach ( $refGroup->refs as $ref ) { foreach ( $refGroup->refs as $ref ) {
$errs = []; $errs = [];
// Mark all refs that are part of a group that is autogenerated // Mark all refs that are part of a group that is autogenerated
if ( $autoGeneratedWithGroup ) { if ( $autoGeneratedWithGroup ) {
$errs[] = new DataMwError( $errs[] = new DataMwError(
'cite_error_group_refs_without_references', 'cite_error_group_refs_without_references',
[ $group ] [ $groupName ]
); );
} }
// Mark all refs that are named without content // Mark all refs that are named without content
@ -554,7 +554,7 @@ class References extends ExtensionTagHandler {
) ) ) )
); );
if ( !$isTplWrapper ) { if ( !$isTemplateWrapper ) {
$dataMw = DOMDataUtils::getDataMw( $refsNode ); $dataMw = DOMDataUtils::getDataMw( $refsNode );
// Mark this auto-generated so that we can skip this during // Mark this auto-generated so that we can skip this during
// html -> wt and so that clients can strip it if necessary. // html -> wt and so that clients can strip it if necessary.
@ -562,12 +562,12 @@ class References extends ExtensionTagHandler {
$dataMw->autoGenerated = true; $dataMw->autoGenerated = true;
} elseif ( $nestedRefsHTML ) { } elseif ( $nestedRefsHTML ) {
$dataMw->body = (object)[ 'html' => "\n" . implode( $nestedRefsHTML ) ]; $dataMw->body = (object)[ 'html' => "\n" . implode( $nestedRefsHTML ) ];
} elseif ( empty( $dp->selfClose ) ) { } elseif ( empty( $nodeDp->selfClose ) ) {
$dataMw->body = (object)[ 'html' => '' ]; $dataMw->body = (object)[ 'html' => '' ];
} else { } else {
unset( $dataMw->body ); unset( $dataMw->body );
} }
unset( $dp->selfClose ); unset( $nodeDp->selfClose );
} }
// Deal with responsive wrapper // Deal with responsive wrapper
@ -597,7 +597,7 @@ class References extends ExtensionTagHandler {
} }
// Remove the group from refsData // Remove the group from refsData
$refsData->removeRefGroup( $group ); $refsData->removeRefGroup( $groupName );
} }
/** /**
@ -606,16 +606,16 @@ class References extends ExtensionTagHandler {
* the end of the DOM. * the end of the DOM.
* *
* @param ParsoidExtensionAPI $extApi * @param ParsoidExtensionAPI $extApi
* @param ReferencesData $refsData * @param ReferencesData $referencesData
* @param Node $node * @param Node $node
*/ */
public function insertMissingReferencesIntoDOM( public function insertMissingReferencesIntoDOM(
ParsoidExtensionAPI $extApi, ReferencesData $refsData, Node $node ParsoidExtensionAPI $extApi, ReferencesData $referencesData, Node $node
): void { ): void {
$doc = $node->ownerDocument; $doc = $node->ownerDocument;
foreach ( $refsData->getRefGroups() as $groupName => $refsGroup ) { foreach ( $referencesData->getRefGroups() as $groupName => $refsGroup ) {
$domFragment = $doc->createDocumentFragment(); $domFragment = $doc->createDocumentFragment();
$frag = $this->createReferences( $refFragment = $this->createReferences(
$extApi, $extApi,
$domFragment, $domFragment,
[ [
@ -639,9 +639,9 @@ class References extends ExtensionTagHandler {
// Add a \n before the <ol> so that when serialized to wikitext, // Add a \n before the <ol> so that when serialized to wikitext,
// each <references /> tag appears on its own line. // each <references /> tag appears on its own line.
$node->appendChild( $doc->createTextNode( "\n" ) ); $node->appendChild( $doc->createTextNode( "\n" ) );
$node->appendChild( $frag ); $node->appendChild( $refFragment );
$this->insertReferencesIntoDOM( $extApi, $frag, $refsData, true ); $this->insertReferencesIntoDOM( $extApi, $refFragment, $referencesData, true );
} }
} }