473,659 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jun 22 '06 #1
1 11214


Jeremy wrote:
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)


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
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
2874
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
2128
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 (PCDATA). All of HTML 4, xhtml – transitional, and xhtml-strict allow <div> to contain “unblocked” text. Does anybody know why – I’m looking for the philosophy behind allowing such. I’m in the process of encapsulating xhtml mark-up in content...
8
1529
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 reason these APIs do it is to make it possible for both text and elements to be children of elements. But there is another way. The XPath/XQuery data model does not allow two consecutive text nodes. As far as I can tell, most XML processing...
2
3207
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 collection as it was pretty much exactly the same thing. However, in Firefox 1.0.7 text nodes are inserted between the TDs. I'm certain that this didn't use to happen with older versions. The HTML specification states that the only element that...
4
3110
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 is the same. The problem is editing doesn't work that well. For example when I type: ABCDFG , I get this code generated and displayed by the Rich Text Editor: <P><FONT style="BACKGROUND-COLOR: #ffff00" color=#ffff00>a<FONT...
2
1568
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 that are not part of the W3C DOM. Thanks, Debbie
1
4094
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. The Web Services are explained as follows: IMPORT WEB SERVICE:
20
16626
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 this swaptext('Shaun, Greg, Violet, Joel, 1999')"; and I would like 1999 to be on a new line. Is there a simple way to do this? I'd swear I've seen a way, but now I can't seem to find it.
1
9873
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 will first find 'sonday' and the next time 'son'. ones it have found everything with the look parameter it restart at the top. Jan
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.