| 1 | /* | 
|---|
| 2 | * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> | 
|---|
| 3 | * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> | 
|---|
| 4 | * Copyright (C) 2018-2019 Apple Inc. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * This library is free software; you can redistribute it and/or | 
|---|
| 7 | * modify it under the terms of the GNU Library General Public | 
|---|
| 8 | * License as published by the Free Software Foundation; either | 
|---|
| 9 | * version 2 of the License, or (at your option) any later version. | 
|---|
| 10 | * | 
|---|
| 11 | * This library is distributed in the hope that it will be useful, | 
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 14 | * Library General Public License for more details. | 
|---|
| 15 | * | 
|---|
| 16 | * You should have received a copy of the GNU Library General Public License | 
|---|
| 17 | * along with this library; see the file COPYING.LIB.  If not, write to | 
|---|
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 
|---|
| 19 | * Boston, MA 02110-1301, USA. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include "config.h" | 
|---|
| 23 | #include "SVGURIReference.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "Document.h" | 
|---|
| 26 | #include "Element.h" | 
|---|
| 27 | #include "SVGElement.h" | 
|---|
| 28 | #include <wtf/URL.h> | 
|---|
| 29 | #include "XLinkNames.h" | 
|---|
| 30 |  | 
|---|
| 31 | namespace WebCore { | 
|---|
| 32 |  | 
|---|
| 33 | SVGURIReference::SVGURIReference(SVGElement* contextElement) | 
|---|
| 34 | : m_href(SVGAnimatedString::create(contextElement)) | 
|---|
| 35 | { | 
|---|
| 36 | static std::once_flag onceFlag; | 
|---|
| 37 | std::call_once(onceFlag, [] { | 
|---|
| 38 | PropertyRegistry::registerProperty<SVGNames::hrefAttr, &SVGURIReference::m_href>(); | 
|---|
| 39 | PropertyRegistry::registerProperty<XLinkNames::hrefAttr, &SVGURIReference::m_href>(); | 
|---|
| 40 | }); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | bool SVGURIReference::isKnownAttribute(const QualifiedName& attributeName) | 
|---|
| 44 | { | 
|---|
| 45 | return PropertyRegistry::isKnownAttribute(attributeName); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | void SVGURIReference::parseAttribute(const QualifiedName& name, const AtomicString& value) | 
|---|
| 49 | { | 
|---|
| 50 | if (isKnownAttribute(name)) | 
|---|
| 51 | m_href->setBaseValInternal(value); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | String SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const Document& document) | 
|---|
| 55 | { | 
|---|
| 56 | size_t start = url.find('#'); | 
|---|
| 57 | if (start == notFound) | 
|---|
| 58 | return emptyString(); | 
|---|
| 59 |  | 
|---|
| 60 | URL base = start ? URL(document.baseURL(), url.substring(0, start)) : document.baseURL(); | 
|---|
| 61 | String fragmentIdentifier = url.substring(start); | 
|---|
| 62 | URL kurl(base, fragmentIdentifier); | 
|---|
| 63 | if (equalIgnoringFragmentIdentifier(kurl, document.url())) | 
|---|
| 64 | return fragmentIdentifier.substring(1); | 
|---|
| 65 |  | 
|---|
| 66 | // The url doesn't have any fragment identifier. | 
|---|
| 67 | return emptyString(); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | auto SVGURIReference::targetElementFromIRIString(const String& iri, const TreeScope& treeScope, RefPtr<Document> externalDocument) -> TargetElementResult | 
|---|
| 71 | { | 
|---|
| 72 | // If there's no fragment identifier contained within the IRI string, we can't lookup an element. | 
|---|
| 73 | size_t startOfFragmentIdentifier = iri.find('#'); | 
|---|
| 74 | if (startOfFragmentIdentifier == notFound) | 
|---|
| 75 | return { }; | 
|---|
| 76 |  | 
|---|
| 77 | // Exclude the '#' character when determining the fragmentIdentifier. | 
|---|
| 78 | auto id = iri.substring(startOfFragmentIdentifier + 1); | 
|---|
| 79 | if (id.isEmpty()) | 
|---|
| 80 | return { }; | 
|---|
| 81 |  | 
|---|
| 82 | auto& document = treeScope.documentScope(); | 
|---|
| 83 | auto url = document.completeURL(iri); | 
|---|
| 84 | if (externalDocument) { | 
|---|
| 85 | // Enforce that the referenced url matches the url of the document that we've loaded for it! | 
|---|
| 86 | ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url())); | 
|---|
| 87 | return { externalDocument->getElementById(id), WTFMove(id) }; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | // Exit early if the referenced url is external, and we have no externalDocument given. | 
|---|
| 91 | if (isExternalURIReference(iri, document)) | 
|---|
| 92 | return { nullptr, WTFMove(id) }; | 
|---|
| 93 |  | 
|---|
| 94 | return { treeScope.getElementById(id), WTFMove(id) }; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|