| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
const TYPE_REFERENCE = 'reference'; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
module.exports = function createReferenceGateway() { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
function findReferenceTextElement( id ) { |
| 17 |
const idSelector = `#${ CSS.escape( id ) }`; |
| 18 |
|
| 19 |
|
| 20 |
t. |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
return document.querySelector( `${ idSelector } .mw-reference-text, ${ idSelector } .reference-text` ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
function findParentReferenceTextElement( el ) { |
| 32 |
|
| 33 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 59 |
ng |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
function fetchPreviewForTitle( title, el ) { |
| 92 |
|
| 93 |
const id = title.getFragment().replace( / /g, '_' ); |
| 94 |
const referenceTextElement = findReferenceTextElement( id ); |
| 95 |
|
| 96 |
if ( !referenceTextElement || |
| 97 |
|
| 98 |
( !referenceTextElement.textContent.trim() && !referenceTextElement.children.length ) |
| 99 |
) { |
| 100 |
return Promise.reject( |
| 101 |
|
| 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 |
|
| 114 |
sourceElementId: el.parentNode.id |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
const promise = Promise.resolve( model ); |
| 119 |
promise.abort = () => {}; |
| 120 |
return promise; |
| 121 |
} |
| 122 |
|
| 123 |
return { |
| 124 |
fetchPreviewForTitle |
| 125 |
}; |
| 126 |
}; |