473,321 Members | 1,748 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,321 software developers and data experts.

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 10786


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="background-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*******@yahoo.de> wrote in message news:<40********@olaf.komtel.net>...
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="background-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.selection 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.getSelection()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContents:
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 surroundContents work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelection != 'undefined') {
var selection = window.getSelection();
if (typeof selection.rangeCount != 'undefined' &&
selection.rangeCount > 0) {
var range = selection.getRangeAt(0);
if (typeof range.surroundContents != 'undefined') {
var span = document.createElement('span');
span.className = cssClassName;
range.surroundContents(span);
}
}
}
}
</script>
<style type="text/javascript">
..selectionHighlight {
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="return false;">
<p>
<input type="button" value="highlight selection"
onclick="wrapSelection('selectionHighlight');">
</p>
</form>
</body>
</html>

I still get an error on the surroundContents call:

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

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

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*******@yahoo.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.selection 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.getSelection()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContents:
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 surroundContents work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelection != 'undefined') {
var selection = window.getSelection();
if (typeof selection.rangeCount != 'undefined' &&
selection.rangeCount > 0) {
var range = selection.getRangeAt(0);
if (typeof range.surroundContents != 'undefined') {
var span = document.createElement('span');
span.className = cssClassName;
range.surroundContents(span);
}
}
}
}
</script>
<style type="text/javascript">
.selectionHighlight {
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="return false;">
<p>
<input type="button" value="highlight selection"
onclick="wrapSelection('selectionHighlight');">
</p>
</form>
</body>
</html>

I still get an error on the surroundContents call:

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

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

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.getSelection();
var copytext = selection.toString();

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

rng = selection.getRangeAt(0);
selection.deleteFromDocument();
newRng = selection.getRangeAt(0);
replaceText = document.createTextNode(newHTML);
newRng.insertNode(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.f9.co.uk (sentinel) wrote in message news:<f1**************************@posting.google. 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*******@yahoo.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.selection 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.getSelection()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContents:
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 surroundContents work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelection != 'undefined') {
var selection = window.getSelection();
if (typeof selection.rangeCount != 'undefined' &&
selection.rangeCount > 0) {
var range = selection.getRangeAt(0);
if (typeof range.surroundContents != 'undefined') {
var span = document.createElement('span');
span.className = cssClassName;
range.surroundContents(span);
}
}
}
}
</script>
<style type="text/javascript">
.selectionHighlight {
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="return false;">
<p>
<input type="button" value="highlight selection"
onclick="wrapSelection('selectionHighlight');">
</p>
</form>
</body>
</html>

I still get an error on the surroundContents call:

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

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

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 HTMLInputElement
and HTMLTextAreaElement objects:

function replaceSelection_Gecko(o, s)
{
var s2 = o.value;
o.value = s2.substring(0, o.selectionStart)
+ s + s2.substr(o.selectionEnd + 1);
}

replaceSelection_Gecko(document.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
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...
33
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...
1
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...
3
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...
1
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...
3
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...
24
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.