1/*
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"
30#include "HTMLAnchorElement.h"
31#include "HTMLImageElement.h"
32#include "Logging.h"
33#include "RuntimeEnabledFeatures.h"
34#include <wtf/IsoMallocInlines.h>
35
36namespace WebCore {
37
38WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLPictureElement);
39
40HTMLPictureElement::HTMLPictureElement(const QualifiedName& tagName, Document& document)
41 : HTMLElement(tagName, document)
42{
43}
44
45HTMLPictureElement::~HTMLPictureElement()
46{
47 document().removeViewportDependentPicture(*this);
48 document().removeAppearanceDependentPicture(*this);
49}
50
51void HTMLPictureElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument)
52{
53 oldDocument.removeViewportDependentPicture(*this);
54 oldDocument.removeAppearanceDependentPicture(*this);
55 HTMLElement::didMoveToNewDocument(oldDocument, newDocument);
56 sourcesChanged();
57}
58
59Ref<HTMLPictureElement> HTMLPictureElement::create(const QualifiedName& tagName, Document& document)
60{
61 return adoptRef(*new HTMLPictureElement(tagName, document));
62}
63
64void HTMLPictureElement::sourcesChanged()
65{
66 for (auto& element : childrenOfType<HTMLImageElement>(*this))
67 element.selectImageSource();
68}
69
70bool HTMLPictureElement::viewportChangeAffectedPicture() const
71{
72 auto documentElement = makeRefPtr(document().documentElement());
73 MediaQueryEvaluator evaluator { document().printing() ? "print" : "screen", document(), documentElement ? documentElement->computedStyle() : nullptr };
74 for (auto& result : m_viewportDependentMediaQueryResults) {
75 LOG(MediaQueries, "HTMLPictureElement %p viewportChangeAffectedPicture evaluating media queries", this);
76 if (evaluator.evaluate(result.expression) != result.result)
77 return true;
78 }
79 return false;
80}
81
82bool 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
94#if USE(SYSTEM_PREVIEW)
95bool HTMLPictureElement::isSystemPreviewImage() const
96{
97 if (!RuntimeEnabledFeatures::sharedFeatures().systemPreviewEnabled())
98 return false;
99
100 const auto* parent = parentElement();
101 if (!is<HTMLAnchorElement>(parent))
102 return false;
103 return downcast<HTMLAnchorElement>(parent)->isSystemPreviewLink();
104}
105#endif
106
107}
108
109