OLD | NEW |
1 /* | 1 /* |
2 * CSS Media Query | 2 * CSS Media Query |
3 * | 3 * |
4 * Copyright (C) 2005, 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>. | 4 * Copyright (C) 2005, 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>. |
5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
6 * | 6 * |
7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
9 * are met: | 9 * are met: |
10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
(...skipping 12 matching lines...) Expand all Loading... |
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "config.h" | 29 #include "config.h" |
30 #include "core/css/MediaQuery.h" | 30 #include "core/css/MediaQuery.h" |
31 | 31 |
32 #include "core/css/MediaQueryExp.h" | 32 #include "core/css/MediaQueryExp.h" |
| 33 #include "core/html/parser/HTMLParserIdioms.h" |
33 #include "wtf/NonCopyingSort.h" | 34 #include "wtf/NonCopyingSort.h" |
34 #include "wtf/text/StringBuilder.h" | 35 #include "wtf/text/StringBuilder.h" |
35 | 36 |
36 namespace WebCore { | 37 namespace WebCore { |
37 | 38 |
| 39 // FIXME - need to add common keywords (e.g. all, only, none, and) to static str
ings |
| 40 |
38 // http://dev.w3.org/csswg/cssom/#serialize-a-media-query | 41 // http://dev.w3.org/csswg/cssom/#serialize-a-media-query |
39 String MediaQuery::serialize() const | 42 String MediaQuery::serialize() const |
40 { | 43 { |
41 StringBuilder result; | 44 StringBuilder result; |
42 switch (m_restrictor) { | 45 switch (m_restrictor) { |
43 case MediaQuery::Only: | 46 case MediaQuery::Only: |
44 result.append("only "); | 47 result.append("only "); |
45 break; | 48 break; |
46 case MediaQuery::Not: | 49 case MediaQuery::Not: |
47 result.append("not "); | 50 result.append("not "); |
(...skipping 18 matching lines...) Expand all Loading... |
66 result.append(m_expressions->at(i)->serialize()); | 69 result.append(m_expressions->at(i)->serialize()); |
67 } | 70 } |
68 return result.toString(); | 71 return result.toString(); |
69 } | 72 } |
70 | 73 |
71 static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<Media
QueryExp>& b) | 74 static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<Media
QueryExp>& b) |
72 { | 75 { |
73 return codePointCompare(a->serialize(), b->serialize()) < 0; | 76 return codePointCompare(a->serialize(), b->serialize()) < 0; |
74 } | 77 } |
75 | 78 |
76 MediaQuery::MediaQuery(Restrictor r, const AtomicString& mediaType, PassOwnPtr<E
xpressionVector> expressions) | 79 MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Express
ionVector> expressions) |
77 : m_restrictor(r) | 80 : m_restrictor(r) |
78 , m_mediaType(mediaType.lower()) | |
79 , m_expressions(expressions) | 81 , m_expressions(expressions) |
80 { | 82 { |
| 83 if (mediaType.impl()) { |
| 84 String lowerMediaType = String(mediaType.lower()); |
| 85 lowerMediaType.ensure16Bit(); |
| 86 m_mediaType = attemptStaticStringCreation(lowerMediaType, Force16Bit); |
| 87 } |
| 88 |
81 if (!m_expressions) { | 89 if (!m_expressions) { |
82 m_expressions = adoptPtr(new ExpressionVector); | 90 m_expressions = adoptPtr(new ExpressionVector); |
83 return; | 91 return; |
84 } | 92 } |
85 | 93 |
86 nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompa
re); | 94 nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompa
re); |
87 | 95 |
88 // Remove all duplicated expressions. | 96 // Remove all duplicated expressions. |
89 MediaQueryExp* key = 0; | 97 MediaQueryExp* key = 0; |
90 for (int i = m_expressions->size() - 1; i >= 0; --i) { | 98 for (int i = m_expressions->size() - 1; i >= 0; --i) { |
91 MediaQueryExp* exp = m_expressions->at(i).get(); | 99 MediaQueryExp* exp = m_expressions->at(i).get(); |
92 | 100 |
93 if (key && *exp == *key) | 101 if (key && *exp == *key) |
94 m_expressions->remove(i); | 102 m_expressions->remove(i); |
95 else | 103 else |
96 key = exp; | 104 key = exp; |
97 } | 105 } |
98 } | 106 } |
99 | 107 |
100 MediaQuery::MediaQuery(const MediaQuery& o) | 108 MediaQuery::MediaQuery(const MediaQuery& o) |
101 : m_restrictor(o.m_restrictor) | 109 : m_restrictor(o.m_restrictor) |
102 , m_mediaType(o.m_mediaType) | 110 , m_mediaType(o.m_mediaType.impl()) |
103 , m_expressions(adoptPtr(new ExpressionVector(o.m_expressions->size()))) | 111 , m_expressions(adoptPtr(new ExpressionVector(o.m_expressions->size()))) |
104 , m_serializationCache(o.m_serializationCache) | 112 , m_serializationCache(o.m_serializationCache) |
105 { | 113 { |
106 for (unsigned i = 0; i < m_expressions->size(); ++i) | 114 for (unsigned i = 0; i < m_expressions->size(); ++i) |
107 (*m_expressions)[i] = o.m_expressions->at(i)->copy(); | 115 (*m_expressions)[i] = o.m_expressions->at(i)->copy(); |
108 } | 116 } |
109 | 117 |
110 MediaQuery::~MediaQuery() | 118 MediaQuery::~MediaQuery() |
111 { | 119 { |
112 } | 120 } |
113 | 121 |
114 // http://dev.w3.org/csswg/cssom/#compare-media-queries | 122 // http://dev.w3.org/csswg/cssom/#compare-media-queries |
115 bool MediaQuery::operator==(const MediaQuery& other) const | 123 bool MediaQuery::operator==(const MediaQuery& other) const |
116 { | 124 { |
117 return cssText() == other.cssText(); | 125 return cssText() == other.cssText(); |
118 } | 126 } |
119 | 127 |
120 // http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries | 128 // http://dev.w3.org/csswg/cssom/#serialize-a-list-of-media-queries |
121 String MediaQuery::cssText() const | 129 String MediaQuery::cssText() const |
122 { | 130 { |
123 if (m_serializationCache.isNull()) | 131 if (m_serializationCache.isNull()) |
124 const_cast<MediaQuery*>(this)->m_serializationCache = serialize(); | 132 const_cast<MediaQuery*>(this)->m_serializationCache = serialize(); |
125 | 133 |
126 return m_serializationCache; | 134 return m_serializationCache; |
127 } | 135 } |
128 | 136 |
129 } | 137 } |
OLD | NEW |