blob: f5a322ad68a79e5f6a262645fc3e74bf944289d6 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * CSS Media Query
3 *
4 * Copyright (C) 2005, 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>.
5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "core/css/MediaQuery.h"
31
32#include "core/css/MediaQueryExp.h"
33#include "core/dom/WebCoreMemoryInstrumentation.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010034#include "wtf/MemoryInstrumentationVector.h"
35#include "wtf/NonCopyingSort.h"
36#include "wtf/text/StringBuilder.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010037
38namespace WebCore {
39
40// http://dev.w3.org/csswg/cssom/#serialize-a-media-query
41String MediaQuery::serialize() const
42{
43 StringBuilder result;
44 if (!m_ignored) {
45 switch (m_restrictor) {
46 case MediaQuery::Only:
47 result.append("only ");
48 break;
49 case MediaQuery::Not:
50 result.append("not ");
51 break;
52 case MediaQuery::None:
53 break;
54 }
55
56 if (m_expressions->isEmpty()) {
57 result.append(m_mediaType);
58 return result.toString();
59 }
60
61 if (m_mediaType != "all" || m_restrictor != None) {
62 result.append(m_mediaType);
63 result.append(" and ");
64 }
65
66 result.append(m_expressions->at(0)->serialize());
67 for (size_t i = 1; i < m_expressions->size(); ++i) {
68 result.append(" and ");
69 result.append(m_expressions->at(i)->serialize());
70 }
71 } else {
72 // If query is invalid, serialized text should turn into "not all".
73 result.append("not all");
74 }
75 return result.toString();
76}
77
78static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<MediaQueryExp>& b)
79{
80 return codePointCompare(a->serialize(), b->serialize()) < 0;
81}
82
83
84MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > exprs)
85 : m_restrictor(r)
86 , m_mediaType(mediaType.lower())
87 , m_expressions(exprs)
88 , m_ignored(false)
89{
90 if (!m_expressions) {
91 m_expressions = adoptPtr(new Vector<OwnPtr<MediaQueryExp> >);
92 return;
93 }
94
95 nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompare);
96
97 // remove all duplicated expressions
98 String key;
99 for (int i = m_expressions->size() - 1; i >= 0; --i) {
100
101 // if not all of the expressions is valid the media query must be ignored.
102 if (!m_ignored)
103 m_ignored = !m_expressions->at(i)->isValid();
104
105 if (m_expressions->at(i)->serialize() == key)
106 m_expressions->remove(i);
107 else
108 key = m_expressions->at(i)->serialize();
109 }
110}
111
112MediaQuery::MediaQuery(const MediaQuery& o)
113 : m_restrictor(o.m_restrictor)
114 , m_mediaType(o.m_mediaType)
115 , m_expressions(adoptPtr(new Vector<OwnPtr<MediaQueryExp> >(o.m_expressions->size())))
116 , m_ignored(o.m_ignored)
117 , m_serializationCache(o.m_serializationCache)
118{
119 for (unsigned i = 0; i < m_expressions->size(); ++i)
120 (*m_expressions)[i] = o.m_expressions->at(i)->copy();
121}
122
123MediaQuery::~MediaQuery()
124{
125}
126
127// http://dev.w3.org/csswg/cssom/#compare-media-queries
128bool MediaQuery::operator==(const MediaQuery& other) const
129{
130 return cssText() == other.cssText();
131}
132
133// http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries
134String MediaQuery::cssText() const
135{
136 if (m_serializationCache.isNull())
137 const_cast<MediaQuery*>(this)->m_serializationCache = serialize();
138
139 return m_serializationCache;
140}
141
142void MediaQuery::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
143{
144 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS);
145 info.addMember(m_mediaType, "mediaType");
146 info.addMember(m_expressions, "expressions");
147 info.addMember(m_serializationCache, "serializationCache");
148}
149
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100150}