473,472 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading surrounding UBB tags in textarea selection

Ik ben bezig met een eigen UBB editor. Als iemand aan het typen is,
zorgt CTRL-B voor een \-tag en nogmaals CTRL-B voor een \ tag.
Als je eerst een selectie maakt en dan CTRL-B drukt, wordt de selectie
ingesloten door \ en \, net als hier op GoT.

Wat ik nu probeer is, om de B-knop uit de toolbar te laten oplichten
als de cursor op een woord staat dat omgeven is door B-tags:

I'm trying to write my own UBB editor. If a user types CTRL-B, a
tag is inserted into the textarea. CTRL-B again inserts
. If CTRL-
B is pressed while text is selected, the selection is captured between
and tags.

What I'm trying now, is to enlighten the "B" button in the toolbar,
whenever the cursor is captured between [b] tags. If the cursor is
captured between [b] and [u] tags, both these buttons should lighten
up. In the example the cursor is stading over the word "bold":

+---+ +---+ +---+
| B*| | U | | I |
+---+ +---+ +---+
+-----------------------------------------------+
| This is bo|ld and this is |
| bold and underlined |
| |
| |
+-----------------------------------------------+

In short, it's the same toolbar behaviour as we all know it from, i.e.
Microsoft Word.

If the editor were an iframe, I could do something like (taken from
widgEditor source)
Expand|Select|Wrap|Line Numbers
  1. theSelection =
  2. theWidgEditor.theIframe.contentWindow.document.selection;
  3. theRange = theSelection.createRange();
  4. theParentNode = theRange.parentElement();
  5.  
  6. switch (theParentNode.nodeName.toLowerCase()) {
  7. case "a":
  8. theWidgEditor.theToolbar.setState("Link", "on");
  9. break;
  10. }
  11.  
But alas, it's a textarea we're dealing with and the tags are not HTML
but UBB, and can therefore not be found in the DOM. I have once
created a function which would remove the html bold tags from a
selection, if the selection was already bold and another "bold"
command would be given:

function selection_replace(type, text) {
switch (type) {
case 'bold':
var re = new RegExp('^<b[^>]*>(.*?)</b>$');
text = (!re.test(text)) ? '<b>'+ text +'</b>' : text.replace(re,
'$1');
break;
}
}

Perhaps part of the solution is here, although I don't think so
because I foresee problems when multiple tags are surrounding a
selection or cursor. Who could help me write a funcion that detects
which UBB tags the cursor in a textarea is captured between?

Your help would be greatly appreciated!

Feb 27 '07 #1
2 2852
My previous post was partially in Dutch, so here it goes again:

I'm trying to write my own UBB editor. If a user types CTRL-B, a
tag is inserted into the textarea. CTRL-B again inserts
. If CTRL-
B is pressed while text is selected, the selection is captured between
and tags.
What I'm trying now, is to enlighten the "B" button in the toolbar,
whenever the cursor is captured between [b] tags. If the cursor is
captured between [b] and [u] tags, both these buttons should lighten
up. In the example the cursor is stading over the word "bold":

+---+ +---+ +---+
| B*| | U | | I |
+---+ +---+ +---+
+-----------------------------------------------+
| This is bo|ld and this is |
| bold and underlined |
| |
| |
+-----------------------------------------------+

In short, it's the same toolbar behaviour as we all know it from,
i.e.
Microsoft Word. If the editor were an iframe, I could do something
like (taken from widgEditor source)

theWidgEditor.theIframe.contentWindow.document.sel ection;
theRange = theSelection.createRange();
theParentNode = theRange.parentElement();

switch (theParentNode.nodeName.toLowerCase()) {
case "a":
theWidgEditor.theToolbar.setState("Link", "on");
break;
}

But alas, it's a textarea we're dealing with and the tags are not HTML
but UBB, and can therefore not be found in the DOM. I have once
created a function which would remove the html bold tags from a
selection, if the selection was already bold and another "bold"
command would be given:

function selection_replace(type, text) {
switch (type) {
case 'bold':
var re = new RegExp('^<b[^>]*>(.*?)</b>$');
text = (!re.test(text)) ? '<b>'+ text +'</b>' : text.replace(re,
'$1');
break;
}
}

Perhaps part of the solution is here, although I don't think so
because I foresee problems when multiple tags are surrounding a
selection or cursor. Who could help me write a funcion that detects
which UBB tags the cursor in a textarea is captured between?

Any help would be greatly appreciated!

Feb 28 '07 #2
de**************@gmail.com wrote:

Hi Dennis,
What I'm trying now, is to enlighten the "B" button in the toolbar,
whenever the cursor is captured between [b] tags. If the cursor is
captured between [b] and [u] tags, both these buttons should lighten
up.
To get information about cursor/caret position in a textarea you need to
manipulate "ranges", i.e. objects which represent text ranges. Not
surprisingly, there are two scriptable models available, the IE one and
the W3C one (sometimes augmented with custom methods).

IE's model has been supported by Internet Explorer since version 4 I
think, however W3C's ranges may not be supported everywhere - check
accordingly.

<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html>
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp>

<snip>
Who could help me write a funcion that detects
which UBB tags the cursor in a textarea is captured between?
See below for some quick example; tested in IE7, Firefox 2 and Opera 9.
IE has problems though, as it does not seem to properly fire left/right
arrow key events in the textarea (or I may be missing something, I
haven't time to investigate now).

---
<style type="text/css".depressed { border:1px inset; } </style>

<form action="#">
<input type="button" value="B" name="B">
<input type="button" value="U" name="U">
<input type="button" value="I" name="I">
<textarea onkeyup="foo(this)" onclick="foo(this)"></textarea>
</form>

<script type="text/javascript">
function foo(textarea){
for(var tags=["B", "U", "I"], ii=0, a; ii<tags.length; ii++) {
a=textarea.
value.
substr(0, getCaretPos(textarea)).
split("[\/"+tags[ii]+"]");

textarea.form.elements[tags[ii]].className=(
a[a.length-1].indexOf("["+tags[ii]+"]")!=-1
) ? "depressed" : "";
}

function getCaretPos(el) {
var rng, ii=-1;
if(typeof el.selectionStart=="number") {
ii=el.selectionStart;
} else if (document.selection && el.createTextRange){
rng=document.selection.createRange();
rng.collapse(true);
rng.moveStart("character", -el.value.length);
ii=rng.text.length;
}
return ii;
}
}
</script>
---
Regards,
Elegie.
Feb 28 '07 #3

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

Similar topics

3
by: Krzysztof Kujawski | last post by:
Hi Is there posibility to (and how to do this): a) add a string in the current cursor position? b) add string before and after selection in textarea e.g. selection c) if I put with...
2
by: Jörg Dembski | last post by:
Betreff: UBB-Style tags with IE and Mozilla Von: Jörg Dembski <gymlet@gmx.de> Gruppen: comp.lang.javascript Datum: Thu, 08 Jul 2004 17:49:29 +0200 Hey, ...
2
by: Mark Szlazak | last post by:
The following code fails in Firefox to get at selected text in the right-side textarea. Any help would be appreciated. <html> <head> <script> var agt = navigator.userAgent.toLowerCase();...
5
by: aqualizard | last post by:
I have searched and searched and searched... Can someone please tell me how (or "if" it is even possible) to change selected text in a textarea? Specifically, say a user has highlighted "ppl"...
1
by: Gernot Frisch | last post by:
Hi, I want to be able to select 3 rows of a textarea, and when pressing "Tab/shift Tab" indent the source code edited. Can I do this with JavaScript? -- -Gernot int main(int argc, char**...
4
by: Keith Bentrup | last post by:
Hi all, I wrote a simple search function to find text in a textarea where not all the text is visible (ie. the text box displays 10 lines but there may be more than 1000 lines to search). I can...
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...
1
by: beary | last post by:
I need to populate a textarea from drop down box. There are a few "solutions" out there, but they seem to remove the first choice from the textarea when a second selection is made. So I need... 1)...
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
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,...
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.