SVGURIReference.cpp [plain text]
#include "config.h"
#include "SVGURIReference.h"
#include "Document.h"
#include "Element.h"
#include "SVGElement.h"
#include <wtf/URL.h>
#include "XLinkNames.h"
namespace WebCore {
SVGURIReference::SVGURIReference(SVGElement* contextElement)
: m_href(SVGAnimatedString::create(contextElement))
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
PropertyRegistry::registerProperty<SVGNames::hrefAttr, &SVGURIReference::m_href>();
PropertyRegistry::registerProperty<XLinkNames::hrefAttr, &SVGURIReference::m_href>();
});
}
bool SVGURIReference::isKnownAttribute(const QualifiedName& attributeName)
{
return PropertyRegistry::isKnownAttribute(attributeName);
}
SVGElement& SVGURIReference::contextElement() const
{
return *m_href->contextElement();
}
void SVGURIReference::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name.matches(SVGNames::hrefAttr))
m_href->setBaseValInternal(value.isNull() ? contextElement().getAttribute(XLinkNames::hrefAttr) : value);
else if (name.matches(XLinkNames::hrefAttr) && !contextElement().hasAttribute(SVGNames::hrefAttr))
m_href->setBaseValInternal(value);
}
String SVGURIReference::fragmentIdentifierFromIRIString(const String& url, const Document& document)
{
size_t start = url.find('#');
if (start == notFound)
return emptyString();
if (!start)
return url.substring(1);
URL base = URL(document.baseURL(), url.substring(0, start));
String fragmentIdentifier = url.substring(start);
URL kurl(base, fragmentIdentifier);
if (equalIgnoringFragmentIdentifier(kurl, document.url()))
return fragmentIdentifier.substring(1);
return emptyString();
}
auto SVGURIReference::targetElementFromIRIString(const String& iri, const TreeScope& treeScope, RefPtr<Document> externalDocument) -> TargetElementResult
{
size_t startOfFragmentIdentifier = iri.find('#');
if (startOfFragmentIdentifier == notFound)
return { };
auto id = iri.substring(startOfFragmentIdentifier + 1);
if (id.isEmpty())
return { };
auto& document = treeScope.documentScope();
auto url = document.completeURL(iri);
if (externalDocument) {
ASSERT(equalIgnoringFragmentIdentifier(url, externalDocument->url()));
return { externalDocument->getElementById(id), WTFMove(id) };
}
if (isExternalURIReference(iri, document))
return { nullptr, WTFMove(id) };
return { treeScope.getElementById(id), WTFMove(id) };
}
bool SVGURIReference::haveLoadedRequiredResources() const
{
if (href().isEmpty() || !isExternalURIReference(href(), contextElement().document()))
return true;
return errorOccurred() || haveFiredLoadEvent();
}
void SVGURIReference::dispatchLoadEvent()
{
if (haveFiredLoadEvent())
return;
setHaveFiredLoadEvent(true);
ASSERT(contextElement().haveLoadedRequiredResources());
contextElement().sendLoadEventIfPossible();
}
}