2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "InspectorCSSAgent.h"
30 #include "CSSComputedStyleDeclaration.h"
31 #include "CSSMutableStyleDeclaration.h"
32 #include "CSSPropertyNames.h"
33 #include "CSSPropertySourceData.h"
35 #include "CSSRuleList.h"
36 #include "CSSStyleRule.h"
37 #include "CSSStyleSelector.h"
38 #include "CSSStyleSheet.h"
39 #include "DOMWindow.h"
40 #include "HTMLHeadElement.h"
41 #include "InspectorDOMAgent.h"
42 #include "InspectorValues.h"
43 #include "InstrumentingAgents.h"
46 #include "StyleSheetList.h"
48 #include <wtf/HashSet.h>
49 #include <wtf/Vector.h>
50 #include <wtf/text/CString.h>
52 // Currently implemented model:
57 // priority : <string>, // "" for non-parsedOk properties
58 // implicit : <boolean>,
59 // parsedOk : <boolean>, // whether property is understood by WebCore
60 // status : <string>, // "disabled" | "active" | "inactive" | "style"
61 // shorthandName : <string>,
62 // startOffset : <number>, // Optional - property text start offset in enclosing style declaration. Absent for computed styles and such.
63 // endOffset : <number>, // Optional - property text end offset in enclosing style declaration. Absent for computed styles and such.
66 // name + value + priority : present when the property is enabled
67 // text : present when the property is disabled
69 // For disabled properties, startOffset === endOffset === insertion point for the property.
72 // "disabled" == property disabled by user
73 // "active" == property participates in the computed style calculation
74 // "inactive" == property does no participate in the computed style calculation (i.e. overridden by a subsequent property with the same name)
75 // "style" == property is active and originates from the WebCore CSSStyleDeclaration rather than CSS source code (e.g. implicit longhand properties)
78 // styleId : <string>, // Optional
84 // shorthandEntries : [
89 // cssText : <string>, // Optional - declaration text
93 // startOffset, // Optional - for source-based styles only
94 // endOffset, // Optional - for source-based styles only
104 // ruleId : <string>, // Optional
105 // selectorText : <string>,
106 // sourceURL : <string>,
107 // sourceLine : <string>,
108 // origin : <string>, // "" || "user-agent" || "user" || "inspector"
109 // style : #cssStyle,
110 // selectorRange: { start: <number>, end: <number> } // Optional - for source-based rules only
113 // cssStyleSheetInfo = {
114 // styleSheetId : <number>
115 // sourceURL : <string>
117 // disabled : <boolean>
121 // styleSheetId : <number>
127 // text : <string> // Optional - whenever the text is available for a text-based stylesheet
133 CSSStyleSheet* InspectorCSSAgent::parentStyleSheet(StyleBase* styleBase)
138 StyleSheet* styleSheet = styleBase->stylesheet();
139 if (styleSheet && styleSheet->isCSSStyleSheet())
140 return static_cast<CSSStyleSheet*>(styleSheet);
146 CSSStyleRule* InspectorCSSAgent::asCSSStyleRule(StyleBase* styleBase)
148 if (!styleBase->isStyleRule())
150 CSSRule* rule = static_cast<CSSRule*>(styleBase);
151 if (rule->type() != CSSRule::STYLE_RULE)
153 return static_cast<CSSStyleRule*>(rule);
156 InspectorCSSAgent::InspectorCSSAgent(InstrumentingAgents* instrumentingAgents, InspectorDOMAgent* domAgent)
157 : m_instrumentingAgents(instrumentingAgents)
158 , m_domAgent(domAgent)
159 , m_lastStyleSheetId(1)
163 m_domAgent->setDOMListener(this);
164 m_instrumentingAgents->setInspectorCSSAgent(this);
167 InspectorCSSAgent::~InspectorCSSAgent()
169 m_instrumentingAgents->setInspectorCSSAgent(0);
170 // DOM agent should be destroyed after CSS agent.
171 m_domAgent->setDOMListener(0);
176 void InspectorCSSAgent::reset()
178 m_idToInspectorStyleSheet.clear();
179 m_cssStyleSheetToInspectorStyleSheet.clear();
180 m_nodeToInspectorStyleSheet.clear();
181 m_documentToInspectorStyleSheet.clear();
184 void InspectorCSSAgent::getStylesForNode(ErrorString* errorString, int nodeId, RefPtr<InspectorObject>* result)
186 Element* element = elementForId(errorString, nodeId);
190 RefPtr<InspectorObject> resultObject = InspectorObject::create();
192 InspectorStyleSheetForInlineStyle* styleSheet = asInspectorStyleSheet(element);
194 resultObject->setObject("inlineStyle", styleSheet->buildObjectForStyle(element->style()));
196 RefPtr<CSSComputedStyleDeclaration> computedStyleInfo = computedStyle(element, true); // Support the viewing of :visited information in computed style.
197 RefPtr<InspectorStyle> computedInspectorStyle = InspectorStyle::create(InspectorCSSId(), computedStyleInfo, 0);
198 resultObject->setObject("computedStyle", computedInspectorStyle->buildObjectForStyle());
200 CSSStyleSelector* selector = element->ownerDocument()->styleSelector();
201 RefPtr<CSSRuleList> matchedRules = selector->styleRulesForElement(element, false, true);
202 resultObject->setArray("matchedCSSRules", buildArrayForRuleList(matchedRules.get()));
204 resultObject->setArray("styleAttributes", buildArrayForAttributeStyles(element));
206 RefPtr<InspectorArray> pseudoElements = InspectorArray::create();
207 for (PseudoId pseudoId = FIRST_PUBLIC_PSEUDOID; pseudoId < AFTER_LAST_INTERNAL_PSEUDOID; pseudoId = static_cast<PseudoId>(pseudoId + 1)) {
208 RefPtr<CSSRuleList> matchedRules = selector->pseudoStyleRulesForElement(element, pseudoId, false, true);
209 if (matchedRules && matchedRules->length()) {
210 RefPtr<InspectorObject> pseudoStyles = InspectorObject::create();
211 pseudoStyles->setNumber("pseudoId", static_cast<int>(pseudoId));
212 pseudoStyles->setArray("rules", buildArrayForRuleList(matchedRules.get()));
213 pseudoElements->pushObject(pseudoStyles.release());
216 resultObject->setArray("pseudoElements", pseudoElements.release());
218 RefPtr<InspectorArray> inheritedStyles = InspectorArray::create();
219 Element* parentElement = element->parentElement();
220 while (parentElement) {
221 RefPtr<InspectorObject> parentStyle = InspectorObject::create();
222 if (parentElement->style() && parentElement->style()->length()) {
223 InspectorStyleSheetForInlineStyle* styleSheet = asInspectorStyleSheet(parentElement);
225 parentStyle->setObject("inlineStyle", styleSheet->buildObjectForStyle(styleSheet->styleForId(InspectorCSSId(styleSheet->id(), 0))));
228 CSSStyleSelector* parentSelector = parentElement->ownerDocument()->styleSelector();
229 RefPtr<CSSRuleList> parentMatchedRules = parentSelector->styleRulesForElement(parentElement, false, true);
230 parentStyle->setArray("matchedCSSRules", buildArrayForRuleList(parentMatchedRules.get()));
231 inheritedStyles->pushObject(parentStyle.release());
232 parentElement = parentElement->parentElement();
234 resultObject->setArray("inherited", inheritedStyles.release());
236 *result = resultObject.release();
239 void InspectorCSSAgent::getInlineStyleForNode(ErrorString* errorString, int nodeId, RefPtr<InspectorObject>* style)
241 Element* element = elementForId(errorString, nodeId);
245 InspectorStyleSheetForInlineStyle* styleSheet = asInspectorStyleSheet(element);
249 *style = styleSheet->buildObjectForStyle(element->style());
252 void InspectorCSSAgent::getComputedStyleForNode(ErrorString* errorString, int nodeId, RefPtr<InspectorObject>* style)
254 Element* element = elementForId(errorString, nodeId);
258 RefPtr<CSSComputedStyleDeclaration> computedStyleInfo = computedStyle(element, true);
259 RefPtr<InspectorStyle> inspectorStyle = InspectorStyle::create(InspectorCSSId(), computedStyleInfo, 0);
260 *style = inspectorStyle->buildObjectForStyle();
263 void InspectorCSSAgent::getAllStyleSheets(ErrorString*, RefPtr<InspectorArray>* styleInfos)
265 Vector<Document*> documents = m_domAgent->documents();
266 for (Vector<Document*>::iterator it = documents.begin(); it != documents.end(); ++it) {
267 StyleSheetList* list = (*it)->styleSheets();
268 for (unsigned i = 0; i < list->length(); ++i) {
269 StyleSheet* styleSheet = list->item(i);
270 if (styleSheet->isCSSStyleSheet()) {
271 InspectorStyleSheet* inspectorStyleSheet = bindStyleSheet(static_cast<CSSStyleSheet*>(styleSheet));
272 (*styleInfos)->pushObject(inspectorStyleSheet->buildObjectForStyleSheetInfo());
278 void InspectorCSSAgent::getStyleSheet(ErrorString* errorString, const String& styleSheetId, RefPtr<InspectorObject>* styleSheetObject)
280 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
281 if (!inspectorStyleSheet)
284 *styleSheetObject = inspectorStyleSheet->buildObjectForStyleSheet();
287 void InspectorCSSAgent::getStyleSheetText(ErrorString* errorString, const String& styleSheetId, String* result)
289 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
290 if (!inspectorStyleSheet)
293 inspectorStyleSheet->text(result);
296 void InspectorCSSAgent::setStyleSheetText(ErrorString* errorString, const String& styleSheetId, const String& text)
298 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
299 if (!inspectorStyleSheet)
302 if (inspectorStyleSheet->setText(text))
303 inspectorStyleSheet->reparseStyleSheet(text);
305 *errorString = "Internal error setting style sheet text";
308 void InspectorCSSAgent::setPropertyText(ErrorString* errorString, const RefPtr<InspectorObject>& fullStyleId, int propertyIndex, const String& text, bool overwrite, RefPtr<InspectorObject>* result)
310 InspectorCSSId compoundId(fullStyleId);
311 ASSERT(!compoundId.isEmpty());
313 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, compoundId.styleSheetId());
314 if (!inspectorStyleSheet)
317 bool success = inspectorStyleSheet->setPropertyText(errorString, compoundId, propertyIndex, text, overwrite);
319 *result = inspectorStyleSheet->buildObjectForStyle(inspectorStyleSheet->styleForId(compoundId));
322 void InspectorCSSAgent::toggleProperty(ErrorString* errorString, const RefPtr<InspectorObject>& fullStyleId, int propertyIndex, bool disable, RefPtr<InspectorObject>* result)
324 InspectorCSSId compoundId(fullStyleId);
325 ASSERT(!compoundId.isEmpty());
327 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, compoundId.styleSheetId());
328 if (!inspectorStyleSheet)
331 bool success = inspectorStyleSheet->toggleProperty(errorString, compoundId, propertyIndex, disable);
333 *result = inspectorStyleSheet->buildObjectForStyle(inspectorStyleSheet->styleForId(compoundId));
336 void InspectorCSSAgent::setRuleSelector(ErrorString* errorString, const RefPtr<InspectorObject>& fullRuleId, const String& selector, RefPtr<InspectorObject>* result)
338 InspectorCSSId compoundId(fullRuleId);
339 ASSERT(!compoundId.isEmpty());
341 InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, compoundId.styleSheetId());
342 if (!inspectorStyleSheet)
345 bool success = inspectorStyleSheet->setRuleSelector(compoundId, selector);
349 *result = inspectorStyleSheet->buildObjectForRule(inspectorStyleSheet->ruleForId(compoundId));
352 void InspectorCSSAgent::addRule(ErrorString*, const int contextNodeId, const String& selector, RefPtr<InspectorObject>* result)
354 Node* node = m_domAgent->nodeForId(contextNodeId);
358 InspectorStyleSheet* inspectorStyleSheet = viaInspectorStyleSheet(node->document(), true);
359 if (!inspectorStyleSheet)
361 CSSStyleRule* newRule = inspectorStyleSheet->addRule(selector);
365 *result = inspectorStyleSheet->buildObjectForRule(newRule);
368 void InspectorCSSAgent::getSupportedCSSProperties(ErrorString*, RefPtr<InspectorArray>* cssProperties)
370 RefPtr<InspectorArray> properties = InspectorArray::create();
371 for (int i = 0; i < numCSSProperties; ++i)
372 properties->pushString(propertyNameStrings[i]);
374 *cssProperties = properties.release();
378 Element* InspectorCSSAgent::inlineStyleElement(CSSStyleDeclaration* style)
380 if (!style || !style->isMutableStyleDeclaration())
382 Node* node = static_cast<CSSMutableStyleDeclaration*>(style)->node();
383 if (!node || !node->isStyledElement() || static_cast<StyledElement*>(node)->getInlineStyleDecl() != style)
385 return static_cast<Element*>(node);
388 InspectorStyleSheetForInlineStyle* InspectorCSSAgent::asInspectorStyleSheet(Element* element)
390 NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(element);
391 if (it == m_nodeToInspectorStyleSheet.end()) {
392 CSSStyleDeclaration* style = element->isStyledElement() ? element->style() : 0;
396 String newStyleSheetId = String::number(m_lastStyleSheetId++);
397 RefPtr<InspectorStyleSheetForInlineStyle> inspectorStyleSheet = InspectorStyleSheetForInlineStyle::create(newStyleSheetId, element, "");
398 m_idToInspectorStyleSheet.set(newStyleSheetId, inspectorStyleSheet);
399 m_nodeToInspectorStyleSheet.set(element, inspectorStyleSheet);
400 return inspectorStyleSheet.get();
403 return it->second.get();
406 Element* InspectorCSSAgent::elementForId(ErrorString* errorString, int nodeId)
408 Node* node = m_domAgent->nodeForId(nodeId);
410 *errorString = "No node with given id found";
413 if (node->nodeType() != Node::ELEMENT_NODE) {
414 *errorString = "Not an element node";
417 return static_cast<Element*>(node);
420 InspectorStyleSheet* InspectorCSSAgent::bindStyleSheet(CSSStyleSheet* styleSheet)
422 RefPtr<InspectorStyleSheet> inspectorStyleSheet = m_cssStyleSheetToInspectorStyleSheet.get(styleSheet);
423 if (!inspectorStyleSheet) {
424 String id = String::number(m_lastStyleSheetId++);
425 inspectorStyleSheet = InspectorStyleSheet::create(id, styleSheet, detectOrigin(styleSheet, styleSheet->document()), m_domAgent->documentURLString(styleSheet->document()));
426 m_idToInspectorStyleSheet.set(id, inspectorStyleSheet);
427 m_cssStyleSheetToInspectorStyleSheet.set(styleSheet, inspectorStyleSheet);
429 return inspectorStyleSheet.get();
432 InspectorStyleSheet* InspectorCSSAgent::viaInspectorStyleSheet(Document* document, bool createIfAbsent)
435 ASSERT(!createIfAbsent);
439 RefPtr<InspectorStyleSheet> inspectorStyleSheet = m_documentToInspectorStyleSheet.get(document);
440 if (inspectorStyleSheet || !createIfAbsent)
441 return inspectorStyleSheet.get();
443 ExceptionCode ec = 0;
444 RefPtr<Element> styleElement = document->createElement("style", ec);
446 styleElement->setAttribute("type", "text/css", ec);
448 ContainerNode* targetNode;
449 // HEAD is absent in ImageDocuments, for example.
450 if (document->head())
451 targetNode = document->head();
452 else if (document->body())
453 targetNode = document->body();
456 targetNode->appendChild(styleElement, ec);
460 StyleSheetList* styleSheets = document->styleSheets();
461 StyleSheet* styleSheet = styleSheets->item(styleSheets->length() - 1);
462 if (!styleSheet->isCSSStyleSheet())
464 CSSStyleSheet* cssStyleSheet = static_cast<CSSStyleSheet*>(styleSheet);
465 String id = String::number(m_lastStyleSheetId++);
466 inspectorStyleSheet = InspectorStyleSheet::create(id, cssStyleSheet, "inspector", m_domAgent->documentURLString(document));
467 m_idToInspectorStyleSheet.set(id, inspectorStyleSheet);
468 m_cssStyleSheetToInspectorStyleSheet.set(cssStyleSheet, inspectorStyleSheet);
469 m_documentToInspectorStyleSheet.set(document, inspectorStyleSheet);
470 return inspectorStyleSheet.get();
473 InspectorStyleSheet* InspectorCSSAgent::assertStyleSheetForId(ErrorString* errorString, const String& styleSheetId)
475 IdToInspectorStyleSheet::iterator it = m_idToInspectorStyleSheet.find(styleSheetId);
476 if (it == m_idToInspectorStyleSheet.end()) {
477 *errorString = "No style sheet with given id found";
480 return it->second.get();
483 String InspectorCSSAgent::detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument)
485 DEFINE_STATIC_LOCAL(String, userAgent, ("user-agent"));
486 DEFINE_STATIC_LOCAL(String, user, ("user"));
487 DEFINE_STATIC_LOCAL(String, inspector, ("inspector"));
490 if (pageStyleSheet && !pageStyleSheet->ownerNode() && pageStyleSheet->href().isEmpty())
492 else if (pageStyleSheet && pageStyleSheet->ownerNode() && pageStyleSheet->ownerNode()->nodeName() == "#document")
495 InspectorStyleSheet* viaInspectorStyleSheetForOwner = viaInspectorStyleSheet(ownerDocument, false);
496 if (viaInspectorStyleSheetForOwner && pageStyleSheet == viaInspectorStyleSheetForOwner->pageStyleSheet())
502 PassRefPtr<InspectorArray> InspectorCSSAgent::buildArrayForRuleList(CSSRuleList* ruleList)
504 RefPtr<InspectorArray> result = InspectorArray::create();
506 return result.release();
508 for (unsigned i = 0, size = ruleList->length(); i < size; ++i) {
509 CSSStyleRule* rule = asCSSStyleRule(ruleList->item(i));
513 InspectorStyleSheet* styleSheet = bindStyleSheet(parentStyleSheet(rule));
515 result->pushObject(styleSheet->buildObjectForRule(rule));
517 return result.release();
520 PassRefPtr<InspectorArray> InspectorCSSAgent::buildArrayForAttributeStyles(Element* element)
522 RefPtr<InspectorArray> attrStyles = InspectorArray::create();
523 NamedNodeMap* attributes = element->attributes();
524 for (unsigned i = 0; attributes && i < attributes->length(); ++i) {
525 Attribute* attribute = attributes->attributeItem(i);
526 if (attribute->style()) {
527 RefPtr<InspectorObject> attrStyleObject = InspectorObject::create();
528 String attributeName = attribute->localName();
529 RefPtr<InspectorStyle> inspectorStyle = InspectorStyle::create(InspectorCSSId(), attribute->style(), 0);
530 attrStyleObject->setString("name", attributeName.utf8().data());
531 attrStyleObject->setObject("style", inspectorStyle->buildObjectForStyle());
532 attrStyles->pushObject(attrStyleObject.release());
536 return attrStyles.release();
539 void InspectorCSSAgent::didRemoveDocument(Document* document)
542 m_documentToInspectorStyleSheet.remove(document);
545 void InspectorCSSAgent::didRemoveDOMNode(Node* node)
550 NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(node);
551 if (it == m_nodeToInspectorStyleSheet.end())
554 m_idToInspectorStyleSheet.remove(it->second->id());
555 m_nodeToInspectorStyleSheet.remove(node);
558 void InspectorCSSAgent::didModifyDOMAttr(Element* element)
563 NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(element);
564 if (it == m_nodeToInspectorStyleSheet.end())
567 it->second->didModifyElementAttribute();
570 } // namespace WebCore
572 #endif // ENABLE(INSPECTOR)