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 "core/XLinkNames.h"
25 #include "core/svg/SVGElement.h"
26 #include "platform/weborigin/KURL.h"
27
28 namespace blink {
29
SVGURIReference(SVGElement * element)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
isKnownAttribute(const QualifiedName & attrName)37 bool SVGURIReference::isKnownAttribute(const QualifiedName& attrName)
38 {
39 return attrName.matches(XLinkNames::hrefAttr);
40 }
41
fragmentIdentifierFromIRIString(const String & url,const TreeScope & treeScope)42 AtomicString SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const TreeScope& treeScope)
43 {
44 size_t start = url.find('#');
45 if (start == kNotFound)
46 return emptyAtom;
47
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));
52
53 return emptyAtom;
54 }
55
urlFromIRIStringWithFragmentIdentifier(const String & url,const TreeScope & treeScope,AtomicString & fragmentIdentifier)56 static inline KURL urlFromIRIStringWithFragmentIdentifier(const String& url, const TreeScope& treeScope, AtomicString& fragmentIdentifier)
57 {
58 size_t startOfFragmentIdentifier = url.find('#');
59 if (startOfFragmentIdentifier == kNotFound)
60 return KURL();
61
62 const Document& document = treeScope.document();
63
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));
69 }
70
71 return KURL(document.baseURI(), url.substring(startOfFragmentIdentifier));
72 }
73
targetElementFromIRIString(const String & iri,const TreeScope & treeScope,AtomicString * fragmentIdentifier,Document * externalDocument)74 Element* SVGURIReference::targetElementFromIRIString(const String& iri, const TreeScope& treeScope, AtomicString* fragmentIdentifier, Document* externalDocument)
75 {
76 const Document& document = treeScope.document();
77
78 // If there's no fragment identifier contained within the IRI string, we can't lookup an element.
79 AtomicString id;
80 KURL url = urlFromIRIStringWithFragmentIdentifier(iri, document, id);
81 if (url == KURL())
82 return 0;
83
84 if (fragmentIdentifier)
85 *fragmentIdentifier = id;
86
87 if (id.isEmpty())
88 return 0;
89
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);
94 }
95
96 // Exit early if the referenced url is external, and we have no externalDocument given.
97 if (isExternalURIReference(iri, document))
98 return 0;
99
100 return treeScope.getElementById(id);
101 }
102
addSupportedAttributes(HashSet<QualifiedName> & supportedAttributes)103 void SVGURIReference::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
104 {
105 supportedAttributes.add(XLinkNames::hrefAttr);
106 }
107
parseAttribute(const QualifiedName & name,const AtomicString & value,SVGParsingError & parseError)108 bool SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value, SVGParsingError& parseError)
109 {
110 if (name.matches(XLinkNames::hrefAttr)) {
111 m_href->setBaseValueAsString(value, parseError);
112 return true;
113 }
114 return false;
115 }
116
117 }
118