| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
const NEW_TOPIC_COMMENT_ID = 'new|' + mw.config.get( 'wgRelevantPageName' ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function isBlockElement( node ) { |
| 14 |
return node instanceof HTMLElement && ve.isBlockElement( node ); |
| 15 |
} |
| 16 |
|
| 17 |
const solTransparentLinkRegexp = /(?:^|\s)mw:PageProp\/(?:Category|redirect|Language)(?=$|\s)/; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
function isRenderingTransparentNode( node ) { |
| 24 |
const nextSibling = node.nextSibling; |
| 25 |
return ( |
| 26 |
node.nodeType === Node.COMMENT_NODE || |
| 27 |
node.nodeType === Node.ELEMENT_NODE && ( |
| 28 |
node.tagName.toLowerCase() === 'meta' || |
| 29 |
( |
| 30 |
node.tagName.toLowerCase() === 'link' && |
| 31 |
solTransparentLinkRegexp.test( node.getAttribute( 'rel' ) || '' ) |
| 32 |
) || |
| 33 |
|
| 34 |
( |
| 35 |
node.tagName.toLowerCase() === 'span' && |
| 36 |
( node.getAttribute( 'typeof' ) || '' ).split( ' ' ).indexOf( 'mw:Transclusion' ) !== -1 && |
| 37 |
!htmlTrim( node.innerHTML ) && |
| 38 |
( |
| 39 |
!nextSibling || nextSibling.nodeType !== Node.ELEMENT_NODE || |
| 40 |
|
| 41 |
|
| 42 |
nextSibling.getAttribute( 'about' ) !== node.getAttribute( 'about' ) |
| 43 |
) |
| 44 |
) |
| 45 |
) |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
function isOurGeneratedNode( node ) { |
| 54 |
return node.nodeType === Node.ELEMENT_NODE && ( |
| 55 |
node.classList.contains( 'ext-discussiontools-init-replylink-buttons' ) || |
| 56 |
node.hasAttribute( 'data-mw-comment-start' ) || |
| 57 |
node.hasAttribute( 'data-mw-comment-end' ) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
const noElementChildrenElementTypes = [ |
| 63 |
|
| 64 |
|
| 65 |
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', |
| 66 |
'link', 'meta', 'param', 'source', 'track', 'wbr', |
| 67 |
|
| 68 |
'script', 'style', |
| 69 |
|
| 70 |
'textarea', 'title', |
| 71 |
|
| 72 |
'math', 'svg', |
| 73 |
|
| 74 |
|
| 75 |
'noscript', |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
'audio', 'canvas', 'iframe', 'object', 'video' |
| 80 |
]; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
function cantHaveElementChildren( node ) { |
| 87 |
return ( |
| 88 |
node.nodeType === Node.COMMENT_NODE || |
| 89 |
node.nodeType === Node.ELEMENT_NODE && ( |
| 90 |
noElementChildrenElementTypes.indexOf( node.tagName.toLowerCase() ) !== -1 || |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
( |
| 98 |
[ 'a', 'span' ].indexOf( node.tagName.toLowerCase() ) !== -1 && |
| 99 |
node.firstChild && |
| 100 |
cantHaveElementChildren( node.firstChild ) |
| 101 |
) || |
| 102 |
|
| 103 |
|
| 104 |
node.tagName.toLowerCase() === 'figure' |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
function isCommentSeparator( node ) { |
| 116 |
if ( node.nodeType !== Node.ELEMENT_NODE ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
const tagName = node.tagName.toLowerCase(); |
| 121 |
if ( tagName === 'br' || tagName === 'hr' ) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
if ( node.nextSibling && |
| 127 |
( tagName === 'link' || tagName === 'style' ) && |
| 128 |
isCommentSeparator( node.nextSibling ) |
| 129 |
) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
const classList = node.classList; |
| 134 |
if ( |
| 135 |
|
| 136 |
classList.contains( 'mw-notalk' ) || |
| 137 |
|
| 138 |
classList.contains( 'outdent-template' ) || |
| 139 |
|
| 140 |
classList.contains( 'mw-trackedTemplate' ) |
| 141 |
) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
if ( tagName === 'dl' && |
| 147 |
node.childNodes.length === 1 && |
| 148 |
node.firstChild.nodeType === Node.ELEMENT_NODE && |
| 149 |
node.firstChild.nodeName.toLowerCase() === 'dt' |
| 150 |
) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
… |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
function isCommentContent( node ) { |
| 164 |
return ( |
| 165 |
( node.nodeType === Node.TEXT_NODE && htmlTrim( node.textContent ) !== '' ) || |
| 166 |
( cantHaveElementChildren( node ) ) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
function childIndexOf( child ) { |
| 177 |
let i = 0; |
| 178 |
while ( ( child = child.previousSibling ) ) { |
| 179 |
i++; |
| 180 |
} |
| 181 |
return i; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
function closestElement( node, tagNames ) { |
| 192 |
do { |
| 193 |
if ( |
| 194 |
node.nodeType === Node.ELEMENT_NODE && |
| 195 |
tagNames.indexOf( node.tagName.toLowerCase() ) !== -1 |
| 196 |
) { |
| 197 |
return node; |
| 198 |
} |
| 199 |
node = node.parentNode; |
| 200 |
} while ( node ); |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
function getTranscludedFromElement( node ) { |
| 215 |
while ( node ) { |
| 216 |
|
| 217 |
if ( |
| 218 |
node.nodeType === Node.ELEMENT_NODE && |
| 219 |
node.getAttribute( 'about' ) && |
| 220 |
/^#mwt\d+$/.test( node.getAttribute( 'about' ) ) |
| 221 |
) { |
| 222 |
const about = node.getAttribute( 'about' ); |
| 223 |
|
| 224 |
|
| 225 |
while ( |
| 226 |
node.previousSibling && |
| 227 |
node.previousSibling.nodeType === Node.ELEMENT_NODE && |
| 228 |
node.previousSibling.getAttribute( 'about' ) === about |
| 229 |
) { |
| 230 |
node = node.previousSibling; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if ( |
| 235 |
node.getAttribute( 'typeof' ) && |
| 236 |
node.getAttribute( 'typeof' ).split( ' ' ).indexOf( 'mw:Transclusion' ) !== -1 |
| 237 |
) { |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
node = node.parentNode; |
| 243 |
} |
| 244 |
return node; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
function getHeadlineNode( heading ) { |
| 254 |
|
| 255 |
let headline = heading; |
| 256 |
|
| 257 |
if ( headline.hasAttribute( 'data-mw-comment-start' ) ) { |
| 258 |
|
| 259 |
headline = headline.parentNode; |
| 260 |
} |
| 261 |
|
| 262 |
if ( !headline.getAttribute( 'id' ) ) { |
| 263 |
|
| 264 |
headline = headline.querySelector( '.mw-headline' ); |
| 265 |
if ( !headline ) { |
| 266 |
headline = heading; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return headline; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
function htmlTrim( str ) { |
| 280 |
|
| 281 |
return str.replace( /^[\t\n\f\r ]+/, '' ).replace( /[\t\n\f\r ]+$/, '' ); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
function getIndentLevel( node, rootNode ) { |
| 295 |
let indent = 0; |
| 296 |
while ( node ) { |
| 297 |
if ( node === rootNode ) { |
| 298 |
break; |
| 299 |
} |
| 300 |
const tagName = node instanceof HTMLElement ? node.tagName.toLowerCase() : null; |
| 301 |
if ( tagName === 'li' || tagName === 'dd' ) { |
| 302 |
indent++; |
| 303 |
} |
| 304 |
node = node.parentNode; |
| 305 |
} |
| 306 |
return indent; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
function getCoveredSiblings( range ) { |
| 316 |
const ancestor = range.commonAncestorContainer; |
| 317 |
|
| 318 |
const siblings = ancestor.childNodes; |
| 319 |
let start = 0; |
| 320 |
let end = siblings.length - 1; |
| 321 |
|
| 322 |
|
| 323 |
if ( ancestor === range.startContainer ) { |
| 324 |
start = range.startOffset; |
| 325 |
} else { |
| 326 |
while ( !siblings[ start ].contains( range.startContainer ) ) { |
| 327 |
start++; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
if ( ancestor === range.endContainer ) { |
| 333 |
end = range.endOffset - 1; |
| 334 |
} else { |
| 335 |
while ( !siblings[ end ].contains( range.endContainer ) ) { |
| 336 |
end--; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return Array.prototype.slice.call( siblings, start, end + 1 ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
function getFullyCoveredSiblings( item, excludedAncestorNode ) { |
| 353 |
let siblings = getCoveredSiblings( item.getRange() ); |
| 354 |
|
| 355 |
function makeRange( sibs ) { |
| 356 |
const range = sibs[ 0 ].ownerDocument.createRange(); |
| 357 |
range.setStartBefore( sibs[ 0 ] ); |
| 358 |
range.setEndAfter( sibs[ sibs.length - 1 ] ); |
| 359 |
return range; |
| 360 |
} |
| 361 |
|
| 362 |
const matches = compareRanges( makeRange( siblings ), item.getRange() ) === 'equal'; |
| 363 |
|
| 364 |
if ( matches ) { |
| 365 |
|
| 366 |
let parent; |
| 367 |
while ( |
| 368 |
( parent = siblings[ 0 ].parentNode ) && |
| 369 |
parent !== excludedAncestorNode && |
| 370 |
compareRanges( makeRange( [ parent ] ), item.getRange() ) === 'equal' |
| 371 |
) { |
| 372 |
siblings = [ parent ]; |
| 373 |
} |
| 374 |
return siblings; |
| 375 |
} |
| 376 |
return null; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
function getTitleFromUrl( url ) { |
| 387 |
if ( !url ) { |
| 388 |
return null; |
| 389 |
} |
| 390 |
const parsedUrl = new URL( url ); |
| 391 |
if ( parsedUrl.searchParams.get( 'title' ) ) { |
| 392 |
return parsedUrl.searchParams.get( 'title' ); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
const articlePathRegexp = new RegExp( |
| 397 |
mw.util.escapeRegExp( mw.config.get( 'wgArticlePath' ) ) |
| 398 |
.replace( '\\$1', '(.*)' ) |
| 399 |
); |
| 400 |
let match; |
| 401 |
if ( ( match = parsedUrl.pathname.match( articlePathRegexp ) ) ) { |
| 402 |
return decodeURIComponent( match[ 1 ] ); |
| 403 |
} |
| 404 |
|
| 405 |
return null; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
function linearWalk( node, callback ) { |
| 422 |
let |
| 423 |
result = null, |
| 424 |
withinNode = node.parentNode, |
| 425 |
beforeNode = node; |
| 426 |
|
| 427 |
while ( beforeNode || withinNode ) { |
| 428 |
if ( beforeNode ) { |
| 429 |
result = callback( 'enter', beforeNode ); |
| 430 |
withinNode = beforeNode; |
| 431 |
beforeNode = beforeNode.firstChild; |
| 432 |
} else { |
| 433 |
result = callback( 'leave', withinNode ); |
| 434 |
beforeNode = withinNode.nextSibling; |
| 435 |
withinNode = withinNode.parentNode; |
| 436 |
} |
| 437 |
|
| 438 |
if ( result ) { |
| 439 |
return result; |
| 440 |
} |
| 441 |
} |
| 442 |
return result; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
function linearWalkBackwards( node, callback ) { |
| 451 |
let |
| 452 |
result = null, |
| 453 |
withinNode = node.parentNode, |
| 454 |
beforeNode = node; |
| 455 |
|
| 456 |
while ( beforeNode || withinNode ) { |
| 457 |
if ( beforeNode ) { |
| 458 |
result = callback( 'enter', beforeNode ); |
| 459 |
withinNode = beforeNode; |
| 460 |
beforeNode = beforeNode.lastChild; |
| 461 |
} else { |
| 462 |
result = callback( 'leave', withinNode ); |
| 463 |
beforeNode = withinNode.previousSibling; |
| 464 |
withinNode = withinNode.parentNode; |
| 465 |
} |
| 466 |
|
| 467 |
if ( result ) { |
| 468 |
return result; |
| 469 |
} |
| 470 |
} |
| 471 |
return result; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
function getRangeFirstNode( range ) { |
| 479 |
return range.startContainer.childNodes.length ? |
| 480 |
range.startContainer.childNodes[ range.startOffset ] : |
| 481 |
range.startContainer; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
function getRangeLastNode( range ) { |
| 489 |
return range.endContainer.childNodes.length ? |
| 490 |
range.endContainer.childNodes[ range.endOffset - 1 ] : |
| 491 |
range.endContainer; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
function compareRanges( a, b ) { |
| 521 |
|
| 522 |
|
| 523 |
let startToStart = a.compareBoundaryPoints( Range.START_TO_START, b ); |
| 524 |
const startToEnd = a.compareBoundaryPoints( Range.END_TO_START, b ); |
| 525 |
const endToStart = a.compareBoundaryPoints( Range.START_TO_END, b ); |
| 526 |
let endToEnd = a.compareBoundaryPoints( Range.END_TO_END, b ); |
| 527 |
|
| 528 |
|
| 529 |
if ( |
| 530 |
( startToStart < 0 && compareRangesAlmostEqualBoundaries( a, b, 'start' ) ) || |
| 531 |
( startToStart > 0 && compareRangesAlmostEqualBoundaries( b, a, 'start' ) ) |
| 532 |
) { |
| 533 |
startToStart = 0; |
| 534 |
} |
| 535 |
if ( |
| 536 |
( endToEnd < 0 && compareRangesAlmostEqualBoundaries( a, b, 'end' ) ) || |
| 537 |
( endToEnd > 0 && compareRangesAlmostEqualBoundaries( b, a, 'end' ) ) |
| 538 |
) { |
| 539 |
endToEnd = 0; |
| 540 |
} |
| 541 |
|
| 542 |
if ( startToStart === 0 && endToEnd === 0 ) { |
| 543 |
return 'equal'; |
| 544 |
} |
| 545 |
if ( startToStart <= 0 && endToEnd >= 0 ) { |
| 546 |
return 'contains'; |
| 547 |
} |
| 548 |
if ( startToStart >= 0 && endToEnd <= 0 ) { |
| 549 |
return 'contained'; |
| 550 |
} |
| 551 |
if ( startToEnd >= 0 ) { |
| 552 |
return 'after'; |
| 553 |
} |
| 554 |
if ( endToStart <= 0 ) { |
| 555 |
return 'before'; |
| 556 |
} |
| 557 |
if ( startToStart > 0 && startToEnd < 0 && endToEnd >= 0 ) { |
| 558 |
return 'overlapstart'; |
| 559 |
} |
| 560 |
if ( endToEnd < 0 && endToStart > 0 && startToStart <= 0 ) { |
| 561 |
return 'overlapend'; |
| 562 |
} |
| 563 |
|
| 564 |
throw new Error( 'Unreachable' ); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
function compareRangesAlmostEqualBoundaries( a, b, boundary ) { |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
const from = boundary === 'end' ? getRangeLastNode( a ) : getRangeFirstNode( a ); |
| 583 |
const to = boundary === 'end' ? getRangeLastNode( b ) : getRangeFirstNode( b ); |
| 584 |
|
| 585 |
let skipNode = null; |
| 586 |
if ( boundary === 'end' ) { |
| 587 |
skipNode = from; |
| 588 |
} |
| 589 |
|
| 590 |
let foundContent = false; |
| 591 |
linearWalk( |
| 592 |
from, |
| 593 |
( event, n ) => { |
| 594 |
if ( n === to && event === ( boundary === 'end' ? 'leave' : 'enter' ) ) { |
| 595 |
return true; |
| 596 |
} |
| 597 |
if ( skipNode ) { |
| 598 |
if ( n === skipNode && event === 'leave' ) { |
| 599 |
skipNode = null; |
| 600 |
} |
| 601 |
return; |
| 602 |
} |
| 603 |
|
| 604 |
if ( event === 'enter' ) { |
| 605 |
if ( |
| 606 |
isCommentSeparator( n ) || |
| 607 |
isRenderingTransparentNode( n ) || |
| 608 |
isOurGeneratedNode( n ) |
| 609 |
) { |
| 610 |
skipNode = n; |
| 611 |
|
| 612 |
} else if ( |
| 613 |
isCommentContent( n ) |
| 614 |
) { |
| 615 |
foundContent = true; |
| 616 |
return true; |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
); |
| 621 |
|
| 622 |
return !foundContent; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
function getNewTopicsSubscriptionId( title ) { |
| 632 |
return 'p-topics-' + title.getNamespaceId() + ':' + title.getMain(); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
function isUnmodifiedLeftClick( e ) { |
| 642 |
return e.which === OO.ui.MouseButtons.LEFT && !( e.shiftKey || e.altKey || e.ctrlKey || e.metaKey ); |
| 643 |
} |
| 644 |
|
| 645 |
module.exports = { |
| 646 |
NEW_TOPIC_COMMENT_ID: NEW_TOPIC_COMMENT_ID, |
| 647 |
isBlockElement: isBlockElement, |
| 648 |
isRenderingTransparentNode: isRenderingTransparentNode, |
| 649 |
isCommentSeparator: isCommentSeparator, |
| 650 |
isCommentContent: isCommentContent, |
| 651 |
cantHaveElementChildren: cantHaveElementChildren, |
| 652 |
childIndexOf: childIndexOf, |
| 653 |
closestElement: closestElement, |
| 654 |
getIndentLevel: getIndentLevel, |
| 655 |
getCoveredSiblings: getCoveredSiblings, |
| 656 |
getFullyCoveredSiblings: getFullyCoveredSiblings, |
| 657 |
getTranscludedFromElement: getTranscludedFromElement, |
| 658 |
getHeadlineNode: getHeadlineNode, |
| 659 |
htmlTrim: htmlTrim, |
| 660 |
getTitleFromUrl: getTitleFromUrl, |
| 661 |
linearWalk: linearWalk, |
| 662 |
linearWalkBackwards: linearWalkBackwards, |
| 663 |
compareRanges: compareRanges, |
| 664 |
getNewTopicsSubscriptionId: getNewTopicsSubscriptionId, |
| 665 |
isUnmodifiedLeftClick: isUnmodifiedLeftClick |
| 666 |
}; |