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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
/*
* 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, 2006, 2007, 2008, 2009, 2010 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 "Attribute.h"
#include "DNS.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameLoaderTypes.h"
#include "FrameSelection.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "KeyboardEvent.h"
#include "MouseEvent.h"
#include "PingLoader.h"
#include "PlatformMouseEvent.h"
#include "RenderImage.h"
#include "ResourceRequest.h"
#include "SVGImage.h"
#include "SecurityOrigin.h"
#include "SecurityPolicy.h"
#include "Settings.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_linkRelations(0)
, m_cachedVisitedLinkHash(0)
{
}
PassRefPtr<HTMLAnchorElement> HTMLAnchorElement::create(Document* document)
{
return adoptRef(new HTMLAnchorElement(aTag, document));
}
PassRefPtr<HTMLAnchorElement> HTMLAnchorElement::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new HTMLAnchorElement(tagName, document));
}
HTMLAnchorElement::~HTMLAnchorElement()
{
clearRootEditableElementForSelectionOnMouseDown();
}
// This function does not allow leading spaces before the port number.
static unsigned parsePortFromStringPosition(const String& value, unsigned portStart, unsigned& portEnd)
{
portEnd = portStart;
while (isASCIIDigit(value[portEnd]))
++portEnd;
return value.substring(portStart, portEnd - portStart).toUInt();
}
bool HTMLAnchorElement::supportsFocus() const
{
if (rendererIsEditable())
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
{
#if !(PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(QT))
// Only allow links with tabIndex or contentEditable to be mouse focusable.
// This is our rule for the Mac platform; on many other platforms we focus any link you click on.
if (isLink())
return HTMLElement::supportsFocus();
#endif
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()));
size_t size = rects.size();
for (size_t i = 0; i < size; ++i) {
if (!rects[i].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 (isInCanvasSubtree())
return true;
return hasNonEmptyBox(renderBoxModelObject());
}
static void appendServerMapMousePosition(StringBuilder& url, Event* event)
{
if (!event->isMouseEvent())
return;
ASSERT(event->target());
Node* target = event->target()->toNode();
ASSERT(target);
if (!isHTMLImageElement(target))
return;
HTMLImageElement* imageElement = toHTMLImageElement(target);
if (!imageElement || !imageElement->isServerMap())
return;
if (!imageElement->renderer() || !imageElement->renderer()->isRenderImage())
return;
RenderImage* renderer = toRenderImage(imageElement->renderer());
// FIXME: This should probably pass true for useTransforms.
FloatPoint absolutePosition = renderer->absoluteToLocal(FloatPoint(static_cast<MouseEvent*>(event)->pageX(), static_cast<MouseEvent*>(event)->pageY()));
int x = absolutePosition.x();
int y = absolutePosition.y();
url.append('?');
url.appendNumber(x);
url.append(',');
url.appendNumber(y);
}
void HTMLAnchorElement::defaultEventHandler(Event* event)
{
if (isLink()) {
if (focused() && isEnterKeyKeydownEvent(event) && treatLinkAsLiveForEventType(NonMouseEvent)) {
event->setDefaultHandled();
dispatchSimulatedClick(event);
return;
}
if (isLinkClick(event) && treatLinkAsLiveForEventType(eventType(event))) {
handleClick(event);
return;
}
if (rendererIsEditable()) {
// 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 && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() != RightButton && document()->frame() && document()->frame()->selection()) {
setRootEditableElementForSelectionOnMouseDown(document()->frame()->selection()->rootEditableElement());
m_wasShiftKeyDownOnMouseDown = static_cast<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 (rendererIsEditable()) {
EditableLinkBehavior editableLinkBehavior = EditableLinkDefaultBehavior;
if (Settings* settings = document()->settings())
editableLinkBehavior = 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()->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())
didAffectSelector(AffectedSelectorLink | AffectedSelectorVisited | AffectedSelectorEnabled);
if (isLink()) {
String parsedURL = stripLeadingAndTrailingHTMLSpaces(value);
if (document()->isDNSPrefetchEnabled()) {
if (protocolIs(parsedURL, "http") || protocolIs(parsedURL, "https") || parsedURL.startsWith("//"))
prefetchDNS(document()->completeURL(parsedURL).host());
}
}
invalidateCachedVisitedLinkHash();
} else if (name == nameAttr || name == titleAttr) {
// Do nothing.
} else if (name == relAttr)
setRel(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
{
// FIXME: We probably want this same behavior in SVGAElement too
if (!isLink())
return HTMLElement::canStartSelection();
return rendererIsEditable();
}
bool HTMLAnchorElement::draggable() const
{
// Should be draggable if we have an href attribute.
const AtomicString& value = getAttribute(draggableAttr);
if (equalIgnoringCase(value, "true"))
return true;
if (equalIgnoringCase(value, "false"))
return false;
return hasAttribute(hrefAttr);
}
KURL HTMLAnchorElement::href() const
{
return document()->completeURL(stripLeadingAndTrailingHTMLSpaces(getAttribute(hrefAttr)));
}
void HTMLAnchorElement::setHref(const AtomicString& value)
{
setAttribute(hrefAttr, value);
}
bool HTMLAnchorElement::hasRel(uint32_t relation) const
{
return m_linkRelations & relation;
}
void HTMLAnchorElement::setRel(const String& value)
{
if (SpaceSplitString::spaceSplitStringContainsValue(value, "noreferrer", true))
m_linkRelations |= RelationNoReferrer;
}
const AtomicString& HTMLAnchorElement::name() const
{
return getNameAttribute();
}
short HTMLAnchorElement::tabIndex() const
{
// Skip the supportsFocus check in HTMLElement.
return Element::tabIndex();
}
String HTMLAnchorElement::target() const
{
return getAttribute(targetAttr);
}
String HTMLAnchorElement::hash() const
{
String fragmentIdentifier = href().fragmentIdentifier();
if (fragmentIdentifier.isEmpty())
return emptyString();
return AtomicString(String("#" + fragmentIdentifier));
}
void HTMLAnchorElement::setHash(const String& value)
{
KURL url = href();
if (value[0] == '#')
url.setFragmentIdentifier(value.substring(1));
else
url.setFragmentIdentifier(value);
setHref(url.string());
}
String HTMLAnchorElement::host() const
{
const KURL& url = href();
if (url.hostEnd() == url.pathStart())
return url.host();
if (isDefaultPortForProtocol(url.port(), url.protocol()))
return url.host();
return url.host() + ":" + String::number(url.port());
}
void HTMLAnchorElement::setHost(const String& value)
{
if (value.isEmpty())
return;
KURL url = href();
if (!url.canSetHostOrPort())
return;
size_t separator = value.find(':');
if (!separator)
return;
if (separator == notFound)
url.setHostAndPort(value);
else {
unsigned portEnd;
unsigned port = parsePortFromStringPosition(value, separator + 1, portEnd);
if (!port) {
// http://dev.w3.org/html5/spec/infrastructure.html#url-decomposition-idl-attributes
// specifically goes against RFC 3986 (p3.2) and
// requires setting the port to "0" if it is set to empty string.
url.setHostAndPort(value.substring(0, separator + 1) + "0");
} else {
if (isDefaultPortForProtocol(port, url.protocol()))
url.setHostAndPort(value.substring(0, separator));
else
url.setHostAndPort(value.substring(0, portEnd));
}
}
setHref(url.string());
}
String HTMLAnchorElement::hostname() const
{
return href().host();
}
void HTMLAnchorElement::setHostname(const String& value)
{
// Before setting new value:
// Remove all leading U+002F SOLIDUS ("/") characters.
unsigned i = 0;
unsigned hostLength = value.length();
while (value[i] == '/')
i++;
if (i == hostLength)
return;
KURL url = href();
if (!url.canSetHostOrPort())
return;
url.setHost(value.substring(i));
setHref(url.string());
}
String HTMLAnchorElement::pathname() const
{
return href().path();
}
void HTMLAnchorElement::setPathname(const String& value)
{
KURL url = href();
if (!url.canSetPathname())
return;
if (value[0] == '/')
url.setPath(value);
else
url.setPath("/" + value);
setHref(url.string());
}
String HTMLAnchorElement::port() const
{
if (href().hasPort())
return String::number(href().port());
return emptyString();
}
void HTMLAnchorElement::setPort(const String& value)
{
KURL url = href();
if (!url.canSetHostOrPort())
return;
// http://dev.w3.org/html5/spec/infrastructure.html#url-decomposition-idl-attributes
// specifically goes against RFC 3986 (p3.2) and
// requires setting the port to "0" if it is set to empty string.
unsigned port = value.toUInt();
if (isDefaultPortForProtocol(port, url.protocol()))
url.removePort();
else
url.setPort(port);
setHref(url.string());
}
String HTMLAnchorElement::protocol() const
{
return href().protocol() + ":";
}
void HTMLAnchorElement::setProtocol(const String& value)
{
KURL url = href();
url.setProtocol(value);
setHref(url.string());
}
String HTMLAnchorElement::search() const
{
String query = href().query();
return query.isEmpty() ? emptyString() : "?" + query;
}
String HTMLAnchorElement::origin() const
{
RefPtr<SecurityOrigin> origin = SecurityOrigin::create(href());
return origin->toString();
}
void HTMLAnchorElement::setSearch(const String& value)
{
KURL url = href();
String newSearch = (value[0] == '?') ? value.substring(1) : value;
// Make sure that '#' in the query does not leak to the hash.
url.setQuery(newSearch.replaceWithLiteral('#', "%23"));
setHref(url.string());
}
String HTMLAnchorElement::text()
{
return innerText();
}
String HTMLAnchorElement::toString() const
{
return href().string();
}
bool HTMLAnchorElement::isLiveLink() const
{
return isLink() && treatLinkAsLiveForEventType(m_wasShiftKeyDownOnMouseDown ? MouseEventWithShiftKey : MouseEventWithoutShiftKey);
}
void HTMLAnchorElement::sendPings(const KURL& destinationURL)
{
if (!hasAttribute(pingAttr) || !document()->settings() || !document()->settings()->hyperlinkAuditingEnabled())
return;
SpaceSplitString pingURLs(getAttribute(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(fastGetAttribute(hrefAttr)));
appendServerMapMousePosition(url, event);
KURL kurl = document()->completeURL(url.toString());
#if ENABLE(DOWNLOAD_ATTRIBUTE)
if (hasAttribute(downloadAttr)) {
ResourceRequest request(kurl);
// FIXME: Why are we not calling addExtraFieldsToMainResourceRequest() if this check fails? It sets many important header fields.
if (!hasRel(RelationNoReferrer)) {
String referrer = SecurityPolicy::generateReferrerHeader(document()->referrerPolicy(), kurl, frame->loader()->outgoingReferrer());
if (!referrer.isEmpty())
request.setHTTPReferrer(referrer);
frame->loader()->addExtraFieldsToMainResourceRequest(request);
}
frame->loader()->client()->startDownload(request, fastGetAttribute(downloadAttr));
} else
#endif
frame->loader()->urlSelected(kurl, target(), event, false, false, hasRel(RelationNoReferrer) ? NeverSendReferrer : MaybeSendReferrer);
sendPings(kurl);
}
HTMLAnchorElement::EventType HTMLAnchorElement::eventType(Event* event)
{
if (!event->isMouseEvent())
return NonMouseEvent;
return static_cast<MouseEvent*>(event)->shiftKey() ? MouseEventWithShiftKey : MouseEventWithoutShiftKey;
}
bool HTMLAnchorElement::treatLinkAsLiveForEventType(EventType eventType) const
{
if (!rendererIsEditable())
return true;
Settings* settings = document()->settings();
if (!settings)
return true;
switch (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 && event->isKeyboardEvent() && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "Enter";
}
bool isLinkClick(Event* event)
{
return event->type() == eventNames().clickEvent && (!event->isMouseEvent() || static_cast<MouseEvent*>(event)->button() != RightButton);
}
bool shouldProhibitLinks(Element* element)
{
#if ENABLE(SVG)
return isInSVGImage(element);
#else
return false;
#endif
}
bool HTMLAnchorElement::willRespondToMouseClickEvents()
{
return isLink() || HTMLElement::willRespondToMouseClickEvents();
}
#if ENABLE(MICRODATA)
String HTMLAnchorElement::itemValueText() const
{
return getURLAttribute(hrefAttr);
}
void HTMLAnchorElement::setItemValueText(const String& value, ExceptionCode&)
{
setAttribute(hrefAttr, value);
}
#endif
typedef HashMap<const HTMLAnchorElement*, RefPtr<Element> > RootEditableElementMap;
static RootEditableElementMap& rootEditableElementMap()
{
DEFINE_STATIC_LOCAL(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;
}
}
|