473,594 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

modifying and writing contents of a textRange

Hi,

I’m trying to modify a DHTML editor that parses a style-sheet
via PHP and instead of modifying the tags via execCommand(), find a
way of writing inline styles by way of adding <span style=> tags
around the selected text.

Easier said than done. My first though was to do a search and replace
on the innerHTML, but falls down if there is more than one match (it
will only modify the first occurrence). I suspect that even if I could
find the text around the selected area, this could still fall down for
same reason.

So, I’ve got the selected text via the createTextRange () method.
How do I modify this text and write it back out to the textarea.

I suspect finding a way of finding the positioning within the
innerHTML would be useful, so any suggestions will be appreciated.

Rgds
Neil.
Jul 23 '05 #1
6 10812


sentinel wrote:

So, I’ve got the selected text via the createTextRange () method.
How do I modify this text and write it back out to the textarea.


You can (with IE/Win only) simply do
range.pasteHTML ('<span style="backgrou nd-color: yellow;>' +
range.text + '<\/span>');
see
http://msdn.microsoft.com/library/de.../pastehtml.asp
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
Thanks for that. Do you know of a solution that will work on Netscape 7.1 ?

Neil.

Martin Honnen <ma*******@yaho o.de> wrote in message news:<40******* *@olaf.komtel.n et>...
sentinel wrote:

So, I’ve got the selected text via the createTextRange () method.
How do I modify this text and write it back out to the textarea.


You can (with IE/Win only) simply do
range.pasteHTML ('<span style="backgrou nd-color: yellow;>' +
range.text + '<\/span>');
see
http://msdn.microsoft.com/library/de.../pastehtml.asp

Jul 23 '05 #3


sentinel wrote:
Do you know of a solution that will work on Netscape 7.1 ?


Netscape doesn't implement the document.select ion object that IE has,
nor the TextRange that IE has.
It has a buggy implementation of the W3C DOM Level 2 Range specification
documented at
http://www.w3.org/TR/DOM-Level-2-Tra...ge/ranges.html
which is connected to its selection object returned by
window.getSelec tion()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContent s:
http://www.w3.org/TR/DOM-Level-2-Tra...ge-Surrounding
to wrap some range content into a span element for instance but last
time I have tried that with Netscape 7 it failed due to bugs.

When I try the following with Mozilla 1.7 or FireFox 0.9

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>does surroundContent s work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelec tion != 'undefined') {
var selection = window.getSelec tion();
if (typeof selection.range Count != 'undefined' &&
selection.range Count > 0) {
var range = selection.getRa ngeAt(0);
if (typeof range.surroundC ontents != 'undefined') {
var span = document.create Element('span') ;
span.className = cssClassName;
range.surroundC ontents(span);
}
}
}
}
</script>
<style type="text/javascript">
..selectionHigh light {
background-color: lightyellow;
}
</style>
</head>
<body>
<p>
Select some text in this paragraph with the mouse.
Then press the highlight button.
All for Kibology. Kibology for all.
</p>
<form action="" onsubmit="retur n false;">
<p>
<input type="button" value="highligh t selection"
onclick="wrapSe lection('select ionHighlight'); ">
</p>
</form>
</body>
</html>

I still get an error on the surroundContent s call:

Error: uncaught exception: [Exception... "Index or size is negative or
greater than the allowed amount" code: "1" nsresult: "0x80530001
(NS_ERROR_DOM_I NDEX_SIZE_ERR)" location:
"http://localhost/javascript/test20040623.ht ml Line: 15"]

which I think is caused by the buggy Mozilla implementation of
Range/surroundContent s.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #4
I guess I'm going to have to do a little experimentation ...

I need to construct something that will work on an OS X browser as well as IE5+.

Martin Honnen <ma*******@yaho o.de> wrote in message news:<40******@ olaf.komtel.net >...
sentinel wrote:
Do you know of a solution that will work on Netscape 7.1 ?


Netscape doesn't implement the document.select ion object that IE has,
nor the TextRange that IE has.
It has a buggy implementation of the W3C DOM Level 2 Range specification
documented at
http://www.w3.org/TR/DOM-Level-2-Tra...ge/ranges.html
which is connected to its selection object returned by
window.getSelec tion()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContent s:
http://www.w3.org/TR/DOM-Level-2-Tra...ge-Surrounding
to wrap some range content into a span element for instance but last
time I have tried that with Netscape 7 it failed due to bugs.

When I try the following with Mozilla 1.7 or FireFox 0.9

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>does surroundContent s work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelec tion != 'undefined') {
var selection = window.getSelec tion();
if (typeof selection.range Count != 'undefined' &&
selection.range Count > 0) {
var range = selection.getRa ngeAt(0);
if (typeof range.surroundC ontents != 'undefined') {
var span = document.create Element('span') ;
span.className = cssClassName;
range.surroundC ontents(span);
}
}
}
}
</script>
<style type="text/javascript">
.selectionHighl ight {
background-color: lightyellow;
}
</style>
</head>
<body>
<p>
Select some text in this paragraph with the mouse.
Then press the highlight button.
All for Kibology. Kibology for all.
</p>
<form action="" onsubmit="retur n false;">
<p>
<input type="button" value="highligh t selection"
onclick="wrapSe lection('select ionHighlight'); ">
</p>
</form>
</body>
</html>

I still get an error on the surroundContent s call:

Error: uncaught exception: [Exception... "Index or size is negative or
greater than the allowed amount" code: "1" nsresult: "0x80530001
(NS_ERROR_DOM_I NDEX_SIZE_ERR)" location:
"http://localhost/javascript/test20040623.ht ml Line: 15"]

which I think is caused by the buggy Mozilla implementation of
Range/surroundContent s.

Jul 23 '05 #5
The only way I could figure around this on Netscape 7.1/Mozilla is to
get the range, delete it, create a new node, insert new node contents,
then use a regular expression search and replace to modify the
innerHTML content.

The is a bit of a quick-fix hack, and major drawback to this is that
getSelection only returns text, not the HTML, so assume I have lots of
line breaks or paragraph breaks within the HTML source, these vanish
when I insert the new node contents.

I seriously can't think of a better way around this....

var re = /(&lt;span)([^&]+)(&gt;)([^&]+)(&lt;\/span&gt;)/img;
var re1 = /(<span[^>]+><\/span>)/img;

var selection = window.getSelec tion();
var copytext = selection.toStr ing();

var newHTML = "<span style='//your style//'</span>";

rng = selection.getRa ngeAt(0);
selection.delet eFromDocument() ;
newRng = selection.getRa ngeAt(0);
replaceText = document.create TextNode(newHTM L);
newRng.insertNo de(replaceText)
var rp = window.document .body.innerHTML ;
rp = rp.replace(re, "<span$2>$4 </span>");
rp = rp.replace(re1, ""); // remove multiple redundant tags
window.document .body.innerHTML = rp;

Neil.
ne**@sentinel.f 9.co.uk (sentinel) wrote in message news:<f1******* *************** ****@posting.go ogle.com>...
I guess I'm going to have to do a little experimentation ...

I need to construct something that will work on an OS X browser as well as IE5+.

Martin Honnen <ma*******@yaho o.de> wrote in message news:<40******@ olaf.komtel.net >...
sentinel wrote:
Do you know of a solution that will work on Netscape 7.1 ?


Netscape doesn't implement the document.select ion object that IE has,
nor the TextRange that IE has.
It has a buggy implementation of the W3C DOM Level 2 Range specification
documented at
http://www.w3.org/TR/DOM-Level-2-Tra...ge/ranges.html
which is connected to its selection object returned by
window.getSelec tion()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContent s:
http://www.w3.org/TR/DOM-Level-2-Tra...ge-Surrounding
to wrap some range content into a span element for instance but last
time I have tried that with Netscape 7 it failed due to bugs.

When I try the following with Mozilla 1.7 or FireFox 0.9

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>does surroundContent s work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelec tion != 'undefined') {
var selection = window.getSelec tion();
if (typeof selection.range Count != 'undefined' &&
selection.range Count > 0) {
var range = selection.getRa ngeAt(0);
if (typeof range.surroundC ontents != 'undefined') {
var span = document.create Element('span') ;
span.className = cssClassName;
range.surroundC ontents(span);
}
}
}
}
</script>
<style type="text/javascript">
.selectionHighl ight {
background-color: lightyellow;
}
</style>
</head>
<body>
<p>
Select some text in this paragraph with the mouse.
Then press the highlight button.
All for Kibology. Kibology for all.
</p>
<form action="" onsubmit="retur n false;">
<p>
<input type="button" value="highligh t selection"
onclick="wrapSe lection('select ionHighlight'); ">
</p>
</form>
</body>
</html>

I still get an error on the surroundContent s call:

Error: uncaught exception: [Exception... "Index or size is negative or
greater than the allowed amount" code: "1" nsresult: "0x80530001
(NS_ERROR_DOM_I NDEX_SIZE_ERR)" location:
"http://localhost/javascript/test20040623.ht ml Line: 15"]

which I think is caused by the buggy Mozilla implementation of
Range/surroundContent s.

Jul 23 '05 #6
sentinel wrote:
Thanks for that. Do you know of a solution that will work on Netscape 7.1 ?
The Gecko DOM as of Netscape 7.1, among other UAs, provides
`selectionStart ' and `selectionEnd' properties for HTMLInputElemen t
and HTMLTextAreaEle ment objects:

function replaceSelectio n_Gecko(o, s)
{
var s2 = o.value;
o.value = s2.substring(0, o.selectionStar t)
+ s + s2.substr(o.sel ectionEnd + 1);
}

replaceSelectio n_Gecko(documen t.forms[0].elements['myInput'], "foobar");
[...]


Please do not top-post, see <http://jibbering.com/faq/>.
PointedEars
Jul 23 '05 #7

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

Similar topics

15
2475
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB code: Sub Change(ByVal x, ByRef y) x = x+1 y = y+1 End Sub
33
2810
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool write_file(std::string name, const std::string &s); void find_replace(std::string &s, std::string first, std::string second); bool find_replace_file(std::string name, std::string first, std::string second) {
1
3027
by: gm1974 | last post by:
Hi all, in a DHTML Editing control (MSIE), I need to "extend" the caret to the nearest text character in order to obtain a text selection (and than a textRange object with the createRange method). This is needed because I have a set of functions doing operations within the edit control's rich text. But all the functions need to start from a selected text (for example, adding a table row). My aim is to get the things work also if no...
3
1418
by: ocomet | last post by:
Hi Everyone.. I want to know how can I use regular expression in TextRange Object. I wrote specific word highlighting code and it is working now. but this is static word highlighting. I want to emplement dynamic word highlighting using regular expression..
1
1114
by: Steve | last post by:
Hi, Person A has $60,000 person B has $70,000 and this sentence now ends. If a user clicks on the "000" of the $60,000, is there any way to associate that with the entire monetary unit $60,000? TIA,
3
1498
by: Chris Bingham | last post by:
Hi, I'm learning VB.Net at the moment, and while I'm doing it I'm writing a couple of programs for work! They all work with the same Access Database, but I'm having a problem with modifying existing rows in the database. In this case I have a button that needs to enter the current time into the database as part of an existing record, here's the code I've got so far;
24
2915
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
0
7936
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
8241
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...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
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
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1203
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.