Permalink
Browse files

Bug fix for issue #20 - [DDXMLNode name] should return qualified name

  • Loading branch information...
1 parent 50e9101 commit 430a40d9b1ccbd17676111792fa8c61be25e7491 @robbiehanson committed Nov 2, 2011
Showing with 84 additions and 5 deletions.
  1. +51 −5 KissXML/DDXMLNode.m
  2. +33 −0 UnitTesting/DDXMLTesting.m
View
@@ -373,17 +373,35 @@ - (void)setName:(NSString *)name
- (NSString *)name
{
// Note: DDXMLNamespaceNode overrides this method
+ // Note: DDXMLAttributeNode overrides this method
#if DDXML_DEBUG_MEMORY_ISSUES
DDXMLNotZombieAssert();
#endif
- const char *name = (const char *)((xmlStdPtr)genericPtr)->name;
-
- if (name == NULL)
+ const xmlChar *xmlName = ((xmlStdPtr)genericPtr)->name;
+ if (xmlName == NULL)
+ {
return nil;
- else
- return [NSString stringWithUTF8String:name];
+ }
+
+ NSString *name = [NSString stringWithUTF8String:(const char *)xmlName];
+
+ if (IsXmlNodePtr(genericPtr))
+ {
+ xmlNodePtr node = (xmlNodePtr)genericPtr;
+
+ NSRange range = [name rangeOfString:@":"];
+ if (range.length == 0)
+ {
+ if (node->ns && node->ns->prefix)
+ {
+ return [NSString stringWithFormat:@"%s:%@", node->ns->prefix, name];
+ }
+ }
+ }
+
+ return name;
}
- (void)setStringValue:(NSString *)string
@@ -2500,6 +2518,34 @@ - (void)dealloc
#pragma mark Properties
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+- (NSString *)name
+{
+#if DDXML_DEBUG_MEMORY_ISSUES
+ DDXMLNotZombieAssert();
+#endif
+
+ xmlAttrPtr attr = (xmlAttrPtr)genericPtr;
+
+ const xmlChar *xmlName = attr->name;
+ if (xmlName == NULL)
+ {
+ return nil;
+ }
+
+ NSString *name = [NSString stringWithUTF8String:(const char *)xmlName];
+
+ NSRange range = [name rangeOfString:@":"];
+ if (range.length == 0)
+ {
+ if (attr->ns && attr->ns->prefix)
+ {
+ return [NSString stringWithFormat:@"%s:%@", attr->ns->prefix, name];
+ }
+ }
+
+ return name;
+}
+
- (void)setStringValue:(NSString *)string
{
#if DDXML_DEBUG_MEMORY_ISSUES
View
@@ -15,6 +15,7 @@ @interface DDAssertionHandler : NSAssertionHandler
@interface DDXMLTesting (Tests)
+ (void)setUp;
+ (void)tearDown;
++ (void)testName;
+ (void)testLocalName;
+ (void)testPrefixName;
+ (void)testDoubleAdd;
@@ -60,6 +61,7 @@ + (void)performTests
[self setUp];
+ [self testName];
[self testLocalName];
[self testPrefixName];
[self testDoubleAdd];
@@ -146,6 +148,37 @@ + (NSException *)tryCatch:(void (^)())block
return result;
}
++ (void)testName
+{
+ NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSString *str = @"<body xmlns:food='http://example.com/' food:genre='italian'>"
+ @" <food:pizza>yumyum</food:pizza>"
+ @"</body>";
+
+ NSError *error = nil;
+
+ NSXMLElement *nsBody = [[NSXMLElement alloc] initWithXMLString:str error:&error];
+ DDXMLElement *ddBody = [[DDXMLElement alloc] initWithXMLString:str error:&error];
+
+ // Test 1 - elements
+
+ NSString *nsNodeName = [[nsBody childAtIndex:0] name];
+ NSString *ddNodeName = [[ddBody childAtIndex:0] name];
+
+ NSAssert([nsNodeName isEqualToString:ddNodeName], @"Failed test 1");
+
+ // Test 2 - attributes
+
+ NSString *nsAttrName = [[nsBody attributeForName:@"food:genre"] name];
+ NSString *ddAttrName = [[ddBody attributeForName:@"food:genre"] name];
+
+ NSAssert([nsAttrName isEqualToString:ddAttrName], @"Failed test 2");
+
+ [pool release];
+}
+
+ (void)testLocalName
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));

0 comments on commit 430a40d

Please sign in to comment.