MediaWiki:WikiHieroTempFix.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// <nowiki>
// 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 = '<p class="wikt-hierokludge"></p>';
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 <div> 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 <p>
		nextText && self.appendChild(document.createTextNode(nextText)), mergeElement(next, self, false), $(next).remove();
	if (allowPrev && prev.tagName == "P") // merge <p>
		prevText && self.insertBefore(document.createTextNode(prevText), self.firstChild), mergeElement(prev, self, true), $(prev).remove(), allowMentionKludge = true;
	else if (allowPrev && prev.tagName == "SPAN") // merge to <span>
		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 <div> 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);
				}
			}
		});
	}
});

// </nowiki>