delphipascalmshtmlihtmldocument2

how to get an IHTMLElementCollection obj which composed of several IHTMLElements?


guys: I got a problem about "how to get an IHTMLElementCollection obj which composed of several IHTMLElements" in object-pascal programming , my codes below:

function TExDomUtils.GetElementsByClassName(vDoc:IHTMLDocument3; strClassName:string):IHTMLElementCollection;
var
  vElementsAll : IHTMLElementCollection;
  vElementsRet : IHTMLElementCollection;
  vElement : IHTMLElement;
  docTmp : IHTMLDocument2;
  I ,J: Integer;
begin
  J := 0;
  vElementsAll := vDoc.getElementsByTagName('*');
  for I:=0 to vElementsAll.length - 1 do
  begin
    vElement := vElementsAll.item(I,0) as IHTMLElement;
    if vElement.getAttribute('class',0) = strClassName then
    begin
      // how to get an IHTMLElementCollection obj which composed of several IHTMLElements?
      J := J + 1;
    end;

  end;

  Result := vElementsRet;
end;

Solution

  • You could simply create your own container class, such as TList<IHTMLElement> or an array of IHTMLElements:

    type
      THTMLElements = array of IHTMLElement;
    
    function GetElementsByClassName(ADoc: IDispatch; const strClassName: string): THTMLElements;
    var
      vDocument: IHTMLDocument2;
      vElementsAll: IHTMLElementCollection;
      vElement: IHTMLElement;
      I, ElementCount: Integer;
    begin
      Result := nil;
      ElementCount := 0;
      if not Supports(ADoc, IHTMLDocument2, vDocument) then
        raise Exception.Create('Invalid HTML document');
      vElementsAll := vDocument.all;
      SetLength(Result, vElementsAll.length); // set length to max elements
      for I := 0 to vElementsAll.length - 1 do
        if Supports(vElementsAll.item(I, EmptyParam), IHTMLElement, vElement) then
          if SameText(vElement.className, strClassName) then
          begin
            Result[ElementCount] := vElement;
            Inc(ElementCount);
          end;
      SetLength(Result, ElementCount); // adjust Result length
    end;
    

    Usage:

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      WebBrowser1.Navigate('http://stackoverflow.com/questions/14535755/how-to-get-an-ihtmlelementcollection-obj-which-composed-of-several-ihtmlelements');
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Elements: THTMLElements;
      I: Integer;
    begin
      // show Tags information for SO page:
      Elements := GetElementsByClassName(WebBrowser1.Document, 'post-tag');
      ShowMessage(IntToStr(Length(Elements)));
      for I := 0 to Length(Elements) - 1 do
        Memo1.Lines.Add(Elements[I].innerHTML + ':' + Elements[I].getAttribute('href', 0));
    end;
    

    The main problem with returning the result as IHTMLElementCollection, is that IHTMLElementCollection is created internally by IHTMLDocument, and I could not find any way of creating a new instance of IHTMLElementCollection and adding references of the elements to it e.g.:

    vElementsRet := CoHTMLElementCollection.Create as IHTMLElementCollection
    

    will result Class not registered exception.