mediawiki-extensions-Discus.../modules/modifier.js
Ed Sanders 9733f72354 Use Parsoid hack to remove whitespace from replies
Change-Id: I1ad0f5498c3c409df6d4eb2d400a0f714dff9908
2019-11-19 21:24:33 +00:00

87 lines
2.3 KiB
JavaScript

/* global $:off */
'use strict';
/**
* Adapted from MDN polyfill (CC0)
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
*
* @param {HTMLElement} el
* @param {string} selector
* @return {HTMLElement|null}
*/
function closest( el, selector ) {
var matches;
el = el.nodeType === Node.ELEMENT_NODE ? el : el.parentElement;
if ( Element.prototype.closest ) {
return el.closest( selector );
}
matches = Element.prototype.matches ||
Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
do {
if ( matches.call( el, selector ) ) {
return el;
}
el = el.parentElement || el.parentNode;
} while ( el !== null && el.nodeType === 1 );
return null;
}
function addListAtComment( comment ) {
var list, listType, lastReply, listItem, endNodeInAncestor,
tsNode = comment.range.endContainer;
if ( comment.replies.length ) {
lastReply = comment.replies[ comment.replies.length - 1 ];
list = closest( lastReply.range.endContainer, 'dl, ul, ol' );
} else {
listItem = closest( tsNode, 'li, dd' );
if ( listItem ) {
listType = closest( listItem, 'dl, ul, ol' ).tagName;
list = document.createElement( listType );
listItem.appendChild( list );
} else {
endNodeInAncestor = comment.range.endContainer;
while ( endNodeInAncestor.parentNode !== comment.range.commonAncestorContainer ) {
endNodeInAncestor = endNodeInAncestor.parentNode;
}
list = document.createElement( 'dl' );
comment.range.commonAncestorContainer.insertBefore(
list,
endNodeInAncestor.nextSibling
);
}
}
return list;
}
function addListItem( list ) {
var listItem = document.createElement( list.nodeName.toLowerCase() === 'dl' ? 'dd' : 'li' );
// HACK: Setting data-parsoid removes the whitespace after the list item,
// which makes nested lists work.
// This is undocumented behaviour and probably very fragile.
listItem.setAttribute( 'data-parsoid', '{}' );
list.appendChild( listItem );
return listItem;
}
function createWikitextNode( wt ) {
var span = document.createElement( 'span' );
span.setAttribute( 'typeof', 'mw:Transclusion' );
span.setAttribute( 'data-mw', JSON.stringify( { parts: [ wt ] } ) );
return span;
}
module.exports = {
addListAtComment: addListAtComment,
addListItem: addListItem,
createWikitextNode: createWikitextNode
};