RE: (repost): Very tough question, about HTML manipulation

From: Julian Turner (Turner_at_discussions.microsoft.com)
Date: 07/13/04


Date: Tue, 13 Jul 2004 06:15:04 -0700

I have some thoughts for you, but this is not a complete answer. It is based on something I am working on myself along similar lines.

I assume that all you want to do is change to upper case.

Important facts:-

- A TextRange can be as small as 1 character, or even nothing (i.e. a cursor insertion point).

- You can have multiple text ranges over the same text.

- setting just one character on the "text" property of the range, I believe would not affect the mark-up

Suggested method

1. get the TextRange from the selection object. Let us call this the "Cursor", which we will use to go character by character.

2. Create a duplicate of the TextRange using the duplicate() method. You will use the duplicate to keep a "Reference" of the start and end points of the range.

3. Collapse the Cursor range, using the collapse() method, to the start of the Reference range. This collapses it so the start and end points are the same.

3. Expand the Cursor by one character, using the expand() method, and get the character, i.e. Cursor.text.

4. Is the character is an a-z character? E.g. in Regular Expression terms: /[a-z]/gi.test(Cursor.text).

5. If so, get the character, convert it to upper case (toUpperCase()), and save it back in Cursor.text. This is the key point, setting just one character on the text property, I believe would not affect the mark-up.

6. Collapse the Cursor to the end of the character, using collapse.

7. Compare the end point of the Cursor to the end point of the Reference using compareStartPoints method (I think that is what it is called) which allows you to compare the start and end points of two ranges.

8. If the Cursor is not at the end of the Reference, go to 3.

It is cumbersome, but it is granularity should help.

"Welman Jordan" wrote:

> Hello,
>
> I've posted this thread on IE.scripting for a month, but
> nobody answered me, thus I reposted it here, I hope
> somebody can help.
>
>
> I have got a very darn tough question that I can not solve
> by myself. Any JavaScript gurus please help!
>
> Here's the problem:
>
> I wanna replace some user selected text in a web page,
> however, after the replacement, the format of the selection
> should be preserved if possible.
> -------------------------------------------
> For example, here's a piece of code:
>
> <P><B>Bold [SELECTION_BEGIN]text</B></P>
> <UL><LI>list item: item [SELECTION_END]things</LI></UL>
>
> If it is rendered on a browser, it should looks like this way:
> a paragraph, bold text,
> and then a bulleted list, an item.
>
> The selection point begins in the middle of the bold text,
> and spans into the list item.
> If I copy the selected text, I will get:
> text
> list item: item
>
> Now, I wanna do something with the script, replace the
> selected text from "text", and "list item: item" to uppercase
> "TEXT", and "LIST ITEM: ITEM". However, the "text"
> should still be bold, and the "list item: item" should still
> stay in the bulleted list. How to do so?
>
> I know that on IE5, windows.selection.createRange ()
> might help. However, when I finish modifying the selected
> text, I don't know how to put them back. Using pasteHTML
> won't work, since there'll be an extra <P> before "TEXT"
> and an extra </LI> after "ITEM". Thus the "Bold TEXT" would
> be broken into two lines.
>
> Somebody can think of another way else?
>
> W. Jordan
>
>
>
>



Relevant Pages

  • Re: Identify page on which a bookmark is located
    ... "Collapse" produces nothing in VBA Help). ... for my GoTo Bookmark code. ... Collapses a range or selection to the starting or ending position. ... character by using the MoveEnd method after the range is collapsed, ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... previous character or next character and there are none at the beginning and ... "Collapse" produces nothing in VBA Help). ... for my GoTo Bookmark code. ... Collapses a range or selection to the starting or ending position. ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... "Collapse" produces nothing in VBA Help). ... for my GoTo Bookmark code. ... Collapses a range or selection to the starting or ending position. ... Returns or sets the starting character position of a selection, range, ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... Here is what VBA Help says about the Collapse method: ... Collapses a range or selection to the starting or ending position. ... character by using the MoveEnd method after the range is collapsed, ... In your code, in order to use Ranges and not have to use the Goto Bookmark, ...
    (microsoft.public.word.vba.general)
  • Re: LostFocus Issue
    ... Note that Chris' previous post about 'backspace' only works if the selection ... is at the end of the edit field. ... Move back one character, delete the character, handle the ... > When I press on one of my keypad buttons, the cursor is not visible ...
    (microsoft.public.dotnet.framework.compactframework)