diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index ac1c43bb..c953be04 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -19,23 +19,6 @@ @property (readonly, copy) NSDictionary *attributesAsDictionary; @end -@implementation NSXMLElement (SUAppcastExtensions) -- (NSDictionary *)attributesAsDictionary -{ - NSEnumerator *attributeEnum = [[self attributes] objectEnumerator]; - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - - for (NSXMLNode *attribute in attributeEnum) { - NSString *attrName = [attribute name]; - if (!attrName) { - continue; - } - dictionary[attrName] = [attribute stringValue]; - } - return dictionary; -} -@end - @interface SUAppcast () @property (strong) void (^completionBlock)(NSError *); @property (copy) NSString *downloadFilename; @@ -115,6 +98,31 @@ } } +- (NSDictionary *)attributesOfNode:(NSXMLElement *)node +{ + NSEnumerator *attributeEnum = [[node attributes] objectEnumerator]; + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + + for (NSXMLNode *attribute in attributeEnum) { + NSString *attrName = [self sparkleNamespacedNameOfNode:attribute]; + if (!attrName) { + continue; + } + dictionary[attrName] = [attribute stringValue]; + } + return dictionary; +} + +-(NSString *)sparkleNamespacedNameOfNode:(NSXMLNode *)node { + // XML namespace prefix is semantically meaningless, so compare namespace URI + if ([[node URI] isEqualToString:@"http://www.andymatuschak.org/xml-namespaces/sparkle"]) { + NSString *localName = [node localName]; assert(localName); + return [@"sparkle:" stringByAppendingString:localName]; + } else { + return [node name]; // Backwards compatibility + } +} + -(NSArray *)parseAppcastItemsFromXMLFile:(NSURL *)appcastFile error:(NSError *__autoreleasing*)errorp { if (errorp) { *errorp = nil; @@ -147,7 +155,7 @@ if ([[node children] count]) { node = [node childAtIndex:0]; while (nil != node) { - NSString *name = [node name]; + NSString *name = [self sparkleNamespacedNameOfNode:node]; if (name) { NSMutableArray *nodes = nodesDict[name]; if (nodes == nil) { @@ -164,7 +172,7 @@ node = [self bestNodeInNodes:nodesDict[name]]; if ([name isEqualToString:SURSSElementEnclosure]) { // enclosure is flattened as a separate dictionary for some reason - NSDictionary *encDict = [(NSXMLElement *)node attributesAsDictionary]; + NSDictionary *encDict = [self attributesOfNode:(NSXMLElement *)node]; dict[name] = encDict; } else if ([name isEqualToString:SURSSElementPubDate]) { @@ -182,7 +190,7 @@ NSEnumerator *childEnum = [[node children] objectEnumerator]; for (NSXMLNode *child in childEnum) { if ([[child name] isEqualToString:SURSSElementEnclosure]) { - [deltas addObject:[(NSXMLElement *)child attributesAsDictionary]]; + [deltas addObject:[self attributesOfNode:(NSXMLElement *)child]]; } } dict[name] = deltas; diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index 0023508e..cc1297b4 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -1,17 +1,18 @@ - + For unit test only Version 2.0 desc Sat, 26 Jul 2014 15:20:11 +0000 - + Version 3.0 Sat, 26 Jul 2014 15:20:12 +0000 - + https://sparkle-project.org/#works + diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 5f39f795..a211b46f 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -11,7 +11,7 @@ import Sparkle; class SUAppcastTest: XCTestCase { - func testExample() { + func testParseAppcast() { let appcast = SUAppcast(); let testFile = NSBundle(forClass: SUAppcastTest.self).pathForResource("testappcast", ofType: "xml")!; let testFileUrl = NSURL(fileURLWithPath: testFile); @@ -26,6 +26,7 @@ class SUAppcastTest: XCTestCase { XCTAssertEqual("Version 2.0", items[1].title); XCTAssertEqual("desc", items[1].itemDescription); + XCTAssertEqual("https://sparkle-project.org/#works", items[0].releaseNotesURL!.absoluteString); } catch let err as NSError { NSLog("%@", err); XCTFail(err.localizedDescription);