HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
Insert a span tag
Posted by: Tim Bushell (---.range86-142.btcentralplus.com)
Date: Tuesday, 28-Nov-2006, 12:52:40

Hello

Firstly, thanks, it's a great control that has made my project so much easier.

I have a question which I know has been asked before, but I've not been able to solve the problem from any of the hints provided.

Anyway, what I want to accomplish is fairly simple. My project's resulting HTML - as edited by the control - is going to be rendered by a Flash engine and there is a requirement for span tags defining classes. So I simply want to take the SelectedText (which I have been able to get from other code on this site), and wrap it inside a "<span class="classname">" tag.

Unfortunately this causes a problem because, whilst initially working with the selected piece of text (when it doesn't have HTML already), if I then select for a different class on that text then my HTMLEditor simply wraps the new span around the existing span.

I thought perhaps I could capture the selectedText and fix it like this (forgive the VB, CS people)

###########################
Dim fix As String = MyEditor.SelectedHTML
'//fix = <span class="ONE">Hello</span>

fix = FunctionToRemoveExistingSpans(fix)
'//fix = Hello

MyEditor.SelectedHTML = "<span class="TWO"> & fix & "</span>"
################################

Code logically looks fine when you read it, but I'm left with the following HTML
<span class="TWO"><span class="ONE">Hello</span><span>"

Now I'm not necessarily after a fix for this problem. if there is any other method I could use to add my span tag, then I'm not fussy!

Hope someone can point me in the right direction (or carry me over the line - again I'm not fussy).

Tim

Re: Insert a span tag
Posted by: Tim (---.gotadsl.co.uk)
Date: Thursday, 30-Nov-2006, 09:12:04

This is inherently quite tricky. Nested spans are legitimate HTML. I think you could successfully remove the existing span in the case you cite, but there are more complex possibilities - eg. the user selects only part of an existing span, or even worse starts the selection within one span and ends in another (if you then wrap a span round the selection it will NOT be valid).

Tim

Re: Insert a span tag
Posted by: Tim Bushell (---.range86-145.btcentralplus.com)
Date: Thursday, 07-Dec-2006, 15:19:58

Thanks for taking a look at this Tim.

"This is inherently quite tricky. " That's good to hear! I'm not a complete idiot then... LOL.


"I think you could successfully remove the existing span in the case you cite" Really? How? This is my SelectedHTML property which I adapted from the HTMLeditor adaption by Darwin. I haven't gone so far as add it to the original HTML control source, but have simply pointed it to my HTMLEditor control called "scoaObjectEditor"



Public Property SelectedHTML() As String
Get
If CType(Me.scoaObjectEditor.HtmlDocument2, HTMLAPI.IHTMLDocument2).Selection Is Nothing Then
Return String.Empty
End If
Dim tr As HTMLAPI.IHTMLTxtRange = CType(CType(Me.scoaObjectEditor.HtmlDocument2, HTMLAPI.IHTMLDocument2).Selection.CreateRange(), HTMLAPI.IHTMLTxtRange)


Return tr.HTMLText
End Get
Set(ByVal value As String)

If CType(Me.scoaObjectEditor.HtmlDocument2, HTMLAPI.IHTMLDocument2).Selection Is Nothing Then
Return
End If
Dim tr As HTMLAPI.IHTMLTxtRange = CType(CType(Me.scoaObjectEditor.HtmlDocument2, HTMLAPI.IHTMLDocument2).Selection.CreateRange(), HTMLAPI.IHTMLTxtRange)

Dim theString As String = value

'If it's full html, strip out the body only.
Dim Pos As Integer = theString.ToUpper().IndexOf("<BODY")
If Pos > -1 Then
Pos = theString.ToUpper().IndexOf(">", Pos)

If Pos > -1 Then
theString = theString.Substring((Pos + 1))

Pos = theString.ToUpper().IndexOf("</BODY")

If Pos > -1 Then
theString = theString.Substring(0, Pos)
End If
End If
End If

Dim markup As HTMLAPI.IMarkupServices = CType(Me.scoaObjectEditor.HtmlDocument2, HTMLAPI.IMarkupServices)
Dim pStart As HTMLAPI.IMarkupPointer = Nothing
Dim pEnd As HTMLAPI.IMarkupPointer = Nothing
markup.CreateMarkupPointer(pStart)
markup.CreateMarkupPointer(pEnd)
markup.MovePointersToRange(tr, pStart, pEnd)

'int BodyLength = ConvertEx.ToString(Body).Length;
tr.PasteHTML(value)


markup.MoveRangeToPointers(pStart, pEnd, tr)
tr.Select()
End Set
End Property


What's perhaps going wrong here is that the Get and Set parts of this property are not interdependant - not, at least in the same way the private member of an Integer property would be.

Therefore selectedHTML = Replace(selectedHTML , "x", "y") does weird things because the SET part does not relate directly to the GET part in the Replace function. But I'm clueless as to what the particular issue is.

##########

"there are more complex possibilities " I agree. However those I can handle. All the HTML is heavily processed in order to meet the HTML requirements of the engine (the Flash developers have specified a small subset of HTML). Therefore I can clean any poor HTML during that process. In fact I could clean the double SPAN problem at this point too. My problem is that unless I can override the existing SPAN tag during the editing stage I will lose WYSIWYG, so the user won't see the colour changes, etc because the inner SPAN i wanted to replace the outer SPAN with overrides the formatting.

One idea I did have was to extract the
a) Store the selectedHTML as a string *
b) Store the GetDocumentSource as a string
c) Replace the selectedHTML in GetDocumentSource with the required span class *
b) Reload the document from amended GetDocumentSource.

Not pretty and *I'm going to have to find a way to "unique-e-fy" the selectedHTML first in case another string in the entire document shares the same pattern.

Would there be another way to handle tackle this problem?

Re: Insert a span tag
Posted by: gdminal (---.ctinets.com)
Date: Monday, 22-Jun-2009, 08:55:53

Treasure every moment that you have! And remember that time waits for no one. Yesterday is history. Tomorrow is a mystery. Today is a gift. Thatís why itís called the present!

Credit Card Terminal| Point of Sale.



Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
nmuJW
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.