1 | /*
|
---|
2 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
|
---|
3 | *
|
---|
4 | * This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Library General Public
|
---|
6 | * License as published by the Free Software Foundation; either
|
---|
7 | * version 2 of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This library is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * Library General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU Library General Public License
|
---|
15 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
17 | * Boston, MA 02110-1301, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "config.h"
|
---|
21 | #include "MediaQueryMatcher.h"
|
---|
22 |
|
---|
23 | #include "Document.h"
|
---|
24 | #include "EventNames.h"
|
---|
25 | #include "Frame.h"
|
---|
26 | #include "FrameView.h"
|
---|
27 | #include "Logging.h"
|
---|
28 | #include "MediaList.h"
|
---|
29 | #include "MediaQueryEvaluator.h"
|
---|
30 | #include "MediaQueryList.h"
|
---|
31 | #include "MediaQueryListEvent.h"
|
---|
32 | #include "MediaQueryParserContext.h"
|
---|
33 | #include "NodeRenderStyle.h"
|
---|
34 | #include "RenderElement.h"
|
---|
35 | #include "StyleResolver.h"
|
---|
36 | #include "StyleScope.h"
|
---|
37 | #include <wtf/text/TextStream.h>
|
---|
38 |
|
---|
39 | namespace WebCore {
|
---|
40 |
|
---|
41 | MediaQueryMatcher::MediaQueryMatcher(Document& document)
|
---|
42 | : m_document(document)
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | MediaQueryMatcher::~MediaQueryMatcher() = default;
|
---|
47 |
|
---|
48 | void MediaQueryMatcher::documentDestroyed()
|
---|
49 | {
|
---|
50 | m_document = nullptr;
|
---|
51 | auto mediaQueryLists = std::exchange(m_mediaQueryLists, { });
|
---|
52 | for (auto& mediaQueryList : mediaQueryLists) {
|
---|
53 | if (mediaQueryList)
|
---|
54 | mediaQueryList->detachFromMatcher();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | String MediaQueryMatcher::mediaType() const
|
---|
59 | {
|
---|
60 | if (!m_document || !m_document->frame() || !m_document->frame()->view())
|
---|
61 | return String();
|
---|
62 |
|
---|
63 | return m_document->frame()->view()->mediaType();
|
---|
64 | }
|
---|
65 |
|
---|
66 | std::unique_ptr<RenderStyle> MediaQueryMatcher::documentElementUserAgentStyle() const
|
---|
67 | {
|
---|
68 | if (!m_document || !m_document->frame())
|
---|
69 | return nullptr;
|
---|
70 |
|
---|
71 | auto* documentElement = m_document->documentElement();
|
---|
72 | if (!documentElement)
|
---|
73 | return nullptr;
|
---|
74 |
|
---|
75 | return m_document->styleScope().resolver().styleForElement(*documentElement, { m_document->renderStyle() }, RuleMatchingBehavior::MatchOnlyUserAgentRules).renderStyle;
|
---|
76 | }
|
---|
77 |
|
---|
78 | bool MediaQueryMatcher::evaluate(const MediaQuerySet& media)
|
---|
79 | {
|
---|
80 | auto style = documentElementUserAgentStyle();
|
---|
81 | if (!style)
|
---|
82 | return false;
|
---|
83 | return MediaQueryEvaluator { mediaType(), *m_document, style.get() }.evaluate(media);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void MediaQueryMatcher::addMediaQueryList(MediaQueryList& list)
|
---|
87 | {
|
---|
88 | ASSERT(!m_mediaQueryLists.contains(&list));
|
---|
89 | m_mediaQueryLists.append(list);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void MediaQueryMatcher::removeMediaQueryList(MediaQueryList& list)
|
---|
93 | {
|
---|
94 | m_mediaQueryLists.removeFirst(&list);
|
---|
95 | }
|
---|
96 |
|
---|
97 | RefPtr<MediaQueryList> MediaQueryMatcher::matchMedia(const String& query)
|
---|
98 | {
|
---|
99 | if (!m_document)
|
---|
100 | return nullptr;
|
---|
101 |
|
---|
102 | auto media = MediaQuerySet::create(query, MediaQueryParserContext(*m_document));
|
---|
103 | bool matches = evaluate(media.get());
|
---|
104 | return MediaQueryList::create(*m_document, *this, WTFMove(media), matches);
|
---|
105 | }
|
---|
106 |
|
---|
107 | void MediaQueryMatcher::evaluateAll()
|
---|
108 | {
|
---|
109 | ASSERT(m_document);
|
---|
110 |
|
---|
111 | ++m_evaluationRound;
|
---|
112 |
|
---|
113 | auto style = documentElementUserAgentStyle();
|
---|
114 | if (!style)
|
---|
115 | return;
|
---|
116 |
|
---|
117 | LOG_WITH_STREAM(MediaQueries, stream << "MediaQueryMatcher::styleResolverChanged " << m_document->url());
|
---|
118 |
|
---|
119 | MediaQueryEvaluator evaluator { mediaType(), *m_document, style.get() };
|
---|
120 |
|
---|
121 | auto mediaQueryLists = m_mediaQueryLists;
|
---|
122 | for (auto& list : mediaQueryLists) {
|
---|
123 | if (!list)
|
---|
124 | continue;
|
---|
125 | bool notify;
|
---|
126 | list->evaluate(evaluator, notify);
|
---|
127 | if (notify) {
|
---|
128 | if (m_document && m_document->quirks().shouldSilenceMediaQueryListChangeEvents())
|
---|
129 | continue;
|
---|
130 |
|
---|
131 | list->dispatchEvent(MediaQueryListEvent::create(eventNames().changeEvent, list->media(), list->matches()));
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | } // namespace WebCore
|
---|