问题
based on MSHTML documentation for IHTMLDocument2 I'm trying to write simple HTML parser. Unfortunately trying to set edit-mode fails, in other words resultState never gets 'complete' value so application hangs.
{$APPTYPE CONSOLE}
function ParseHtml(doc: TStringList): TStringList;
var
iHtml: IHTMLDocument2;
v: Variant;
msg: tagMSG;
begin
iHtml := CreateComObject(CLASS_HTMLDocument) as IHTMLDocument2;
Result := TStringList.Create;
try
try
iHtml.designMode := 'on';
while iHtml.readyState <> 'complete' do
PeekMessage(msg, 0, 0, 0, PM_NOREMOVE);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// above loop never finishes
v := VarArrayCreate([0, 0], varVariant);
v[0] := doc.Text;
iHtml.write( PSafeArray(TVarData(v).VArray) );
iHtml.designMode := 'off';
while iHtml.readyState <> 'complete' do
PeekMessage(msg, 0, 0, 0, PM_NOREMOVE);
// processing iHtml.body
...
except
...
end;
finally
...
end;
...
end;
begin
CoInitialize(nil);
...
CoUninitialize;
end.
Just curious why readyState property of IHTMLDocument2 interface is never set to 'complete', although based by official documentation it should ?
回答1:
The readyState
property is not being set to 'complete'
because you have not told the IHTMLDocument2
object to actually load a document yet. You have to load a document, even a blank one (ie: the 'about:blank'
URL), in order to affect the readyState
property, otherwise it remains at its initial value of 'uninitialized'
.
回答2:
No need to set designMode
to on
. no need to poll the readyState
either. it will be set to "complete"
as soon as you write
and close
the document:
program Test;
{$APPTYPE CONSOLE}
uses
SysUtils,
MSHTML,
ActiveX,
ComObj;
procedure DocumentFromString(Document: IHTMLDocument2; const S: WideString);
var
v: OleVariant;
begin
v := VarArrayCreate([0, 0], varVariant);
v[0] := S;
Document.Write(PSafeArray(TVarData(v).VArray));
Document.Close;
end;
var
Document: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
begin
CoInitialize(nil);
Document := CreateComObject(CLASS_HTMLDocument) as IHTMLDocument2;
DocumentFromString(Document, '<b>Hello</b>');
Writeln(string(Document.readyState));
// process the Document here
Elements := Document.all.tags('b') as IHTMLElementCollection;
Element := Elements.item(0, '') as IHTMLElement;
Writeln(string(Element.innerText));
Readln;
CoUninitialize;
end.
来源:https://stackoverflow.com/questions/8663167/trying-to-edit-ihtmldocument-hangs-an-application