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 Document& document)
44 size_t start = url.find('#');
45 if (start == kNotFound)
48 KURL base = start ? KURL(document.baseURI(), url.substring(0, start)) : document.baseURI();
49 if (equalIgnoringFragmentIdentifier(base, document.url()))
50 return AtomicString(url.substring(start + 1));
55 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, const Document& document, AtomicString& fragmentIdentifier)
57 size_t startOfFragmentIdentifier = url.find('#');
58 if (startOfFragmentIdentifier == kNotFound)
61 // Exclude the '#' character when determining the fragmentIdentifier.
62 fragmentIdentifier = AtomicString(url.substring(startOfFragmentIdentifier + 1));
63 if (startOfFragmentIdentifier) {
64 KURL base(document.baseURI(), url.substring(0, startOfFragmentIdentifier));
65 return KURL(base, url.substring(startOfFragmentIdentifier));
68 return KURL(document.baseURI(), url.substring(startOfFragmentIdentifier));
71 Element* SVGURIReference::targetElementFromIRIString(const String& iri, const Document& document, AtomicString* fragmentIdentifier, Document* externalDocument)
73 // If there's no fragment identifier contained within the IRI string, we can't lookup an element.
75 KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id);
79 if (fragmentIdentifier)
80 *fragmentIdentifier = id;
85 if (externalDocument) {
86 // Enforce that the referenced url matches the url of the document that we've loaded for it!
87 ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url()));
88 return externalDocument->getElementById(id);
91 // Exit early if the referenced url is external, and we have no externalDocument given.
92 if (isExternalURIReference(iri, document))
95 return document.getElementById(id);
98 void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
100 supportedAttributes.add(XLinkNames::hrefAttr);
103 bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value, SVGParsingError& parseError)
105 if (name.matches(XLinkNames::hrefAttr)) {
106 m_href->setBaseValueAsString(value, parseError);