Skip to content

sax_html does not build ID hash table

The sax_html parser does not create and fill the ID hashmap of the returned DOM document. Thus methods like TDOMDocument.GetElementByID() do neither return any result nor indicate the inability to work with an appropriate exception.

Also there is no provided method for rebuilding the index which I implemented like this:

procedure TDOMDocumentHelper.RebuildIDsOfElement(aRoot: TDOMElement);
var
  i: Integer;
  AttribNode: TDOMNode;
  id: DOMString;
  Item: PHashItem;
begin
  for i := 0 to aRoot.Attributes.Length - 1 do
  begin
    AttribNode := aRoot.Attributes.Item[i];
    if LowerCase(AttribNode.NodeName) = 'id' then
    begin
      id := AttribNode.TextContent;
      Item := FIDList.FindOrAdd(PWideChar(id), Length(id));
      Item^.Data := aRoot;
      break;
    end;
  end;

  for i := 0 to aRoot.ChildNodes.Count - 1 do
  begin
    if aroot.ChildNodes[i] is TDOMElement then
      RebuildIDsOfElement(TDOMElement(aroot.ChildNodes[i]));
  end;
end;

procedure TDOMDocumentHelper.RebuildIDList;
begin
  if not Assigned(FIDList) then
    FIDList := THashTable.Create(256, False);
  FIDList.Clear;
  RebuildIDsOfElement(Self.DocumentElement);
end;    
Edited by Simon Ameis
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information