| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
const |
| 5 |
utils = require( './utils.js' ); |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function sanitizeWikitextLinebreaks( wikitext ) { |
| 14 |
return utils.htmlTrim( wikitext ) |
| 15 |
.replace( /\r/g, '\n' ) |
| 16 |
.replace( /\n+/g, '\n' ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
function addReplyLink( comment, linkNode ) { |
| 27 |
const target = comment.range.endContainer; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
if ( target.nodeType === Node.TEXT_NODE ) { |
| 35 |
target.splitText( target.textContent.match( /\s*$/ ).index ); |
| 36 |
} |
| 37 |
|
| 38 |
target.parentNode.insertBefore( linkNode, target.nextSibling ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
function addListItem( comment, replyIndentation ) { |
| 55 |
const listTypeMap = { |
| 56 |
li: 'ul', |
| 57 |
dd: 'dl' |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
let curComment = comment; |
| 66 |
while ( curComment.replies.length ) { |
| 67 |
curComment = curComment.replies[ curComment.replies.length - 1 ]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
let itemType; |
| 72 |
if ( replyIndentation === 'invisible' ) { |
| 73 |
itemType = 'dd'; |
| 74 |
} else if ( replyIndentation === 'bullet' ) { |
| 75 |
itemType = 'li'; |
| 76 |
} else { |
| 77 |
throw new Error( "Invalid reply indentation syntax '" + replyIndentation + "'" ); |
| 78 |
} |
| 79 |
const listType = listTypeMap[ itemType ]; |
| 80 |
|
| 81 |
const desiredLevel = comment.level + 1; |
| 82 |
let target = curComment.range.endContainer; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
let parent = utils.closestElement( target, [ 'li', 'dd', 'p' ] ) || target.parentNode; |
| 89 |
while ( target.parentNode !== parent ) { |
| 90 |
target = target.parentNode; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
const excludedWrapper = utils.closestElement( target, [ 'section' ] ) || |
| 99 |
curComment.rootNode; |
| 100 |
const covered = utils.getFullyCoveredSiblings( curComment, excludedWrapper ); |
| 101 |
if ( curComment.level === 1 && covered ) { |
| 102 |
target = covered[ covered.length - 1 ]; |
| 103 |
parent = target.parentNode; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
let transclusionNode = utils.getTranscludedFromElement( target ); |
| 109 |
if ( transclusionNode ) { |
| 110 |
while ( |
| 111 |
transclusionNode.nextSibling && |
| 112 |
transclusionNode.nextSibling.nodeType === Node.ELEMENT_NODE && |
| 113 |
transclusionNode.nextSibling.getAttribute( 'about' ) === transclusionNode.getAttribute( 'about' ) |
| 114 |
) { |
| 115 |
transclusionNode = transclusionNode.nextSibling; |
| 116 |
} |
| 117 |
target = transclusionNode; |
| 118 |
parent = target.parentNode; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if ( |
| 129 |
parent.tagName.toLowerCase() === 'p' || |
| 130 |
parent.tagName.toLowerCase() === 'pre' || |
| 131 |
parent.tagName.toLowerCase() === 'ul' || |
| 132 |
parent.tagName.toLowerCase() === 'dl' |
| 133 |
) { |
| 134 |
parent = parent.parentNode; |
| 135 |
target = target.parentNode; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if ( target.nextSibling && target.nextSibling.nodeType === Node.ELEMENT_NODE && target.nextSibling.classList.contains( 'ext-discussiontools-init-replylink-buttons' ) ) { |
| 140 |
target = target.nextSibling; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
let curLevel = utils.getIndentLevel( target, curComment.rootNode ) + 1; |
| 146 |
|
| 147 |
let item, list; |
| 148 |
if ( desiredLevel === 1 ) { |
| 149 |
|
| 150 |
item = target.ownerDocument.createElement( 'div' ); |
| 151 |
item.discussionToolsModified = 'new'; |
| 152 |
parent.insertBefore( item, target.nextSibling ); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
} else if ( curLevel < desiredLevel ) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
let pointer = target; |
| 164 |
while ( |
| 165 |
pointer.nextSibling && ( |
| 166 |
utils.isRenderingTransparentNode( pointer.nextSibling ) || |
| 167 |
( |
| 168 |
pointer.nextSibling.nodeType === Node.TEXT_NODE && |
| 169 |
utils.htmlTrim( pointer.nextSibling.textContent ) === '' && |
| 170 |
|
| 171 |
|
| 172 |
!/\n[^\n]*\n/.test( pointer.nextSibling.textContent ) |
| 173 |
) |
| 174 |
) |
| 175 |
) { |
| 176 |
pointer = pointer.nextSibling; |
| 177 |
if ( utils.isRenderingTransparentNode( pointer ) ) { |
| 178 |
target = pointer; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
while ( curLevel < desiredLevel ) { |
| 184 |
list = target.ownerDocument.createElement( listType ); |
| 185 |
list.discussionToolsModified = 'new'; |
| 186 |
item = target.ownerDocument.createElement( itemType ); |
| 187 |
item.discussionToolsModified = 'new'; |
| 188 |
|
| 189 |
parent.insertBefore( list, target.nextSibling ); |
| 190 |
list.appendChild( item ); |
| 191 |
|
| 192 |
target = item; |
| 193 |
parent = list; |
| 194 |
curLevel++; |
| 195 |
} |
| 196 |
} else { |
| 197 |
|
| 198 |
|
| 199 |
let newNode; |
| 200 |
do { |
| 201 |
if ( !target || !parent ) { |
| 202 |
throw new Error( 'Can not decrease nesting any more' ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if ( target.nextSibling ) { |
| 207 |
|
| 208 |
newNode = parent.cloneNode( false ); |
| 209 |
parent.discussionToolsModified = 'split'; |
| 210 |
parent.parentNode.insertBefore( newNode, parent.nextSibling ); |
| 211 |
|
| 212 |
|
| 213 |
while ( target.nextSibling ) { |
| 214 |
newNode.appendChild( target.nextSibling ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
target = parent; |
| 219 |
parent = parent.parentNode; |
| 220 |
|
| 221 |
|
| 222 |
if ( listTypeMap[ target.tagName.toLowerCase() ] ) { |
| 223 |
curLevel--; |
| 224 |
} |
| 225 |
} while ( curLevel >= desiredLevel ); |
| 226 |
|
| 227 |
|
| 228 |
if ( itemType === target.tagName.toLowerCase() ) { |
| 229 |
item = target.ownerDocument.createElement( itemType ); |
| 230 |
item.discussionToolsModified = 'new'; |
| 231 |
parent.insertBefore( item, target.nextSibling ); |
| 232 |
|
| 233 |
} else { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if ( target.nextSibling ) { |
| 238 |
|
| 239 |
newNode = parent.cloneNode( false ); |
| 240 |
parent.discussionToolsModified = 'split'; |
| 241 |
parent.parentNode.insertBefore( newNode, parent.nextSibling ); |
| 242 |
|
| 243 |
|
| 244 |
while ( target.nextSibling ) { |
| 245 |
newNode.appendChild( target.nextSibling ); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
target = parent; |
| 250 |
parent = parent.parentNode; |
| 251 |
|
| 252 |
|
| 253 |
list = target.ownerDocument.createElement( listType ); |
| 254 |
list.discussionToolsModified = 'new'; |
| 255 |
item = target.ownerDocument.createElement( itemType ); |
| 256 |
item.discussionToolsModified = 'new'; |
| 257 |
|
| 258 |
parent.insertBefore( list, target.nextSibling ); |
| 259 |
list.appendChild( item ); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return item; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
function removeAddedListItem( node ) { |
| 272 |
while ( node && node.discussionToolsModified ) { |
| 273 |
let nextNode; |
| 274 |
if ( node.discussionToolsModified === 'new' ) { |
| 275 |
nextNode = node.previousSibling || node.parentNode; |
| 276 |
|
| 277 |
|
| 278 |
delete node.discussionToolsModified; |
| 279 |
node.parentNode.removeChild( node ); |
| 280 |
|
| 281 |
} else if ( node.discussionToolsModified === 'split' ) { |
| 282 |
|
| 283 |
if ( node.lastChild && node.lastChild.discussionToolsModified === 'split' ) { |
| 284 |
node.discussionToolsModified = 'done'; |
| 285 |
nextNode = node.lastChild; |
| 286 |
} else { |
| 287 |
delete node.discussionToolsModified; |
| 288 |
nextNode = node.parentNode; |
| 289 |
} |
| 290 |
|
| 291 |
while ( node.nextSibling.firstChild ) { |
| 292 |
node.appendChild( node.nextSibling.firstChild ); |
| 293 |
} |
| 294 |
node.parentNode.removeChild( node.nextSibling ); |
| 295 |
|
| 296 |
} else { |
| 297 |
nextNode = node.parentNode; |
| 298 |
} |
| 299 |
|
| 300 |
node = nextNode; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
function unwrapList( list, fragment ) { |
| 313 |
if ( !( |
| 314 |
list.nodeType === Node.ELEMENT_NODE && ( |
| 315 |
list.tagName.toLowerCase() === 'dl' || |
| 316 |
list.tagName.toLowerCase() === 'ol' || |
| 317 |
list.tagName.toLowerCase() === 'ul' |
| 318 |
) |
| 319 |
) ) { |
| 320 |
|
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
if ( utils.getTranscludedFromElement( list ) ) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
const doc = list.ownerDocument; |
| 330 |
const container = fragment || list.parentNode; |
| 331 |
let referenceNode = list; |
| 332 |
while ( list.firstChild ) { |
| 333 |
if ( list.firstChild.nodeType === Node.ELEMENT_NODE ) { |
| 334 |
|
| 335 |
let p = doc.createElement( 'p' ); |
| 336 |
while ( list.firstChild.firstChild ) { |
| 337 |
|
| 338 |
|
| 339 |
if ( utils.isBlockElement( list.firstChild.firstChild ) ) { |
| 340 |
if ( p.firstChild ) { |
| 341 |
const insertBefore2 = referenceNode.nextSibling; |
| 342 |
referenceNode = p; |
| 343 |
container.insertBefore( p, insertBefore2 ); |
| 344 |
} |
| 345 |
const insertBefore = referenceNode.nextSibling; |
| 346 |
referenceNode = list.firstChild.firstChild; |
| 347 |
container.insertBefore( list.firstChild.firstChild, insertBefore ); |
| 348 |
p = doc.createElement( 'p' ); |
| 349 |
} else { |
| 350 |
p.appendChild( list.firstChild.firstChild ); |
| 351 |
} |
| 352 |
} |
| 353 |
if ( p.firstChild ) { |
| 354 |
const insertBefore = referenceNode.nextSibling; |
| 355 |
referenceNode = p; |
| 356 |
container.insertBefore( p, insertBefore ); |
| 357 |
} |
| 358 |
list.removeChild( list.firstChild ); |
| 359 |
} else { |
| 360 |
|
| 361 |
const insertBefore = referenceNode.nextSibling; |
| 362 |
referenceNode = list.firstChild; |
| 363 |
container.insertBefore( list.firstChild, insertBefore ); |
| 364 |
} |
| 365 |
} |
| 366 |
container.removeChild( list ); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
function addSiblingListItem( previousItem ) { |
| 376 |
const listItem = previousItem.ownerDocument.createElement( previousItem.tagName ); |
| 377 |
previousItem.parentNode.insertBefore( listItem, previousItem.nextSibling ); |
| 378 |
return listItem; |
| 379 |
} |
| 380 |
|
| 381 |
module.exports = { |
| 382 |
addReplyLink: addReplyLink, |
| 383 |
addListItem: addListItem, |
| 384 |
removeAddedListItem: removeAddedListItem, |
| 385 |
addSiblingListItem: addSiblingListItem, |
| 386 |
unwrapList: unwrapList, |
| 387 |
sanitizeWikitextLinebreaks: sanitizeWikitextLinebreaks |
| 388 |
}; |