File: format_block.js

package info (click to toggle)
pageedit 2.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,956 kB
  • sloc: ansic: 31,806; cpp: 15,036; python: 1,141; javascript: 87; sh: 13; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function format_block( startNode, element_name, preserve ) {
    var nodeContent = startNode.innerHTML;

    // Because people will moan like hell if these are left in
    nodeContent = nodeContent.replace( /xmlns=\"http:\/\/www\.w3\.org\/1999\/xhtml\"/g, "" );

    // Create a new tag with the desired name.
    var newBlock = document.createElement( element_name, "http://www.w3.org/1999/xhtml" );

    if (preserve) {
        // Copy over all the attributes from the old block-level tag.
        var arrAttr = startNode.attributes;
        for(var j = 0; j < arrAttr.length; j++) {
            if(arrAttr[j].value != "" && arrAttr[j].value != "null") {
                var a = arrAttr[j].nodeName.toLowerCase();
                var v = arrAttr[j].nodeValue;
                
                switch(a) {
                    case "class":
                        newBlock.className = v;
                        break;
                    case "style":
                        newBlock.style.cssText = v;
                        break;
                    default:
                        newBlock.setAttribute( a, v );
                        break;
                }
            }
        }
    }
    
    // Inject the content from the old tag and replace the node.
    newBlock.innerHTML = nodeContent;
    
    return newBlock;
};