1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Simon Hausmann <hausmann@kde.org>
* Copyright (C) 2003-2016 Apple Inc. All rights reserved.
* (C) 2006 Graham Dennis (graham.dennis@gmail.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "HTMLAnchorElement.h"
#include "DOMTokenList.h"
#include "ElementIterator.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameLoaderTypes.h"
#include "FrameSelection.h"
#include "HTMLCanvasElement.h"
#include "HTMLImageElement.h"
#include "HTMLParserIdioms.h"
#include "KeyboardEvent.h"
#include "MouseEvent.h"
#include "PingLoader.h"
#include "PlatformMouseEvent.h"
#include "RenderImage.h"
#include "ResourceRequest.h"
#include "RuntimeEnabledFeatures.h"
#include "SVGImage.h"
#include "ScriptController.h"
#include "SecurityOrigin.h"
#include "SecurityPolicy.h"
#include "Settings.h"
#include "URLUtils.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace HTMLNames;
HTMLAnchorElement::HTMLAnchorElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
, m_hasRootEditableElementForSelectionOnMouseDown(false)
, m_wasShiftKeyDownOnMouseDown(false)
, m_cachedVisitedLinkHash(0)
{
}
Ref<HTMLAnchorElement> HTMLAnchorElement::create(Document& document)
{
return adoptRef(*new HTMLAnchorElement(aTag, document));
}
Ref<HTMLAnchorElement> HTMLAnchorElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new HTMLAnchorElement(tagName, document));
}
HTMLAnchorElement::~HTMLAnchorElement()
{
clearRootEditableElementForSelectionOnMouseDown();
}
bool HTMLAnchorElement::supportsFocus() const
{
if (hasEditableStyle())
return HTMLElement::supportsFocus();
// If not a link we should still be able to focus the element if it has tabIndex.
return isLink() || HTMLElement::supportsFocus();
}
bool HTMLAnchorElement::isMouseFocusable() const
{
// Only allow links with tabIndex or contentEditable to be mouse focusable.
if (isLink())
return HTMLElement::supportsFocus();
return HTMLElement::isMouseFocusable();
}
static bool hasNonEmptyBox(RenderBoxModelObject* renderer)
{
if (!renderer)
return false;
// Before calling absoluteRects, check for the common case where borderBoundingBox
// is non-empty, since this is a faster check and almost always returns true.
// FIXME: Why do we need to call absoluteRects at all?
if (!renderer->borderBoundingBox().isEmpty())
return true;
// FIXME: Since all we are checking is whether the rects are empty, could we just
// pass in 0,0 for the layout point instead of calling localToAbsolute?
Vector<IntRect> rects;
renderer->absoluteRects(rects, flooredLayoutPoint(renderer->localToAbsolute()));
for (auto& rect : rects) {
if (!rect.isEmpty())
return true;
}
return false;
}
bool HTMLAnchorElement::isKeyboardFocusable(KeyboardEvent& event) const
{
if (!isLink())
return HTMLElement::isKeyboardFocusable(event);
if (!isFocusable())
return false;
if (!document().frame())
return false;
if (!document().frame()->eventHandler().tabsToLinks(event))
return false;
if (!renderer() && ancestorsOfType<HTMLCanvasElement>(*this).first())
return true;
return hasNonEmptyBox(renderBoxModelObject());
}
static void appendServerMapMousePosition(StringBuilder& url, Event& event)
{
if (!is<MouseEvent>(event))
return;
auto& mouseEvent = downcast<MouseEvent>(event);
ASSERT(mouseEvent.target());
auto* target = mouseEvent.target()->toNode();
ASSERT(target);
if (!is<HTMLImageElement>(*target))
return;
auto& imageElement = downcast<HTMLImageElement>(*target);
if (!imageElement.isServerMap())
return;
auto* renderer = imageElement.renderer();
if (!is<RenderImage>(renderer))
return;
// FIXME: This should probably pass UseTransforms in the MapCoordinatesFlags.
auto absolutePosition = downcast<RenderImage>(*renderer).absoluteToLocal(FloatPoint(mouseEvent.pageX(), mouseEvent.pageY()));
url.append('?');
url.appendNumber(std::lround(absolutePosition.x()));
url.append(',');
url.appendNumber(std::lround(absolutePosition.y()));
}
void HTMLAnchorElement::defaultEventHandler(Event& event)
{
if (isLink()) {
if (focused() && isEnterKeyKeydownEvent(event) && treatLinkAsLiveForEventType(NonMouseEvent)) {
event.setDefaultHandled();
dispatchSimulatedClick(&event);
return;
}
if (MouseEvent::canTriggerActivationBehavior(event) && treatLinkAsLiveForEventType(eventType(event))) {
handleClick(event);
return;
}
if (hasEditableStyle()) {
// This keeps track of the editable block that the selection was in (if it was in one) just before the link was clicked
// for the LiveWhenNotFocused editable link behavior
if (event.type() == eventNames().mousedownEvent && is<MouseEvent>(event) && downcast<MouseEvent>(event).button() != RightButton && document().frame()) {
setRootEditableElementForSelectionOnMouseDown(document().frame()->selection().selection().rootEditableElement());
m_wasShiftKeyDownOnMouseDown = downcast<MouseEvent>(event).shiftKey();
} else if (event.type() == eventNames().mouseoverEvent) {
// These are cleared on mouseover and not mouseout because their values are needed for drag events,
// but drag events happen after mouse out events.
clearRootEditableElementForSelectionOnMouseDown();
m_wasShiftKeyDownOnMouseDown = false;
}
}
}
HTMLElement::defaultEventHandler(event);
}
void HTMLAnchorElement::setActive(bool down, bool pause)
{
if (hasEditableStyle()) {
EditableLinkBehavior editableLinkBehavior = document().settings().editableLinkBehavior();
switch (editableLinkBehavior) {
default:
case EditableLinkDefaultBehavior:
case EditableLinkAlwaysLive:
break;
case EditableLinkNeverLive:
return;
// Don't set the link to be active if the current selection is in the same editable block as
// this link
case EditableLinkLiveWhenNotFocused:
if (down && document().frame() && document().frame()->selection().selection().rootEditableElement() == rootEditableElement())
return;
break;
case EditableLinkOnlyLiveWithShiftKey:
return;
}
}
HTMLElement::setActive(down, pause);
}
void HTMLAnchorElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
if (name == hrefAttr) {
bool wasLink = isLink();
setIsLink(!value.isNull() && !shouldProhibitLinks(this));
if (wasLink != isLink())
invalidateStyleForSubtree();
if (isLink()) {
String parsedURL = stripLeadingAndTrailingHTMLSpaces(value);
if (document().isDNSPrefetchEnabled() && document().frame()) {
if (protocolIsInHTTPFamily(parsedURL) || parsedURL.startsWith("//"))
document().frame()->loader().client().prefetchDNS(document().completeURL(parsedURL).host());
}
}
invalidateCachedVisitedLinkHash();
} else if (name == nameAttr || name == titleAttr) {
// Do nothing.
} else if (name == relAttr) {
// Update HTMLAnchorElement::relList() if more rel attributes values are supported.
static NeverDestroyed<AtomicString> noReferrer("noreferrer", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> noOpener("noopener", AtomicString::ConstructFromLiteral);
const bool shouldFoldCase = true;
SpaceSplitString relValue(value, shouldFoldCase);
if (relValue.contains(noReferrer))
m_linkRelations |= Relation::NoReferrer;
if (relValue.contains(noOpener))
m_linkRelations |= Relation::NoOpener;
if (m_relList)
m_relList->associatedAttributeValueChanged(value);
}
else
HTMLElement::parseAttribute(name, value);
}
void HTMLAnchorElement::accessKeyAction(bool sendMouseEvents)
{
dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
}
bool HTMLAnchorElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name().localName() == hrefAttr || HTMLElement::isURLAttribute(attribute);
}
bool HTMLAnchorElement::canStartSelection() const
{
if (!isLink())
return HTMLElement::canStartSelection();
return hasEditableStyle();
}
bool HTMLAnchorElement::draggable() const
{
const AtomicString& value = attributeWithoutSynchronization(draggableAttr);
if (equalLettersIgnoringASCIICase(value, "true"))
return true;
if (equalLettersIgnoringASCIICase(value, "false"))
return false;
return hasAttributeWithoutSynchronization(hrefAttr);
}
URL HTMLAnchorElement::href() const
{
return document().completeURL(stripLeadingAndTrailingHTMLSpaces(attributeWithoutSynchronization(hrefAttr)));
}
void HTMLAnchorElement::setHref(const AtomicString& value)
{
setAttributeWithoutSynchronization(hrefAttr, value);
}
bool HTMLAnchorElement::hasRel(Relation relation) const
{
return m_linkRelations.contains(relation);
}
DOMTokenList& HTMLAnchorElement::relList()
{
if (!m_relList)
m_relList = std::make_unique<DOMTokenList>(*this, HTMLNames::relAttr, [](StringView token) {
return equalIgnoringASCIICase(token, "noreferrer") || equalIgnoringASCIICase(token, "noopener");
});
return *m_relList;
}
const AtomicString& HTMLAnchorElement::name() const
{
return getNameAttribute();
}
int HTMLAnchorElement::tabIndex() const
{
// Skip the supportsFocus check in HTMLElement.
return Element::tabIndex();
}
String HTMLAnchorElement::target() const
{
return attributeWithoutSynchronization(targetAttr);
}
String HTMLAnchorElement::origin() const
{
return SecurityOrigin::create(href()).get().toString();
}
String HTMLAnchorElement::text()
{
return textContent();
}
void HTMLAnchorElement::setText(const String& text)
{
setTextContent(text);
}
bool HTMLAnchorElement::isLiveLink() const
{
return isLink() && treatLinkAsLiveForEventType(m_wasShiftKeyDownOnMouseDown ? MouseEventWithShiftKey : MouseEventWithoutShiftKey);
}
void HTMLAnchorElement::sendPings(const URL& destinationURL)
{
if (!document().frame())
return;
if (!hasAttributeWithoutSynchronization(pingAttr) || !document().settings().hyperlinkAuditingEnabled())
return;
SpaceSplitString pingURLs(attributeWithoutSynchronization(pingAttr), false);
for (unsigned i = 0; i < pingURLs.size(); i++)
PingLoader::sendPing(*document().frame(), document().completeURL(pingURLs[i]), destinationURL);
}
void HTMLAnchorElement::handleClick(Event& event)
{
event.setDefaultHandled();
Frame* frame = document().frame();
if (!frame)
return;
StringBuilder url;
url.append(stripLeadingAndTrailingHTMLSpaces(attributeWithoutSynchronization(hrefAttr)));
appendServerMapMousePosition(url, event);
URL completedURL = document().completeURL(url.toString());
String downloadAttribute;
#if ENABLE(DOWNLOAD_ATTRIBUTE)
if (RuntimeEnabledFeatures::sharedFeatures().downloadAttributeEnabled()) {
// Ignore the download attribute completely if the href URL is cross origin.
bool isSameOrigin = completedURL.protocolIsData() || document().securityOrigin().canRequest(completedURL);
if (isSameOrigin)
downloadAttribute = ResourceResponse::sanitizeSuggestedFilename(attributeWithoutSynchronization(downloadAttr));
else if (hasAttributeWithoutSynchronization(downloadAttr))
document().addConsoleMessage(MessageSource::Security, MessageLevel::Warning, "The download attribute on anchor was ignored because its href URL has a different security origin.");
}
#endif
ShouldSendReferrer shouldSendReferrer = hasRel(Relation::NoReferrer) ? NeverSendReferrer : MaybeSendReferrer;
auto newFrameOpenerPolicy = hasRel(Relation::NoOpener) ? std::make_optional(NewFrameOpenerPolicy::Suppress) : std::nullopt;
frame->loader().urlSelected(completedURL, target(), &event, LockHistory::No, LockBackForwardList::No, shouldSendReferrer, document().shouldOpenExternalURLsPolicyToPropagate(), newFrameOpenerPolicy, downloadAttribute);
sendPings(completedURL);
}
HTMLAnchorElement::EventType HTMLAnchorElement::eventType(Event& event)
{
if (!is<MouseEvent>(event))
return NonMouseEvent;
return downcast<MouseEvent>(event).shiftKey() ? MouseEventWithShiftKey : MouseEventWithoutShiftKey;
}
bool HTMLAnchorElement::treatLinkAsLiveForEventType(EventType eventType) const
{
if (!hasEditableStyle())
return true;
switch (document().settings().editableLinkBehavior()) {
case EditableLinkDefaultBehavior:
case EditableLinkAlwaysLive:
return true;
case EditableLinkNeverLive:
return false;
// If the selection prior to clicking on this link resided in the same editable block as this link,
// and the shift key isn't pressed, we don't want to follow the link.
case EditableLinkLiveWhenNotFocused:
return eventType == MouseEventWithShiftKey || (eventType == MouseEventWithoutShiftKey && rootEditableElementForSelectionOnMouseDown() != rootEditableElement());
case EditableLinkOnlyLiveWithShiftKey:
return eventType == MouseEventWithShiftKey;
}
ASSERT_NOT_REACHED();
return false;
}
bool isEnterKeyKeydownEvent(Event& event)
{
return event.type() == eventNames().keydownEvent && is<KeyboardEvent>(event) && downcast<KeyboardEvent>(event).keyIdentifier() == "Enter";
}
bool shouldProhibitLinks(Element* element)
{
return isInSVGImage(element);
}
bool HTMLAnchorElement::willRespondToMouseClickEvents()
{
return isLink() || HTMLElement::willRespondToMouseClickEvents();
}
typedef HashMap<const HTMLAnchorElement*, RefPtr<Element>> RootEditableElementMap;
static RootEditableElementMap& rootEditableElementMap()
{
static NeverDestroyed<RootEditableElementMap> map;
return map;
}
Element* HTMLAnchorElement::rootEditableElementForSelectionOnMouseDown() const
{
if (!m_hasRootEditableElementForSelectionOnMouseDown)
return 0;
return rootEditableElementMap().get(this);
}
void HTMLAnchorElement::clearRootEditableElementForSelectionOnMouseDown()
{
if (!m_hasRootEditableElementForSelectionOnMouseDown)
return;
rootEditableElementMap().remove(this);
m_hasRootEditableElementForSelectionOnMouseDown = false;
}
void HTMLAnchorElement::setRootEditableElementForSelectionOnMouseDown(Element* element)
{
if (!element) {
clearRootEditableElementForSelectionOnMouseDown();
return;
}
rootEditableElementMap().set(this, element);
m_hasRootEditableElementForSelectionOnMouseDown = true;
}
}
|