La librería DOMhelp

Esto es una librería que me he compuesto sacando diversas funciones de un libro, y que principalmente contiene funciones para el manejo de la estructura DOM de un documento (encontrar nodos, cambiarles el texto, etc)

var DOMhelp =
{
  // Find the last sibling of the current node
  lastSibling:
  function(node) {
    var tempObj=node.parentNode.lastChild;
    while (tempObj.nodeType!=1 && tempObj.previousSibling!=null)
      tempObj=tempObj.previousSibling;
    return (tempObj.nodeType==1)?tempObj:false;
  },

  // Find the first sibling of the current node
  firstSibling:
  function(node) {
    var tempObj=node.parentNode.firstChild;
    while (tempObj.nodeType!=1 && tempObj.nextSibling!=null)
      tempObj=tempObj.nextSibling;
    return (tempObj.nodeType==1)?tempObj:false;
  },

  // Retrieve the content of the first text node sibling of the current node
  getText:
  function(node) {
    if(!node.hasChildNodes()) {return false;}
    var reg=/^s+$/;
    var tempObj=node.firstChild;
    while (tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue))
      tempObj=tempObj.nextSibling;
    return tempObj.nodeType==3?tempObj.nodeValue:false;
  },

  // Set the content of the first text node sibling of the current node
  setText:
  function(node,txt) {
    if(!node.hasChildNodes()) {return false;}
    var reg=/^s+$/;
    var tempObj=node.firstChild;
    while (tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue))
      tempObj=tempObj.nextSibling;
    if (tempObj.nodeType==3)
      tempObj.nodeValue=txt;
    else
      return false;
  },

  // Find the next or previous sibling that is an element and not a text node or line break
  closestSibling:
  function(node,direction) {
    var tempObj;
    if(direction==-1 && node.previousSibling!=null) {
      tempObj=node.previousSibling;
      while (tempObj.nodeType!=1 && tempObj.previousSibling!=null)
        tempObj=tempObj.previousSibling;
    }
    else if(direction==1 && node.nextSibling!=null) {
      tempObj=node.nextSibling;
      while (tempObj.nodeType!=1 && tempObj.nextSibling!=null)
        tempObj=tempObj.nextSibling;
    }

    return tempObj.nodeType==1?tempObj:false;
  },

  // Create a new link containing the given text
  createLink:
  function(to,txt) {
    var tempObj=document.createElement('a');
    tempObj.appendChild(document.createTextNode(txt));
    tempObj.setAttribute('href',to);
    return tempObj;
  },

  // Create a new element containing the given text
  createTextElm:
  function(elm,txt) {
    var tempObj=document.createElement(elm);
    tempObj.appendChild(document.createTextNode(txt));
    return tempObj;
  },

  // Cancels a click
  cancelClick:
  function(e) {
    if (window.event && window.event.cancelBubble && window.event.returnValue) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
      return;
    }
    if (e && e.stopPropagation && e.preventDefault) {
      e.stopPropagation();
      e.preventDefault();
    }
  },

  // Cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
  // By Scott Andrew
  addEvent:
  function(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    }
    else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      return r;
    }
    else
      elm['on'+window.event.srcElement:e?e.target:null;
    if (!target) {return false;}
    return target;
  },

  // Stops the bubbling propagation of the event
  stopBubble:
    function(e) {
    if(window.event && window.event.cancelBubble) {
      window.event.cancelBubble = true;
    }
    if (e && e.stopPropagation) {
      e.stopPropagation();
    }
  },

  // Prevents for default actions (cross-browser except for Safari)
  stopDefault:
  function(e) {
    if(window.event && window.event.returnValue) {
      window.event.cancelBubble = true;
    }
    if (e && e.preventDefault) {
      e.preventDefault();
    }
  },

  // Simulate a debugging console to avoid the need for alerts
  initDebug:
  function() {
    if(DOMhelp.debug) {DOMhelp.stopDebug();}
    DOMhelp.debug=document.createElement('div');
    DOMhelp.debug.setAttribute('id',DOMhelp.debugWindowId);

    if (document.documentElement)
      document.getElementsByTagName('body')[0].insertBefore(DOMhelp.debug,document.getElementsByTagName('body')[0].firstChild);
    else if (document.body)
      document.body.insertBefore(DOMhelp.debug,document.body.firstChild);
    //else
    //[Netscape stuff]
  },

  setDebug:
  function(bug) {
    if(!DOMhelp.debug) {DOMhelp.initDebug();}
    DOMhelp.debug.innerHTML+=bug+'n';
  },

  stopDebug:
  function() {
    if(DOMhelp.debug) {
      DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);
      DOMhelp.debug=null;
    }
  }
}

Otros artículos de esta serie:

[seriesposts show_date=0 order=asc]

Publicar un Comentario

Si es la primera vez que escribes, tu comentario será moderado por un administrador.

Con el fin de garantizar un ambiente de debate respetuoso, no se permitirán comentarios:

  • insultantes, difamatorios, racistas, sexistas, y/o discriminatorios
  • excesivamente críticos con otros participanes
  • que no aporten nada, sin sentido o repetidos
  • con enlaces considerados publicidad o spam
  • con material protegido por derechos de autor
*
*