Page Speed Optimization Libraries  1.11.33.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
srcset_slot.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
20 
21 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_SRCSET_SLOT_H_
22 #define NET_INSTAWEB_REWRITER_PUBLIC_SRCSET_SLOT_H_
23 
24 #include <vector>
25 
28 #include "pagespeed/kernel/base/basictypes.h"
29 #include "pagespeed/kernel/base/ref_counted_ptr.h"
30 #include "pagespeed/kernel/base/string.h"
31 #include "pagespeed/kernel/base/string_util.h"
32 #include "pagespeed/kernel/html/html_element.h"
33 #include "pagespeed/kernel/http/google_url.h"
34 
35 namespace net_instaweb {
36 
37 class CommonFilter;
38 class RewriteDriver;
39 class SrcSetSlot;
40 
43 class SrcSetSlotCollection : public RefCounted<SrcSetSlotCollection> {
44  public:
45  struct ImageCandidate {
46  ImageCandidate() : slot(nullptr) {}
47 
48  GoogleString url;
49  GoogleString descriptor;
50 
54  };
55 
58  explicit SrcSetSlotCollection(RewriteDriver* driver,
59  CommonFilter* filter,
60  HtmlElement* element,
61  HtmlElement::Attribute* attribute);
62 
63  int num_image_candidates() { return candidates_.size(); }
64 
66  SrcSetSlot* slot(int idx) { return candidates_[idx].slot; }
67  const GoogleString& url(int idx) { return candidates_[idx].url; }
68  void set_url(int idx, GoogleString new_url) {
69  candidates_[idx].url = new_url;
70  }
71  const GoogleString& descriptor(int idx) {
72  return candidates_[idx].descriptor;
73  }
74 
75  HtmlElement* element() { return element_; }
76  RewriteDriver* driver() { return driver_; }
77 
81  void Commit();
82 
86  static void ParseSrcSet(StringPiece input, std::vector<ImageCandidate>* out);
87 
88  static GoogleString Serialize(const std::vector<ImageCandidate>& in);
89 
90  protected:
91  virtual ~SrcSetSlotCollection() {}
92  REFCOUNT_FRIEND_DECLARATION(SrcSetSlotCollection);
93 
94  private:
95  std::vector<ImageCandidate> candidates_;
96  RewriteDriver* driver_;
97  HtmlElement* element_;
98  HtmlElement::Attribute* attribute_;
99 
100 
101 };
102 
103 class SrcSetSlot : public ResourceSlot {
104  public:
105  HtmlElement* element() const override { return parent_->element(); }
106  void Render() override;
107  GoogleString LocationString() const override;
108 
109  protected:
110  friend class SrcSetSlotCollection;
111  SrcSetSlot(const ResourcePtr& resource,
112  SrcSetSlotCollection* parent,
113  int index);
114 
115  REFCOUNT_FRIEND_DECLARATION(SrcSetSlot);
116  ~SrcSetSlot() override;
117 
118  private:
119  RefCountedPtr<SrcSetSlotCollection> parent_;
120  int index_;
121  UrlRelativity url_relativity_;
122 
123 
124 };
125 
128 
129 }
130 
131 #endif
HtmlElement * element() const override
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: srcset_slot.h:105
Definition: common_filter.h:47
void Render() override
GoogleString LocationString() const override
Definition: srcset_slot.h:103
Definition: resource_slot.h:53
SrcSetSlotCollection(RewriteDriver *driver, CommonFilter *filter, HtmlElement *element, HtmlElement::Attribute *attribute)
Definition: srcset_slot.h:43
static void ParseSrcSet(StringPiece input, std::vector< ImageCandidate > *out)
SrcSetSlot * slot(int idx)
may be nullptr.
Definition: srcset_slot.h:66
Definition: rewrite_driver.h:98
SrcSetSlot * slot
Definition: srcset_slot.h:53