1 /* 2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis (at) kde.org> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #include "config.h" 22 23 #include "core/svg/SVGURIReference.h" 24 25 #include "XLinkNames.h" 26 #include "core/dom/Document.h" 27 #include "weborigin/KURL.h" 28 29 namespace WebCore { 30 31 bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value) 32 { 33 if (name.matches(XLinkNames::hrefAttr)) { 34 setHrefBaseValue(value); 35 return true; 36 } 37 38 return false; 39 } 40 41 bool SVGURIReference::isKnownAttribute(const QualifiedName& attrName) 42 { 43 return attrName.matches(XLinkNames::hrefAttr); 44 } 45 46 String SVGURIReference::fragmentIdentifierFromIRIString(const String& url, Document* document) 47 { 48 ASSERT(document); 49 size_t start = url.find('#'); 50 if (start == notFound) 51 return emptyString(); 52 53 KURL base = start ? KURL(document->baseURI(), url.substring(0, start)) : document->baseURI(); 54 if (equalIgnoringFragmentIdentifier(base, document->url())) 55 return url.substring(start + 1); 56 57 return emptyString(); 58 } 59 60 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, Document* document, String& fragmentIdentifier) 61 { 62 ASSERT(document); 63 size_t startOfFragmentIdentifier = url.find('#'); 64 if (startOfFragmentIdentifier == notFound) 65 return KURL(); 66 67 // Exclude the '#' character when determining the fragmentIdentifier. 68 fragmentIdentifier = url.substring(startOfFragmentIdentifier + 1); 69 if (startOfFragmentIdentifier) { 70 KURL base(document->baseURI(), url.substring(0, startOfFragmentIdentifier)); 71 return KURL(base, url.substring(startOfFragmentIdentifier)); 72 } 73 74 return KURL(document->baseURI(), url.substring(startOfFragmentIdentifier)); 75 } 76 77 Element* SVGURIReference::targetElementFromIRIString(const String& iri, Document* document, String* fragmentIdentifier, Document* externalDocument) 78 { 79 // If there's no fragment identifier contained within the IRI string, we can't lookup an element. 80 String id; 81 KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id); 82 if (url == KURL()) 83 return 0; 84 85 if (fragmentIdentifier) 86 *fragmentIdentifier = id; 87 88 if (id.isEmpty()) 89 return 0; 90 91 if (externalDocument) { 92 // Enforce that the referenced url matches the url of the document that we've loaded for it! 93 ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url())); 94 return externalDocument->getElementById(id); 95 } 96 97 // Exit early if the referenced url is external, and we have no externalDocument given. 98 if (isExternalURIReference(iri, document)) 99 return 0; 100 101 return document->getElementById(id); 102 } 103 104 void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes) 105 { 106 supportedAttributes.add(XLinkNames::hrefAttr); 107 } 108 109 } 110