#include "config.h"
#include "SVGAElement.h"
#include "Attr.h"
#include "Attribute.h"
#include "Document.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderTypes.h"
#include "HTMLAnchorElement.h"
#include "HTMLParserIdioms.h"
#include "KeyboardEvent.h"
#include "MouseEvent.h"
#include "PlatformMouseEvent.h"
#include "RenderSVGInline.h"
#include "RenderSVGText.h"
#include "RenderSVGTransformableContainer.h"
#include "ResourceRequest.h"
#include "SVGElementInstance.h"
#include "SVGNames.h"
#include "SVGSMILElement.h"
#include "XLinkNames.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
using namespace HTMLNames;
DEFINE_ANIMATED_STRING(SVGAElement, SVGNames::targetAttr, SVGTarget, svgTarget)
DEFINE_ANIMATED_STRING(SVGAElement, XLinkNames::hrefAttr, Href, href)
DEFINE_ANIMATED_BOOLEAN(SVGAElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGAElement)
REGISTER_LOCAL_ANIMATED_PROPERTY(svgTarget)
REGISTER_LOCAL_ANIMATED_PROPERTY(href)
REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
END_REGISTER_ANIMATED_PROPERTIES
inline SVGAElement::SVGAElement(const QualifiedName& tagName, Document& document)
: SVGGraphicsElement(tagName, document)
{
ASSERT(hasTagName(SVGNames::aTag));
registerAnimatedPropertiesForSVGAElement();
}
PassRefPtr<SVGAElement> SVGAElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(new SVGAElement(tagName, document));
}
String SVGAElement::title() const
{
const AtomicString& title = fastGetAttribute(XLinkNames::titleAttr);
if (!title.isEmpty())
return title;
return SVGElement::title();
}
bool SVGAElement::isSupportedAttribute(const QualifiedName& attrName)
{
static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
if (supportedAttributes.get().isEmpty()) {
SVGURIReference::addSupportedAttributes(supportedAttributes);
SVGLangSpace::addSupportedAttributes(supportedAttributes);
SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
supportedAttributes.get().add(SVGNames::targetAttr);
}
return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
}
void SVGAElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
if (!isSupportedAttribute(name)) {
SVGGraphicsElement::parseAttribute(name, value);
return;
}
if (name == SVGNames::targetAttr) {
setSVGTargetBaseValue(value);
return;
}
if (SVGURIReference::parseAttribute(name, value))
return;
if (SVGLangSpace::parseAttribute(name, value))
return;
if (SVGExternalResourcesRequired::parseAttribute(name, value))
return;
ASSERT_NOT_REACHED();
}
void SVGAElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (!isSupportedAttribute(attrName)) {
SVGGraphicsElement::svgAttributeChanged(attrName);
return;
}
SVGElementInstance::InvalidationGuard invalidationGuard(this);
if (SVGURIReference::isKnownAttribute(attrName)) {
bool wasLink = isLink();
setIsLink(!href().isNull() && !shouldProhibitLinks(this));
if (wasLink != isLink())
setNeedsStyleRecalc();
}
}
RenderPtr<RenderElement> SVGAElement::createElementRenderer(PassRef<RenderStyle> style)
{
if (parentNode() && parentNode()->isSVGElement() && toSVGElement(parentNode())->isTextContent())
return createRenderer<RenderSVGInline>(*this, WTF::move(style));
return createRenderer<RenderSVGTransformableContainer>(*this, WTF::move(style));
}
void SVGAElement::defaultEventHandler(Event* event)
{
if (isLink()) {
if (focused() && isEnterKeyKeydownEvent(event)) {
event->setDefaultHandled();
dispatchSimulatedClick(event);
return;
}
if (isLinkClick(event)) {
String url = stripLeadingAndTrailingHTMLSpaces(href());
if (url[0] == '#') {
Element* targetElement = treeScope().getElementById(url.substringSharingImpl(1));
if (targetElement && isSVGSMILElement(*targetElement)) {
toSVGSMILElement(*targetElement).beginByLinkActivation();
event->setDefaultHandled();
return;
}
if (targetElement && !targetElement->hasTagName(SVGNames::viewTag))
return;
}
String target = this->target();
if (target.isEmpty() && fastGetAttribute(XLinkNames::showAttr) == "new")
target = "_blank";
event->setDefaultHandled();
Frame* frame = document().frame();
if (!frame)
return;
frame->loader().urlSelected(document().completeURL(url), target, event, LockHistory::No, LockBackForwardList::No, MaybeSendReferrer);
return;
}
}
SVGGraphicsElement::defaultEventHandler(event);
}
short SVGAElement::tabIndex() const
{
return Element::tabIndex();
}
bool SVGAElement::supportsFocus() const
{
if (hasEditableStyle())
return SVGGraphicsElement::supportsFocus();
return isLink() || Element::supportsFocus();
}
bool SVGAElement::isFocusable() const
{
if (renderer() && renderer()->absoluteClippedOverflowRect().isEmpty())
return false;
return SVGElement::isFocusable();
}
bool SVGAElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name().localName() == hrefAttr || SVGGraphicsElement::isURLAttribute(attribute);
}
bool SVGAElement::isMouseFocusable() const
{
if (isLink())
return Element::supportsFocus();
return SVGElement::isMouseFocusable();
}
bool SVGAElement::isKeyboardFocusable(KeyboardEvent* event) const
{
if (isFocusable() && Element::supportsFocus())
return SVGElement::isKeyboardFocusable(event);
if (isLink())
return document().frame()->eventHandler().tabsToLinks(event);
return SVGElement::isKeyboardFocusable(event);
}
bool SVGAElement::canStartSelection() const
{
if (!isLink())
return SVGElement::canStartSelection();
return hasEditableStyle();
}
bool SVGAElement::childShouldCreateRenderer(const Node& child) const
{
if (child.hasTagName(SVGNames::aTag))
return false;
if (parentNode() && parentNode()->isSVGElement())
return parentNode()->childShouldCreateRenderer(child);
return SVGElement::childShouldCreateRenderer(child);
}
bool SVGAElement::willRespondToMouseClickEvents()
{
return isLink() || SVGGraphicsElement::willRespondToMouseClickEvents();
}
}