Page Speed Optimization Libraries
1.5.27.2
|
00001 /* 00002 * Copyright 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 00023 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_UTIL_H_ 00024 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_UTIL_H_ 00025 00026 #include <vector> 00027 00028 #include "net/instaweb/util/public/basictypes.h" 00029 #include "net/instaweb/util/public/scoped_ptr.h" 00030 #include "net/instaweb/util/public/string.h" 00031 #include "net/instaweb/util/public/string_util.h" 00032 00033 namespace Css { 00034 class Declarations; 00035 class MediaQueries; 00036 class MediaQuery; 00037 class Selector; 00038 } 00039 00040 namespace net_instaweb { 00041 00042 class HtmlElement; 00043 00046 namespace css_util { 00047 00048 static const char kAllMedia[] = "all"; 00049 static const int kNoValue = -1; 00050 00051 enum DimensionState { 00052 kNoDimensions, 00053 kHasHeightOnly, 00054 kHasWidthOnly, 00055 kHasBothDimensions, 00056 kNotParsable 00057 }; 00058 00066 DimensionState GetDimensions(Css::Declarations* decls, int* width, int* height); 00067 00068 class StyleExtractor { 00069 public: 00070 explicit StyleExtractor(HtmlElement* element); 00071 virtual ~StyleExtractor(); 00072 00073 00074 DimensionState state() const { return state_; } 00075 00077 int width() const { return width_px_; } 00078 int height() const { return height_px_; } 00079 00082 bool HasAnyDimensions() { return (state_ != kNoDimensions); } 00083 00084 DimensionState dimension_state() { return state_; } 00085 00086 private: 00087 static Css::Declarations* GetDeclsFromElement(HtmlElement* element); 00088 scoped_ptr<Css::Declarations> decls_; 00089 int width_px_; 00090 int height_px_; 00091 DimensionState state_; 00092 DISALLOW_COPY_AND_ASSIGN(StyleExtractor); 00093 }; 00094 00099 00104 void VectorizeMediaAttribute(const StringPiece& input_media, 00105 StringVector* output_vector); 00106 00111 GoogleString StringifyMediaVector(const StringVector& import_media); 00112 00115 bool IsComplexMediaQuery(const Css::MediaQuery& query); 00116 00122 bool ConvertMediaQueriesToStringVector(const Css::MediaQueries& in_vector, 00123 StringVector* out_vector); 00124 00129 void ConvertStringVectorToMediaQueries(const StringVector& in_vector, 00130 Css::MediaQueries* out_vector); 00131 00134 void ClearVectorIfContainsMediaAll(StringVector* media); 00135 00137 bool CanMediaAffectScreen(const StringPiece& media); 00138 00141 GoogleString JsDetectableSelector(const Css::Selector& selector); 00142 00149 template<typename T> 00150 void EliminateElementsNotIn(std::vector<T>* sorted_inner, 00151 const std::vector<T>& sorted_outer) { 00152 if (!sorted_outer.empty()) { 00153 if (sorted_inner->empty()) { 00154 *sorted_inner = sorted_outer; 00155 } else { 00156 typename std::vector<T>::const_iterator outer_iter = sorted_outer.begin(); 00157 typename std::vector<T>::iterator inner_iter = sorted_inner->begin(); 00158 00159 while (inner_iter != sorted_inner->end()) { 00160 if (outer_iter == sorted_outer.end()) { 00162 inner_iter = sorted_inner->erase(inner_iter, sorted_inner->end()); 00163 } else if (*outer_iter == *inner_iter) { 00165 ++outer_iter; 00166 ++inner_iter; 00167 } else if (*outer_iter < *inner_iter) { 00169 ++outer_iter; 00170 } else { 00172 inner_iter = sorted_inner->erase(inner_iter); 00173 } 00174 } 00175 } 00176 } 00177 } 00178 00179 } 00180 00181 } 00182 00183 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_UTIL_H_