1/*
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@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#if ENABLE(SVG)
24#include "SVGURIReference.h"
25
26#include "Attribute.h"
27#include "Document.h"
28#include "Element.h"
29#include "KURL.h"
30
31namespace WebCore {
32
33bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value)
34{
35    if (name.matches(XLinkNames::hrefAttr)) {
36        setHrefBaseValue(value);
37        return true;
38    }
39
40    return false;
41}
42
43bool SVGURIReference::isKnownAttribute(const QualifiedName& attrName)
44{
45    return attrName.matches(XLinkNames::hrefAttr);
46}
47
48String SVGURIReference::fragmentIdentifierFromIRIString(const String& url, Document* document)
49{
50    ASSERT(document);
51    size_t start = url.find('#');
52    if (start == notFound)
53        return emptyString();
54
55    KURL base = start ? KURL(document->baseURI(), url.substring(0, start)) : document->baseURI();
56    String fragmentIdentifier = url.substring(start);
57    KURL kurl(base, fragmentIdentifier);
58    if (equalIgnoringFragmentIdentifier(kurl, document->url()))
59        return fragmentIdentifier.substring(1);
60
61    // The url doesn't have any fragment identifier.
62    return emptyString();
63}
64
65static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, Document* document, String& fragmentIdentifier)
66{
67    ASSERT(document);
68    size_t startOfFragmentIdentifier = url.find('#');
69    if (startOfFragmentIdentifier == notFound)
70        return KURL();
71
72    // Exclude the '#' character when determining the fragmentIdentifier.
73    fragmentIdentifier = url.substring(startOfFragmentIdentifier + 1);
74    if (startOfFragmentIdentifier) {
75        KURL base(document->baseURI(), url.substring(0, startOfFragmentIdentifier));
76        return KURL(base, url.substring(startOfFragmentIdentifier));
77    }
78
79    return KURL(document->baseURI(), url.substring(startOfFragmentIdentifier));
80}
81
82Element* SVGURIReference::targetElementFromIRIString(const String& iri, Document* document, String* fragmentIdentifier, Document* externalDocument)
83{
84    // If there's no fragment identifier contained within the IRI string, we can't lookup an element.
85    String id;
86    KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id);
87    if (url == KURL())
88        return 0;
89
90    if (fragmentIdentifier)
91        *fragmentIdentifier = id;
92
93    if (id.isEmpty())
94        return 0;
95
96    if (externalDocument) {
97        // Enforce that the referenced url matches the url of the document that we've loaded for it!
98        ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url()));
99        return externalDocument->getElementById(id);
100    }
101
102    // Exit early if the referenced url is external, and we have no externalDocument given.
103    if (isExternalURIReference(iri, document))
104        return 0;
105
106    return document->getElementById(id);
107}
108
109void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
110{
111    supportedAttributes.add(XLinkNames::hrefAttr);
112}
113
114}
115
116#endif // ENABLE(SVG)
117