Revision: 26759
          http://sourceforge.net/p/bibdesk/svn/26759
Author:   hofman
Date:     2021-08-29 09:23:15 +0000 (Sun, 29 Aug 2021)
Log Message:
-----------
Get favicon based on preferred rel type

Modified Paths:
--------------
    trunk/bibdesk/BDSKWebView.m

Modified: trunk/bibdesk/BDSKWebView.m
===================================================================
--- trunk/bibdesk/BDSKWebView.m 2021-08-29 06:30:52 UTC (rev 26758)
+++ trunk/bibdesk/BDSKWebView.m 2021-08-29 09:23:15 UTC (rev 26759)
@@ -177,36 +177,55 @@
     NSMutableArray *favicons = [NSMutableArray array];
     WebFrame *frame = [self mainFrame];
     NSURL *frameURL = [[[frame dataSource] request] URL];
-    NSArray *nodes = [[frame DOMDocument] 
nodesForXPath:@"/html/head/link[@rel='icon' or @rel='shortcut icon']"];
+    NSArray *nodes = [[frame DOMDocument] 
nodesForXPath:@"/html/head/link[@rel='icon' or @rel='shortcut icon' or 
@rel='apple-touch-icon' or @rel='apple-touch-icon-precomposed']"];
     for (DOMElement *node in nodes) {
-        NSURL *url = [NSURL URLWithString:[node getAttribute:@"href"] 
relativeToURL:frameURL];
+        NSString *url = [node getAttribute:@"href"];
+        if (url == nil) continue;
+        NSUInteger type = [[NSArray arrayWithObjects:@"apple-touch-icon", 
@"apple-touch-icon-precomposed", @"shortcut icon", @"icon", nil] 
indexOfObject:[node getAttribute:@"rel"]];
         NSArray *sizes = [[node getAttribute:@"sizes"] 
componentsSeparatedByString:@" "];
         BOOL foundSize = NO;
         for (NSString *sizeString in sizes) {
             NSArray *widthAndHeight = [sizeString 
componentsSeparatedByString:@"x"];
             if ([sizes count] == 2) {
-                [favicons addObject:[NSDictionary 
dictionaryWithObjectsAndKeys:url, @"url", [NSNumber numberWithInteger:[[sizes 
firstObject] integerValue]], @"width", [NSNumber numberWithInteger:[[sizes 
lastObject] integerValue]], @"height", nil]];
+                [favicons addObject:[NSDictionary 
dictionaryWithObjectsAndKeys:url, @"url", [NSNumber numberWithInteger:[[sizes 
firstObject] integerValue]], @"width", [NSNumber numberWithInteger:[[sizes 
lastObject] integerValue]], @"height", [NSNumber 
numberWithUnsignedInteger:type], @"type", nil]];
                 foundSize = YES;
             }
         }
         if (foundSize == NO)
-            [favicons addObject:[NSDictionary 
dictionaryWithObjectsAndKeys:url, @"url", nil]];
+            [favicons addObject:[NSDictionary 
dictionaryWithObjectsAndKeys:url, @"url", [NSNumber 
numberWithUnsignedInteger:type], @"type", nil]];
     }
-    [favicons sortUsingComparator:^(id obj1, id obj2){ return [[obj1 
valueForKey:@"width"] compare:[obj2 valueForKey:@"width"]]; }];
-    NSInteger size = 16 * (NSInteger)[[frame webView] 
convertSizeToBacking:NSMakeSize(1.0, 1.0)].width;
-    NSURL *faviconURL = nil;
-    for (NSDictionary *info in favicons) {
-        NSInteger width = [[info valueForKey:@"width"] integerValue];
-        if (width >= size) {
-            faviconURL = [info valueForKey:@"url"];
-            break;
+    NSString *urlString = nil;
+    if ([favicons count] > 1) {
+        // get favicons of the preferred type
+        NSArray *sortDescriptors = [NSArray 
arrayWithObjects:[[[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES] 
autorelease], nil];
+        [favicons sortUsingDescriptors:sortDescriptors];
+        NSUInteger preferredType = [[[favicons firstObject] 
valueForKey:@"type"] unsignedIntegerValue];
+        while ([favicons count] > 1) {
+            if ([[[favicons lastObject] valueForKey:@"type"] 
unsignedIntegerValue] == preferredType)
+                break;
+            [favicons removeLastObject];
         }
+        if ([favicons count] > 1) {
+            // get favicon with the preferred size
+            sortDescriptors = [NSArray arrayWithObjects:[[[NSSortDescriptor 
alloc] initWithKey:@"width" ascending:YES] autorelease], nil];
+            [favicons sortUsingDescriptors:sortDescriptors];
+            NSInteger preferredSize = 16 * (NSInteger)[[frame webView] 
convertSizeToBacking:NSMakeSize(1.0, 1.0)].width;
+            for (NSDictionary *info in favicons) {
+                NSInteger width = [[info valueForKey:@"width"] integerValue];
+                if (width >= preferredSize) {
+                    urlString = [info valueForKey:@"url"];
+                    break;
+                }
+            }
+        }
     }
+    if (urlString == nil)
+        urlString = [[favicons firstObject] valueForKey:@"url"] ?: 
@"favicon.ico";
+    if (urlString == nil)
+        return;
+    NSURL *faviconURL = [NSURL URLWithString:urlString relativeToURL:frameURL];
     if (faviconURL == nil)
-        faviconURL = [[favicons firstObject] valueForKey:@"url"] ?: [NSURL 
URLWithString:@"favicon.ico" relativeToURL:frameURL];
-    if (faviconURL == nil)
         return;
-    
     if (faviconDownload) {
         [faviconDownload cancel];
         [faviconDownload release];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to