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