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 | |
Torne (Richard Coles) | 5d92fed | 2014-06-20 14:52:37 +0100 | [diff] [blame] | 32 | #include "core/MediaTypeNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 33 | #include "core/css/MediaQueryExp.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 34 | #include "core/html/parser/HTMLParserIdioms.h" |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 35 | #include "wtf/NonCopyingSort.h" |
| 36 | #include "wtf/text/StringBuilder.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | // http://dev.w3.org/csswg/cssom/#serialize-a-media-query |
| 41 | String MediaQuery::serialize() const |
| 42 | { |
| 43 | StringBuilder result; |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 44 | switch (m_restrictor) { |
| 45 | case MediaQuery::Only: |
| 46 | result.append("only "); |
| 47 | break; |
| 48 | case MediaQuery::Not: |
| 49 | result.append("not "); |
| 50 | break; |
| 51 | case MediaQuery::None: |
| 52 | break; |
| 53 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 54 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 55 | if (m_expressions->isEmpty()) { |
| 56 | result.append(m_mediaType); |
| 57 | return result.toString(); |
| 58 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 59 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 60 | if (m_mediaType != MediaTypeNames::all || m_restrictor != None) { |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 61 | result.append(m_mediaType); |
| 62 | result.append(" and "); |
| 63 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 64 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 65 | result.append(m_expressions->at(0)->serialize()); |
| 66 | for (size_t i = 1; i < m_expressions->size(); ++i) { |
| 67 | result.append(" and "); |
| 68 | result.append(m_expressions->at(i)->serialize()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 69 | } |
| 70 | return result.toString(); |
| 71 | } |
| 72 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 73 | static bool expressionCompare(const OwnPtrWillBeMember<MediaQueryExp>& a, const OwnPtrWillBeMember<MediaQueryExp>& b) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 74 | { |
| 75 | return codePointCompare(a->serialize(), b->serialize()) < 0; |
| 76 | } |
| 77 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 78 | PassOwnPtrWillBeRawPtr<MediaQuery> MediaQuery::createNotAll() |
| 79 | { |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 80 | return adoptPtrWillBeNoop(new MediaQuery(MediaQuery::Not, MediaTypeNames::all, nullptr)); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtrWillBeRawPtr<ExpressionHeapVector> expressions) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 84 | : m_restrictor(r) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 85 | , m_mediaType(attemptStaticStringCreation(mediaType.lower())) |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 86 | , m_expressions(expressions) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 87 | { |
| 88 | if (!m_expressions) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 89 | m_expressions = adoptPtrWillBeNoop(new ExpressionHeapVector); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompare); |
| 94 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 95 | // Remove all duplicated expressions. |
| 96 | MediaQueryExp* key = 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 97 | for (int i = m_expressions->size() - 1; i >= 0; --i) { |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 98 | MediaQueryExp* exp = m_expressions->at(i).get(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 99 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 100 | if (key && *exp == *key) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 101 | m_expressions->remove(i); |
| 102 | else |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 103 | key = exp; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | MediaQuery::MediaQuery(const MediaQuery& o) |
| 108 | : m_restrictor(o.m_restrictor) |
| 109 | , m_mediaType(o.m_mediaType) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 110 | , m_expressions(adoptPtrWillBeNoop(new ExpressionHeapVector(o.m_expressions->size()))) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 111 | , m_serializationCache(o.m_serializationCache) |
| 112 | { |
| 113 | for (unsigned i = 0; i < m_expressions->size(); ++i) |
| 114 | (*m_expressions)[i] = o.m_expressions->at(i)->copy(); |
| 115 | } |
| 116 | |
| 117 | MediaQuery::~MediaQuery() |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | // http://dev.w3.org/csswg/cssom/#compare-media-queries |
| 122 | bool MediaQuery::operator==(const MediaQuery& other) const |
| 123 | { |
| 124 | return cssText() == other.cssText(); |
| 125 | } |
| 126 | |
| 127 | // http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries |
| 128 | String MediaQuery::cssText() const |
| 129 | { |
| 130 | if (m_serializationCache.isNull()) |
| 131 | const_cast<MediaQuery*>(this)->m_serializationCache = serialize(); |
| 132 | |
| 133 | return m_serializationCache; |
| 134 | } |
| 135 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 136 | void MediaQuery::trace(Visitor* visitor) |
| 137 | { |
| 138 | // We don't support tracing of vectors of OwnPtrs (ie. OwnPtr<Vector<OwnPtr<MediaQuery> > >). |
| 139 | // Since this is a transitional object we are just ifdef'ing it out when oilpan is not enabled. |
| 140 | #if ENABLE(OILPAN) |
| 141 | visitor->trace(m_expressions); |
| 142 | #endif |
| 143 | } |
| 144 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 145 | } |