Debian Codegraph @

extensions/Cite/modules/ext.cite.referencePreviews/createReferenceGateway.js

raw symbols SCIP
1 /**
2 * @module gateway/reference
3 */
4
5 const TYPE_REFERENCE = 'reference';
6
7 /**
8 * @return {Gateway}
9 */
10 module.exports = function createReferenceGateway() {
11
12 /**
13 * @param {string} id
14 * @return {HTMLElement|null}
15 */
16 function findReferenceTextElement( id ) {
17 const idSelector = `#${ CSS.escape( id ) }`;
18
19 /**
20 * Same alternative selectors with and without mw-… as in the RESTbased endpoint.
21 *
22 * @see https://phabricator.wikimedia.org/diffusion/GMOA/browse/master/lib/transformations/references/structureReferenceListContent.js$138
23 */
24 return document.querySelector( `${ idSelector } .mw-reference-text, ${ idSelector } .reference-text` );
25 }
26
27 /**
28 * @param {HTMLElement} el
29 * @return {HTMLElement|null}
30 */
31 function findParentReferenceTextElement( el ) {
32 // This finds either the inner <ol class="mw-extended-references">, or the outer
33 // <ol class="references">
34 const ol = el.closest( 'ol' );
35
36 return ol && ol.classList.contains( 'mw-extended-references' ) ?
37 ol.parentElement.querySelector( '.mw-reference-text, .reference-text' ) :
38 null;
39 }
40
41 /**
42 * @param {HTMLElement} referenceElement
43 * @param {(HTMLElement|null)} parentElement
44 * @return {string}
45 */
46 function scrapeReferenceText( referenceElement, parentElement ) {
47 if ( !parentElement ) {
48 return referenceElement.innerHTML;
49 }
50
51 return `
52 <div class="mw-reference-previews-parent">${ parentElement.innerHTML }</div>
53 <div>${ referenceElement.innerHTML }</div>
54 `;
55 }
56
57 /**
58 * Attempts to find a single reference type identifier, limited to a list of known types.
59 * - When a `class="…"` attribute mentions multiple known types, the last one is used, following
60 * CSS semantics.
61 * - When there are multiple <cite> tags, the first with a known type is used.
62 *
63 * @param {HTMLElement} referenceElement
64 * @return {string|null}
65 */
66 function scrapeReferenceType( referenceElement ) {
67 const KNOWN_TYPES = [ 'book', 'journal', 'news', 'note', 'web' ];
68 let type = null;
69 const citeTags = referenceElement.querySelectorAll( 'cite[class]' );
70 Array.prototype.forEach.call( citeTags, ( element ) => {
71 // don't need to keep scanning if one is found.
72 if ( type ) {
73 return;
74 }
75 const classNames = element.className.split( /\s+/ );
76 for ( let i = classNames.length; i--; ) {
77 if ( KNOWN_TYPES.indexOf( classNames[ i ] ) !== -1 ) {
78 type = classNames[ i ];
79 return false;
80 }
81 }
82 } );
83 return type;
84 }
85
86 /**
87 * @param {mw.Title} title
88 * @param {HTMLAnchorElement} el
89 * @return {Promise<ext.popups.PreviewModel>}
90 */
91 function fetchPreviewForTitle( title, el ) {
92 // Need to encode the fragment again as mw.Title returns it as decoded text
93 const id = title.getFragment().replace( / /g, '_' );
94 const referenceTextElement = findReferenceTextElement( id );
95
96 if ( !referenceTextElement ||
97 // Skip references that don't contain anything but whitespace, e.g. a single &nbsp;
98 ( !referenceTextElement.textContent.trim() && !referenceTextElement.children.length )
99 ) {
100 return Promise.reject(
101 // Required to set showNullPreview to false and not open an error popup
102 { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } }
103 );
104 }
105
106 const referenceParentTextElement = findParentReferenceTextElement( referenceTextElement );
107
108 const model = {
109 url: `#${ id }`,
110 extract: scrapeReferenceText( referenceTextElement, referenceParentTextElement ),
111 type: TYPE_REFERENCE,
112 referenceType: scrapeReferenceType( referenceParentTextElement || referenceTextElement ),
113 // Note: Even the top-most HTMLHtmlElement is guaranteed to have a parent.
114 sourceElementId: el.parentNode.id
115 };
116
117 // Make promise abortable.
118 const promise = Promise.resolve( model );
119 promise.abort = () => {};
120 return promise;
121 }
122
123 return {
124 fetchPreviewForTitle
125 };
126 };