InspectorStyleSheet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2010, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef InspectorStyleSheet_h
  25. #define InspectorStyleSheet_h
  26. #include "CSSPropertySourceData.h"
  27. #include "CSSStyleDeclaration.h"
  28. #include "ExceptionCode.h"
  29. #include "InspectorStyleTextEditor.h"
  30. #include "InspectorTypeBuilder.h"
  31. #include "InspectorValues.h"
  32. #include <wtf/HashMap.h>
  33. #include <wtf/HashSet.h>
  34. #include <wtf/PassRefPtr.h>
  35. #include <wtf/RefPtr.h>
  36. #include <wtf/Vector.h>
  37. #include <wtf/text/WTFString.h>
  38. class ParsedStyleSheet;
  39. namespace WebCore {
  40. class CSSRuleList;
  41. class CSSStyleDeclaration;
  42. class CSSStyleRule;
  43. class CSSStyleSheet;
  44. class Document;
  45. class Element;
  46. class InspectorPageAgent;
  47. class InspectorStyleSheet;
  48. class Node;
  49. #if ENABLE(INSPECTOR)
  50. typedef String ErrorString;
  51. class InspectorCSSId {
  52. public:
  53. InspectorCSSId()
  54. : m_ordinal(0)
  55. {
  56. }
  57. explicit InspectorCSSId(RefPtr<InspectorObject> value)
  58. {
  59. if (!value->getString("styleSheetId", &m_styleSheetId))
  60. return;
  61. RefPtr<InspectorValue> ordinalValue = value->get("ordinal");
  62. if (!ordinalValue || !ordinalValue->asNumber(&m_ordinal))
  63. m_styleSheetId = "";
  64. }
  65. InspectorCSSId(const String& styleSheetId, unsigned ordinal)
  66. : m_styleSheetId(styleSheetId)
  67. , m_ordinal(ordinal)
  68. {
  69. }
  70. bool isEmpty() const { return m_styleSheetId.isEmpty(); }
  71. const String& styleSheetId() const { return m_styleSheetId; }
  72. unsigned ordinal() const { return m_ordinal; }
  73. // ID type is either TypeBuilder::CSS::CSSStyleId or TypeBuilder::CSS::CSSRuleId.
  74. template<typename ID>
  75. PassRefPtr<ID> asProtocolValue() const
  76. {
  77. if (isEmpty())
  78. return 0;
  79. RefPtr<ID> result = ID::create()
  80. .setStyleSheetId(m_styleSheetId)
  81. .setOrdinal(m_ordinal);
  82. return result.release();
  83. }
  84. private:
  85. String m_styleSheetId;
  86. unsigned m_ordinal;
  87. };
  88. struct InspectorStyleProperty {
  89. InspectorStyleProperty()
  90. : hasSource(false)
  91. , disabled(false)
  92. {
  93. }
  94. InspectorStyleProperty(CSSPropertySourceData sourceData, bool hasSource, bool disabled)
  95. : sourceData(sourceData)
  96. , hasSource(hasSource)
  97. , disabled(disabled)
  98. {
  99. }
  100. void setRawTextFromStyleDeclaration(const String& styleDeclaration)
  101. {
  102. unsigned start = sourceData.range.start;
  103. unsigned end = sourceData.range.end;
  104. ASSERT(start < end);
  105. ASSERT(end <= styleDeclaration.length());
  106. rawText = styleDeclaration.substring(start, end - start);
  107. }
  108. bool hasRawText() const { return !rawText.isEmpty(); }
  109. CSSPropertySourceData sourceData;
  110. bool hasSource;
  111. bool disabled;
  112. String rawText;
  113. };
  114. class InspectorStyle : public RefCounted<InspectorStyle> {
  115. public:
  116. static PassRefPtr<InspectorStyle> create(const InspectorCSSId& styleId, PassRefPtr<CSSStyleDeclaration> style, InspectorStyleSheet* parentStyleSheet);
  117. virtual ~InspectorStyle();
  118. CSSStyleDeclaration* cssStyle() const { return m_style.get(); }
  119. PassRefPtr<TypeBuilder::CSS::CSSStyle> buildObjectForStyle() const;
  120. PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSComputedStyleProperty> > buildArrayForComputedStyle() const;
  121. bool hasDisabledProperties() const { return !m_disabledProperties.isEmpty(); }
  122. bool setPropertyText(unsigned index, const String& text, bool overwrite, String* oldText, ExceptionCode&);
  123. bool toggleProperty(unsigned index, bool disable, ExceptionCode&);
  124. bool getText(String* result) const;
  125. bool setText(const String&, ExceptionCode&);
  126. private:
  127. InspectorStyle(const InspectorCSSId& styleId, PassRefPtr<CSSStyleDeclaration> style, InspectorStyleSheet* parentStyleSheet);
  128. // FIXME: Remove these aliases and update all the current call sites to use the new public methods.
  129. bool styleText(String* result) const { return getText(result); }
  130. bool applyStyleText(const String& text) { ExceptionCode ec = 0; return setText(text, ec); }
  131. bool populateAllProperties(Vector<InspectorStyleProperty>* result) const;
  132. PassRefPtr<TypeBuilder::CSS::CSSStyle> styleWithProperties() const;
  133. PassRefPtr<CSSRuleSourceData> extractSourceData() const;
  134. String shorthandValue(const String& shorthandProperty) const;
  135. String shorthandPriority(const String& shorthandProperty) const;
  136. Vector<String> longhandProperties(const String& shorthandProperty) const;
  137. NewLineAndWhitespace& newLineAndWhitespaceDelimiters() const;
  138. InspectorCSSId m_styleId;
  139. RefPtr<CSSStyleDeclaration> m_style;
  140. InspectorStyleSheet* m_parentStyleSheet;
  141. Vector<InspectorStyleProperty> m_disabledProperties;
  142. mutable std::pair<String, String> m_format;
  143. mutable bool m_formatAcquired;
  144. };
  145. class InspectorStyleSheet : public RefCounted<InspectorStyleSheet> {
  146. public:
  147. class Listener {
  148. public:
  149. Listener() { }
  150. virtual ~Listener() { }
  151. virtual void styleSheetChanged(InspectorStyleSheet*) = 0;
  152. };
  153. typedef HashMap<CSSStyleDeclaration*, RefPtr<InspectorStyle> > InspectorStyleMap;
  154. static PassRefPtr<InspectorStyleSheet> create(InspectorPageAgent*, const String& id, PassRefPtr<CSSStyleSheet> pageStyleSheet, TypeBuilder::CSS::StyleSheetOrigin::Enum, const String& documentURL, Listener*);
  155. static String styleSheetURL(CSSStyleSheet* pageStyleSheet);
  156. virtual ~InspectorStyleSheet();
  157. String id() const { return m_id; }
  158. String finalURL() const;
  159. CSSStyleSheet* pageStyleSheet() const { return m_pageStyleSheet.get(); }
  160. void reparseStyleSheet(const String&);
  161. bool setText(const String&, ExceptionCode&);
  162. String ruleSelector(const InspectorCSSId&, ExceptionCode&);
  163. bool setRuleSelector(const InspectorCSSId&, const String& selector, ExceptionCode&);
  164. CSSStyleRule* addRule(const String& selector, ExceptionCode&);
  165. bool deleteRule(const InspectorCSSId&, ExceptionCode&);
  166. CSSStyleRule* ruleForId(const InspectorCSSId&) const;
  167. PassRefPtr<TypeBuilder::CSS::CSSStyleSheetBody> buildObjectForStyleSheet();
  168. PassRefPtr<TypeBuilder::CSS::CSSStyleSheetHeader> buildObjectForStyleSheetInfo();
  169. PassRefPtr<TypeBuilder::CSS::CSSRule> buildObjectForRule(CSSStyleRule*);
  170. PassRefPtr<TypeBuilder::CSS::CSSStyle> buildObjectForStyle(CSSStyleDeclaration*);
  171. bool setStyleText(const InspectorCSSId&, const String& text, String* oldText, ExceptionCode&);
  172. bool setPropertyText(const InspectorCSSId&, unsigned propertyIndex, const String& text, bool overwrite, String* oldPropertyText, ExceptionCode&);
  173. bool toggleProperty(const InspectorCSSId&, unsigned propertyIndex, bool disable, ExceptionCode&);
  174. virtual bool getText(String* result) const;
  175. virtual CSSStyleDeclaration* styleForId(const InspectorCSSId&) const;
  176. void fireStyleSheetChanged();
  177. InspectorCSSId ruleId(CSSStyleRule*) const;
  178. InspectorCSSId styleId(CSSStyleDeclaration* style) const { return ruleOrStyleId(style); }
  179. protected:
  180. InspectorStyleSheet(InspectorPageAgent*, const String& id, PassRefPtr<CSSStyleSheet> pageStyleSheet, TypeBuilder::CSS::StyleSheetOrigin::Enum, const String& documentURL, Listener*);
  181. bool canBind() const { return m_origin != TypeBuilder::CSS::StyleSheetOrigin::User_agent && m_origin != TypeBuilder::CSS::StyleSheetOrigin::User; }
  182. InspectorCSSId ruleOrStyleId(CSSStyleDeclaration* style) const;
  183. virtual Document* ownerDocument() const;
  184. virtual RefPtr<CSSRuleSourceData> ruleSourceDataFor(CSSStyleDeclaration* style) const;
  185. virtual unsigned ruleIndexByStyle(CSSStyleDeclaration*) const;
  186. virtual bool ensureParsedDataReady();
  187. virtual PassRefPtr<InspectorStyle> inspectorStyleForId(const InspectorCSSId&);
  188. virtual void rememberInspectorStyle(RefPtr<InspectorStyle> inspectorStyle);
  189. virtual void forgetInspectorStyle(CSSStyleDeclaration* style);
  190. // Also accessed by friend class InspectorStyle.
  191. virtual bool setStyleText(CSSStyleDeclaration*, const String&, ExceptionCode&);
  192. virtual PassOwnPtr<Vector<size_t> > lineEndings() const;
  193. private:
  194. typedef Vector<RefPtr<CSSStyleRule> > CSSStyleRuleVector;
  195. friend class InspectorStyle;
  196. static void collectFlatRules(PassRefPtr<CSSRuleList>, CSSStyleRuleVector* result);
  197. bool checkPageStyleSheet(ExceptionCode&) const;
  198. bool ensureText() const;
  199. bool ensureSourceData();
  200. void ensureFlatRules() const;
  201. bool styleSheetTextWithChangedStyle(CSSStyleDeclaration*, const String& newStyleText, String* result);
  202. void revalidateStyle(CSSStyleDeclaration*);
  203. bool originalStyleSheetText(String* result) const;
  204. bool resourceStyleSheetText(String* result) const;
  205. bool inlineStyleSheetText(String* result) const;
  206. PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSRule> > buildArrayForRuleList(CSSRuleList*);
  207. PassRefPtr<TypeBuilder::CSS::SelectorList> buildObjectForSelectorList(CSSStyleRule*);
  208. InspectorPageAgent* m_pageAgent;
  209. String m_id;
  210. RefPtr<CSSStyleSheet> m_pageStyleSheet;
  211. TypeBuilder::CSS::StyleSheetOrigin::Enum m_origin;
  212. String m_documentURL;
  213. bool m_isRevalidating;
  214. ParsedStyleSheet* m_parsedStyleSheet;
  215. InspectorStyleMap m_inspectorStyles;
  216. mutable CSSStyleRuleVector m_flatRules;
  217. Listener* m_listener;
  218. };
  219. class InspectorStyleSheetForInlineStyle : public InspectorStyleSheet {
  220. public:
  221. static PassRefPtr<InspectorStyleSheetForInlineStyle> create(InspectorPageAgent*, const String& id, PassRefPtr<Element>, TypeBuilder::CSS::StyleSheetOrigin::Enum, Listener*);
  222. void didModifyElementAttribute();
  223. virtual bool getText(String* result) const;
  224. virtual CSSStyleDeclaration* styleForId(const InspectorCSSId& id) const { ASSERT_UNUSED(id, !id.ordinal()); return inlineStyle(); }
  225. protected:
  226. InspectorStyleSheetForInlineStyle(InspectorPageAgent*, const String& id, PassRefPtr<Element>, TypeBuilder::CSS::StyleSheetOrigin::Enum, Listener*);
  227. virtual Document* ownerDocument() const;
  228. virtual RefPtr<CSSRuleSourceData> ruleSourceDataFor(CSSStyleDeclaration* style) const { ASSERT_UNUSED(style, style == inlineStyle()); return m_ruleSourceData; }
  229. virtual unsigned ruleIndexByStyle(CSSStyleDeclaration*) const { return 0; }
  230. virtual bool ensureParsedDataReady();
  231. virtual PassRefPtr<InspectorStyle> inspectorStyleForId(const InspectorCSSId&);
  232. virtual void rememberInspectorStyle(RefPtr<InspectorStyle>) { }
  233. virtual void forgetInspectorStyle(CSSStyleDeclaration*) { }
  234. // Also accessed by friend class InspectorStyle.
  235. virtual bool setStyleText(CSSStyleDeclaration*, const String&, ExceptionCode&);
  236. virtual PassOwnPtr<Vector<size_t> > lineEndings() const;
  237. private:
  238. CSSStyleDeclaration* inlineStyle() const;
  239. const String& elementStyleText() const;
  240. bool getStyleAttributeRanges(CSSRuleSourceData* result) const;
  241. RefPtr<Element> m_element;
  242. RefPtr<CSSRuleSourceData> m_ruleSourceData;
  243. RefPtr<InspectorStyle> m_inspectorStyle;
  244. // Contains "style" attribute value.
  245. mutable String m_styleText;
  246. mutable bool m_isStyleTextValid;
  247. };
  248. #endif
  249. } // namespace WebCore
  250. #endif // !defined(InspectorStyleSheet_h)