基于 IHTMLDocument2 的 MSHTML 文档,我正在尝试编写简单的 HTML 解析器。不幸的是,尝试设置编辑模式失败,换句话说,resultState永远不会获得“完整”值,因此应用程序挂起。
{$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.
只是好奇为什么IHTMLDocument2 接口的 readyState 属性从未设置为“完成”,尽管它应该基于官方文档?