Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGURIReference.cpp
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 #include "core/svg/SVGURIReference.h"
23
24 #include "XLinkNames.h"
25 #include "core/svg/SVGElement.h"
26 #include "platform/weborigin/KURL.h"
27
28 namespace WebCore {
29
30 SVGURIReference::SVGURIReference(SVGElement* element)
31     : m_href(SVGAnimatedString::create(element, XLinkNames::hrefAttr, SVGString::create()))
32 {
33     ASSERT(element);
34     element->addToPropertyMap(m_href);
35 }
36
37 bool SVGURIReference::isKnownAttribute(const QualifiedName& attrName)
38 {
39     return attrName.matches(XLinkNames::hrefAttr);
40 }
41
42 AtomicString SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const Document& document)
43 {
44     size_t start = url.find('#');
45     if (start == kNotFound)
46         return emptyAtom;
47
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));
51
52     return emptyAtom;
53 }
54
55 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, const Document& document, AtomicString& fragmentIdentifier)
56 {
57     size_t startOfFragmentIdentifier = url.find('#');
58     if (startOfFragmentIdentifier == kNotFound)
59         return KURL();
60
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));
66     }
67
68     return KURL(document.baseURI(), url.substring(startOfFragmentIdentifier));
69 }
70
71 Element* SVGURIReference::targetElementFromIRIString(const String& iri, const Document& document, AtomicString* fragmentIdentifier, Document* externalDocument)
72 {
73     // If there's no fragment identifier contained within the IRI string, we can't lookup an element.
74     AtomicString id;
75     KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id);
76     if (url == KURL())
77         return 0;
78
79     if (fragmentIdentifier)
80         *fragmentIdentifier = id;
81
82     if (id.isEmpty())
83         return 0;
84
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);
89     }
90
91     // Exit early if the referenced url is external, and we have no externalDocument given.
92     if (isExternalURIReference(iri, document))
93         return 0;
94
95     return document.getElementById(id);
96 }
97
98 void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
99 {
100     supportedAttributes.add(XLinkNames::hrefAttr);
101 }
102
103 bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value, SVGParsingError& parseError)
104 {
105     if (name.matches(XLinkNames::hrefAttr)) {
106         m_href->setBaseValueAsString(value, parseError);
107         return true;
108     }
109     return false;
110 }
111
112 }