HTMLNode.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // HTMLNode.h
  3. // StackOverflow
  4. //
  5. // Created by Ben Reeves on 09/03/2010.
  6. // Copyright 2010 Ben Reeves. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <libxml/HTMLparser.h>
  10. #import "HTMLParser.h"
  11. @class HTMLParser;
  12. #define ParsingDepthUnlimited 0
  13. #define ParsingDepthSame -1
  14. #define ParsingDepth size_t
  15. typedef enum
  16. {
  17. HTMLHrefNode,
  18. HTMLTextNode,
  19. HTMLUnkownNode,
  20. HTMLCodeNode,
  21. HTMLSpanNode,
  22. HTMLPNode,
  23. HTMLLiNode,
  24. HTMLUlNode,
  25. HTMLImageNode,
  26. HTMLOlNode,
  27. HTMLStrongNode,
  28. HTMLPreNode,
  29. HTMLBlockQuoteNode,
  30. } HTMLNodeType;
  31. @interface HTMLNode : NSObject
  32. {
  33. @public
  34. xmlNode * _node;
  35. }
  36. //Init with a lib xml node (shouldn't need to be called manually)
  37. //Use [parser doc] to get the root Node
  38. -(id)initWithXMLNode:(xmlNode*)xmlNode;
  39. //Returns a single child of class
  40. -(HTMLNode*)findChildOfClass:(NSString*)className;
  41. //Returns all children of class
  42. -(NSArray*)findChildrenOfClass:(NSString*)className;
  43. //Finds a single child with a matching attribute
  44. //set allowPartial to match partial matches
  45. //e.g. <img src="http://www.google.com> [findChildWithAttribute:@"src" matchingName:"google.com" allowPartial:TRUE]
  46. -(HTMLNode*)findChildWithAttribute:(NSString*)attribute matchingName:(NSString*)className allowPartial:(BOOL)partial;
  47. //Finds all children with a matching attribute
  48. -(NSArray*)findChildrenWithAttribute:(NSString*)attribute matchingName:(NSString*)className allowPartial:(BOOL)partial;
  49. //Gets the attribute value matching tha name
  50. -(NSString*)getAttributeNamed:(NSString*)name;
  51. //Find childer with the specified tag name
  52. -(NSArray*)findChildTags:(NSString*)tagName;
  53. //Looks for a tag name e.g. "h3"
  54. -(HTMLNode*)findChildTag:(NSString*)tagName;
  55. //Returns the first child element
  56. -(HTMLNode*)firstChild;
  57. //Returns the plaintext contents of node
  58. -(NSString*)contents;
  59. //Returns the plaintext contents of this node + all children
  60. -(NSString*)allContents;
  61. //Returns the html contents of the node
  62. -(NSString*)rawContents;
  63. //Returns next sibling in tree
  64. -(HTMLNode*)nextSibling;
  65. //Returns previous sibling in tree
  66. -(HTMLNode*)previousSibling;
  67. //Returns the class name
  68. -(NSString*)className;
  69. //Returns the tag name
  70. -(NSString*)tagName;
  71. //Returns the parent
  72. -(HTMLNode*)parent;
  73. //Returns the first level of children
  74. -(NSArray*)children;
  75. //Returns the node type if know
  76. -(HTMLNodeType)nodetype;
  77. //C functions for minor performance increase in tight loops
  78. NSString * getAttributeNamed(xmlNode * node, const char * nameStr);
  79. void setAttributeNamed(xmlNode * node, const char * nameStr, const char * value);
  80. HTMLNodeType nodeType(xmlNode* node);
  81. NSString * allNodeContents(xmlNode*node);
  82. NSString * rawContentsOfNode(xmlNode * node);
  83. @end