OSDN Git Service

am 3c54ece0: am 5dc34a85: activeDocumentLoader() causes crash in WebCoreFrameBridge.cpp
[android-x86/external-webkit.git] / WebCore / css / MediaQuery.cpp
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 "StringBuilder.h"
34
35 #include <algorithm>
36
37 namespace WebCore {
38
39 // http://dev.w3.org/csswg/cssom/#serialize-a-media-query
40 String MediaQuery::serialize() const
41 {
42     StringBuilder result;
43
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     }
54
55     if (m_expressions->isEmpty()) {
56         result.append(m_mediaType);
57         return result.toString();
58     }
59
60     if (m_mediaType != "all" || m_restrictor != None) {
61         result.append(m_mediaType);
62         result.append(" and ");
63     }
64
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());
69     }
70     return result.toString();
71 }
72
73 static bool expressionCompare(const MediaQueryExp* a, const MediaQueryExp* b)
74 {
75     return codePointCompare(a->serialize(), b->serialize()) < 0;
76 }
77
78 MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Vector<MediaQueryExp*> > exprs)
79     : m_restrictor(r)
80     , m_mediaType(mediaType.lower())
81     , m_expressions(exprs)
82     , m_ignored(false)
83 {
84     if (!m_expressions) {
85         m_expressions = new Vector<MediaQueryExp*>;
86         return;
87     }
88
89     std::sort(m_expressions->begin(), m_expressions->end(), expressionCompare);
90
91     // remove all duplicated expressions
92     String key;
93     for (int i = m_expressions->size() - 1; i >= 0; --i) {
94
95         // if not all of the expressions is valid the media query must be ignored.
96         if (!m_ignored)
97             m_ignored = !m_expressions->at(i)->isValid();
98
99         if (m_expressions->at(i)->serialize() == key) {
100             MediaQueryExp* item = m_expressions->at(i);
101             m_expressions->remove(i);
102             delete item;
103         } else
104             key = m_expressions->at(i)->serialize();
105     }
106 }
107
108 MediaQuery::~MediaQuery()
109 {
110     deleteAllValues(*m_expressions);
111 }
112
113 // http://dev.w3.org/csswg/cssom/#compare-media-queries
114 bool MediaQuery::operator==(const MediaQuery& other) const
115 {
116     return cssText() == other.cssText();
117 }
118
119 // http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries
120 String MediaQuery::cssText() const
121 {
122     if (m_serializationCache.isNull())
123         const_cast<MediaQuery*>(this)->m_serializationCache = serialize();
124
125     return m_serializationCache;
126 }
127
128 } //namespace