Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select substring in html element
#1
Supposed you have an html element in the form :
Code:
Copy      Help
el=htm("BODY" "" "" hwnd 0 0 0x20)
Is it possible to select a substring in it in the way you can do it for a window (h), ie:
Code:
Copy      Help
SendMessage(h EM_SETSEL i i+length)
Many thanks in advance.
#2
Tested only with IE 9.
May fail with some pages/elements/arguments.

Member function Htm.SelectText
Code:
Copy      Help
function [iStart] [iLength]

;Selects text of this element.
;Error if fails.

;iStart - selection start offset.
;iLength - selection length. If omitted or 0, selects all to the end. If negative, selection will be empty.


MSHTML.IHTMLTxtRange r
MSHTML.IHTMLBodyElement b
MSHTML.IHTMLTextAreaElement ta
MSHTML.IHTMLInputElement in

_s=el.tagName
sel _s 1
,case "BODY" b=+el; r=b.createTextRange
,case "TEXTAREA" ta=+el; r=ta.createTextRange
,case "INPUT" in=+el; r=in.createTextRange; err ;;only for type=text. Fails if moveToElementText.

if !r
,b=el.document.body
,r=b.createTextRange
,r.moveToElementText(el)

if(iStart) r.moveStart("character" iStart)
if(iLength) r.collapse(TRUE); if(iLength>0) r.moveEnd("character" iLength)

r.select

err+ end _error
#3
Dear Gintaras,

Much Obliged, I am obliged, as always.

Regards
#4
Dear Gintaras,

Further to the above topic, I would kindly ask you for advice on the issue of getting the caret position, in analogy to
Quote:SendMessage(st EM_GETSEL 0 &j)

Best regards.
#5
Try to find something in stackoverflow. It seems there are no API for this, but maybe possible some dirty workaround.
#6
Thank you !

Dirty workaround :

Function tempf07
Code:
Copy      Help
Htm elm
int+ jOEF
int k
int+ g_newsearch

;..........................
if(g_newsearch)
,key CH
,jOEF=0

int length=rx.lenu
k=findrxu(st rx jOEF flags|8)
if(k<0) g_newsearch=1
;..........................
g_newsearch=0
elm.SelectText(k length)
jOEF=k+length
#7
I am sorry if I disturb you, but I have one more relevant question :

What is the best way to replace the text selected by the member function above : Htm.SelectText ?

Looking forward to your advice. Bets regards.
#8
Modify Htm.SelectText:
after line
r.select
insert line
r.pasteHTML(replacement)
#9
Excellent ! Many thanks.


Forum Jump:


Users browsing this thread: 6 Guest(s)