Fix Cite extension <ref> no name and no content error handling

* The html generated matches the spec:
  https://www.mediawiki.org/wiki/Specs/HTML/2.1.0#Error_handling
  data-mw errors format.

* The html generated also matches the spec:
  https://phabricator.wikimedia.org/T251842

Bug: T51538
Change-Id: I7b3a3ddd72abfab22b4565f7282f7ba95b246301
This commit is contained in:
sbailey 2020-03-30 17:25:07 -07:00 committed by Arlo Breault
parent 46f749b92f
commit 0cac84e6b2
2 changed files with 22 additions and 5 deletions

View file

@ -62,7 +62,10 @@ class Ref extends ExtensionTagHandler {
if ( WTUtils::fromExtensionContent( $ref, 'references' ) ) { if ( WTUtils::fromExtensionContent( $ref, 'references' ) ) {
return $ref->nextSibling; return $ref->nextSibling;
} }
// Ignore content from reference errors
if ( DOMUtils::hasTypeOf( $ref, 'mw:Error' ) ) {
return $ref->nextSibling;
}
$refFirstChild = $ref->firstChild; $refFirstChild = $ref->firstChild;
DOMUtils::assertElt( $refFirstChild ); DOMUtils::assertElt( $refFirstChild );
$linkBackId = preg_replace( '/[^#]*#/', '', $refFirstChild->getAttribute( 'href' ), 1 ); $linkBackId = preg_replace( '/[^#]*#/', '', $refFirstChild->getAttribute( 'href' ), 1 );

View file

@ -144,6 +144,9 @@ class References extends ExtensionTagHandler {
// Add ref-index linkback // Add ref-index linkback
$linkBack = $doc->createElement( 'sup' ); $linkBack = $doc->createElement( 'sup' );
// Check for missing name and content and generate error code
$hasMissingNameAndContent = ( $refName === '' && !empty( $cDp->empty ) );
// FIXME: Lot of useless work for an edge case // FIXME: Lot of useless work for an edge case
if ( !empty( $cDp->empty ) ) { if ( !empty( $cDp->empty ) ) {
// Discard wrapper if there was no input wikitext // Discard wrapper if there was no input wikitext
@ -182,6 +185,10 @@ class References extends ExtensionTagHandler {
] ]
); );
DOMUtils::addTypeOf( $linkBack, 'mw:Extension/ref' ); DOMUtils::addTypeOf( $linkBack, 'mw:Extension/ref' );
if ( $hasMissingNameAndContent ) {
DOMUtils::addTypeOf( $linkBack, 'mw:Error' );
}
$dataParsoid = new stdClass; $dataParsoid = new stdClass;
if ( isset( $nodeDp->src ) ) { if ( isset( $nodeDp->src ) ) {
$dataParsoid->src = $nodeDp->src; $dataParsoid->src = $nodeDp->src;
@ -193,11 +200,18 @@ class References extends ExtensionTagHandler {
$dataParsoid->pi = $nodeDp->pi; $dataParsoid->pi = $nodeDp->pi;
} }
DOMDataUtils::setDataParsoid( $linkBack, $dataParsoid ); DOMDataUtils::setDataParsoid( $linkBack, $dataParsoid );
if ( $isTplWrapper ) {
DOMDataUtils::setDataMw( $linkBack, $tplDmw ); $dmw = $isTplWrapper ? $tplDmw : $refDmw;
} else { if ( $hasMissingNameAndContent ) {
DOMDataUtils::setDataMw( $linkBack, $refDmw ); $errs = [
[ 'key' => 'cite_error_ref_no_input' ],
];
if ( is_array( $dmw->errors ?? null ) ) {
$errs = array_merge( $dmw->errors, $errs );
}
$dmw->errors = $errs;
} }
DOMDataUtils::setDataMw( $linkBack, $dmw );
// refLink is the link to the citation // refLink is the link to the citation
$refLink = $doc->createElement( 'a' ); $refLink = $doc->createElement( 'a' );