blob: 0046d19c866528f6057d190ddd3e303f06599ebb [file] [log] [blame]
hyatt@apple.com341a8662015-12-02 20:13:50 +00001/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "HTMLPictureElement.h"
28
29#include "ElementChildIterator.h"
dino@apple.com332a3402018-04-18 22:13:50 +000030#include "HTMLAnchorElement.h"
hyatt@apple.com341a8662015-12-02 20:13:50 +000031#include "HTMLImageElement.h"
simon.fraser@apple.comf610b8a2017-08-16 22:12:12 +000032#include "Logging.h"
dino@apple.comba6857da2018-06-13 23:20:49 +000033#include "RuntimeEnabledFeatures.h"
fpizlo@apple.com197cd322018-03-17 06:11:00 +000034#include <wtf/IsoMallocInlines.h>
hyatt@apple.com341a8662015-12-02 20:13:50 +000035
36namespace WebCore {
37
fpizlo@apple.com197cd322018-03-17 06:11:00 +000038WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLPictureElement);
39
hyatt@apple.com341a8662015-12-02 20:13:50 +000040HTMLPictureElement::HTMLPictureElement(const QualifiedName& tagName, Document& document)
41 : HTMLElement(tagName, document)
42{
43}
44
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000045HTMLPictureElement::~HTMLPictureElement()
46{
hyatt@apple.com3c5984732016-01-26 20:07:07 +000047 document().removeViewportDependentPicture(*this);
timothy@apple.comde46f172018-11-06 19:53:13 +000048 document().removeAppearanceDependentPicture(*this);
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000049}
50
rniwa@webkit.orgca240892017-06-07 08:02:46 +000051void HTMLPictureElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument)
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000052{
cdumez@apple.com33db0592016-11-17 00:39:55 +000053 oldDocument.removeViewportDependentPicture(*this);
timothy@apple.comde46f172018-11-06 19:53:13 +000054 oldDocument.removeAppearanceDependentPicture(*this);
rniwa@webkit.orgca240892017-06-07 08:02:46 +000055 HTMLElement::didMoveToNewDocument(oldDocument, newDocument);
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000056 sourcesChanged();
57}
58
hyatt@apple.com341a8662015-12-02 20:13:50 +000059Ref<HTMLPictureElement> HTMLPictureElement::create(const QualifiedName& tagName, Document& document)
60{
61 return adoptRef(*new HTMLPictureElement(tagName, document));
62}
63
64void HTMLPictureElement::sourcesChanged()
65{
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000066 for (auto& element : childrenOfType<HTMLImageElement>(*this))
commit-queue@webkit.orgd8a99322017-12-15 01:39:29 +000067 element.selectImageSource();
hyatt@apple.com341a8662015-12-02 20:13:50 +000068}
69
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000070bool HTMLPictureElement::viewportChangeAffectedPicture() const
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000071{
jiewen_tan@apple.com667a08c2017-11-03 09:32:05 +000072 auto documentElement = makeRefPtr(document().documentElement());
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000073 MediaQueryEvaluator evaluator { document().printing() ? "print" : "screen", document(), documentElement ? documentElement->computedStyle() : nullptr };
74 for (auto& result : m_viewportDependentMediaQueryResults) {
simon.fraser@apple.comf610b8a2017-08-16 22:12:12 +000075 LOG(MediaQueries, "HTMLPictureElement %p viewportChangeAffectedPicture evaluating media queries", this);
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000076 if (evaluator.evaluate(result.expression) != result.result)
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000077 return true;
78 }
79 return false;
80}
81
timothy@apple.comde46f172018-11-06 19:53:13 +000082bool HTMLPictureElement::appearanceChangeAffectedPicture() const
83{
84 auto documentElement = makeRefPtr(document().documentElement());
85 MediaQueryEvaluator evaluator { document().printing() ? "print" : "screen", document(), documentElement ? documentElement->computedStyle() : nullptr };
86 for (auto& result : m_appearanceDependentMediaQueryResults) {
87 LOG(MediaQueries, "HTMLPictureElement %p appearanceChangeAffectedPicture evaluating media queries", this);
88 if (evaluator.evaluate(result.expression) != result.result)
89 return true;
90 }
91 return false;
92}
93
dino@apple.com5237a662018-04-25 15:16:51 +000094#if USE(SYSTEM_PREVIEW)
dino@apple.com332a3402018-04-18 22:13:50 +000095bool HTMLPictureElement::isSystemPreviewImage() const
96{
dino@apple.comba6857da2018-06-13 23:20:49 +000097 if (!RuntimeEnabledFeatures::sharedFeatures().systemPreviewEnabled())
98 return false;
99
dino@apple.com332a3402018-04-18 22:13:50 +0000100 const auto* parent = parentElement();
101 if (!is<HTMLAnchorElement>(parent))
102 return false;
103 return downcast<HTMLAnchorElement>(parent)->isSystemPreviewLink();
104}
dino@apple.com5237a662018-04-25 15:16:51 +0000105#endif
dino@apple.com332a3402018-04-18 22:13:50 +0000106
hyatt@apple.com341a8662015-12-02 20:13:50 +0000107}
108