How do I know if a tag contains a certain attribute?

Normally, if you're using the OnAttribute event, this is not an issue. However, if you're reading tags via the OnTag event, then ATagParser v2.04+ has a new pair of functions to help with this problem.

·TagHasAttribute  
·TagHasAttributeAndValue  

TagHasAttribute is used when you just want to know if the tag contains a specific attribute, but don't care about its value. TagHasAttributeAndValue allows you to check for a specific tag name and a specific value.

procedure TForm1.ATagParserTag(Sender: TObject; Tag: TTagElement;
  var Abort: Boolean);
begin
  If Tag.ElementType In [etComplexTag, etXHTMLTag] Then
  Begin { we're only interested in tags with attributes }
    If ATagParser.TagHasAttributeAndValue('href', 'http://www.yahoo.com') Then
      ShowMessage('This reference is pointing to Yahoo!')
    Else If ATagParser.TagHasAttribute('width') Then
      ShowMessage('This tag has a width attribute');
  End;
end;

 
The functions above are located in ATagParser and NOT in the TagElement. This was intentional. Since the TTagElement is persistent, and may find itself stored in a list, stack or tree, it shoudn't store the overhead from the functions


Copyright 2000 - 2006 John E McTaggart - All rights reserved worldwide