Get all text nodes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy

    Get all text nodes?

    I have a script that runs a regular expression replace on all text in
    the document. Currently, I recurse the entire document looking for text
    nodes and run the replacement on the text nodes when I find them. The
    problem is, this gets really slow for complicated documents.

    Is there a better way to either:
    a) Get a flat list of all the text nodes in the document (like
    getElementsByTa gName, only for text nodes)
    b) Run a regex replace on only text in the document (not tags or their
    attributes)?

    Thanks,
    Jeremy
  • Martin Honnen

    #2
    Re: Get all text nodes?



    Jeremy wrote:
    [color=blue]
    > Is there a better way to either:
    > a) Get a flat list of all the text nodes in the document (like
    > getElementsByTa gName, only for text nodes)[/color]

    Mozilla and the new Opera 9 provide XPath over HTML so there you could
    do e.g.

    var xPathResult = document.evalua te(
    './/text()[normalize-space(.) != ""]',
    document.body,
    null,
    XPathResult.ORD ERED_NODE_SNAPS HOT_TYPE,
    null
    );
    for (var i = 0, l = xPathResult.sna pshotLength; i < l; i++) {
    var textNode = xPathResult.sna pshotItem(i);
    textNode.data = textNode.data.r eplace(/somePattern/g, 'replacement');
    }

    to iterate over all text nodes (well I have choosen the XPath to ignore
    white space text nodes).

    I haven't checked whether that has any performance gains.

    Opera 8 and 9 also implement the W3C DOM Level 2 NodeIterator interface
    so you could also use that to iterate over all text nodes e.g. here is
    an example that use a node filter with a regular expression check so
    that the iterator simply has to manipulate the nodes found:

    var nodeIterator = document.create NodeIterator(
    document.body,
    NodeFilter.SHOW _TEXT,
    { acceptNode : function (node) {
    return /somePattern/.test(node.data ) ? NodeFilter.FILT ER_ACCEPT :
    NodeFilter.FILT ER_REJECT;
    }},
    true
    );

    var textNode;

    while ((textNode = nodeIterator.ne xtNode()) != null) {
    textNode.data = textNode.data.r eplace(/somePattern/g, 'replacement');
    }

    --

    Martin Honnen

    Comment

    Working...