Page Speed Optimization Libraries
1.2.24.1
|
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 } 00038 00039 namespace net_instaweb { 00040 00041 class HtmlElement; 00042 00045 namespace css_util { 00046 00047 static const char kAllMedia[] = "all"; 00048 static const int kNoValue = -1; 00049 00050 enum DimensionState { 00051 kNoDimensions, 00052 kHasHeightOnly, 00053 kHasWidthOnly, 00054 kHasBothDimensions, 00055 kNotParsable 00056 }; 00057 00065 DimensionState GetDimensions(Css::Declarations* decls, int* width, int* height); 00066 00067 class StyleExtractor { 00068 public: 00069 StyleExtractor(HtmlElement* element); 00070 virtual ~StyleExtractor(); 00071 00072 00073 DimensionState state() const { return state_; } 00074 00076 int width() const { return width_px_; } 00077 int height() const { return height_px_; } 00078 00081 bool HasAnyDimensions() { return (state_ != kNoDimensions); } 00082 00083 DimensionState dimension_state() { return state_; } 00084 00085 private: 00086 static Css::Declarations* GetDeclsFromElement(HtmlElement* element); 00087 scoped_ptr<Css::Declarations> decls_; 00088 int width_px_; 00089 int height_px_; 00090 DimensionState state_; 00091 DISALLOW_COPY_AND_ASSIGN(StyleExtractor); 00092 }; 00093 00098 00103 void VectorizeMediaAttribute(const StringPiece& input_media, 00104 StringVector* output_vector); 00105 00110 GoogleString StringifyMediaVector(const StringVector& import_media); 00111 00114 bool IsComplexMediaQuery(const Css::MediaQuery& query); 00115 00121 bool ConvertMediaQueriesToStringVector(const Css::MediaQueries& in_vector, 00122 StringVector* out_vector); 00123 00128 void ConvertStringVectorToMediaQueries(const StringVector& in_vector, 00129 Css::MediaQueries* out_vector); 00130 00133 void ClearVectorIfContainsMediaAll(StringVector* media); 00134 00141 template<typename T> 00142 void EliminateElementsNotIn(std::vector<T>* sorted_inner, 00143 const std::vector<T>& sorted_outer) { 00144 if (!sorted_outer.empty()) { 00145 if (sorted_inner->empty()) { 00146 *sorted_inner = sorted_outer; 00147 } else { 00148 typename std::vector<T>::const_iterator outer_iter = sorted_outer.begin(); 00149 typename std::vector<T>::iterator inner_iter = sorted_inner->begin(); 00150 00151 while (inner_iter != sorted_inner->end()) { 00152 if (outer_iter == sorted_outer.end()) { 00154 inner_iter = sorted_inner->erase(inner_iter, sorted_inner->end()); 00155 } else if (*outer_iter == *inner_iter) { 00157 ++outer_iter; 00158 ++inner_iter; 00159 } else if (*outer_iter < *inner_iter) { 00161 ++outer_iter; 00162 } else { 00164 inner_iter = sorted_inner->erase(inner_iter); 00165 } 00166 } 00167 } 00168 } 00169 } 00170 00171 } 00172 00173 } 00174 00175 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_UTIL_H_