hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 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" |
dino@apple.com | 332a340 | 2018-04-18 22:13:50 +0000 | [diff] [blame] | 30 | #include "HTMLAnchorElement.h" |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 31 | #include "HTMLImageElement.h" |
simon.fraser@apple.com | f610b8a | 2017-08-16 22:12:12 +0000 | [diff] [blame] | 32 | #include "Logging.h" |
dino@apple.com | ba6857da | 2018-06-13 23:20:49 +0000 | [diff] [blame] | 33 | #include "RuntimeEnabledFeatures.h" |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 34 | #include <wtf/IsoMallocInlines.h> |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 35 | |
| 36 | namespace WebCore { |
| 37 | |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 38 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLPictureElement); |
| 39 | |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 40 | HTMLPictureElement::HTMLPictureElement(const QualifiedName& tagName, Document& document) |
| 41 | : HTMLElement(tagName, document) |
| 42 | { |
| 43 | } |
| 44 | |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 45 | HTMLPictureElement::~HTMLPictureElement() |
| 46 | { |
hyatt@apple.com | 3c598473 | 2016-01-26 20:07:07 +0000 | [diff] [blame] | 47 | document().removeViewportDependentPicture(*this); |
timothy@apple.com | de46f17 | 2018-11-06 19:53:13 +0000 | [diff] [blame] | 48 | document().removeAppearanceDependentPicture(*this); |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
rniwa@webkit.org | ca24089 | 2017-06-07 08:02:46 +0000 | [diff] [blame] | 51 | void HTMLPictureElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument) |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 52 | { |
cdumez@apple.com | 33db059 | 2016-11-17 00:39:55 +0000 | [diff] [blame] | 53 | oldDocument.removeViewportDependentPicture(*this); |
timothy@apple.com | de46f17 | 2018-11-06 19:53:13 +0000 | [diff] [blame] | 54 | oldDocument.removeAppearanceDependentPicture(*this); |
rniwa@webkit.org | ca24089 | 2017-06-07 08:02:46 +0000 | [diff] [blame] | 55 | HTMLElement::didMoveToNewDocument(oldDocument, newDocument); |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 56 | sourcesChanged(); |
| 57 | } |
| 58 | |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 59 | Ref<HTMLPictureElement> HTMLPictureElement::create(const QualifiedName& tagName, Document& document) |
| 60 | { |
| 61 | return adoptRef(*new HTMLPictureElement(tagName, document)); |
| 62 | } |
| 63 | |
| 64 | void HTMLPictureElement::sourcesChanged() |
| 65 | { |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 66 | for (auto& element : childrenOfType<HTMLImageElement>(*this)) |
commit-queue@webkit.org | d8a9932 | 2017-12-15 01:39:29 +0000 | [diff] [blame] | 67 | element.selectImageSource(); |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 68 | } |
| 69 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 70 | bool HTMLPictureElement::viewportChangeAffectedPicture() const |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 71 | { |
jiewen_tan@apple.com | 667a08c | 2017-11-03 09:32:05 +0000 | [diff] [blame] | 72 | auto documentElement = makeRefPtr(document().documentElement()); |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 73 | MediaQueryEvaluator evaluator { document().printing() ? "print" : "screen", document(), documentElement ? documentElement->computedStyle() : nullptr }; |
| 74 | for (auto& result : m_viewportDependentMediaQueryResults) { |
simon.fraser@apple.com | f610b8a | 2017-08-16 22:12:12 +0000 | [diff] [blame] | 75 | LOG(MediaQueries, "HTMLPictureElement %p viewportChangeAffectedPicture evaluating media queries", this); |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 76 | if (evaluator.evaluate(result.expression) != result.result) |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
timothy@apple.com | de46f17 | 2018-11-06 19:53:13 +0000 | [diff] [blame] | 82 | bool 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.com | 5237a66 | 2018-04-25 15:16:51 +0000 | [diff] [blame] | 94 | #if USE(SYSTEM_PREVIEW) |
dino@apple.com | 332a340 | 2018-04-18 22:13:50 +0000 | [diff] [blame] | 95 | bool HTMLPictureElement::isSystemPreviewImage() const |
| 96 | { |
dino@apple.com | ba6857da | 2018-06-13 23:20:49 +0000 | [diff] [blame] | 97 | if (!RuntimeEnabledFeatures::sharedFeatures().systemPreviewEnabled()) |
| 98 | return false; |
| 99 | |
dino@apple.com | 332a340 | 2018-04-18 22:13:50 +0000 | [diff] [blame] | 100 | const auto* parent = parentElement(); |
| 101 | if (!is<HTMLAnchorElement>(parent)) |
| 102 | return false; |
| 103 | return downcast<HTMLAnchorElement>(parent)->isSystemPreviewLink(); |
| 104 | } |
dino@apple.com | 5237a66 | 2018-04-25 15:16:51 +0000 | [diff] [blame] | 105 | #endif |
dino@apple.com | 332a340 | 2018-04-18 22:13:50 +0000 | [diff] [blame] | 106 | |
hyatt@apple.com | 341a866 | 2015-12-02 20:13:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |