Skip to content

Commit

Permalink
Bug fix. Attempting to get an attribute with a colon in the name didn…
Browse files Browse the repository at this point in the history
…'t work if you tried to access the attribute by name. E.g. an attribute with a name like "xml:name".
  • Loading branch information
robbiehanson committed May 12, 2010
1 parent 20a5afb commit 12c142c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
23 changes: 21 additions & 2 deletions DDXMLElement.m
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions DDXMLTesting.m
Expand Up @@ -29,6 +29,7 @@ + (void)testNodesForXPath;
+ (void)testNSXMLBugs;
+ (void)testInsertChild;
+ (void)testElementSerialization;
+ (void)testAttributeWithColonInName;
@end

@implementation DDXMLTesting
Expand Down Expand Up @@ -64,6 +65,7 @@ + (void)performTests
[self testNSXMLBugs];
[self testInsertChild];
[self testElementSerialization];
[self testAttributeWithColonInName];

[self tearDown];

Expand Down Expand Up @@ -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.