1/*
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 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 "MediaQuery.h"
31
32#include <wtf/text/StringBuilder.h>
33#include <wtf/text/TextStream.h>
34
35namespace WebCore {
36
37// http://dev.w3.org/csswg/cssom/#serialize-a-media-query
38String MediaQuery::serialize() const
39{
40 if (m_ignored) {
41 // If query is invalid, serialized text should turn into "not all".
42 return "not all"_s;
43 }
44
45 bool shouldOmitMediaType = false;
46 StringBuilder result;
47 switch (m_restrictor) {
48 case MediaQuery::Only:
49 result.appendLiteral("only ");
50 break;
51 case MediaQuery::Not:
52 result.appendLiteral("not ");
53 break;
54 case MediaQuery::None:
55 shouldOmitMediaType = !m_expressions.isEmpty() && m_mediaType == "all";
56 break;
57 }
58 bool needsAnd = false;
59 if (!shouldOmitMediaType) {
60 result.append(m_mediaType);
61 needsAnd = true;
62 }
63 for (auto& expression : m_expressions) {
64 if (needsAnd)
65 result.appendLiteral(" and ");
66 result.append(expression.serialize());
67 needsAnd = true;
68 }
69 return result.toString();
70}
71
72MediaQuery::MediaQuery(Restrictor restrictor, const String& mediaType, Vector<MediaQueryExpression>&& expressions)
73 : m_mediaType(mediaType.convertToASCIILowercase())
74 , m_expressions(WTFMove(expressions))
75 , m_restrictor(restrictor)
76{
77 std::sort(m_expressions.begin(), m_expressions.end(), [](auto& a, auto& b) {
78 return codePointCompare(a.serialize(), b.serialize()) < 0;
79 });
80
81 // Remove all duplicated expressions.
82 String key;
83 for (int i = m_expressions.size() - 1; i >= 0; --i) {
84
85 // If any expression is invalid the media query must be ignored.
86 if (!m_ignored)
87 m_ignored = !m_expressions[i].isValid();
88
89 if (m_expressions[i].serialize() == key)
90 m_expressions.remove(i);
91 else
92 key = m_expressions[i].serialize();
93 }
94}
95
96// http://dev.w3.org/csswg/cssom/#compare-media-queries
97bool MediaQuery::operator==(const MediaQuery& other) const
98{
99 return cssText() == other.cssText();
100}
101
102// http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries
103const String& MediaQuery::cssText() const
104{
105 if (m_serializationCache.isNull())
106 m_serializationCache = serialize();
107 return m_serializationCache;
108}
109
110TextStream& operator<<(TextStream& ts, const MediaQuery& query)
111{
112 ts << query.cssText();
113 return ts;
114}
115
116} //namespace
117