summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/inspector/inspector_style_sheet.h
blob: 999d00e295cdef334d0a91887ae88bcc7947ceb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2010, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_INSPECTOR_STYLE_SHEET_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_INSPECTOR_STYLE_SHEET_H_

#include <memory>
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/core/css/css_property_source_data.h"
#include "third_party/blink/renderer/core/css/css_style_declaration.h"
#include "third_party/blink/renderer/core/inspector/protocol/CSS.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class CSSKeyframeRule;
class CSSMediaRule;
class CSSStyleDeclaration;
class CSSStyleRule;
class CSSStyleSheet;
class Document;
class Element;
class ExceptionState;
class InspectorNetworkAgent;
class InspectorResourceContainer;
class InspectorStyleSheetBase;

typedef HeapVector<Member<CSSRule>> CSSRuleVector;
typedef Vector<unsigned> LineEndings;

class InspectorStyle final : public GarbageCollected<InspectorStyle> {
 public:
  InspectorStyle(CSSStyleDeclaration*,
                 CSSRuleSourceData*,
                 InspectorStyleSheetBase* parent_style_sheet);

  CSSStyleDeclaration* CssStyle() { return style_.Get(); }
  InspectorStyleSheetBase* InspectorStyleSheet() {
    return parent_style_sheet_.Get();
  }
  std::unique_ptr<protocol::CSS::CSSStyle> BuildObjectForStyle();
  bool StyleText(String* result);
  bool TextForRange(const SourceRange&, String* result);

  void Trace(Visitor*) const;

 private:
  void PopulateAllProperties(Vector<CSSPropertySourceData>& result);
  std::unique_ptr<protocol::CSS::CSSStyle> StyleWithProperties();
  String ShorthandValue(const String& shorthand_property);

  Member<CSSStyleDeclaration> style_;
  Member<CSSRuleSourceData> source_data_;
  Member<InspectorStyleSheetBase> parent_style_sheet_;
};

class InspectorStyleSheetBase
    : public GarbageCollected<InspectorStyleSheetBase> {
 public:
  class CORE_EXPORT Listener {
   public:
    Listener() = default;
    virtual ~Listener() = default;
    virtual void StyleSheetChanged(InspectorStyleSheetBase*) = 0;
  };
  virtual ~InspectorStyleSheetBase() = default;
  virtual void Trace(Visitor* visitor) const {}

  String Id() { return id_; }

  virtual bool SetText(const String&, ExceptionState&) = 0;
  virtual bool GetText(String* result) = 0;
  virtual String SourceMapURL() { return String(); }
  virtual const Document* GetDocument() = 0;

  std::unique_ptr<protocol::CSS::CSSStyle> BuildObjectForStyle(
      CSSStyleDeclaration*);
  std::unique_ptr<protocol::CSS::SourceRange> BuildSourceRangeObject(
      const SourceRange&);
  bool LineNumberAndColumnToOffset(unsigned line_number,
                                   unsigned column_number,
                                   unsigned* offset);
  virtual bool IsInlineStyle() = 0;

 protected:
  explicit InspectorStyleSheetBase(Listener*);

  Listener* GetListener() { return listener_; }
  void OnStyleSheetTextChanged();
  const LineEndings* GetLineEndings();
  void ResetLineEndings();

  virtual InspectorStyle* GetInspectorStyle(CSSStyleDeclaration*) = 0;

 private:
  friend class InspectorStyle;

  String id_;
  Listener* listener_;
  std::unique_ptr<LineEndings> line_endings_;
};

class InspectorStyleSheet : public InspectorStyleSheetBase {
 public:
  InspectorStyleSheet(InspectorNetworkAgent*,
                      CSSStyleSheet* page_style_sheet,
                      const String& origin,
                      const String& document_url,
                      InspectorStyleSheetBase::Listener*,
                      InspectorResourceContainer*);
  ~InspectorStyleSheet() override;
  void Trace(Visitor*) const override;

  String FinalURL();
  bool SetText(const String&, ExceptionState&) override;
  bool GetText(String* result) override;
  void MarkForSync() { marked_for_sync_ = true; }
  void SyncTextIfNeeded();

  CSSStyleRule* SetRuleSelector(const SourceRange&,
                                const String& selector,
                                SourceRange* new_range,
                                String* old_selector,
                                ExceptionState&);
  CSSKeyframeRule* SetKeyframeKey(const SourceRange&,
                                  const String& text,
                                  SourceRange* new_range,
                                  String* old_text,
                                  ExceptionState&);
  CSSRule* SetStyleText(const SourceRange&,
                        const String& text,
                        SourceRange* new_range,
                        String* old_selector,
                        ExceptionState&);
  CSSMediaRule* SetMediaRuleText(const SourceRange&,
                                 const String& selector,
                                 SourceRange* new_range,
                                 String* old_selector,
                                 ExceptionState&);
  CSSStyleRule* AddRule(const String& rule_text,
                        const SourceRange& location,
                        SourceRange* added_range,
                        ExceptionState&);
  bool DeleteRule(const SourceRange&, ExceptionState&);
  std::unique_ptr<protocol::Array<String>> CollectClassNames();
  CSSStyleSheet* PageStyleSheet() { return page_style_sheet_.Get(); }

  std::unique_ptr<protocol::CSS::CSSStyleSheetHeader>
  BuildObjectForStyleSheetInfo();
  std::unique_ptr<protocol::CSS::CSSRule> BuildObjectForRuleWithoutMedia(
      CSSStyleRule*);
  std::unique_ptr<protocol::CSS::RuleUsage> BuildObjectForRuleUsage(CSSRule*,
                                                                    bool);
  std::unique_ptr<protocol::CSS::CSSKeyframeRule> BuildObjectForKeyframeRule(
      CSSKeyframeRule*);
  std::unique_ptr<protocol::CSS::SelectorList> BuildObjectForSelectorList(
      CSSStyleRule*);

  std::unique_ptr<protocol::CSS::SourceRange> RuleHeaderSourceRange(CSSRule*);
  std::unique_ptr<protocol::CSS::SourceRange> MediaQueryExpValueSourceRange(
      CSSRule*,
      wtf_size_t media_query_index,
      wtf_size_t media_query_exp_index);
  bool IsInlineStyle() override { return false; }
  const CSSRuleVector& FlatRules();
  CSSRuleSourceData* SourceDataForRule(CSSRule*);
  String SourceMapURL() override;
  const Document* GetDocument() override;

 protected:
  InspectorStyle* GetInspectorStyle(CSSStyleDeclaration*) override;

 private:
  CSSRuleSourceData* RuleSourceDataAfterSourceRange(const SourceRange&);
  CSSRuleSourceData* FindRuleByHeaderRange(const SourceRange&);
  CSSRuleSourceData* FindRuleByBodyRange(const SourceRange&);
  CSSRule* RuleForSourceData(CSSRuleSourceData*);
  CSSStyleRule* InsertCSSOMRuleInStyleSheet(CSSRule* insert_before,
                                            const String& rule_text,
                                            ExceptionState&);
  CSSStyleRule* InsertCSSOMRuleInMediaRule(CSSMediaRule*,
                                           CSSRule* insert_before,
                                           const String& rule_text,
                                           ExceptionState&);
  CSSStyleRule* InsertCSSOMRuleBySourceRange(const SourceRange&,
                                             const String& rule_text,
                                             ExceptionState&);
  String SourceURL();
  void RemapSourceDataToCSSOMIfNecessary();
  void MapSourceDataToCSSOM();
  bool ResourceStyleSheetText(String* result);
  bool InlineStyleSheetText(String* result);
  bool InspectorStyleSheetText(String* result);
  String CollectStyleSheetRules();
  bool CSSOMStyleSheetText(String* result);
  std::unique_ptr<protocol::Array<protocol::CSS::Value>> SelectorsFromSource(
      CSSRuleSourceData*,
      const String&);
  String Url();
  bool HasSourceURL();
  bool StartsAtZero();

  bool IsMutable() const;
  void Reset();
  void UpdateText();

  void ReplaceText(const SourceRange&,
                   const String& text,
                   SourceRange* new_range,
                   String* old_text);
  void InnerSetText(const String& new_text, bool mark_as_locally_modified);
  void ParseText(const String& text);
  String MergeCSSOMRulesWithText(const String& text);
  Element* OwnerStyleElement();

  Member<InspectorResourceContainer> resource_container_;
  Member<InspectorNetworkAgent> network_agent_;
  Member<CSSStyleSheet> page_style_sheet_;
  String origin_;
  String document_url_;
  Member<CSSRuleSourceDataList> source_data_;
  String text_;
  CSSRuleVector cssom_flat_rules_;
  CSSRuleVector parsed_flat_rules_;
  typedef HashMap<unsigned,
                  unsigned,
                  WTF::IntHash<unsigned>,
                  WTF::UnsignedWithZeroKeyHashTraits<unsigned>>
      IndexMap;
  IndexMap rule_to_source_data_;
  IndexMap source_data_to_rule_;
  String source_url_;
  // True means that CSSOM rules are to be synced with the original source text.
  bool marked_for_sync_;
};

class InspectorStyleSheetForInlineStyle final : public InspectorStyleSheetBase {
 public:
  InspectorStyleSheetForInlineStyle(Element*, Listener*);
  void DidModifyElementAttribute();
  bool SetText(const String&, ExceptionState&) override;
  bool GetText(String* result) override;
  CSSStyleDeclaration* InlineStyle();
  CSSRuleSourceData* RuleSourceData();

  void Trace(Visitor*) const override;

  const Document* GetDocument() override;

 protected:
  InspectorStyle* GetInspectorStyle(CSSStyleDeclaration*) override;

  // Also accessed by friend class InspectorStyle.
  bool IsInlineStyle() override { return true; }

 private:
  const String& ElementStyleText();

  Member<Element> element_;
  Member<InspectorStyle> inspector_style_;
};

}  // namespace blink

#endif  // !defined(InspectorStyleSheet_h)