MediaWiki:Common.js: различия между версиями

мНет описания правки
мНет описания правки
Строка 362: Строка 362:
         // Пропускаем первую строку, если нет thead и нет класса 'no-header'
         // Пропускаем первую строку, если нет thead и нет класса 'no-header'
         var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
         var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
        var hasInvalidRowspan = false;
        var hasTooManyRowspan = false;
        // Проходим по строкам первого уровня для проверки корректности rowspan
        topLevelRows.forEach(function(row) {
            var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')).filter(function(cell) {
                return !cell.classList.contains('mobile');
            });
            var cellCount = cells.length;
            // Проверка на наличие ячеек с rowspan
            var rowspanCount = cells.filter(function(cell) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1';
            }).length;
            if (rowspanCount > 2) {
                hasTooManyRowspan = true;
                return;
            }
            if (cellCount <= 3 && rowspanCount > 1) {
                hasTooManyRowspan = true;
                return;
            }
            // Проверка корректности rowspan на краю строки
            var hasValidRowspanEdge = cells.some(function(cell, index) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && (index === 0 || index === cells.length - 1);
            });
            if (!hasValidRowspanEdge && cells.some(function(cell, index) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && index > 0 && index < cells.length - 1;
            })) {
                hasInvalidRowspan = true;
            }
        });
        // Если некорректные строки с rowspan или слишком много rowspan — игнорируем таблицу
        if (hasTooManyRowspan || hasInvalidRowspan) return;


         // Объект для хранения оригинальных стилей
         // Объект для хранения оригинальных стилей
Строка 435: Строка 395:
                     highlightedCells[cellId] = true;
                     highlightedCells[cellId] = true;


                     // Подсвечиваем ячейку с rowspan
                     // Подсвечиваем текущую ячейку
                     cell.style.backgroundColor = brightenColor(getComputedStyle(cell).backgroundColor);
                     cell.style.backgroundColor = brightenColor(getComputedStyle(cell).backgroundColor);
                     cell.style.color = brightenColor(getComputedStyle(cell).color);
                     cell.style.color = brightenColor(getComputedStyle(cell).color);
Строка 455: Строка 415:
                     }
                     }


                     // Подсвечиваем все строки, которые содержат ячейки с rowspan, затрагиваемые текущей ячейкой
                     // Подсвечиваем все строки с rowspan в том же столбце
                     for (var j = 0; j < topLevelRows.length; j++) {
                     topLevelRows.forEach(function(checkRow, checkRowIndex) {
                         if (j === rowIndex) continue; // Пропускаем текущую строку
                         if (checkRowIndex === rowIndex) return;


                        var checkRow = topLevelRows[j];
                         var checkCells = Array.prototype.slice.call(checkRow.querySelectorAll('td, th'));
                         var checkCells = Array.prototype.slice.call(checkRow.querySelectorAll('td, th'));
 
                        var targetCell = checkCells[cellIndex];
                         checkCells.forEach(function(checkCell, checkCellIndex) {
                         if (targetCell && targetCell.hasAttribute('rowspan')) {
                             var checkCellId = 'row-' + j + '-cell-' + checkCellIndex;
                             var targetCellId = 'row-' + checkRowIndex + '-cell-' + cellIndex;
                             if (highlightedCells[checkCellId]) return;
                             if (!highlightedCells[targetCellId]) {
 
                                 targetCell.style.backgroundColor = brightenColor(getComputedStyle(targetCell).backgroundColor);
                            var checkCellRowspan = parseInt(checkCell.getAttribute('rowspan'), 10) || 1;
                                 targetCell.style.color = brightenColor(getComputedStyle(targetCell).color);
                            if (checkCellRowspan > 1 && cell.contains(checkCell)) {
                                 highlightedCells[targetCellId] = true;
                                 checkCell.style.backgroundColor = brightenColor(getComputedStyle(checkCell).backgroundColor);
                                 checkCell.style.color = brightenColor(getComputedStyle(checkCell).color);
                                 highlightedCells[checkCellId] = true;
                             }
                             }
                         });
                         }
                     }
                     });
                 });
                 });