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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
* Copyright (C) 2010-2015 Google Inc. All rights reserved.
*
* 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 "HTMLImageElement.h"
#include "CSSPropertyNames.h"
#include "CSSSerializationContext.h"
#include "CSSValueKeywords.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "CommonAtomStrings.h"
#include "Editor.h"
#include "ElementChildIteratorInlines.h"
#include "ElementRareData.h"
#include "EventLoop.h"
#include "EventNames.h"
#include "HTMLAnchorElement.h"
#include "HTMLAttachmentElement.h"
#include "HTMLDocument.h"
#include "HTMLFormElement.h"
#include "HTMLImageLoader.h"
#include "HTMLMapElement.h"
#include "HTMLParserIdioms.h"
#include "HTMLPictureElement.h"
#include "HTMLSourceElement.h"
#include "HTMLSrcsetParser.h"
#include "JSRequestPriority.h"
#include "LazyLoadImageObserver.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "MIMETypeRegistry.h"
#include "MediaQueryEvaluator.h"
#include "MouseEvent.h"
#include "NodeName.h"
#include "NodeTraversal.h"
#include "PlatformMouseEvent.h"
#include "RenderBoxInlines.h"
#include "RenderElementInlines.h"
#include "RenderImage.h"
#include "RenderView.h"
#include "RequestPriority.h"
#include "ScriptController.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "SizesAttributeParser.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/StringBuilder.h>
#if ENABLE(SERVICE_CONTROLS)
#include "ImageControlsMac.h"
#endif
#if ENABLE(SPATIAL_IMAGE_CONTROLS)
#include "SpatialImageControls.h"
#endif
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(HTMLImageElement);
using namespace HTMLNames;
HTMLImageElement::HTMLImageElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
: HTMLElement(tagName, document, { TypeFlag::HasCustomStyleResolveCallbacks, TypeFlag::HasDidMoveToNewDocument })
, FormAssociatedElement(form)
, ActiveDOMObject(document)
, m_imageLoader(makeUniqueWithoutRefCountedCheck<HTMLImageLoader>(*this))
, m_imageDevicePixelRatio(1.0f)
{
ASSERT(hasTagName(imgTag));
}
Ref<HTMLImageElement> HTMLImageElement::create(Document& document)
{
auto image = adoptRef(*new HTMLImageElement(imgTag, document));
image->suspendIfNeeded();
return image;
}
Ref<HTMLImageElement> HTMLImageElement::create(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
{
auto image = adoptRef(*new HTMLImageElement(tagName, document, form));
image->suspendIfNeeded();
return image;
}
HTMLImageElement::~HTMLImageElement()
{
disconnectFromIntersectionObservers();
document().removeDynamicMediaQueryDependentImage(*this);
setForm(nullptr);
#if ENABLE(ACCESSIBILITY_ANIMATION_CONTROL)
if (RefPtr page = document().page())
page->removeIndividuallyPlayingAnimationElement(*this);
#endif
}
void HTMLImageElement::resetFormOwner()
{
setForm(HTMLFormElement::findClosestFormAncestor(*this));
}
void HTMLImageElement::setFormInternal(RefPtr<HTMLFormElement>&& newForm)
{
if (auto* form = FormAssociatedElement::form())
form->unregisterImgElement(*this);
FormAssociatedElement::setFormInternal(newForm.copyRef());
if (newForm)
newForm->registerImgElement(*this);
}
void HTMLImageElement::formOwnerRemovedFromTree(const Node& formRoot)
{
auto& rootNode = traverseToRootNode(); // Do not rely on rootNode() because our IsInTreeScope can be outdated.
if (&rootNode != &formRoot)
setForm(nullptr);
}
Ref<HTMLImageElement> HTMLImageElement::createForLegacyFactoryFunction(Document& document, std::optional<unsigned> width, std::optional<unsigned> height)
{
auto image = adoptRef(*new HTMLImageElement(imgTag, document));
if (width)
image->setWidth(width.value());
if (height)
image->setHeight(height.value());
image->suspendIfNeeded();
return image;
}
bool HTMLImageElement::hasPresentationalHintsForAttribute(const QualifiedName& name) const
{
switch (name.nodeName()) {
case AttributeNames::widthAttr:
case AttributeNames::heightAttr:
case AttributeNames::borderAttr:
case AttributeNames::vspaceAttr:
case AttributeNames::hspaceAttr:
case AttributeNames::valignAttr:
return true;
default:
break;
}
return HTMLElement::hasPresentationalHintsForAttribute(name);
}
void HTMLImageElement::collectPresentationalHintsForAttribute(const QualifiedName& name, const AtomString& value, MutableStyleProperties& style)
{
switch (name.nodeName()) {
case AttributeNames::widthAttr:
addHTMLMultiLengthToStyle(style, CSSPropertyWidth, value);
applyAspectRatioFromWidthAndHeightAttributesToStyle(value, attributeWithoutSynchronization(heightAttr), style);
break;
case AttributeNames::heightAttr:
addHTMLMultiLengthToStyle(style, CSSPropertyHeight, value);
applyAspectRatioFromWidthAndHeightAttributesToStyle(attributeWithoutSynchronization(widthAttr), value, style);
break;
case AttributeNames::borderAttr:
applyBorderAttributeToStyle(value, style);
break;
case AttributeNames::vspaceAttr:
addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
break;
case AttributeNames::hspaceAttr:
addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
break;
case AttributeNames::alignAttr:
applyAlignmentAttributeToStyle(value, style);
break;
case AttributeNames::valignAttr:
addPropertyToPresentationalHintStyle(style, CSSPropertyVerticalAlign, value);
break;
default:
HTMLElement::collectPresentationalHintsForAttribute(name, value, style);
break;
}
}
void HTMLImageElement::collectExtraStyleForPresentationalHints(MutableStyleProperties& style)
{
if (!sourceElement())
return;
auto& widthAttrFromSource = sourceElement()->attributeWithoutSynchronization(widthAttr);
auto& heightAttrFromSource = sourceElement()->attributeWithoutSynchronization(heightAttr);
// If both width and height attributes of <source> is undefined, the style's value should not
// be overwritten. Otherwise, <souce> will overwrite it. I.e., if <source> only has one attribute
// defined, the other one and aspect-ratio shouldn't be set to auto.
if (widthAttrFromSource.isNull() && heightAttrFromSource.isNull())
return;
if (!widthAttrFromSource.isNull())
addHTMLLengthToStyle(style, CSSPropertyWidth, widthAttrFromSource);
else
addPropertyToPresentationalHintStyle(style, CSSPropertyWidth, CSSValueAuto);
if (!heightAttrFromSource.isNull())
addHTMLLengthToStyle(style, CSSPropertyHeight, heightAttrFromSource);
else
addPropertyToPresentationalHintStyle(style, CSSPropertyHeight, CSSValueAuto);
if (!widthAttrFromSource.isNull() && !heightAttrFromSource.isNull())
applyAspectRatioFromWidthAndHeightAttributesToStyle(widthAttrFromSource, heightAttrFromSource, style);
else
addPropertyToPresentationalHintStyle(style, CSSPropertyAspectRatio, CSSValueAuto);
}
const AtomString& HTMLImageElement::imageSourceURL() const
{
return m_bestFitImageURL.isEmpty() ? attributeWithoutSynchronization(srcAttr) : m_bestFitImageURL;
}
const AtomString& HTMLImageElement::currentSrc()
{
if (m_currentSrc.isNull()) {
if (!m_currentURL.isEmpty())
m_currentSrc = AtomString(m_currentURL.string());
}
return m_currentSrc;
}
void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidate& candidate)
{
m_bestFitImageURL = candidate.string.toAtomString();
m_currentURL = document().completeURL(imageSourceURL());
m_currentSrc = { };
if (candidate.density >= 0)
m_imageDevicePixelRatio = 1 / candidate.density;
if (CheckedPtr renderImage = dynamicDowncast<RenderImage>(renderer()))
renderImage->setImageDevicePixelRatio(m_imageDevicePixelRatio);
}
static String extractMIMETypeFromTypeAttributeForLookup(const String& typeAttribute)
{
auto semicolonIndex = typeAttribute.find(';');
if (semicolonIndex == notFound)
return typeAttribute.trim(isASCIIWhitespace);
return StringView(typeAttribute).left(semicolonIndex).trim(isASCIIWhitespace<UChar>).toStringWithoutCopying();
}
ImageCandidate HTMLImageElement::bestFitSourceFromPictureElement()
{
RefPtr picture = pictureElement();
if (!picture)
return { };
ImageCandidate candidate;
for (RefPtr<Node> child = picture->firstChild(); child && child != this; child = child->nextSibling()) {
auto* source = dynamicDowncast<HTMLSourceElement>(*child);
if (!source)
continue;
auto& srcset = source->attributeWithoutSynchronization(srcsetAttr);
if (srcset.isEmpty())
continue;
auto& typeAttribute = source->attributeWithoutSynchronization(typeAttr);
if (!typeAttribute.isNull()) {
auto type = extractMIMETypeFromTypeAttributeForLookup(typeAttribute);
if (!type.isEmpty() && !MIMETypeRegistry::isSupportedImageVideoOrSVGMIMEType(type))
continue;
}
RefPtr documentElement = document().documentElement();
MQ::MediaQueryEvaluator evaluator { document().printing() ? printAtom() : screenAtom(), document(), documentElement ? documentElement->computedStyle() : nullptr };
auto& queries = source->parsedMediaAttribute(document());
LOG(MediaQueries, "HTMLImageElement %p bestFitSourceFromPictureElement evaluating media queries", this);
auto result = evaluator.evaluate(queries);
if (!evaluator.collectDynamicDependencies(queries).isEmpty())
m_dynamicMediaQueryResults.append({ queries, result });
if (!result)
continue;
SizesAttributeParser sizesParser(source->attributeWithoutSynchronization(sizesAttr).string(), document());
m_dynamicMediaQueryResults.appendVector(sizesParser.dynamicMediaQueryResults());
auto sourceSize = sizesParser.length();
candidate = bestFitSourceForImageAttributes(document().deviceScaleFactor(), nullAtom(), srcset, sourceSize, [&](auto& candidate) {
return m_imageLoader->shouldIgnoreCandidateWhenLoadingFromArchive(candidate);
});
if (!candidate.isEmpty()) {
setSourceElement(source);
break;
}
}
return candidate;
}
void HTMLImageElement::evaluateDynamicMediaQueryDependencies()
{
RefPtr documentElement = document().documentElement();
MQ::MediaQueryEvaluator evaluator { document().printing() ? printAtom() : screenAtom(), document(), documentElement ? documentElement->computedStyle() : nullptr };
auto hasChanges = [&] {
for (auto& results : m_dynamicMediaQueryResults) {
if (results.result != evaluator.evaluate(results.mediaQueryList))
return true;
}
return false;
}();
if (!hasChanges)
return;
selectImageSource(RelevantMutation::No);
}
void HTMLImageElement::selectImageSource(RelevantMutation relevantMutation)
{
m_dynamicMediaQueryResults = { };
document().removeDynamicMediaQueryDependentImage(*this);
// First look for the best fit source from our <picture> parent if we have one.
ImageCandidate candidate = bestFitSourceFromPictureElement();
if (candidate.isEmpty()) {
setSourceElement(nullptr);
auto srcAttribute = attributeWithoutSynchronization(srcAttr);
auto srcsetAttribute = attributeWithoutSynchronization(srcsetAttr);
// This is extremely common case. We should not invoke SizesAttributeParser at all.
if (srcsetAttribute.isNull()) {
if (srcAttribute.isNull())
candidate = { };
else
candidate = ImageCandidate(StringViewWithUnderlyingString(srcAttribute, srcAttribute), DescriptorParsingResult(), ImageCandidate::SrcOrigin);
} else {
// If we don't have a <picture> or didn't find a source, then we use our own attributes.
SizesAttributeParser sizesParser(attributeWithoutSynchronization(sizesAttr).string(), document());
m_dynamicMediaQueryResults.appendVector(sizesParser.dynamicMediaQueryResults());
auto sourceSize = sizesParser.length();
candidate = bestFitSourceForImageAttributes(document().deviceScaleFactor(), srcAttribute, srcsetAttribute, sourceSize, [&](auto& candidate) {
return m_imageLoader->shouldIgnoreCandidateWhenLoadingFromArchive(candidate);
});
}
}
setBestFitURLAndDPRFromImageCandidate(candidate);
m_imageLoader->updateFromElementIgnoringPreviousError(relevantMutation);
if (!m_dynamicMediaQueryResults.isEmpty())
document().addDynamicMediaQueryDependentImage(*this);
}
bool HTMLImageElement::hasLazyLoadableAttributeValue(StringView attributeValue)
{
return equalLettersIgnoringASCIICase(attributeValue, "lazy"_s);
}
void HTMLImageElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
HTMLElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
switch (name.nodeName()) {
case AttributeNames::altAttr:
if (auto* renderImage = dynamicDowncast<RenderImage>(renderer()))
renderImage->updateAltText();
break;
case AttributeNames::srcAttr:
case AttributeNames::srcsetAttr:
case AttributeNames::sizesAttr:
if (oldValue != newValue)
selectImageSource(RelevantMutation::Yes);
else
m_imageLoader->updateFromElementIgnoringPreviousErrorToSameValue();
break;
case AttributeNames::usemapAttr:
if (isInTreeScope() && !m_parsedUsemap.isNull())
treeScope().removeImageElementByUsemap(m_parsedUsemap, *this);
m_parsedUsemap = parseHTMLHashNameReference(newValue);
if (isInTreeScope() && !m_parsedUsemap.isNull())
treeScope().addImageElementByUsemap(m_parsedUsemap, *this);
break;
case AttributeNames::loadingAttr:
// No action needed for eager to lazy transition.
if (!hasLazyLoadableAttributeValue(newValue))
loadDeferredImage();
break;
case AttributeNames::referrerpolicyAttr: {
auto oldReferrerPolicy = parseReferrerPolicy(oldValue, ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
auto newReferrerPolicy = parseReferrerPolicy(newValue, ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
if (oldReferrerPolicy != newReferrerPolicy)
m_imageLoader->updateFromElementIgnoringPreviousError(RelevantMutation::Yes);
break;
}
case AttributeNames::crossoriginAttr:
if (parseCORSSettingsAttribute(oldValue) != parseCORSSettingsAttribute(newValue))
m_imageLoader->updateFromElementIgnoringPreviousError(RelevantMutation::Yes);
break;
case AttributeNames::nameAttr: {
bool willHaveName = !newValue.isEmpty();
if (auto* document = dynamicDowncast<HTMLDocument>(this->document()); m_hadNameBeforeAttributeChanged != willHaveName && isConnected() && !isInShadowTree() && document) {
const AtomString& id = getIdAttribute();
if (!id.isEmpty() && id != getNameAttribute()) {
if (willHaveName)
document->addDocumentNamedItem(id, *this);
else
document->removeDocumentNamedItem(id, *this);
}
}
m_hadNameBeforeAttributeChanged = willHaveName;
break;
}
#if ENABLE(SPATIAL_IMAGE_CONTROLS)
case AttributeNames::controlsAttr:
SpatialImageControls::updateSpatialImageControls(*this);
break;
#endif
default:
break;
}
#if ENABLE(SERVICE_CONTROLS)
if (isImageMenuEnabled())
ImageControlsMac::updateImageControls(*this);
#endif
}
void HTMLImageElement::loadDeferredImage()
{
m_imageLoader->loadDeferredImage();
}
const AtomString& HTMLImageElement::altText() const
{
// lets figure out the alt text.. magic stuff
// http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
// also heavily discussed by Hixie on bugzilla
const AtomString& alt = attributeWithoutSynchronization(altAttr);
if (!alt.isNull())
return alt;
// fall back to title attribute
return attributeWithoutSynchronization(titleAttr);
}
RenderPtr<RenderElement> HTMLImageElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
if (style.hasContent())
return RenderElement::createFor(*this, WTFMove(style));
return createRenderer<RenderImage>(RenderObject::Type::Image, *this, WTFMove(style), nullptr, m_imageDevicePixelRatio);
}
bool HTMLImageElement::isReplaced(const RenderStyle& style) const
{
return !style.hasContent();
}
bool HTMLImageElement::canStartSelection() const
{
if (shadowRoot())
return HTMLElement::canStartSelection();
return false;
}
bool HTMLImageElement::isInteractiveContent() const
{
return hasAttributeWithoutSynchronization(usemapAttr);
}
void HTMLImageElement::didAttachRenderers()
{
CheckedPtr renderImage = dynamicDowncast<RenderImage>(renderer());
if (!renderImage)
return;
if (m_imageLoader->hasPendingBeforeLoadEvent())
return;
#if ENABLE(SERVICE_CONTROLS)
ImageControlsMac::updateImageControls(*this);
#endif
RenderImageResource& renderImageResource = renderImage->imageResource();
if (renderImageResource.cachedImage())
return;
renderImageResource.setCachedImage(m_imageLoader->protectedImage());
// If we have no image at all because we have no src attribute, set
// image height and width for the alt text instead.
if (!m_imageLoader->image() && !renderImageResource.cachedImage())
renderImage->setImageSizeForAltText();
}
Node::InsertedIntoAncestorResult HTMLImageElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
FormAssociatedElement::elementInsertedIntoAncestor(*this, insertionType);
if (!form())
resetFormOwner();
// Insert needs to complete first, before we start updating the loader. Loader dispatches events which could result
// in callbacks back to this node.
Node::InsertedIntoAncestorResult insertNotificationRequest = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (insertionType.treeScopeChanged && !m_parsedUsemap.isNull())
treeScope().addImageElementByUsemap(m_parsedUsemap, *this);
if (auto* parentPicture = dynamicDowncast<HTMLPictureElement>(parentOfInsertedTree); parentPicture && &parentOfInsertedTree == parentElement()) {
// FIXME: When the hack in HTMLConstructionSite::createHTMLElementOrFindCustomElementInterface to eagerly call setPictureElement is removed, we can just assert !pictureElement().
ASSERT(!pictureElement() || pictureElement() == &parentOfInsertedTree);
setPictureElement(parentPicture);
selectImageSource(RelevantMutation::Yes);
return insertNotificationRequest;
}
// If we have been inserted from a renderer-less document,
// our loader may have not fetched the image, so do it now.
if (insertionType.connectedToDocument && !m_imageLoader->image())
m_imageLoader->updateFromElement();
return insertNotificationRequest;
}
void HTMLImageElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
if (removalType.treeScopeChanged && !m_parsedUsemap.isNull())
oldParentOfRemovedTree.treeScope().removeImageElementByUsemap(m_parsedUsemap, *this);
if (is<HTMLPictureElement>(oldParentOfRemovedTree) && !parentElement()) {
ASSERT(pictureElement() == &oldParentOfRemovedTree);
setPictureElement(nullptr);
selectImageSource(RelevantMutation::Yes);
}
HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
FormAssociatedElement::elementRemovedFromAncestor(*this, removalType);
}
HTMLPictureElement* HTMLImageElement::pictureElement() const
{
return m_pictureElement.get();
}
void HTMLImageElement::setPictureElement(HTMLPictureElement* pictureElement)
{
m_pictureElement = pictureElement;
}
unsigned HTMLImageElement::width()
{
if (inRenderedDocument())
protectedDocument()->updateLayoutIgnorePendingStylesheets({ LayoutOptions::ContentVisibilityForceLayout }, this);
if (!renderer()) {
// check the attribute first for an explicit pixel value
auto optionalWidth = parseHTMLNonNegativeInteger(attributeWithoutSynchronization(widthAttr));
if (optionalWidth)
return optionalWidth.value();
// if the image is available, use its width
if (m_imageLoader->image())
return m_imageLoader->image()->imageSizeForRenderer(renderer(), 1.0f).width().toUnsigned();
}
RenderBox* box = renderBox();
if (!box)
return 0;
LayoutRect contentRect = box->contentBoxRect();
return adjustForAbsoluteZoom(snappedIntRect(contentRect).width(), *box);
}
unsigned HTMLImageElement::height()
{
if (inRenderedDocument())
protectedDocument()->updateLayoutIgnorePendingStylesheets({ LayoutOptions::ContentVisibilityForceLayout }, this);
if (!renderer()) {
// check the attribute first for an explicit pixel value
auto optionalHeight = parseHTMLNonNegativeInteger(attributeWithoutSynchronization(heightAttr));
if (optionalHeight)
return optionalHeight.value();
// if the image is available, use its height
if (m_imageLoader->image())
return m_imageLoader->image()->imageSizeForRenderer(renderer(), 1.0f).height().toUnsigned();
}
RenderBox* box = renderBox();
if (!box)
return 0;
LayoutRect contentRect = box->contentBoxRect();
return adjustForAbsoluteZoom(snappedIntRect(contentRect).height(), *box);
}
float HTMLImageElement::effectiveImageDevicePixelRatio() const
{
if (!m_imageLoader->image())
return 1.0f;
auto* image = m_imageLoader->image()->image();
if (image && image->drawsSVGImage())
return 1.0f;
return m_imageDevicePixelRatio;
}
unsigned HTMLImageElement::naturalWidth() const
{
if (!m_imageLoader->image())
return 0;
return m_imageLoader->image()->unclampedImageSizeForRenderer(renderer(), effectiveImageDevicePixelRatio()).width().toUnsigned();
}
unsigned HTMLImageElement::naturalHeight() const
{
if (!m_imageLoader->image())
return 0;
return m_imageLoader->image()->unclampedImageSizeForRenderer(renderer(), effectiveImageDevicePixelRatio()).height().toUnsigned();
}
bool HTMLImageElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name() == srcAttr
|| attribute.name() == lowsrcAttr
|| attribute.name() == longdescAttr
|| (attribute.name() == usemapAttr && attribute.value().string()[0] != '#')
|| HTMLElement::isURLAttribute(attribute);
}
bool HTMLImageElement::attributeContainsURL(const Attribute& attribute) const
{
return attribute.name() == srcsetAttr
|| HTMLElement::attributeContainsURL(attribute);
}
String HTMLImageElement::completeURLsInAttributeValue(const URL& base, const Attribute& attribute, ResolveURLs resolveURLs) const
{
if (attribute.name() == srcsetAttr) {
if (resolveURLs == ResolveURLs::No)
return attribute.value();
Vector<ImageCandidate> imageCandidates = parseImageCandidatesFromSrcsetAttribute(StringView(attribute.value()));
if (resolveURLs == ResolveURLs::NoExcludingURLsForPrivacy) {
bool needsToResolveURLs = false;
for (const auto& candidate : imageCandidates) {
auto urlString = candidate.string.toString();
auto completeURL = base.isNull() ? document().completeURL(urlString) : URL(base, urlString);
if (document().shouldMaskURLForBindings(completeURL)) {
needsToResolveURLs = true;
break;
}
}
if (!needsToResolveURLs)
return attribute.value();
}
StringBuilder result;
for (const auto& candidate : imageCandidates) {
if (&candidate != &imageCandidates[0])
result.append(", "_s);
result.append(resolveURLStringIfNeeded(candidate.string.toString(), resolveURLs, base));
if (candidate.density != UninitializedDescriptor)
result.append(' ', candidate.density, 'x');
if (candidate.resourceWidth != UninitializedDescriptor)
result.append(' ', candidate.resourceWidth, 'w');
}
return result.toString();
}
return HTMLElement::completeURLsInAttributeValue(base, attribute, resolveURLs);
}
Attribute HTMLImageElement::replaceURLsInAttributeValue(const Attribute& attribute, const CSS::SerializationContext& serializationContext) const
{
if (attribute.name() != srcsetAttr)
return attribute;
if (serializationContext.replacementURLStrings.isEmpty())
return attribute;
return Attribute { srcsetAttr, AtomString { replaceURLsInSrcsetAttribute(*this, StringView(attribute.value()), serializationContext) } };
}
bool HTMLImageElement::matchesUsemap(const AtomString& name) const
{
return m_parsedUsemap == name;
}
RefPtr<HTMLMapElement> HTMLImageElement::associatedMapElement() const
{
return treeScope().getImageMap(m_parsedUsemap);
}
const AtomString& HTMLImageElement::alt() const
{
return attributeWithoutSynchronization(altAttr);
}
void HTMLImageElement::setHeight(unsigned value)
{
setUnsignedIntegralAttribute(heightAttr, value);
}
URL HTMLImageElement::src() const
{
return document().completeURL(attributeWithoutSynchronization(srcAttr));
}
void HTMLImageElement::setSrc(const AtomString& value)
{
setAttributeWithoutSynchronization(srcAttr, value);
}
void HTMLImageElement::setWidth(unsigned value)
{
setUnsignedIntegralAttribute(widthAttr, value);
}
int HTMLImageElement::x() const
{
protectedDocument()->updateLayoutIgnorePendingStylesheets({ LayoutOptions::ContentVisibilityForceLayout }, this);
auto renderer = this->renderer();
if (!renderer)
return 0;
// FIXME: This doesn't work correctly with transforms.
return renderer->localToAbsolute().x();
}
int HTMLImageElement::y() const
{
protectedDocument()->updateLayoutIgnorePendingStylesheets({ LayoutOptions::ContentVisibilityForceLayout }, this);
auto renderer = this->renderer();
if (!renderer)
return 0;
// FIXME: This doesn't work correctly with transforms.
return renderer->localToAbsolute().y();
}
bool HTMLImageElement::complete() const
{
return m_imageLoader->imageComplete();
}
void HTMLImageElement::setDecoding(AtomString&& decodingMode)
{
setAttributeWithoutSynchronization(decodingAttr, WTFMove(decodingMode));
}
String HTMLImageElement::decoding() const
{
switch (decodingMode()) {
case DecodingMode::Auto:
break;
case DecodingMode::Synchronous:
return "sync"_s;
case DecodingMode::Asynchronous:
return "async"_s;
}
return autoAtom();
}
DecodingMode HTMLImageElement::decodingMode() const
{
const AtomString& decodingMode = attributeWithoutSynchronization(decodingAttr);
if (equalLettersIgnoringASCIICase(decodingMode, "sync"_s))
return DecodingMode::Synchronous;
if (equalLettersIgnoringASCIICase(decodingMode, "async"_s))
return DecodingMode::Asynchronous;
return DecodingMode::Auto;
}
void HTMLImageElement::decode(Ref<DeferredPromise>&& promise)
{
return m_imageLoader->decode(WTFMove(promise));
}
void HTMLImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
{
HTMLElement::addSubresourceAttributeURLs(urls);
addSubresourceURL(urls, document().completeURL(imageSourceURL()));
// FIXME: What about when the usemap attribute begins with "#"?
addSubresourceURL(urls, document().completeURL(attributeWithoutSynchronization(usemapAttr)));
}
void HTMLImageElement::addCandidateSubresourceURLs(ListHashSet<URL>& urls) const
{
auto src = attributeWithoutSynchronization(srcAttr);
if (!src.isEmpty()) {
URL url { resolveURLStringIfNeeded(src) };
if (!url.isNull())
urls.add(url);
}
getURLsFromSrcsetAttribute(*this, attributeWithoutSynchronization(srcsetAttr), urls);
}
void HTMLImageElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument)
{
ActiveDOMObject::didMoveToNewDocument(newDocument);
oldDocument.removeDynamicMediaQueryDependentImage(*this);
selectImageSource(RelevantMutation::No);
m_imageLoader->elementDidMoveToNewDocument(oldDocument);
HTMLElement::didMoveToNewDocument(oldDocument, newDocument);
if (RefPtr element = pictureElement())
element->sourcesChanged();
}
bool HTMLImageElement::isServerMap() const
{
if (!hasAttributeWithoutSynchronization(ismapAttr))
return false;
const AtomString& usemap = attributeWithoutSynchronization(usemapAttr);
// If the usemap attribute starts with '#', it refers to a map element in the document.
if (usemap.string()[0] == '#')
return false;
return document().completeURL(usemap).isEmpty();
}
void HTMLImageElement::setCrossOrigin(const AtomString& value)
{
setAttributeWithoutSynchronization(crossoriginAttr, value);
}
String HTMLImageElement::crossOrigin() const
{
return parseCORSSettingsAttribute(attributeWithoutSynchronization(crossoriginAttr));
}
bool HTMLImageElement::allowsOrientationOverride() const
{
if (auto* cachedImage = this->cachedImage())
return cachedImage->allowsOrientationOverride();
return true;
}
Image* HTMLImageElement::image() const
{
if (auto* cachedImage = this->cachedImage())
return cachedImage->image();
return nullptr;
}
bool HTMLImageElement::allowsAnimation() const
{
if (auto* image = this->image())
return image->allowsAnimation().value_or(document().page() ? document().page()->imageAnimationEnabled() : false);
return false;
}
#if ENABLE(ACCESSIBILITY_ANIMATION_CONTROL)
void HTMLImageElement::setAllowsAnimation(std::optional<bool> allowsAnimation)
{
if (!document().settings().imageAnimationControlEnabled())
return;
if (auto* image = this->image()) {
image->setAllowsAnimation(allowsAnimation);
if (auto* renderer = this->renderer())
renderer->repaint();
if (RefPtr page = document().page()) {
if (allowsAnimation.value_or(false))
page->addIndividuallyPlayingAnimationElement(*this);
else
page->removeIndividuallyPlayingAnimationElement(*this);
}
}
}
#endif
#if ENABLE(ATTACHMENT_ELEMENT)
void HTMLImageElement::setAttachmentElement(Ref<HTMLAttachmentElement>&& attachment)
{
AttachmentAssociatedElement::setAttachmentElement(WTFMove(attachment));
#if ENABLE(SERVICE_CONTROLS)
bool shouldEnableImageMenu = true;
#if ENABLE(MULTI_REPRESENTATION_HEIC)
shouldEnableImageMenu = !isMultiRepresentationHEIC();
#endif
setImageMenuEnabled(shouldEnableImageMenu);
#endif // ENABLE(SERVICE_CONTROLS)
}
#endif // ENABLE(ATTACHMENT_ELEMENT)
#if ENABLE(SERVICE_CONTROLS)
bool HTMLImageElement::childShouldCreateRenderer(const Node& child) const
{
return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child);
}
#endif
#if PLATFORM(IOS_FAMILY)
// FIXME: We should find a better place for the touch callout logic. See rdar://problem/48937767.
bool HTMLImageElement::willRespondToMouseClickEventsWithEditability(Editability editability, IgnoreTouchCallout ignoreTouchCallout) const
{
auto renderer = this->renderer();
if (ignoreTouchCallout == IgnoreTouchCallout::No && (!renderer || renderer->style().touchCalloutEnabled()))
return true;
return HTMLElement::willRespondToMouseClickEventsWithEditability(editability);
}
bool HTMLImageElement::willRespondToMouseClickEventsWithEditability(Editability editability) const
{
return willRespondToMouseClickEventsWithEditability(editability, IgnoreTouchCallout::No);
}
#endif
#if USE(SYSTEM_PREVIEW)
bool HTMLImageElement::isSystemPreviewImage() const
{
if (!document().settings().systemPreviewEnabled())
return false;
auto* parent = parentElement();
if (auto* anchorElement = dynamicDowncast<HTMLAnchorElement>(parent))
return anchorElement->isSystemPreviewLink();
if (auto* pictureElement = dynamicDowncast<HTMLPictureElement>(parent))
return pictureElement->isSystemPreviewImage();
return false;
}
#endif
#if ENABLE(MULTI_REPRESENTATION_HEIC)
bool HTMLImageElement::isMultiRepresentationHEIC() const
{
if (!m_sourceElement)
return false;
auto& typeAttribute = m_sourceElement->attributeWithoutSynchronization(typeAttr);
return typeAttribute == "image/x-apple-adaptive-glyph"_s;
}
#endif
void HTMLImageElement::copyNonAttributePropertiesFromElement(const Element& source)
{
#if ENABLE(ATTACHMENT_ELEMENT)
auto& sourceImage = downcast<HTMLImageElement>(source);
copyAttachmentAssociatedPropertiesFromElement(sourceImage);
#endif
Element::copyNonAttributePropertiesFromElement(source);
}
CachedImage* HTMLImageElement::cachedImage() const
{
return m_imageLoader->image();
}
void HTMLImageElement::setLoadManually(bool loadManually)
{
m_imageLoader->setLoadManually(loadManually);
}
bool HTMLImageElement::virtualHasPendingActivity() const
{
return m_imageLoader->hasPendingActivity();
}
size_t HTMLImageElement::pendingDecodePromisesCountForTesting() const
{
return m_imageLoader->pendingDecodePromisesCountForTesting();
}
bool HTMLImageElement::usesSrcsetOrPicture() const
{
return !attributeWithoutSynchronization(srcsetAttr).isNull() || !!pictureElement();
}
AtomString HTMLImageElement::srcsetForBindings() const
{
return getAttributeForBindings(srcsetAttr);
}
void HTMLImageElement::setSrcsetForBindings(const AtomString& value)
{
setAttributeWithoutSynchronization(srcsetAttr, value);
}
const AtomString& HTMLImageElement::loadingForBindings() const
{
auto& attributeValue = attributeWithoutSynchronization(HTMLNames::loadingAttr);
return hasLazyLoadableAttributeValue(attributeValue) ? lazyAtom() : eagerAtom();
}
void HTMLImageElement::setLoadingForBindings(const AtomString& value)
{
setAttributeWithoutSynchronization(loadingAttr, value);
}
bool HTMLImageElement::isDeferred() const
{
return m_imageLoader->isDeferred();
}
bool HTMLImageElement::isLazyLoadable() const
{
if (!document().frame() || !document().frame()->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return false;
return hasLazyLoadableAttributeValue(attributeWithoutSynchronization(HTMLNames::loadingAttr));
}
void HTMLImageElement::setReferrerPolicyForBindings(const AtomString& value)
{
setAttributeWithoutSynchronization(referrerpolicyAttr, value);
}
String HTMLImageElement::referrerPolicyForBindings() const
{
return referrerPolicyToString(referrerPolicy());
}
ReferrerPolicy HTMLImageElement::referrerPolicy() const
{
return parseReferrerPolicy(attributeWithoutSynchronization(referrerpolicyAttr), ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
}
HTMLSourceElement* HTMLImageElement::sourceElement() const
{
return m_sourceElement.get();
}
void HTMLImageElement::setSourceElement(HTMLSourceElement* sourceElement)
{
if (m_sourceElement == sourceElement)
return;
m_sourceElement = sourceElement;
invalidateAttributeMapping();
}
void HTMLImageElement::invalidateAttributeMapping()
{
ensureUniqueElementData().setPresentationalHintStyleIsDirty(true);
invalidateStyle();
}
Ref<Element> HTMLImageElement::cloneElementWithoutAttributesAndChildren(Document& document, CustomElementRegistry*)
{
auto clone = create(document);
#if ENABLE(ATTACHMENT_ELEMENT)
cloneAttachmentAssociatedElementWithoutAttributesAndChildren(clone, document);
#endif
return clone;
}
void HTMLImageElement::setFetchPriorityForBindings(const AtomString& value)
{
setAttributeWithoutSynchronization(fetchpriorityAttr, value);
}
String HTMLImageElement::fetchPriorityForBindings() const
{
return convertEnumerationToString(fetchPriority());
}
RequestPriority HTMLImageElement::fetchPriority() const
{
return parseEnumerationFromString<RequestPriority>(attributeWithoutSynchronization(fetchpriorityAttr)).value_or(RequestPriority::Auto);
}
bool HTMLImageElement::originClean(const SecurityOrigin& origin) const
{
UNUSED_PARAM(origin);
auto* cachedImage = this->cachedImage();
if (!cachedImage)
return true;
RefPtr image = cachedImage->image();
if (!image)
return true;
if (image->renderingTaintsOrigin())
return false;
if (image->sourceURL().protocolIsData())
return true;
if (cachedImage->isCORSCrossOrigin())
return false;
ASSERT(cachedImage->origin());
ASSERT(origin.toString() == cachedImage->origin()->toString());
return true;
}
IntersectionObserverData& HTMLImageElement::ensureIntersectionObserverData()
{
if (!m_intersectionObserverData)
m_intersectionObserverData = makeUnique<IntersectionObserverData>();
return *m_intersectionObserverData;
}
IntersectionObserverData* HTMLImageElement::intersectionObserverDataIfExists()
{
return m_intersectionObserverData.get();
}
}
|