MediaWiki:Common.js: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 575: | Zeile 575: | ||
/* --- List Repair --- */ | /* --- List Repair --- */ | ||
// MW parser ejects template-generated spans from list items | // MW parser ejects template-generated spans (Place) from list items: | ||
// | // Pattern A (start): <li class="mw-empty-elt"/> + <p><span>...</span>text</p> | ||
// Pattern B (end): <li>text</li> + <p><span>...</span></p> | |||
// < | // Both followed optionally by a continuation list. | ||
// This repairs the DOM before structure analysis. | // This repairs the DOM before structure analysis. | ||
function repairListPlacement() { | function repairListPlacement() { | ||
| Zeile 584: | Zeile 584: | ||
if (!$output.length) return; | if (!$output.length) return; | ||
var repaired = 0; | var repaired = 0; | ||
function findEmptyItem($list) { | |||
var $li = $list.find('li.mw-empty-elt').last(); | |||
if ($li.length) return $li; | |||
var $dd = $list.find('dd').filter(function() { | |||
return this.childNodes.length === 0; | |||
}).last(); | |||
return $dd.length ? $dd : null; | |||
} | |||
function isNestingWrapper(el) { | |||
if (el.tagName !== 'LI') return false; | |||
if (el.children.length !== 1) return false; | |||
if (!/^(UL|OL|DL)$/.test(el.firstElementChild.tagName)) return false; | |||
for (var i = 0; i < el.childNodes.length; i++) { | |||
if (el.childNodes[i].nodeType === 3 && el.childNodes[i].textContent.trim()) return false; | |||
} | |||
return true; | |||
} | |||
$output.find('p').each(function() { | $output.find('p').each(function() { | ||
var p = this; | var p = this; | ||
if (!p.parentNode) return; | |||
var first = p.firstChild; | var first = p.firstChild; | ||
if (!first || first.nodeType !== 1) return; | if (!first || first.nodeType !== 1) return; | ||
| Zeile 595: | Zeile 615: | ||
if (!$prevList.length) return; | if (!$prevList.length) return; | ||
// | // Pattern A: empty item (Place at start of list item) | ||
var $ | var $target = findEmptyItem($prevList); | ||
var isPatternA = $target !== null; | |||
// Pattern B: no empty item (Place at end of list item) | |||
if (!$target) { | |||
$target = $prevList.find('li').last(); | |||
if (!$target.length) $target = $prevList.find('dd').last(); | |||
if (!$target.length) return; | |||
} | } | ||
if (isPatternA) $target.removeClass('mw-empty-elt'); | |||
// Move <p> contents into target item | |||
while (p.firstChild) { | while (p.firstChild) { | ||
$ | $target[0].appendChild(p.firstChild); | ||
} | } | ||
// Merge continuation list if same type follows | // Merge continuation list if same type follows | ||
var $next = $p.next(); | var $next = $p.next(); | ||
if ($next.length && $next[0].tagName === | if ($next.length && $next[0].tagName === $prevList[0].tagName) { | ||
var targetAtRoot = $target[0].parentNode === $prevList[0]; | |||
$ | |||
var children = $next.children().get(); | |||
for (var i = 0; i < children.length; i++) { | |||
if (isNestingWrapper(children[i])) { | |||
var subList = children[i].firstElementChild; | |||
if (targetAtRoot) { | |||
// Pattern B: sub-list becomes child of target | |||
$target[0].appendChild(subList); | |||
} else { | |||
// Pattern A: sub-list items become siblings | |||
var parentList = $target[0].parentNode; | |||
while (subList.firstChild) { | |||
parentList.appendChild(subList.firstChild); | |||
} | |||
} | |||
} else { | |||
$prevList[0].appendChild(children[i]); | |||
} | |||
} | } | ||
$next.remove(); | $next.remove(); | ||
| Zeile 1.684: | Zeile 1.723: | ||
})(); | })(); | ||