1 | /* |
2 | * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
3 | * Copyright (C) 2004, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved. |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public License |
16 | * along with this library; see the file COPYING.LIB. If not, write to |
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include "ExceptionOr.h" |
24 | #include "MediaQueryParserContext.h" |
25 | #include <memory> |
26 | #include <wtf/Forward.h> |
27 | #include <wtf/Vector.h> |
28 | |
29 | namespace WTF { |
30 | class TextStream; |
31 | } |
32 | |
33 | namespace WebCore { |
34 | |
35 | class CSSParser; |
36 | class CSSRule; |
37 | class CSSStyleSheet; |
38 | class Document; |
39 | class MediaQuery; |
40 | |
41 | class MediaQuerySet final : public RefCounted<MediaQuerySet> { |
42 | public: |
43 | static Ref<MediaQuerySet> create() |
44 | { |
45 | return adoptRef(*new MediaQuerySet); |
46 | } |
47 | static WEBCORE_EXPORT Ref<MediaQuerySet> create(const String& mediaString, MediaQueryParserContext = MediaQueryParserContext()); |
48 | |
49 | WEBCORE_EXPORT ~MediaQuerySet(); |
50 | |
51 | bool set(const String&); |
52 | bool add(const String&); |
53 | bool remove(const String&); |
54 | |
55 | void addMediaQuery(MediaQuery&&); |
56 | |
57 | const Vector<MediaQuery>& queryVector() const { return m_queries; } |
58 | |
59 | int lastLine() const { return m_lastLine; } |
60 | void setLastLine(int lastLine) { m_lastLine = lastLine; } |
61 | |
62 | WEBCORE_EXPORT String mediaText() const; |
63 | |
64 | Ref<MediaQuerySet> copy() const { return adoptRef(*new MediaQuerySet(*this)); } |
65 | |
66 | void shrinkToFit(); |
67 | |
68 | private: |
69 | MediaQuerySet(); |
70 | WEBCORE_EXPORT MediaQuerySet(const String& mediaQuery); |
71 | MediaQuerySet(const MediaQuerySet&); |
72 | |
73 | int m_lastLine { 0 }; |
74 | Vector<MediaQuery> m_queries; |
75 | }; |
76 | |
77 | class MediaList final : public RefCounted<MediaList> { |
78 | public: |
79 | static Ref<MediaList> create(MediaQuerySet* mediaQueries, CSSStyleSheet* parentSheet) |
80 | { |
81 | return adoptRef(*new MediaList(mediaQueries, parentSheet)); |
82 | } |
83 | static Ref<MediaList> create(MediaQuerySet* mediaQueries, CSSRule* parentRule) |
84 | { |
85 | return adoptRef(*new MediaList(mediaQueries, parentRule)); |
86 | } |
87 | |
88 | WEBCORE_EXPORT ~MediaList(); |
89 | |
90 | unsigned length() const { return m_mediaQueries->queryVector().size(); } |
91 | WEBCORE_EXPORT String item(unsigned index) const; |
92 | WEBCORE_EXPORT ExceptionOr<void> deleteMedium(const String& oldMedium); |
93 | WEBCORE_EXPORT void appendMedium(const String& newMedium); |
94 | |
95 | String mediaText() const { return m_mediaQueries->mediaText(); } |
96 | WEBCORE_EXPORT ExceptionOr<void> setMediaText(const String&); |
97 | |
98 | CSSRule* parentRule() const { return m_parentRule; } |
99 | CSSStyleSheet* parentStyleSheet() const { return m_parentStyleSheet; } |
100 | void clearParentStyleSheet() { ASSERT(m_parentStyleSheet); m_parentStyleSheet = nullptr; } |
101 | void clearParentRule() { ASSERT(m_parentRule); m_parentRule = nullptr; } |
102 | const MediaQuerySet* queries() const { return m_mediaQueries.get(); } |
103 | |
104 | void reattach(MediaQuerySet*); |
105 | |
106 | private: |
107 | MediaList(); |
108 | MediaList(MediaQuerySet*, CSSStyleSheet* parentSheet); |
109 | MediaList(MediaQuerySet*, CSSRule* parentRule); |
110 | |
111 | RefPtr<MediaQuerySet> m_mediaQueries; |
112 | CSSStyleSheet* m_parentStyleSheet { nullptr }; |
113 | CSSRule* m_parentRule { nullptr }; |
114 | }; |
115 | |
116 | // Adds message to inspector console whenever dpi or dpcm values are used for "screen" media. |
117 | // FIXME: Seems strange to have this here in this file, and unclear exactly who should call this and when. |
118 | void reportMediaQueryWarningIfNeeded(Document*, const MediaQuerySet*); |
119 | |
120 | #if !ENABLE(RESOLUTION_MEDIA_QUERY) |
121 | |
122 | inline void reportMediaQueryWarningIfNeeded(Document*, const MediaQuerySet*) |
123 | { |
124 | } |
125 | |
126 | #endif |
127 | |
128 | WTF::TextStream& operator<<(WTF::TextStream&, const MediaQuerySet&); |
129 | WTF::TextStream& operator<<(WTF::TextStream&, const MediaList&); |
130 | |
131 | } // namespace |
132 | |