Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1041)

Side by Side Diff: Source/core/css/MediaQuery.cpp

Issue 16208004: No need to store invalid media queries. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/css/MediaQuery.h ('k') | Source/core/css/MediaQueryEvaluator.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 23 matching lines...) Expand all
34 #include "wtf/MemoryInstrumentationVector.h" 34 #include "wtf/MemoryInstrumentationVector.h"
35 #include "wtf/NonCopyingSort.h" 35 #include "wtf/NonCopyingSort.h"
36 #include "wtf/text/StringBuilder.h" 36 #include "wtf/text/StringBuilder.h"
37 37
38 namespace WebCore { 38 namespace WebCore {
39 39
40 // http://dev.w3.org/csswg/cssom/#serialize-a-media-query 40 // http://dev.w3.org/csswg/cssom/#serialize-a-media-query
41 String MediaQuery::serialize() const 41 String MediaQuery::serialize() const
42 { 42 {
43 StringBuilder result; 43 StringBuilder result;
44 if (!m_ignored) { 44 switch (m_restrictor) {
45 switch (m_restrictor) { 45 case MediaQuery::Only:
46 case MediaQuery::Only: 46 result.append("only ");
47 result.append("only "); 47 break;
48 break; 48 case MediaQuery::Not:
49 case MediaQuery::Not: 49 result.append("not ");
50 result.append("not "); 50 break;
51 break; 51 case MediaQuery::None:
52 case MediaQuery::None: 52 break;
53 break; 53 }
54 }
55 54
56 if (m_expressions->isEmpty()) { 55 if (m_expressions->isEmpty()) {
57 result.append(m_mediaType); 56 result.append(m_mediaType);
58 return result.toString(); 57 return result.toString();
59 } 58 }
60 59
61 if (m_mediaType != "all" || m_restrictor != None) { 60 if (m_mediaType != "all" || m_restrictor != None) {
62 result.append(m_mediaType); 61 result.append(m_mediaType);
63 result.append(" and "); 62 result.append(" and ");
64 } 63 }
65 64
66 result.append(m_expressions->at(0)->serialize()); 65 result.append(m_expressions->at(0)->serialize());
67 for (size_t i = 1; i < m_expressions->size(); ++i) { 66 for (size_t i = 1; i < m_expressions->size(); ++i) {
68 result.append(" and "); 67 result.append(" and ");
69 result.append(m_expressions->at(i)->serialize()); 68 result.append(m_expressions->at(i)->serialize());
70 }
71 } else {
72 // If query is invalid, serialized text should turn into "not all".
73 result.append("not all");
74 } 69 }
75 return result.toString(); 70 return result.toString();
76 } 71 }
77 72
78 static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<Media QueryExp>& b) 73 static bool expressionCompare(const OwnPtr<MediaQueryExp>& a, const OwnPtr<Media QueryExp>& b)
79 { 74 {
80 return codePointCompare(a->serialize(), b->serialize()) < 0; 75 return codePointCompare(a->serialize(), b->serialize()) < 0;
81 } 76 }
82 77
83 MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Vector< OwnPtr<MediaQueryExp> > > exprs) 78 MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Vector< OwnPtr<MediaQueryExp> > > exprs)
84 : m_restrictor(r) 79 : m_restrictor(r)
85 , m_mediaType(mediaType.lower()) 80 , m_mediaType(mediaType.lower())
86 , m_expressions(exprs) 81 , m_expressions(exprs)
87 , m_ignored(false)
88 { 82 {
89 if (!m_expressions) { 83 if (!m_expressions) {
90 m_expressions = adoptPtr(new Vector<OwnPtr<MediaQueryExp> >); 84 m_expressions = adoptPtr(new Vector<OwnPtr<MediaQueryExp> >);
91 return; 85 return;
92 } 86 }
93 87
94 nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompa re); 88 nonCopyingSort(m_expressions->begin(), m_expressions->end(), expressionCompa re);
95 89
96 // Remove all duplicated expressions. 90 // Remove all duplicated expressions.
97 MediaQueryExp* key = 0; 91 MediaQueryExp* key = 0;
98 for (int i = m_expressions->size() - 1; i >= 0; --i) { 92 for (int i = m_expressions->size() - 1; i >= 0; --i) {
99 MediaQueryExp* exp = m_expressions->at(i).get(); 93 MediaQueryExp* exp = m_expressions->at(i).get();
100 94
101 // If not all of the expressions are valid the media query must be ignor ed.
102 if (!m_ignored)
103 m_ignored = !exp->isValid();
104
105 if (key && *exp == *key) 95 if (key && *exp == *key)
106 m_expressions->remove(i); 96 m_expressions->remove(i);
107 else 97 else
108 key = exp; 98 key = exp;
109 } 99 }
110 } 100 }
111 101
112 MediaQuery::MediaQuery(const MediaQuery& o) 102 MediaQuery::MediaQuery(const MediaQuery& o)
113 : m_restrictor(o.m_restrictor) 103 : m_restrictor(o.m_restrictor)
114 , m_mediaType(o.m_mediaType) 104 , m_mediaType(o.m_mediaType)
115 , m_expressions(adoptPtr(new Vector<OwnPtr<MediaQueryExp> >(o.m_expressions- >size()))) 105 , m_expressions(adoptPtr(new Vector<OwnPtr<MediaQueryExp> >(o.m_expressions- >size())))
116 , m_ignored(o.m_ignored)
117 , m_serializationCache(o.m_serializationCache) 106 , m_serializationCache(o.m_serializationCache)
118 { 107 {
119 for (unsigned i = 0; i < m_expressions->size(); ++i) 108 for (unsigned i = 0; i < m_expressions->size(); ++i)
120 (*m_expressions)[i] = o.m_expressions->at(i)->copy(); 109 (*m_expressions)[i] = o.m_expressions->at(i)->copy();
121 } 110 }
122 111
123 MediaQuery::~MediaQuery() 112 MediaQuery::~MediaQuery()
124 { 113 {
125 } 114 }
126 115
(...skipping 14 matching lines...) Expand all
141 130
142 void MediaQuery::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const 131 void MediaQuery::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
143 { 132 {
144 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS); 133 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS);
145 info.addMember(m_mediaType, "mediaType"); 134 info.addMember(m_mediaType, "mediaType");
146 info.addMember(m_expressions, "expressions"); 135 info.addMember(m_expressions, "expressions");
147 info.addMember(m_serializationCache, "serializationCache"); 136 info.addMember(m_serializationCache, "serializationCache");
148 } 137 }
149 138
150 } 139 }
OLDNEW
« no previous file with comments | « Source/core/css/MediaQuery.h ('k') | Source/core/css/MediaQueryEvaluator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698