MediaWiki:WikiHieroTempFix.js

// // temp fix for WikiHiero // https://phabricator.wikimedia.org/T210695 for more permanent fix, after which this can be removed // (this is referenced by common.js)

// copies (or rather moves) all child nodes from one to another var hieroQuery = 'table.mw-hiero-outer'; var wrapper = ' '; var mergeElement = function(src, dst, prepend) { var el, oldFirst = dst.firstChild; while ((el = src.firstChild)) prepend ? dst.insertBefore(el, oldFirst) : dst.appendChild(el); }; var shouldWrap = function { return this.parentNode && this.parentNode.tagName == "DIV" && this.parentNode.className != ""; }; var shouldRewrap = function(node) { return (node.tagName == "SPAN" && $(node).hasClass("mention-tr")) || (["A", "B", "I"].indexOf(node.tagName) >= 0); }; $(hieroQuery).filter(shouldWrap).wrap(wrapper).each(function {	var self = this.parentNode, oldSelf;	/*	while (self.parentNode && shouldRewrap(self.parentNode)) {		// ok, there's something that should be inlined. do a swap/rotate;		// make the old parent the child of the div and not the other way around.		// we'll do this by rewrapping the element and then unwrapping the inner div		$(self.parentNode).wrap(wrapper);		// self is now the outer wrapper		oldSelf = self, self = self.parentNode.parentNode;		$(oldSelf.firstChild).unwrap;	}	*/	var next = self.nextSibling, prev = self.previousSibling;	var allowNext = !!next, allowPrev = !!prev;	// text nodes if they follow or precede the hiero table	var nextText = "", prevText = "";	var allowMentionKludge = false;	if (allowNext && next.nodeType == 3) // text node, extract text and disallow merge if it has a newline		allowNext = (nextText = next.textContent).indexOf('\n') < 0, next = next.nextSibling, allowNext &= !!next; if (allowPrev && prev.nodeType == 3) // text node, extract text and disallow merge if it has a newline allowPrev = (prevText = prev.textContent).indexOf('\n') < 0, prev = prev.previousSibling, allowPrev &= !!prev; if (allowNext && next.tagName == "P") // merge nextText && self.appendChild(document.createTextNode(nextText)), mergeElement(next, self, false), $(next).remove; if (allowPrev && prev.tagName == "P") // merge prevText && self.insertBefore(document.createTextNode(prevText), self.firstChild), mergeElement(prev, self, true), $(prev).remove, allowMentionKludge = true; else if (allowPrev && prev.tagName == "SPAN") // merge to 		prevText && self.insertBefore(document.createTextNode(prevText), self.firstChild), mergeElement(self, prev, false), $(self).remove; else if (allowPrev && prev.tagName == "DIV" && $(prev).hasClass("wikt-hierokludge")) // merge to existing wrapper prevText && self.insertBefore(document.createTextNode(prevText), self.firstChild), mergeElement(self, prev, false), $(self).remove; if (allowMentionKludge) { // merge table to preceding empty I+A $(self).find(hieroQuery).each(function {			prev = this.previousSibling;			if (prev && prev.tagName == "I" && prev.getAttribute('lang') == 'egy' && prev.childNodes.length == 1) {				var childA = prev.childNodes[0];				if (childA.tagName == "A" && !childA.childNodes.length) {					childA.appendChild(this);				}			}		}); } });

//