| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include "config.h" |
| 33 | #include "LinkRelAttribute.h" |
| 34 | |
| 35 | #include "Document.h" |
| 36 | #include "LinkIconType.h" |
| 37 | #include "RuntimeEnabledFeatures.h" |
| 38 | #include "Settings.h" |
| 39 | #include <wtf/text/StringView.h> |
| 40 | #include <wtf/text/WTFString.h> |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | LinkRelAttribute::LinkRelAttribute() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | // Keep LinkRelAttribute::isSupported() in sync when updating this constructor. |
| 49 | LinkRelAttribute::LinkRelAttribute(Document& document, const String& rel) |
| 50 | { |
| 51 | if (equalLettersIgnoringASCIICase(rel, "stylesheet" )) |
| 52 | isStyleSheet = true; |
| 53 | else if (equalLettersIgnoringASCIICase(rel, "icon" ) || equalLettersIgnoringASCIICase(rel, "shortcut icon" )) |
| 54 | iconType = LinkIconType::Favicon; |
| 55 | else if (equalLettersIgnoringASCIICase(rel, "apple-touch-icon" )) |
| 56 | iconType = LinkIconType::TouchIcon; |
| 57 | else if (equalLettersIgnoringASCIICase(rel, "apple-touch-icon-precomposed" )) |
| 58 | iconType = LinkIconType::TouchPrecomposedIcon; |
| 59 | else if (equalLettersIgnoringASCIICase(rel, "dns-prefetch" )) |
| 60 | isDNSPrefetch = true; |
| 61 | else if (document.settings().linkPreconnectEnabled() && equalLettersIgnoringASCIICase(rel, "preconnect" )) |
| 62 | isLinkPreconnect = true; |
| 63 | else if (RuntimeEnabledFeatures::sharedFeatures().linkPreloadEnabled() && equalLettersIgnoringASCIICase(rel, "preload" )) |
| 64 | isLinkPreload = true; |
| 65 | else if (RuntimeEnabledFeatures::sharedFeatures().linkPrefetchEnabled() && equalLettersIgnoringASCIICase(rel, "prefetch" )) |
| 66 | isLinkPrefetch = true; |
| 67 | else if (equalLettersIgnoringASCIICase(rel, "alternate stylesheet" ) || equalLettersIgnoringASCIICase(rel, "stylesheet alternate" )) { |
| 68 | isStyleSheet = true; |
| 69 | isAlternate = true; |
| 70 | #if ENABLE(APPLICATION_MANIFEST) |
| 71 | } else if (equalLettersIgnoringASCIICase(rel, "manifest" )) { |
| 72 | isApplicationManifest = true; |
| 73 | #endif |
| 74 | } else { |
| 75 | // Tokenize the rel attribute and set bits based on specific keywords that we find. |
| 76 | String relCopy = rel; |
| 77 | relCopy.replace('\n', ' '); |
| 78 | for (auto word : StringView(relCopy).split(' ')) { |
| 79 | if (equalLettersIgnoringASCIICase(word, "stylesheet" )) |
| 80 | isStyleSheet = true; |
| 81 | else if (equalLettersIgnoringASCIICase(word, "alternate" )) |
| 82 | isAlternate = true; |
| 83 | else if (equalLettersIgnoringASCIICase(word, "icon" )) |
| 84 | iconType = LinkIconType::Favicon; |
| 85 | else if (equalLettersIgnoringASCIICase(word, "apple-touch-icon" )) |
| 86 | iconType = LinkIconType::TouchIcon; |
| 87 | else if (equalLettersIgnoringASCIICase(word, "apple-touch-icon-precomposed" )) |
| 88 | iconType = LinkIconType::TouchPrecomposedIcon; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // https://html.spec.whatwg.org/#linkTypes |
| 94 | bool LinkRelAttribute::isSupported(Document& document, StringView attribute) |
| 95 | { |
| 96 | static const char* const supportedAttributes[] = { |
| 97 | "alternate" , "dns-prefetch" , "icon" , "stylesheet" , "apple-touch-icon" , "apple-touch-icon-precomposed" , |
| 98 | #if ENABLE(APPLICATION_MANIFEST) |
| 99 | "manifest" , |
| 100 | #endif |
| 101 | }; |
| 102 | |
| 103 | for (auto* supportedAttribute : supportedAttributes) { |
| 104 | if (equalIgnoringASCIICase(attribute, supportedAttribute)) |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | if (document.settings().linkPreconnectEnabled() && equalIgnoringASCIICase(attribute, "preconnect" )) |
| 109 | return true; |
| 110 | |
| 111 | if (RuntimeEnabledFeatures::sharedFeatures().linkPreloadEnabled() && equalIgnoringASCIICase(attribute, "preload" )) |
| 112 | return true; |
| 113 | |
| 114 | if (RuntimeEnabledFeatures::sharedFeatures().linkPrefetchEnabled() && equalIgnoringASCIICase(attribute, "prefetch" )) |
| 115 | return true; |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |