All files / mobile.startup/references ReferencesHtmlScraperGateway.js

94.73% Statements 18/19
87.5% Branches 7/8
100% Functions 4/4
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 801x 1x 1x                   3x     1x                               3x   3x 3x     2x 2x           2x 2x         1x   3x             4x                         3x   3x       1x  
var ReferencesGateway = require( './ReferencesGateway' ),
	mfExtend = require( './../mfExtend' ),
	util = require( './../util' );
 
/**
 * Gateway for retrieving references via the content of the Page
 *
 * @class ReferencesHtmlScraperGateway
 * @extends ReferencesGateway
 * @inheritdoc
 */
function ReferencesHtmlScraperGateway() {
	ReferencesGateway.apply( this, arguments );
}
 
mfExtend( ReferencesHtmlScraperGateway, ReferencesGateway, {
	/**
	 * @memberof ReferencesHtmlScraperGateway
	 * @property EXTERNAL_LINK_CLASS a CSS class to place on external links
	 * in addition to the default 'external' class.
	 */
	EXTERNAL_LINK_CLASS: 'external--reference',
	/**
	 * @memberof ReferencesHtmlScraperGateway
	 * @instance
	 * @param {string} id ID of a DOM element in the page.
	 * @param {jQuery.Object} $container to scan for an element
	 * @return {jQuery.Promise} that can be used by getReference
	 */
	getReferenceFromContainer( id, $container ) {
		var $el, $ol, $parent,
			result = util.Deferred();
 
		$el = $container.find( '#' + util.escapeSelector( id ) );
		if ( $el.length ) {
			// This finds either the inner <ol class="mw-extended-references">, or the outer
			// <ol class="references">
			$ol = $el.closest( 'ol' );
			Iif ( $ol.hasClass( 'mw-extended-references' ) ) {
				$parent = $ol.parent();
			}
			// The following classes are used here:
			// * external--reference
			// * other values of EXTERNAL_LINK_CLASS in sub-classes
			( $parent || $el ).find( '.external' ).addClass( this.EXTERNAL_LINK_CLASS );
			result.resolve( {
				text: this.getReferenceHtml( $el ),
				parentText: this.getReferenceHtml( $parent )
			} );
		} else {
			result.reject( ReferencesGateway.ERROR_NOT_EXIST );
		}
		return result.promise();
	},
	/**
	 * @param {jQuery.Object|undefined} $reference
	 * @returns {string}
	 */
	getReferenceHtml( $reference ) {
		return $reference ?
			$reference.find( '.mw-reference-text, .reference-text' ).first().html() :
			'';
	},
	/**
	 * @inheritdoc
	 * @memberof ReferencesHtmlScraperGateway
	 * @instance
	 * @param {string} hash Hash fragment with leading '#'
	 * @param {Page} page (unused)
	 * @param {PageHTMLParser} pageHTMLParser
	 */
	getReference( hash, page, pageHTMLParser ) {
		var id = mw.util.percentDecodeFragment( hash.slice( 1 ) );
		// If an id is not found it's possible the id passed needs decoding (per T188547).
		return this.getReferenceFromContainer( id, pageHTMLParser.$el.find( 'ol.references' ) );
	}
} );
 
module.exports = ReferencesHtmlScraperGateway;