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, producing:
         // MW parser ejects template-generated spans (Place) from list items:
         //  <ul>...<li class="mw-empty-elt"/></ul>
         //  Pattern A (start): <li class="mw-empty-elt"/> + <p><span>...</span>text</p>
        //  <p><span class="place-lh|place-oc">...</span>text</p>
         //   Pattern B (end):   <li>text</li> + <p><span>...</span></p>
         //  <ul><li>continuation...</li></ul> (optional)
        // 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;
                  
                  
                 // Find deepest empty list item
                 // Pattern A: empty item (Place at start of list item)
                 var $emptyItem = $prevList.find('li.mw-empty-elt').last();
                 var $target = findEmptyItem($prevList);
                if (!$emptyItem.length) {
                var isPatternA = $target !== null;
                    $emptyItem = $prevList.find('dd').filter(function() {
               
                        return this.childNodes.length === 0;
                // Pattern B: no empty item (Place at end of list item)
                     }).last();
                if (!$target) {
                    $target = $prevList.find('li').last();
                    if (!$target.length) $target = $prevList.find('dd').last();
                     if (!$target.length) return;
                 }
                 }
                if (!$emptyItem.length) return;
                  
                  
                 // Move <p> contents into empty item
                 if (isPatternA) $target.removeClass('mw-empty-elt');
                $emptyItem.removeClass('mw-empty-elt');
               
                // Move <p> contents into target item
                 while (p.firstChild) {
                 while (p.firstChild) {
                     $emptyItem[0].appendChild(p.firstChild);
                     $target[0].appendChild(p.firstChild);
                 }
                 }
                  
                  
                 // Merge continuation list if same type follows
                 // Merge continuation list if same type follows
                var listTag = $prevList[0].tagName;
                 var $next = $p.next();
                 var $next = $p.next();
                 if ($next.length && $next[0].tagName === listTag) {
                 if ($next.length && $next[0].tagName === $prevList[0].tagName) {
                     while ($next[0].firstChild) {
                     var targetAtRoot = $target[0].parentNode === $prevList[0];
                         $prevList[0].appendChild($next[0].firstChild);
                   
                    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:


})();
})();