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 "MediaQueryExp.h" 33#include <wtf/text/StringBuilder.h> 34 35namespace WebCore { 36 37// http://dev.w3.org/csswg/cssom/#serialize-a-media-query 38String MediaQuery::serialize() const 39{ 40 StringBuilder result; 41 if (!m_ignored) { 42 switch (m_restrictor) { 43 case MediaQuery::Only: 44 result.append("only "); 45 break; 46 case MediaQuery::Not: 47 result.append("not "); 48 break; 49 case MediaQuery::None: 50 break; 51 } 52 53 if (m_expressions->isEmpty()) { 54 result.append(m_mediaType); 55 return result.toString(); 56 } 57 58 if (m_mediaType != "all" || m_restrictor != None) { 59 result.append(m_mediaType); 60 result.append(" and "); 61 } 62 63 result.append(m_expressions->at(0)->serialize()); 64 for (size_t i = 1; i < m_expressions->size(); ++i) { 65 result.append(" and "); 66 result.append(m_expressions->at(i)->serialize()); 67 } 68 } else { 69 // If query is invalid, serialized text should turn into "not all". 70 result.append("not all"); 71 } 72 return result.toString(); 73} 74 75MediaQuery::MediaQuery(Restrictor r, const String& mediaType, std::unique_ptr<ExpressionVector> exprs) 76 : m_restrictor(r) 77 , m_mediaType(mediaType.lower()) 78 , m_expressions(WTF::move(exprs)) 79 , m_ignored(false) 80{ 81 if (!m_expressions) { 82 m_expressions = std::make_unique<ExpressionVector>(); 83 return; 84 } 85 86 std::sort(m_expressions->begin(), m_expressions->end(), [](const std::unique_ptr<MediaQueryExp>& a, const std::unique_ptr<MediaQueryExp>& b) { 87 return codePointCompare(a->serialize(), b->serialize()) < 0; 88 }); 89 90 // remove all duplicated expressions 91 String key; 92 for (int i = m_expressions->size() - 1; i >= 0; --i) { 93 94 // if not all of the expressions is valid the media query must be ignored. 95 if (!m_ignored) 96 m_ignored = !m_expressions->at(i)->isValid(); 97 98 if (m_expressions->at(i)->serialize() == key) 99 m_expressions->remove(i); 100 else 101 key = m_expressions->at(i)->serialize(); 102 } 103} 104 105MediaQuery::MediaQuery(const MediaQuery& o) 106 : m_restrictor(o.m_restrictor) 107 , m_mediaType(o.m_mediaType) 108 , m_expressions(std::make_unique<ExpressionVector>(o.m_expressions->size())) 109 , m_ignored(o.m_ignored) 110 , m_serializationCache(o.m_serializationCache) 111{ 112 for (unsigned i = 0; i < m_expressions->size(); ++i) 113 (*m_expressions)[i] = o.m_expressions->at(i)->copy(); 114} 115 116MediaQuery::~MediaQuery() 117{ 118} 119 120// http://dev.w3.org/csswg/cssom/#compare-media-queries 121bool MediaQuery::operator==(const MediaQuery& other) const 122{ 123 return cssText() == other.cssText(); 124} 125 126// http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries 127String MediaQuery::cssText() const 128{ 129 if (m_serializationCache.isNull()) 130 const_cast<MediaQuery*>(this)->m_serializationCache = serialize(); 131 132 return m_serializationCache; 133} 134 135} //namespace 136