Element.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #import "Element.h"
  2. #import "NamedNodeMap.h"
  3. #import "DOMHelperUtilities.h"
  4. @interface Element()
  5. @property(nonatomic,strong,readwrite) NSString* tagName;
  6. @end
  7. @implementation Element
  8. @synthesize tagName;
  9. - (id)initWithLocalName:(NSString*) n attributes:(NSMutableDictionary*) attributes {
  10. self = [super initType:DOMNodeType_ELEMENT_NODE name:n];
  11. if (self) {
  12. self.tagName = n;
  13. for( NSString* attributeName in attributes.allKeys )
  14. {
  15. [self setAttribute:attributeName value:[attributes objectForKey:attributeName]];
  16. }
  17. }
  18. return self;
  19. }
  20. - (id)initWithQualifiedName:(NSString*) n inNameSpaceURI:(NSString*) nsURI attributes:(NSMutableDictionary *)attributes
  21. {
  22. self = [super initType:DOMNodeType_ELEMENT_NODE name:n inNamespace:nsURI];
  23. if (self) {
  24. self.tagName = n;
  25. for( Attr* attribute in attributes.allValues )
  26. {
  27. [self.attributes setNamedItemNS:attribute inNodeNamespace:nsURI];
  28. }
  29. }
  30. return self;
  31. }
  32. -(NSString*) getAttribute:(NSString*) name
  33. {
  34. /**
  35. WARNING: the definition in the spec WILL CORRUPT unsuspecting Objective-C code (including a lot of the original SVGKit code!).
  36. The spec - instead of defining 'nil' - defines "" (empty string) as the
  37. correct response.
  38. But in most of the modern, C-based, (non-scripting) languages, "" means 0.
  39. Very dangerous!
  40. */
  41. Attr* result = (Attr*) [self.attributes getNamedItem:name];
  42. if( result == nil || result.value == nil )
  43. return @""; // according to spec
  44. else
  45. return result.value;
  46. }
  47. -(void) setAttribute:(NSString*) name value:(NSString*) value
  48. {
  49. Attr* att = [[Attr alloc] initWithName:name value:value];
  50. [self.attributes setNamedItem:att];
  51. }
  52. -(void) removeAttribute:(NSString*) name
  53. {
  54. [self.attributes removeNamedItem:name];
  55. NSAssert( FALSE, @"Not fully implemented. Spec says: If the removed attribute is known to have a default value, an attribute immediately appears containing the default value as well as the corresponding namespace URI, local name, and prefix when applicable." );
  56. }
  57. -(Attr*) getAttributeNode:(NSString*) name
  58. {
  59. return (Attr*) [self.attributes getNamedItem:name];
  60. }
  61. -(Attr*) setAttributeNode:(Attr*) newAttr
  62. {
  63. Attr* oldAtt = (Attr*) [self.attributes getNamedItem:newAttr.nodeName];
  64. [self.attributes setNamedItem:newAttr];
  65. return oldAtt;
  66. }
  67. -(Attr*) removeAttributeNode:(Attr*) oldAttr
  68. {
  69. [self.attributes removeNamedItem:oldAttr.nodeName];
  70. NSAssert( FALSE, @"Not fully implemented. Spec: If the removed Attr has a default value it is immediately replaced. The replacing attribute has the same namespace URI and local name, as well as the original prefix, when applicable. " );
  71. return oldAttr;
  72. }
  73. -(NodeList*) getElementsByTagName:(NSString*) name
  74. {
  75. NodeList* accumulator = [[NodeList alloc] init];
  76. [DOMHelperUtilities privateGetElementsByName:name inNamespace:nil childrenOfElement:self addToList:accumulator];
  77. return accumulator;
  78. }
  79. // Introduced in DOM Level 2:
  80. -(NSString*) getAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
  81. {
  82. Attr* result = (Attr*) [self.attributes getNamedItemNS:namespaceURI localName:localName];
  83. if( result == nil || result.value == nil )
  84. return @""; // according to spec
  85. else
  86. return result.value;
  87. }
  88. // Introduced in DOM Level 2:
  89. -(void) setAttributeNS:(NSString*) namespaceURI qualifiedName:(NSString*) qualifiedName value:(NSString*) value
  90. {
  91. Attr* att = [[Attr alloc] initWithNamespace:namespaceURI qualifiedName:qualifiedName value:value];
  92. [self.attributes setNamedItemNS:att];
  93. }
  94. // Introduced in DOM Level 2:
  95. -(void) removeAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
  96. {
  97. NSAssert( FALSE, @"Not implemented yet" );
  98. }
  99. // Introduced in DOM Level 2:
  100. -(Attr*) getAttributeNodeNS:(NSString*) namespaceURI localName:(NSString*) localName
  101. {
  102. Attr* result = (Attr*) [self.attributes getNamedItemNS:namespaceURI localName:localName];
  103. return result;
  104. }
  105. // Introduced in DOM Level 2:
  106. -(Attr*) setAttributeNodeNS:(Attr*) newAttr
  107. {
  108. NSAssert( FALSE, @"Not implemented yet" );
  109. return nil;
  110. }
  111. // Introduced in DOM Level 2:
  112. -(NodeList*) getElementsByTagNameNS:(NSString*) namespaceURI localName:(NSString*) localName
  113. {
  114. NodeList* accumulator = [[NodeList alloc] init];
  115. [DOMHelperUtilities privateGetElementsByName:localName inNamespace:namespaceURI childrenOfElement:self addToList:accumulator];
  116. return accumulator;
  117. }
  118. // Introduced in DOM Level 2:
  119. -(BOOL) hasAttribute:(NSString*) name
  120. {
  121. Attr* result = (Attr*) [self.attributes getNamedItem:name];
  122. return result != nil;
  123. }
  124. // Introduced in DOM Level 2:
  125. -(BOOL) hasAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
  126. {
  127. NSAssert( FALSE, @"Not implemented yet" );
  128. return FALSE;
  129. }
  130. @end