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

enclose selection with html?

Hello everyone,

first my question in short - I am looking for a working replacement
of the following function for Mozilla (this was written for IE):

// encloses the selected area into starting and closing tag
// if no closing tag is given the starting tag will replace the selection
function encloseSelection(startingTag, closingTag)
{
editRange = editArea.document.selection.createRange();

if (closingTag) {
editRange.pasteHTML(startingTag+editRange.htmlText +closingTag);
return;
}

//editRange.collapse(false); // move insertion point to end of text range (append tag)
editRange.pasteHTML(startingTag); // replace selection with startingTag
}

Longer version:
I am working on making an older "WYSIWYMG"-editor (written for IE)
Mozilla-compatible. Everything works fine so far, but I am not able
to figure out a solution to the above problem.

I'd like to enclose a selected text in one of the famous
"designMode=on" iframes with HTML-strings. I've tried numerous
ways - by now without luck.

Perhaps anyone here has a solution or hint that can help me?

Balu

Jul 23 '05 #1
6 1993
See the answers to the thread "Making a MS jscript ECMA/DOM compliant" a
little earlier and http://www.mozilla.org/editor/ie2midas.html
Jul 23 '05 #2
Hello Paul,

* Paul R <no*********@email.address>:
See the answers to the thread "Making a MS jscript ECMA/DOM compliant" a
little earlier and http://www.mozilla.org/editor/ie2midas.html


None of those helped :-(. Migrating most of the editor functionality
was not too difficult - especially with the help from the ie2midas
document, but I am not able to figure out how to solve the problem I
described in my last post.

The thread you mention is describing how to do things in a textarea
(which has a .value, etc.). I've seen other examples like that, but
was not able to modify them so that it'll work with an editable
iframe. I guess the textarea is a lot easier, because you don't have to
watch for nodetypes, etc. - everything in there is text?

The midasdemo itself has a function to replace the selection with a
different node (or insert it, if nothing is selected) -
"insertNodeAtSelection()", but I am stuck there too as my task to
enclose it and not replace it. I also need to be able to simply put
html around it - I don't have a "node", but only HTML (I know there
is createContextualFragment() - but that did not help me either by
now).

Perhaps I am just not seeing the light anymore since I am working
too long on this.

Balu
Jul 23 '05 #3
A little to my surprise, the following appears to work:

function encloseSelectionMoz(startingTag, closingTag)
{
var r;
var editorWin = document.getElementById("editarea").contentWindow;
var editorDoc = editorWin.document;

try{ r = editorWin.getSelection().getRangeAt(0);}
catch(e)
{
alert("no selection!");
return 0;
}

// can't use r.surroundContents(); mozilla bug #135928

var frag = r.extractContents();
// rough and ready extraction of tag name! :
var newnode = editorDoc.createElement(startingTag.substring(1,
startingTag.length - 1));
r.insertNode(newnode);
newnode.appendChild(frag);
}

The Gecko DOM Reference at
http://www.mozilla.org/docs/dom/domr..._shortTOC.html has some more
information on ranges.
Jul 23 '05 #4
Thanks Paul,

* Paul R <no*********@email.address>:
A little to my surprise, the following appears to work:

function encloseSelectionMoz(startingTag, closingTag)
{ .... }
Looks great, but you are not using closingTag anywhere :-). Also my
problem is not to add a HTML tag around the selection, but a HTML
"phrase" - e.g.

<span style="color: #f00;">&lt;!-- if --&gt;</span>
SELECTION
<span style="color: #f00;">&lt;!-- /if --&gt;</span>

This <!-- if --> is some kind of marker or makro for the parser of
the edited text. And those markers are marked red, so the user
can see them more easily...
The Gecko DOM Reference at
http://www.mozilla.org/docs/dom/domr..._shortTOC.html has some more
information on ranges.


This is my browsers homepage for the last 3-4 days, but I'll
probably don't get it anymore. After this time my brain just is
overloaded with ranges, selections, nodes, etc. :-]

I hope that this discussion will help me get my knowledge sorted
again...

Balu
Jul 23 '05 #5
You don't want to make life easy for yourself do you? :-)

No, I didn't polish this example; besides if you're using the DOM to add
nodes to a document, one would hope the end tag is the same as the start
tag!

But then you have a slightly different meaning for "enclosing the
selection". What you want to do in DOM terms is insert a node (SPAN
containing text) before and insert a node (another SPAN containing text)
after some content.

That means something like:
n = doc.createElement("span");
n.setAttribute("color", "#f00");
n.appendChild(doc.createTextNode("<!-- if -->"));
rng.insertNode(n);
rng.collapse(false);
n = doc.createElement("span");
n.setAttribute("color", "#f00");
n.appendChild(doc.createTextNode("<!-- /if -->"));
rng.insertNode(n);

I haven't tested this, but it's what I'd expect it to look like. Try not
to think about adding "text". If Mozilla were a text editor, it would
say so on the box ;-)
Jul 23 '05 #6
* Paul R <no*********@email.address>:
You don't want to make life easy for yourself do you? :-)
Probably a little, but it's really more that I simply "don't get it
anymore" after 4 days of puzzling around and trying all kinds of
methods.
No, I didn't polish this example; besides if you're using the DOM to add
nodes to a document, one would hope the end tag is the same as the start
tag!
Of course, but as I said the problem is not to enclose the selection
in a new tag, but with HTML texts.
But then you have a slightly different meaning for "enclosing the
selection". What you want to do in DOM terms is insert a node (SPAN
containing text) before and insert a node (another SPAN containing text)
after some content.
Yep - and that was exactly something I'd like to avoid. The
span-if-stuff was the easier example and some of the elements need
to be dynamically created, so it's hard to create the nodes when
everything you have is a HTML string. Because of this I probably
need to use createContextualFragment() which is a Gecko extension to
the ranges interface, but that keeps erroring on me...
That means something like:
n = doc.createElement("span");
n.setAttribute("color", "#f00");
n.appendChild(doc.createTextNode("<!-- if -->"));
rng.insertNode(n);
rng.collapse(false);
n = doc.createElement("span");
n.setAttribute("color", "#f00");
n.appendChild(doc.createTextNode("<!-- /if -->"));
rng.insertNode(n);

I haven't tested this, but it's what I'd expect it to look like. Try not
to think about adding "text". If Mozilla were a text editor, it would
say so on the box ;-)


If I'd knew the elements I need to create, I would have done this
in a similar way. I'd probably have used insertNode() wrong, because
my brain just told me that it adds the new element to the end... But
as I said in my first paragraph - the javascript section of my brain
is just shuffled at the moment :-].

Thanks for your help sorting that stuff again.

Balu
Jul 23 '05 #7

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

Similar topics

1
by: Jean Biver | last post by:
Dear readers: I found a web page with 2 selection lists in a form. The contents of the second selection list are updated based on the entry in the first selection list. For example: if the...
8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
3
kendall
by: kendall | last post by:
I'm currently working on a PHP CMS for my school that uses simple forms to update the pages and database. To get it out in use, it will have to be usable by people who don't know that is bold, and...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
7
by: robtyketto | last post by:
Greetings, I'm slowly building up code to do the following:- Display TWO selection option boxes (cascading). If the FIRST selection option box changes then reload the jsp using onchange...
6
by: liketofindoutwhy | last post by:
There is a link that encloses a span (or a div), but the link won't work in IE 7 (clicking on video image works, but not on the play button), while it works well in Firefox 2 and 3, and Safari 3....
2
by: vanditnagar | last post by:
Hi, I am facing the difficulty in the selecting a text on the browser . And highlighting the text. I am using the window.getSelection() to get the object and after...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.