Permalink
Browse files

Bug fix. Attempting to get an attribute with a colon in the name didn…

…'t work if you tried to access the attribute by name. E.g. an attribute with a name like "xml:name".
  • Loading branch information...
1 parent 20a5afb commit 12c142ca001d060e9bd18ee4a345c9b49008fc92 @robbiehanson committed May 12, 2010
Showing with 42 additions and 2 deletions.
  1. +21 −2 DDXMLElement.m
  2. +21 −0 DDXMLTesting.m
View
@@ -277,13 +277,32 @@ - (NSArray *)attributes
- (DDXMLNode *)attributeForName:(NSString *)name
{
+ const xmlChar *attrName = [name xmlChar];
+
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
while(attr != NULL)
{
- if(xmlStrEqual([name xmlChar], attr->name))
+ if(attr->ns && attr->ns->prefix)
{
- return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
+ // If the attribute name was originally something like "xml:quack",
+ // then attr->name is "quack" and attr->ns->prefix is "xml".
+ //
+ // So if the user is searching for "xml:quack" we need to take the prefix into account.
+ // Note that "xml:quack" is what would be printed if we output the attribute.
+
+ if(xmlStrQEqual(attr->ns->prefix, attr->name, attrName))
+ {
+ return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
+ }
}
+ else
+ {
+ if(xmlStrEqual(attr->name, attrName))
+ {
+ return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
+ }
+ }
+
attr = attr->next;
}
return nil;
View
@@ -29,6 +29,7 @@ + (void)testNodesForXPath;
+ (void)testNSXMLBugs;
+ (void)testInsertChild;
+ (void)testElementSerialization;
++ (void)testAttributeWithColonInName;
@end
@implementation DDXMLTesting
@@ -64,6 +65,7 @@ + (void)performTests
[self testNSXMLBugs];
[self testInsertChild];
[self testElementSerialization];
+ [self testAttributeWithColonInName];
[self tearDown];
@@ -1513,4 +1515,23 @@ + (void)testElementSerialization
[pool release];
}
++ (void)testAttributeWithColonInName
+{
+ NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSString *str = @"<artist name='Jay-Z' xml:pimp='yes' />";
+
+ NSXMLDocument *nsDoc = [[[NSXMLDocument alloc] initWithXMLString:str options:0 error:nil] autorelease];
+ DDXMLDocument *ddDoc = [[[DDXMLDocument alloc] initWithXMLString:str options:0 error:nil] autorelease];
+
+ NSXMLNode *nsa = [[nsDoc rootElement] attributeForName:@"xml:pimp"];
+ DDXMLNode *dda = [[ddDoc rootElement] attributeForName:@"xml:pimp"];
+
+ NSAssert(nsa != nil, @"Failed CHECK 1");
+ NSAssert(dda != nil, @"Failed test 1");
+
+ [pool release];
+}
+
@end

0 comments on commit 12c142c

Please sign in to comment.