473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
getElementsByTagName, only for text nodes)
b) Run a regex replace on only text in the document (not tags or their
attributes)?

Thanks,
Jeremy
Jun 22 '06 #1
1 11166


Jeremy wrote:
Is there a better way to either:
a) Get a flat list of all the text nodes in the document (like
getElementsByTagName, only for text nodes)


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

var xPathResult = document.evaluate(
'.//text()[normalize-space(.) != ""]',
document.body,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (var i = 0, l = xPathResult.snapshotLength; i < l; i++) {
var textNode = xPathResult.snapshotItem(i);
textNode.data = textNode.data.replace(/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.createNodeIterator(
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode : function (node) {
return /somePattern/.test(node.data) ? NodeFilter.FILTER_ACCEPT :
NodeFilter.FILTER_REJECT;
}},
true
);

var textNode;

while ((textNode = nodeIterator.nextNode()) != null) {
textNode.data = textNode.data.replace(/somePattern/g, 'replacement');
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jürgen Holly | last post by:
Hi! I have the following xml-node: <docu> <p>Sample: <b>bold</b></p> <p>and text in <i>italic</i></p> </docu> I need to create a text-file, so I set the output-mode to text.
1
by: Gordon - Adelphia | last post by:
I have a question regarding xhtml. Why, why, why does the ELEMENT <body> allow “unblocked” text. HTML does not (though, most browsers will render). Xhtml (transitional) however allows text nodes...
8
by: Xamle Eng | last post by:
One of the things I find most unnatural about most XML APIs is that they try to abstract both elements and text into some kind of "node" object when they have virtually nothing in common. The...
2
by: RobG | last post by:
Why does Firefox insert #text nodes as children of TR elements? As a work-around for older Safari versions not properly supporting a table row's cells collection, I used the row's childNodes...
4
by: pbreah | last post by:
I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for kids. I'm doing a special case in with every keystroke from A-Z creates a background and foreground color for that letter, witch...
2
by: Debbie | last post by:
Is there a standard way to extract text from a web page, without using innertext/innerhtml? It's an academic exercise, and we've been advised that we can't use Internet Explorer DOM extensions...
1
by: Darsin | last post by:
What i am doing is to pull the data from a CMS and import it to Word 2007 Beta and i also have to export the data from Word 2007 Beta back to that CMS. We have with us two Web Services of the CMS....
20
by: alice | last post by:
I'm doing some text swapping with javascript, got it working fine, but I would like the line to have line breaks and being a beginner, I don't even know if this is possible. So I have a line like...
1
by: JR | last post by:
Hi, I need a routine/finction that finds a text, starting at the selected node, where a given text is in. say i have some nodes with 1 'sonday' and another with 'son' and I look for 'so' it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.