Skip to content

Instantly share code, notes, and snippets.

@dpashkevich
Created November 6, 2012 11:41

Revisions

  1. dpashkevich revised this gist Nov 7, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions domToPre.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * See https://github.com/sellside/pre
    /**
    * See https://github.com/sellside/pre
    */

    /**
    @@ -27,7 +27,7 @@ function toPre(el) {

    (function traverse(el, depth) {
    depth = depth || 0;

    var childNodes,
    indent = Array(depth * tabWidth + 1).join(tabChar),
    nextIndent = indent + Array(tabWidth + 1).join(tabChar),
    @@ -43,20 +43,20 @@ function toPre(el) {
    if(tagName === 'div' && (classes || id)) {
    tagName = '';
    }
    selector = tagName
    + (id ? ('#' + id) : '')

    selector = tagName
    + (id ? ('#' + id) : '')
    + (classes ? ('.' + classes) : '');
    break;

    case 3: // TEXT_NODE
    nodeValue = el.nodeValue;
    if(wsRe.test(nodeValue)) { // filter whitespace-only text nodes
    return;
    }
    selector = 'text';
    break;

    case 8: // COMMENT_NODE
    nodeValue = el.nodeValue;
    selector = 'comment';
    @@ -66,13 +66,13 @@ function toPre(el) {
    // skip other kinds of nodes...
    return;
    }


    // selector + opening brace
    s += indent + selector +' {';
    if(el.nodeType === 1) { // ELEMENT_NODE

    if(el.nodeType === 1) { // ELEMENT_NODE

    // output attributes
    if(el.hasAttributes()) {
    isEmpty = false; // body not empty
  2. dpashkevich revised this gist Nov 6, 2012. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions domToPre.js
    Original file line number Diff line number Diff line change
    @@ -73,6 +73,7 @@ function toPre(el) {

    if(el.nodeType === 1) { // ELEMENT_NODE

    // output attributes
    if(el.hasAttributes()) {
    isEmpty = false; // body not empty
    attrs = el.attributes;
    @@ -86,7 +87,7 @@ function toPre(el) {
    }
    }
    }

    // output child nodes
    if(el.hasChildNodes()) {
    isEmpty = false;
    @@ -96,11 +97,11 @@ function toPre(el) {
    traverse(childNodes[i], depth + 1);
    }
    }

    if(!isEmpty) {
    s += indent; // add indentation before the closing bracket if it's on new line
    }

    } else { // Comment or text nodes...
    // just put the value directly inside the body
    if(nodeValue.indexOf('\n') > -1) { // multiline - surround value with newlines
    @@ -111,7 +112,7 @@ function toPre(el) {

    // closing brace
    s += '}\n';

    })(el);

    return s;
  3. dpashkevich revised this gist Nov 6, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion domToPre.js
    Original file line number Diff line number Diff line change
    @@ -98,7 +98,7 @@ function toPre(el) {
    }

    if(!isEmpty) {
    s += indent; // add indentation before the closing bracket
    s += indent; // add indentation before the closing bracket if it's on new line
    }

    } else { // Comment or text nodes...
  4. dpashkevich renamed this gist Nov 6, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. dpashkevich revised this gist Nov 6, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions toPre.js
    Original file line number Diff line number Diff line change
    @@ -117,8 +117,8 @@ function toPre(el) {
    return s;
    }

    console.log(toPre(document.documentElement));
    //console.log(toPre(document.documentElement));

    // For executing directly from the console:
    //copy(toPre(document.documentElement));
    //console.log('Markup copied to clipboard');
    copy(toPre(document.documentElement));
    console.log('Markup copied to clipboard');
  6. dpashkevich created this gist Nov 6, 2012.
    124 changes: 124 additions & 0 deletions toPre.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    /**
    * See https://github.com/sellside/pre
    */

    /**
    * References/todos:
    * http://www.lemoda.net/javascript/dump-dom/dump-dom.html
    * http://www.howtocreate.co.uk/tutorials/javascript/dombasics
    * Use treewalker or nodefilter??
    * https://developer.mozilla.org/en-US/docs/DOM/NodeFilter
    * https://developer.mozilla.org/en-US/docs/DOM/treeWalker
    */

    function toPre(el) {
    "use strict";
    var s = '',
    tagName,
    classes,
    id,
    tabChar = ' ',
    tabWidth = 2,
    attrs,
    attrName,
    wsRe = /^\s+$/,
    selector,
    slice = Array.prototype.slice;

    (function traverse(el, depth) {
    depth = depth || 0;

    var childNodes,
    indent = Array(depth * tabWidth + 1).join(tabChar),
    nextIndent = indent + Array(tabWidth + 1).join(tabChar),
    nodeValue,
    isEmpty = true;

    switch(el.nodeType) {
    case 1: // ELEMENT_NODE
    classes = slice.call(el.classList).join('.');
    id = el.id;
    tagName = el.tagName.toLowerCase();
    // omit 'div' tag name is classes and/or id present
    if(tagName === 'div' && (classes || id)) {
    tagName = '';
    }

    selector = tagName
    + (id ? ('#' + id) : '')
    + (classes ? ('.' + classes) : '');
    break;

    case 3: // TEXT_NODE
    nodeValue = el.nodeValue;
    if(wsRe.test(nodeValue)) { // filter whitespace-only text nodes
    return;
    }
    selector = 'text';
    break;

    case 8: // COMMENT_NODE
    nodeValue = el.nodeValue;
    selector = 'comment';
    break;

    default:
    // skip other kinds of nodes...
    return;
    }


    // selector + opening brace
    s += indent + selector +' {';

    if(el.nodeType === 1) { // ELEMENT_NODE

    if(el.hasAttributes()) {
    isEmpty = false; // body not empty
    attrs = el.attributes;
    s += '\n';
    for(var i = 0, len = attrs.length; i < len; i++) {
    // normalize attr name
    attrName = attrs[i].name.replace(/:/g, '-').toLowerCase();
    // skip 'id' and 'class' attributes (they are present in the selector)
    if(attrName !== 'id' && attrName !== 'class') {
    s += nextIndent + attrName + ': "' + attrs[i].nodeValue + '";\n';
    }
    }
    }

    // output child nodes
    if(el.hasChildNodes()) {
    isEmpty = false;
    s += '\n';
    childNodes = el.childNodes;
    for(var i = 0, len = childNodes.length; i < len; i++) {
    traverse(childNodes[i], depth + 1);
    }
    }

    if(!isEmpty) {
    s += indent; // add indentation before the closing bracket
    }

    } else { // Comment or text nodes...
    // just put the value directly inside the body
    if(nodeValue.indexOf('\n') > -1) { // multiline - surround value with newlines
    nodeValue = '\n' + nodeValue + '\n' + indent;
    }
    s += nodeValue;
    }

    // closing brace
    s += '}\n';

    })(el);

    return s;
    }

    console.log(toPre(document.documentElement));

    // For executing directly from the console:
    //copy(toPre(document.documentElement));
    //console.log('Markup copied to clipboard');