Merge "Add reference type detection to HTML scraping gateway"

This commit is contained in:
jenkins-bot 2019-02-26 12:23:47 +00:00 committed by Gerrit Code Review
commit f812611470
4 changed files with 24 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View file

@ -21,6 +21,22 @@ export default function createReferenceGateway() {
return $( `${ idSelector } .mw-reference-text, ${ idSelector } .reference-text` );
}
/**
* This duplicates the strict type detection from
* @see https://phabricator.wikimedia.org/diffusion/GMOA/browse/master/lib/transformations/references/structureReferenceListContent.js$93
*
* @param {JQuery} $referenceText
* @returns {string|null}
*/
function scrapeReferenceType( $referenceText ) {
const $cite = $referenceText.find( 'cite[class]' );
if ( $cite.length === 1 ) {
return $cite.attr( 'class' ).replace( /\bcitation\b/g, '' ).trim();
}
return null;
}
/**
* @param {mw.Title} title
* @param {Element} el
@ -43,6 +59,7 @@ export default function createReferenceGateway() {
url: `#${ id }`,
extract: $referenceText.html(),
type: previewTypes.TYPE_REFERENCE,
referenceType: scrapeReferenceType( $referenceText ),
sourceElementId: el && el.parentNode && el.parentNode.id
};

View file

@ -14,7 +14,9 @@ QUnit.module( 'ext.popups/gateway/reference', {
$( '<span>' ).addClass( 'mw-reference-text' ).text( 'Footnote 1' )
),
$( '<li>' ).attr( 'id', 'cite_note--2' ).append(
$( '<span>' ).addClass( 'reference-text' ).text( 'Footnote 2' )
$( '<span>' ).addClass( 'reference-text' ).append(
$( '<cite>' ).addClass( 'citation web unknown' ).text( 'Footnote 2' )
)
)
).appendTo( document.body );
},
@ -36,6 +38,7 @@ QUnit.test( 'Reference preview gateway returns the correct data', function ( ass
url: '#cite_note--1',
extract: 'Footnote 1',
type: 'reference',
referenceType: null,
sourceElementId: undefined
}
);
@ -51,8 +54,9 @@ QUnit.test( 'Reference preview gateway accepts alternative text node class name'
result,
{
url: '#cite_note--2',
extract: 'Footnote 2',
extract: '<cite class="citation web unknown">Footnote 2</cite>',
type: 'reference',
referenceType: 'web unknown',
sourceElementId: undefined
}
);
@ -70,6 +74,7 @@ QUnit.test( 'Reference preview gateway returns source element id', function ( as
url: '#cite_note--1',
extract: 'Footnote 1',
type: 'reference',
referenceType: null,
sourceElementId: 'cite_ref-1'
}
);