2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
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.
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.
22 #include "core/svg/SVGURIReference.h"
24 #include "XLinkNames.h"
25 #include "core/svg/SVGElement.h"
26 #include "platform/weborigin/KURL.h"
30 SVGURIReference::SVGURIReference(SVGElement* element)
31 : m_href(SVGAnimatedString::create(element, XLinkNames::hrefAttr, SVGString::create()))
34 element->addToPropertyMap(m_href);
37 bool SVGURIReference::isKnownAttribute(const QualifiedName& attrName)
39 return attrName.matches(XLinkNames::hrefAttr);
42 AtomicString SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const TreeScope& treeScope)
44 size_t start = url.find('#');
45 if (start == kNotFound)
48 const Document& document = treeScope.document();
49 KURL base = start ? KURL(document.baseURI(), url.substring(0, start)) : document.baseURI();
50 if (equalIgnoringFragmentIdentifier(base, document.url()))
51 return AtomicString(url.substring(start + 1));
56 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, const TreeScope& treeScope, AtomicString& fragmentIdentifier)
58 size_t startOfFragmentIdentifier = url.find('#');
59 if (startOfFragmentIdentifier == kNotFound)
62 const Document& document = treeScope.document();
64 // Exclude the '#' character when determining the fragmentIdentifier.
65 fragmentIdentifier = AtomicString(url.substring(startOfFragmentIdentifier + 1));
66 if (startOfFragmentIdentifier) {
67 KURL base(document.baseURI(), url.substring(0, startOfFragmentIdentifier));
68 return KURL(base, url.substring(startOfFragmentIdentifier));
71 return KURL(document.baseURI(), url.substring(startOfFragmentIdentifier));
74 Element* SVGURIReference::targetElementFromIRIString(const String& iri, const TreeScope& treeScope, AtomicString* fragmentIdentifier, Document* externalDocument)
76 const Document& document = treeScope.document();
78 // If there's no fragment identifier contained within the IRI string, we can't lookup an element.
80 KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id);
84 if (fragmentIdentifier)
85 *fragmentIdentifier = id;
90 if (externalDocument) {
91 // Enforce that the referenced url matches the url of the document that we've loaded for it!
92 ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url()));
93 return externalDocument->getElementById(id);
96 // Exit early if the referenced url is external, and we have no externalDocument given.
97 if (isExternalURIReference(iri, document))
100 return treeScope.getElementById(id);
103 void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
105 supportedAttributes.add(XLinkNames::hrefAttr);
108 bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value, SVGParsingError& parseError)
110 if (name.matches(XLinkNames::hrefAttr)) {
111 m_href->setBaseValueAsString(value, parseError);