Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 "core/css/MediaQuery.h" |
| 31 | |
| 32 | #include "core/css/MediaQueryExp.h" |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 33 | #include "wtf/NonCopyingSort.h" |
| 34 | #include "wtf/text/StringBuilder.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | // http://dev.w3.org/csswg/cssom/#serialize-a-media-query |
| 39 | String MediaQuery::serialize() const |
| 40 | { |
| 41 | StringBuilder result; |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 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 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 52 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 53 | if (m_expressions->isEmpty()) { |
| 54 | result.append(m_mediaType); |
| 55 | return result.toString(); |
| 56 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 57 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 58 | if (m_mediaType != "all" || m_restrictor != None) { |
| 59 | result.append(m_mediaType); |
| 60 | result.append(" and "); |
| 61 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 62 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 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()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 67 | } |
| 68 | return result.toString(); |
| 69 | } |
| 70 | |
| 71 | static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<MediaQueryExp>& b) |
| 72 | { |
| 73 | return codePointCompare(a->serialize(), b->serialize()) < 0; |
| 74 | } |
| 75 | |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame^] | 76 | MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<ExpressionVector> expressions) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 77 | : m_restrictor(r) |
| 78 | , m_mediaType(mediaType.lower()) |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame^] | 79 | , m_expressions(expressions) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 80 | { |
| 81 | if (!m_expressions) { |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame^] | 82 | m_expressions = adoptPtr(new ExpressionVector); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompare); |
| 87 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 88 | // Remove all duplicated expressions. |
| 89 | MediaQueryExp* key = 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 90 | for (int i = m_expressions->size() - 1; i >= 0; --i) { |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 91 | MediaQueryExp* exp = m_expressions->at(i).get(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 92 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 93 | if (key && *exp == *key) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 94 | m_expressions->remove(i); |
| 95 | else |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 96 | key = exp; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | MediaQuery::MediaQuery(const MediaQuery& o) |
| 101 | : m_restrictor(o.m_restrictor) |
| 102 | , m_mediaType(o.m_mediaType) |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame^] | 103 | , m_expressions(adoptPtr(new ExpressionVector(o.m_expressions->size()))) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 104 | , m_serializationCache(o.m_serializationCache) |
| 105 | { |
| 106 | for (unsigned i = 0; i < m_expressions->size(); ++i) |
| 107 | (*m_expressions)[i] = o.m_expressions->at(i)->copy(); |
| 108 | } |
| 109 | |
| 110 | MediaQuery::~MediaQuery() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | // http://dev.w3.org/csswg/cssom/#compare-media-queries |
| 115 | bool MediaQuery::operator==(const MediaQuery& other) const |
| 116 | { |
| 117 | return cssText() == other.cssText(); |
| 118 | } |
| 119 | |
| 120 | // http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries |
| 121 | String MediaQuery::cssText() const |
| 122 | { |
| 123 | if (m_serializationCache.isNull()) |
| 124 | const_cast<MediaQuery*>(this)->m_serializationCache = serialize(); |
| 125 | |
| 126 | return m_serializationCache; |
| 127 | } |
| 128 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 129 | } |