Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: third_party/WebKit/Source/core/svg/SVGURIReference.cpp

Issue 2170453002: Simplify URL-resolving in targetElementFromIRIString (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> 2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 if (element.hasAttribute(SVGNames::hrefAttr)) 49 if (element.hasAttribute(SVGNames::hrefAttr))
50 return element.getAttribute(SVGNames::hrefAttr); 50 return element.getAttribute(SVGNames::hrefAttr);
51 return element.getAttribute(XLinkNames::hrefAttr); 51 return element.getAttribute(XLinkNames::hrefAttr);
52 } 52 }
53 53
54 KURL SVGURIReference::legacyHrefURL(const Document& document) const 54 KURL SVGURIReference::legacyHrefURL(const Document& document) const
55 { 55 {
56 return document.completeURL(stripLeadingAndTrailingHTMLSpaces(hrefString())) ; 56 return document.completeURL(stripLeadingAndTrailingHTMLSpaces(hrefString())) ;
57 } 57 }
58 58
59 AtomicString SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const TreeScope& treeScope) 59 AtomicString SVGURIReference::fragmentIdentifierFromIRIString(const String& urlS tring, const TreeScope& treeScope)
60 {
61 size_t start = url.find('#');
62 if (start == kNotFound)
63 return emptyAtom;
64
65 const Document& document = treeScope.document();
66 KURL base = start ? KURL(document.baseURI(), url.substring(0, start)) : docu ment.baseURI();
67 if (equalIgnoringFragmentIdentifier(base, document.url()))
68 return AtomicString(url.substring(start + 1));
69
70 return emptyAtom;
71 }
72
73 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, con st TreeScope& treeScope, AtomicString& fragmentIdentifier)
74 {
75 size_t startOfFragmentIdentifier = url.find('#');
76 if (startOfFragmentIdentifier == kNotFound)
77 return KURL();
78
79 const Document& document = treeScope.document();
80
81 // Exclude the '#' character when determining the fragmentIdentifier.
82 fragmentIdentifier = AtomicString(url.substring(startOfFragmentIdentifier + 1));
83 if (startOfFragmentIdentifier) {
84 KURL base(document.baseURI(), url.substring(0, startOfFragmentIdentifier ));
85 return KURL(base, url.substring(startOfFragmentIdentifier));
86 }
87
88 return KURL(document.baseURI(), url.substring(startOfFragmentIdentifier));
89 }
90
91 Element* SVGURIReference::targetElementFromIRIString(const String& iri, const Tr eeScope& treeScope, AtomicString* fragmentIdentifier, Document* externalDocument )
92 { 60 {
93 const Document& document = treeScope.document(); 61 const Document& document = treeScope.document();
94 62
95 // If there's no fragment identifier contained within the IRI string, we can 't lookup an element. 63 KURL url = document.completeURL(urlString);
96 AtomicString id; 64 if (!url.hasFragmentIdentifier() || !equalIgnoringFragmentIdentifier(url, do cument.url()))
97 KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id); 65 return emptyAtom;
98 if (url == KURL()) 66 return AtomicString(url.fragmentIdentifier());
67 }
68
69 Element* SVGURIReference::targetElementFromIRIString(const String& urlString, co nst TreeScope& treeScope, AtomicString* fragmentIdentifier, Document* externalDo cument)
70 {
71 const Document& document = treeScope.document();
72
73 KURL url = document.completeURL(urlString);
74 if (!url.hasFragmentIdentifier())
99 return nullptr; 75 return nullptr;
100 76
77 AtomicString id(url.fragmentIdentifier());
101 if (fragmentIdentifier) 78 if (fragmentIdentifier)
102 *fragmentIdentifier = id; 79 *fragmentIdentifier = id;
103 80
104 if (id.isEmpty())
105 return nullptr;
106
107 if (externalDocument) { 81 if (externalDocument) {
108 // Enforce that the referenced url matches the url of the document that we've loaded for it! 82 // Enforce that the referenced url matches the url of the document that we've loaded for it!
109 ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url())); 83 ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url()));
110 return externalDocument->getElementById(id); 84 return externalDocument->getElementById(id);
Stephen Chennney 2016/07/20 15:33:21 Does this return nullptr for an empty id? Otherwis
111 } 85 }
112 86
113 // Exit early if the referenced url is external, and we have no externalDocu ment given. 87 // Exit early if the referenced url is external, and we have no externalDocu ment given.
114 if (isExternalURIReference(iri, document)) 88 if (!equalIgnoringFragmentIdentifier(url, document.url()))
115 return nullptr; 89 return nullptr;
116 90
117 return treeScope.getElementById(id); 91 return treeScope.getElementById(id);
Stephen Chennney 2016/07/20 15:33:21 Ditto.
118 } 92 }
119 93
120 } // namespace blink 94 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698