Skip to content

Commit 12c142c

Browse files
committed
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".
1 parent 20a5afb commit 12c142c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

DDXMLElement.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,32 @@ - (NSArray *)attributes
277277

278278
- (DDXMLNode *)attributeForName:(NSString *)name
279279
{
280+
const xmlChar *attrName = [name xmlChar];
281+
280282
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
281283
while(attr != NULL)
282284
{
283-
if(xmlStrEqual([name xmlChar], attr->name))
285+
if(attr->ns && attr->ns->prefix)
284286
{
285-
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
287+
// If the attribute name was originally something like "xml:quack",
288+
// then attr->name is "quack" and attr->ns->prefix is "xml".
289+
//
290+
// So if the user is searching for "xml:quack" we need to take the prefix into account.
291+
// Note that "xml:quack" is what would be printed if we output the attribute.
292+
293+
if(xmlStrQEqual(attr->ns->prefix, attr->name, attrName))
294+
{
295+
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
296+
}
286297
}
298+
else
299+
{
300+
if(xmlStrEqual(attr->name, attrName))
301+
{
302+
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
303+
}
304+
}
305+
287306
attr = attr->next;
288307
}
289308
return nil;

DDXMLTesting.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ + (void)testNodesForXPath;
2929
+ (void)testNSXMLBugs;
3030
+ (void)testInsertChild;
3131
+ (void)testElementSerialization;
32+
+ (void)testAttributeWithColonInName;
3233
@end
3334

3435
@implementation DDXMLTesting
@@ -64,6 +65,7 @@ + (void)performTests
6465
[self testNSXMLBugs];
6566
[self testInsertChild];
6667
[self testElementSerialization];
68+
[self testAttributeWithColonInName];
6769

6870
[self tearDown];
6971

@@ -1513,4 +1515,23 @@ + (void)testElementSerialization
15131515
[pool release];
15141516
}
15151517

1518+
+ (void)testAttributeWithColonInName
1519+
{
1520+
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
1521+
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1522+
1523+
NSString *str = @"<artist name='Jay-Z' xml:pimp='yes' />";
1524+
1525+
NSXMLDocument *nsDoc = [[[NSXMLDocument alloc] initWithXMLString:str options:0 error:nil] autorelease];
1526+
DDXMLDocument *ddDoc = [[[DDXMLDocument alloc] initWithXMLString:str options:0 error:nil] autorelease];
1527+
1528+
NSXMLNode *nsa = [[nsDoc rootElement] attributeForName:@"xml:pimp"];
1529+
DDXMLNode *dda = [[ddDoc rootElement] attributeForName:@"xml:pimp"];
1530+
1531+
NSAssert(nsa != nil, @"Failed CHECK 1");
1532+
NSAssert(dda != nil, @"Failed test 1");
1533+
1534+
[pool release];
1535+
}
1536+
15161537
@end

0 commit comments

Comments
 (0)