| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
mw.libs.ve = mw.libs.ve || {}; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
mw.libs.ve.decodeURIComponentIntoArticleTitle = function ( s, preserveUnderscores ) { |
| 20 |
try { |
| 21 |
s = decodeURIComponent( s ); |
| 22 |
} catch ( e ) { |
| 23 |
return s; |
| 24 |
} |
| 25 |
if ( preserveUnderscores ) { |
| 26 |
return s; |
| 27 |
} |
| 28 |
return s.replace( /_/g, ' ' ); |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
mw.libs.ve.unwrapParsoidSections = function ( element, keepSection ) { |
| 43 |
Array.prototype.forEach.call( element.querySelectorAll( 'section[data-mw-section-id]' ), ( section ) => { |
| 44 |
const parent = section.parentNode, |
| 45 |
sectionId = section.getAttribute( 'data-mw-section-id' ); |
| 46 |
|
| 47 |
|
| 48 |
if ( sectionId !== null && +sectionId > 0 ) { |
| 49 |
section.firstChild.setAttribute( 'data-mw-section-id', sectionId ); |
| 50 |
} |
| 51 |
if ( keepSection !== undefined && sectionId === keepSection ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
while ( section.firstChild ) { |
| 55 |
parent.insertBefore( section.firstChild, section ); |
| 56 |
} |
| 57 |
parent.removeChild( section ); |
| 58 |
} ); |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
mw.libs.ve.stripParsoidFallbackIds = function ( element ) { |
| 68 |
Array.prototype.forEach.call( element.querySelectorAll( 'span[typeof="mw:FallbackId"][id]:empty' ), ( legacySpan ) => { |
| 69 |
legacySpan.parentNode.removeChild( legacySpan ); |
| 70 |
} ); |
| 71 |
}; |
| 72 |
|
| 73 |
mw.libs.ve.restbaseIdRegExp = /^mw[a-zA-Z0-9\-_]{2,6}$/; |
| 74 |
|
| 75 |
mw.libs.ve.stripRestbaseIds = function ( doc ) { |
| 76 |
const restbaseIdRegExp = mw.libs.ve.restbaseIdRegExp; |
| 77 |
Array.prototype.forEach.call( doc.querySelectorAll( '[id^="mw"]' ), ( element ) => { |
| 78 |
if ( restbaseIdRegExp.test( element.id ) ) { |
| 79 |
element.removeAttribute( 'id' ); |
| 80 |
} |
| 81 |
} ); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
mw.libs.ve.reduplicateStyles = function ( element ) { |
| 91 |
Array.prototype.forEach.call( element.querySelectorAll( 'link[rel~="mw-deduplicated-inline-style"]' ), ( link ) => { |
| 92 |
const href = link.getAttribute( 'href' ); |
| 93 |
if ( !href || href.slice( 0, 'mw-data:'.length ) !== 'mw-data:' ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
const key = href.slice( 'mw-data:'.length ); |
| 97 |
const style = element.querySelector( 'style[data-mw-deduplicate="' + key + '"]' ); |
| 98 |
if ( !style ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
const newStyle = link.ownerDocument.createElement( 'style' ); |
| 103 |
newStyle.setAttribute( 'data-mw-deduplicate', key ); |
| 104 |
|
| 105 |
|
| 106 |
for ( let i = 0; i < style.childNodes.length; i++ ) { |
| 107 |
newStyle.appendChild( style.childNodes[ i ].cloneNode( true ) ); |
| 108 |
} |
| 109 |
|
| 110 |
Array.prototype.forEach.call( link.attributes, ( attr ) => { |
| 111 |
if ( attr.name !== 'rel' && attr.name !== 'href' ) { |
| 112 |
newStyle.setAttribute( attr.name, attr.value ); |
| 113 |
} |
| 114 |
} ); |
| 115 |
|
| 116 |
link.parentNode.replaceChild( newStyle, link ); |
| 117 |
} ); |
| 118 |
|
| 119 |
Array.prototype.forEach.call( element.querySelectorAll( 'style[data-mw-deduplicate]:empty' ), ( style ) => { |
| 120 |
const key = style.getAttribute( 'data-mw-deduplicate' ); |
| 121 |
const firstStyle = element.querySelector( 'style[data-mw-deduplicate="' + key + '"]' ); |
| 122 |
if ( !firstStyle || firstStyle === style ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
for ( let i = 0; i < firstStyle.childNodes.length; i++ ) { |
| 128 |
style.appendChild( firstStyle.childNodes[ i ].cloneNode( true ) ); |
| 129 |
} |
| 130 |
} ); |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.libs.ve.deduplicateStyles = function ( element ) { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
function isFosterablePosition( node ) { |
| 149 |
const fosterablePositions = [ 'table', 'thead', 'tbody', 'tfoot', 'tr' ]; |
| 150 |
return node && fosterablePositions.indexOf( node.parentNode.nodeName.toLowerCase() ) !== -1; |
| 151 |
} |
| 152 |
|
| 153 |
const styleTagKeys = {}; |
| 154 |
|
| 155 |
Array.prototype.forEach.call( element.querySelectorAll( 'style[data-mw-deduplicate]' ), ( style ) => { |
| 156 |
const key = style.getAttribute( 'data-mw-deduplicate' ); |
| 157 |
|
| 158 |
if ( !styleTagKeys[ key ] ) { |
| 159 |
|
| 160 |
styleTagKeys[ key ] = true; |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if ( !isFosterablePosition( style ) ) { |
| 165 |
|
| 166 |
const link = style.ownerDocument.createElement( 'link' ); |
| 167 |
link.setAttribute( 'rel', 'mw-deduplicated-inline-style' ); |
| 168 |
link.setAttribute( 'href', 'mw-data:' + key ); |
| 169 |
|
| 170 |
|
| 171 |
Array.prototype.forEach.call( style.attributes, ( attr ) => { |
| 172 |
if ( attr.name !== 'rel' && attr.name !== 'data-mw-deduplicate' ) { |
| 173 |
link.setAttribute( attr.name, attr.value ); |
| 174 |
} |
| 175 |
} ); |
| 176 |
|
| 177 |
style.parentNode.replaceChild( link, style ); |
| 178 |
} else { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
style.innerHTML = ''; |
| 184 |
} |
| 185 |
} ); |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
mw.libs.ve.fixFragmentLinks = function ( container, docTitle, prefix ) { |
| 202 |
const docTitleText = docTitle.getPrefixedText(); |
| 203 |
prefix = prefix || ''; |
| 204 |
Array.prototype.forEach.call( container.querySelectorAll( 'a[href*="#"]' ), ( el ) => { |
| 205 |
let fragment = null; |
| 206 |
if ( el.getAttribute( 'href' )[ 0 ] === '#' ) { |
| 207 |
|
| 208 |
fragment = el.getAttribute( 'href' ).slice( 1 ); |
| 209 |
} else { |
| 210 |
|
| 211 |
const targetData = mw.libs.ve.getTargetDataFromHref( el.href, el.ownerDocument ); |
| 212 |
|
| 213 |
if ( targetData.isInternal ) { |
| 214 |
const title = mw.Title.newFromText( targetData.title ); |
| 215 |
if ( title && title.getPrefixedText() === docTitleText ) { |
| 216 |
fragment = new URL( el.href ).hash.slice( 1 ); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if ( fragment !== null ) { |
| 222 |
if ( !fragment ) { |
| 223 |
|
| 224 |
el.setAttribute( 'href', '#' ); |
| 225 |
} else { |
| 226 |
if ( prefix ) { |
| 227 |
const target = container.querySelector( '#' + $.escapeSelector( fragment ) ); |
| 228 |
|
| 229 |
|
| 230 |
if ( target ) { |
| 231 |
target.setAttribute( 'id', prefix + fragment ); |
| 232 |
target.setAttribute( 'data-mw-id-fixed', '' ); |
| 233 |
} |
| 234 |
} |
| 235 |
el.setAttribute( 'href', '#' + prefix + fragment ); |
| 236 |
} |
| 237 |
el.removeAttribute( 'target' ); |
| 238 |
} |
| 239 |
} ); |
| 240 |
|
| 241 |
Array.prototype.forEach.call( container.querySelectorAll( 'h1, h2, h3, h4, h5, h6' ), ( el ) => { |
| 242 |
if ( el.hasAttribute( 'id' ) && !el.hasAttribute( 'data-mw-id-fixed' ) ) { |
| 243 |
el.removeAttribute( 'id' ); |
| 244 |
} |
| 245 |
} ); |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
mw.libs.ve.getTargetDataFromHref = function ( href, doc ) { |
| 263 |
function regexEscape( str ) { |
| 264 |
return str.replace( /([.?*+^$[\]\\(){}|-])/g, '\\$1' ); |
| 265 |
} |
| 266 |
|
| 267 |
function returnExternalData() { |
| 268 |
return { isInternal: false }; |
| 269 |
} |
| 270 |
|
| 271 |
function returnInternalData( titleish ) { |
| 272 |
|
| 273 |
|
| 274 |
const data = mw.libs.ve.parseParsoidResourceName( titleish ); |
| 275 |
data.isInternal = true; |
| 276 |
return data; |
| 277 |
} |
| 278 |
|
| 279 |
let url; |
| 280 |
try { |
| 281 |
url = new URL( href, doc.baseURI ); |
| 282 |
} catch ( e ) { |
| 283 |
|
| 284 |
return returnExternalData(); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
const externalLinkUrlProtocolsRegExp = new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + ')', 'i' ); |
| 289 |
|
| 290 |
|
| 291 |
if ( !externalLinkUrlProtocolsRegExp.test( url.toString() ) ) { |
| 292 |
return returnInternalData( url.toString() ); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
if ( url.searchParams.get( 'action' ) === 'edit' && url.searchParams.get( 'redlink' ) === '1' ) { |
| 297 |
url.searchParams.delete( 'action' ); |
| 298 |
url.searchParams.delete( 'redlink' ); |
| 299 |
} |
| 300 |
|
| 301 |
const keys = []; |
| 302 |
url.searchParams.forEach( ( val, key ) => { |
| 303 |
keys.push( key ); |
| 304 |
} ); |
| 305 |
const queryLength = keys.length; |
| 306 |
|
| 307 |
const relativeHref = url.toString().replace( /^https?:/i, '' ); |
| 308 |
|
| 309 |
const scriptBase = new URL( mw.config.get( 'wgScript' ), doc.baseURI ).toString().replace( /^https?:/i, '' ); |
| 310 |
if ( relativeHref.indexOf( scriptBase ) === 0 ) { |
| 311 |
if ( queryLength === 1 && url.searchParams.get( 'title' ) ) { |
| 312 |
return returnInternalData( url.searchParams.get( 'title' ) + url.hash ); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
const articleBase = new URL( mw.config.get( 'wgArticlePath' ), doc.baseURI ).toString().replace( /^https?:/i, '' ); |
| 318 |
const articleBaseRegex = new RegExp( regexEscape( articleBase ).replace( regexEscape( '$1' ), '(.*)' ) ); |
| 319 |
const matches = relativeHref.match( articleBaseRegex ); |
| 320 |
if ( matches ) { |
| 321 |
if ( queryLength === 0 && matches && matches[ 1 ].split( '#' )[ 0 ].indexOf( '?' ) === -1 ) { |
| 322 |
|
| 323 |
return returnInternalData( matches[ 1 ] ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
return returnExternalData(); |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
mw.libs.ve.encodeParsoidResourceName = function ( title ) { |
| 338 |
|
| 339 |
const idx = title.indexOf( '#' ); |
| 340 |
let anchor = null; |
| 341 |
if ( idx !== -1 ) { |
| 342 |
anchor = title.slice( idx + 1 ); |
| 343 |
title = title.slice( 0, idx ); |
| 344 |
} |
| 345 |
let encodedTitle = title.replace( /[%? [\]#|<>]/g, ( match ) => mw.util.wikiUrlencode( match ) ); |
| 346 |
if ( anchor !== null ) { |
| 347 |
encodedTitle += '#' + mw.util.escapeIdForLink( anchor ); |
| 348 |
} |
| 349 |
return './' + encodedTitle; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
mw.libs.ve.parseParsoidResourceName = function ( resourceName ) { |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
const matches = resourceName.match( /^(\.\/|)(.*)$/ ); |
| 369 |
return { |
| 370 |
|
| 371 |
|
| 372 |
title: mw.libs.ve.decodeURIComponentIntoArticleTitle( matches[ 2 ] ) |
| 373 |
}; |
| 374 |
}; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
mw.libs.ve.normalizeParsoidResourceName = function ( resourceName ) { |
| 383 |
return mw.libs.ve.parseParsoidResourceName( resourceName ).title; |
| 384 |
}; |