Misplaced Pages

:WikiProject User scripts/Scripts/Fix diff width: Difference between revisions - Misplaced Pages

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< Misplaced Pages:WikiProject User scripts | Scripts Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 13:46, 17 January 2006 view sourceIlmari Karonen (talk | contribs)Extended confirmed users14,453 editsm hide nonstandard IE CSS syntax from other browsers← Previous edit Revision as of 13:14, 25 January 2006 view source Ilmari Karonen (talk | contribs)Extended confirmed users14,453 edits only use one wrapper span for signs, refactor CSSNext edit →
Line 6: Line 6:
' .xdiff { width: 100%; background: white; }' + ' .xdiff { width: 100%; background: white; }' +
' .xdiff-row { width: 100%; margin: 0 0 3px 0; overflow: hidden; }' + ' .xdiff-row { width: 100%; margin: 0 0 3px 0; overflow: hidden; }' +
' .xdiff-col { width: 49%; float: left; clear: none; position: relative; }' + ' .xdiff-col { width: 49%; margin: 0; float: left; clear: none; position: relative; }' +
' .xdiff-sign1 { display: block; position: relative; width: 0; height: 0; }' + ' .xdiff-sign, .xdiff-outer, .xdiff-inner { display: block; margin: 0; }' +
' .xdiff-sign2 { display: block; position: absolute; top: 0; left: 0; width: 2em; text-align: center; }' + ' .xdiff-sign { position: absolute; top: 0; left: 0; width: 2em; text-align: center; }' +
' .xdiff-outer { display: block; }' + ' .xdiff-outer { padding: 0 0 0 2em; }' +
' .xdiff-outer.diff-addedline { margin: 0 0 0 2em; font-size: 85%; background: #cfc; }' + ' .xdiff-inner { overflow: auto; overflow-y: visible; width: 100%; }' +
' .xdiff-outer.diff-deletedline { margin: 0 0 0 2em; font-size: 85%; background: #ffa; }' + ' .xdiff-inner.diff-addedline { font-size: 85%; background: #cfc; }' +
' .xdiff-outer.diff-context { margin: 0 0 0 2em; font-size: 85%; background: #eee; }' + ' .xdiff-inner.diff-deletedline { font-size: 85%; background: #ffa; }' +
' .xdiff-inner { display: block; overflow: auto; overflow-y: visible; width: 100%; padding: 1px; }' + ' .xdiff-inner.diff-context { font-size: 85%; background: #eee; }' +
(!document.recalc ? '' : // IE kluge: (!document.recalc ? '' : // IE kluge:
' * html .xdiff-inner { padding-bottom: expression(this.scrollWidth > this.offsetWidth ? "16px" : 0); }' + ' * html .xdiff-inner { padding-bottom: expression(this.scrollWidth > this.offsetWidth ? "16px" : 0); }' +
' * html .xdiff-sign2 { top: expression((this.parentNode.parentNode.clientHeight - this.offsetHeight)/2 + "px"); }') + ' * html .xdiff-sign { top: expression((this.parentNode.clientHeight - this.offsetHeight)/2 + "px"); }') +
'<'+'/style>'); '<'+'/style>');


Line 43: Line 43:
rowDiv.appendChild(colDiv); rowDiv.appendChild(colDiv);
} }
// use spans instead of divs so that an eventual non-js solution will look nice in lynx!
var outerSpan = document.createElement('span');
var innerSpan = document.createElement('span');

if (cols.getAttribute('colspan') == 2 || cols.className.substring(0,5) == 'diff-') { if (cols.getAttribute('colspan') == 2 || cols.className.substring(0,5) == 'diff-') {
outerSpan.className = 'xdiff-outer ' + cols.className; // use spans instead of divs so that an eventual non-js solution will look nice in lynx!
innerSpan.className = 'xdiff-inner'; var outerSpan = document.createElement('span');
var innerSpan = document.createElement('span');
outerSpan.className = 'xdiff-outer';
innerSpan.className = 'xdiff-inner ' + cols.className;
innerSpan.style.textAlign = cols.getAttribute('align'); innerSpan.style.textAlign = cols.getAttribute('align');
for (var node = cols.firstChild; node; node = node.nextSibling)
innerSpan.appendChild(node.cloneNode(true));
innerSpan.appendChild(document.createTextNode(String.fromCharCode(0xa0))); // add nbsp
outerSpan.appendChild(innerSpan);
colDiv.appendChild(outerSpan);
colDiv = null; // start new column
} }
else if (cols.firstChild && (cols.firstChild.nextSibling || else if (cols.firstChild && (cols.firstChild.nextSibling ||
cols.firstChild.nodeType != 3 || cols.firstChild.nodeValue.match(/\S/))) { cols.firstChild.nodeType != 3 || cols.firstChild.nodeValue.match(/\S/))) {
outerSpan.className = 'xdiff-sign1'; // use spans instead of divs so that an eventual non-js solution will look nice in lynx!
innerSpan.className = 'xdiff-sign2'; var signSpan = document.createElement('span');
if (!innerSpan.style.setExpression) signSpan.className = 'xdiff-sign';
diffSigns = innerSpan; if (!signSpan.style.setExpression)
diffSigns = signSpan;
for (var node = cols.firstChild; node; node = node.nextSibling)
signSpan.appendChild(node.cloneNode(true));
colDiv.appendChild(signSpan);
} }
else continue;

for (var node = cols.firstChild; node; node = node.nextSibling)
innerSpan.appendChild(node.cloneNode(true));
innerSpan.appendChild(document.createTextNode(String.fromCharCode(0xa0))); // add nbsp
outerSpan.appendChild(innerSpan);
colDiv.appendChild(outerSpan);

if (innerSpan.className == 'xdiff-inner')
colDiv = null; // start new column
} }
diffDiv.appendChild(rowDiv); diffDiv.appendChild(rowDiv);
Line 80: Line 79:
var parentHeight; var parentHeight;
if (!( parentHeight = diffSigns.parentNode )) continue; if (!( parentHeight = diffSigns.parentNode )) continue;
if (!( parentHeight = parentHeight.parentNode )) continue;
if (!( parentHeight = parentHeight.clientHeight )) continue; if (!( parentHeight = parentHeight.clientHeight )) continue;
diffSigns.style.top = ((parentHeight - diffSigns.offsetHeight)/2) + "px"; diffSigns.style.top = ((parentHeight - diffSigns.offsetHeight)/2) + "px";

Revision as of 13:14, 25 January 2006

/* This script adds scroll bars to wide diffs such as . It replaces the table-based diff layout with a CSS-based layout where the column width is always exactly 50%. Known to work in Firefox, Konqueror, Opera and at least some versions of Internet Explorer. This script is a workaround for Bug 1229. If you have any problems with this script, please let me know. */

//

// inline style sheet to keep this whole thing self-contained:
document.write('<style type="text/css">' +
    ' .xdiff { width: 100%; background: white; }' +
    ' .xdiff-row { width: 100%; margin: 0 0 3px 0; overflow: hidden; }' +
    ' .xdiff-col { width: 49%; margin: 0; float: left; clear: none; position: relative; }' +
    ' .xdiff-sign, .xdiff-outer, .xdiff-inner { display: block; margin: 0; }' +
    ' .xdiff-sign { position: absolute; top: 0; left: 0; width: 2em; text-align: center; }' +
    ' .xdiff-outer { padding: 0 0 0 2em; }' +
    ' .xdiff-inner { overflow: auto; overflow-y: visible; width: 100%; }' +
    ' .xdiff-inner.diff-addedline { font-size: 85%; background: #cfc; }' +
    ' .xdiff-inner.diff-deletedline { font-size: 85%; background: #ffa; }' +
    ' .xdiff-inner.diff-context { font-size: 85%; background: #eee; }' +
    (!document.recalc ? '' :  // IE kluge:
        ' * html .xdiff-inner { padding-bottom: expression(this.scrollWidth > this.offsetWidth ? "16px" : 0); }' +
        ' * html .xdiff-sign { top: expression((this.parentNode.clientHeight - this.offsetHeight)/2 + "px"); }') +
    '<'+'/style>');
addOnloadHook(function () {
    var diffSigns = new Array();
    var fixDiffWidth = function () {
        var tables = document.getElementsByTagName('table');
        for (var i = 0; i < tables.length; i++) {
            if (tables.className != 'diff') continue;
            var rows = tables.getElementsByTagName('tr');
            var diffDiv = document.createElement('div');
            diffDiv.className = 'xdiff';
            for (var j = 0; j < rows.length; j++) {
                var rowDiv = document.createElement('div');
                rowDiv.className = 'xdiff-row';
                var colDiv = null;
                var cols = rows.getElementsByTagName('td');
                for (var k = 0; k < cols.length; k++) { 
                    if (!colDiv) {
                        colDiv = document.createElement('div');
                        colDiv.className = 'xdiff-col';
                        rowDiv.appendChild(colDiv);
                    }
                    if (cols.getAttribute('colspan') == 2 || cols.className.substring(0,5) == 'diff-') {                
                        // use spans instead of divs so that an eventual non-js solution will look nice in lynx!
                        var outerSpan = document.createElement('span');
                        var innerSpan = document.createElement('span');
                        outerSpan.className = 'xdiff-outer';
                        innerSpan.className = 'xdiff-inner ' + cols.className;
                        innerSpan.style.textAlign = cols.getAttribute('align');
                        for (var node = cols.firstChild; node; node = node.nextSibling)
                            innerSpan.appendChild(node.cloneNode(true));
                        innerSpan.appendChild(document.createTextNode(String.fromCharCode(0xa0))); // add nbsp
                        outerSpan.appendChild(innerSpan);
                        colDiv.appendChild(outerSpan);
                        colDiv = null;  // start new column
                    }
                    else if (cols.firstChild && (cols.firstChild.nextSibling ||
                             cols.firstChild.nodeType != 3 || cols.firstChild.nodeValue.match(/\S/))) {
                        // use spans instead of divs so that an eventual non-js solution will look nice in lynx!
                        var signSpan = document.createElement('span');
                        signSpan.className = 'xdiff-sign';
                        if (!signSpan.style.setExpression)
                            diffSigns = signSpan;
                        for (var node = cols.firstChild; node; node = node.nextSibling)
                            signSpan.appendChild(node.cloneNode(true));
                        colDiv.appendChild(signSpan);
                    }
                }
                diffDiv.appendChild(rowDiv);
            }
            tables.parentNode.replaceChild(diffDiv, tables);
        }
    };
    // finally, a kluge to vertically center the +/- signs
    var centerDiffSigns = function () {
        for (var i = 0; i < diffSigns.length; i++) {
            var parentHeight;
            if (!( parentHeight = diffSigns.parentNode )) continue; 
            if (!( parentHeight = parentHeight.clientHeight )) continue; 
            diffSigns.style.top = ((parentHeight - diffSigns.offsetHeight)/2) + "px";
        }
    };
    fixDiffWidth();
    if (diffSigns.length) {
        hookEvent('resize', centerDiffSigns);
        setTimeout(centerDiffSigns, 250); 
    }
});
// 
Misplaced Pages:WikiProject User scripts/Scripts/Fix diff width: Difference between revisions Add topic