DOMHelperUtilities.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #import "DOMHelperUtilities.h"
  2. #import "Element.h"
  3. #import "NodeList.h"
  4. #import "NodeList+Mutable.h" // needed for access to underlying array, because SVG doesnt specify how lists are made mutable
  5. @implementation DOMHelperUtilities
  6. /*! This useful method provides both the DOM level 1 and the DOM level 2 implementations of searching the tree for a node - because THEY ARE DIFFERENT
  7. yet very similar
  8. */
  9. +(void) privateGetElementsByName:(NSString*) name inNamespace:(NSString*) namespaceURI childrenOfElement:(Node*) parent addToList:(NodeList*) accumulator
  10. {
  11. /** According to spec, this is only valid for ELEMENT nodes */
  12. if( [parent isKindOfClass:[Element class]] )
  13. {
  14. if( namespaceURI != nil && ! [parent.namespaceURI isEqualToString:namespaceURI] )
  15. {
  16. // skip
  17. }
  18. else
  19. {
  20. Element* parentAsElement = (Element*) parent;
  21. /** According to spec, "tag name" for an Element is the value of its .nodeName property; that means SOMETIMES its a qualified name! */
  22. BOOL includeThisNode = FALSE;
  23. if( [name isEqualToString:@"*"] )
  24. includeThisNode = TRUE;
  25. if( !includeThisNode )
  26. {
  27. if( namespaceURI == nil ) // No namespace? then do a qualified compare
  28. {
  29. includeThisNode = [parentAsElement.tagName isEqualToString:name];
  30. }
  31. else // namespace? then do an UNqualified compare
  32. {
  33. includeThisNode = [parentAsElement.localName isEqualToString:name];
  34. }
  35. }
  36. if( includeThisNode )
  37. {
  38. [accumulator.internalArray addObject:parent];
  39. }
  40. }
  41. }
  42. for( Node* childNode in parent.childNodes )
  43. {
  44. [self privateGetElementsByName:name inNamespace:namespaceURI childrenOfElement:childNode addToList:accumulator];
  45. }
  46. }
  47. +(Element*) privateGetElementById:(NSString*) idValue childrenOfElement:(Node*) parent
  48. {
  49. /** According to spec, this is only valid for ELEMENT nodes */
  50. if( [parent isKindOfClass:[Element class]] )
  51. {
  52. Element* parentAsElement = (Element*) parent;
  53. if( [[parentAsElement getAttribute:@"id"] isEqualToString:idValue])
  54. return parentAsElement;
  55. #if DEBUG_DOM_MATCH_ELEMENTS_IDS_AND_NAMES
  56. else
  57. {
  58. SVGKitLogVerbose(@"parent <%@ id='%@'..> does not match id='%@'", parentAsElement.nodeName, [parentAsElement getAttribute:@"id"], idValue );
  59. SVGKitLogVerbose(@"parent <%@ id='%@'..> has %li child nodes = %@", parentAsElement.nodeName, [parentAsElement getAttribute:@"id"], parent.childNodes.length, parent.childNodes );
  60. }
  61. #endif
  62. }
  63. for( Node* childNode in parent.childNodes )
  64. {
  65. Element* childResult = [self privateGetElementById:idValue childrenOfElement:childNode];
  66. if( childResult != nil )
  67. return childResult;
  68. }
  69. return nil;
  70. }
  71. @end