Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: Source/core/inspector/InspectorCSSAgent.cpp

Issue 1310923003: Devtools [LayoutEditor]: Patch values in the selected rule (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@medias
Patch Set: Address comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 1497 matching lines...) Expand 10 before | Expand all | Expand 10 after
1508 Element* element = toElement(m_domAgent->nodeForId(state.key)); 1508 Element* element = toElement(m_domAgent->nodeForId(state.key));
1509 if (element && element->ownerDocument()) 1509 if (element && element->ownerDocument())
1510 documentsToChange.add(element->ownerDocument()); 1510 documentsToChange.add(element->ownerDocument());
1511 } 1511 }
1512 1512
1513 m_nodeIdToForcedPseudoState.clear(); 1513 m_nodeIdToForcedPseudoState.clear();
1514 for (auto& document : documentsToChange) 1514 for (auto& document : documentsToChange)
1515 document->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTr acing::create(StyleChangeReason::Inspector)); 1515 document->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTr acing::create(StyleChangeReason::Inspector));
1516 } 1516 }
1517 1517
1518 PassRefPtrWillBeRawPtr<CSSStyleDeclaration> InspectorCSSAgent::findEffectiveDecl aration(Element* element, CSSPropertyID propertyId) 1518 PassRefPtrWillBeRawPtr<CSSRuleList> InspectorCSSAgent::matchedRulesList(Element* element)
1519 { 1519 {
1520 PseudoId elementPseudoId = element->pseudoId(); 1520 PseudoId elementPseudoId = element->pseudoId();
1521 CSSStyleDeclaration* inlineStyle = element->style();
1522
1523 if (elementPseudoId) { 1521 if (elementPseudoId) {
1524 element = element->parentOrShadowHostElement(); 1522 element = element->parentOrShadowHostElement();
1525 if (!element) 1523 if (!element)
1526 return nullptr; 1524 return nullptr;
1527 } 1525 }
1528 1526
1529 Document* ownerDocument = element->ownerDocument(); 1527 Document* ownerDocument = element->ownerDocument();
1530 // A non-active document has no styles. 1528 // A non-active document has no styles.
1531 if (!ownerDocument->isActive()) 1529 if (!ownerDocument->isActive())
1532 return nullptr; 1530 return nullptr;
1533 1531
1532 StyleResolver& styleResolver = ownerDocument->ensureStyleResolver();
1533 element->updateDistribution();
1534 return styleResolver.pseudoCSSRulesForElement(element, elementPseudoId, Styl eResolver::AllCSSRules);
1535 }
1536
1537 PassRefPtrWillBeRawPtr<CSSStyleDeclaration> InspectorCSSAgent::findEffectiveDecl aration(CSSPropertyID propertyId, CSSRuleList* ruleList, CSSStyleDeclaration* in lineStyle)
1538 {
1539 if (!ruleList && !inlineStyle)
1540 return nullptr;
1541
1534 String longhand = getPropertyNameString(propertyId); 1542 String longhand = getPropertyNameString(propertyId);
1535
1536 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = nullptr; 1543 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = nullptr;
1537 bool isImportant = false; 1544 bool isImportant = false;
1538 1545
1539 if (inlineStyle && !inlineStyle->getPropertyValue(longhand).isEmpty()) { 1546 if (inlineStyle && !inlineStyle->getPropertyValue(longhand).isEmpty()) {
1540 foundStyle = inlineStyle; 1547 foundStyle = inlineStyle;
1541 isImportant = inlineStyle->getPropertyPriority(longhand) == "important"; 1548 isImportant = inlineStyle->getPropertyPriority(longhand) == "important";
1542 } 1549 }
1543 1550
1544 StyleResolver& styleResolver = ownerDocument->ensureStyleResolver();
1545 element->updateDistribution();
1546 RefPtrWillBeRawPtr<CSSRuleList> ruleList = styleResolver.pseudoCSSRulesForEl ement(element, elementPseudoId, StyleResolver::AllCSSRules);
1547
1548 for (unsigned i = 0, size = ruleList ? ruleList->length() : 0; i < size; ++i ) { 1551 for (unsigned i = 0, size = ruleList ? ruleList->length() : 0; i < size; ++i ) {
1549 if (isImportant) 1552 if (isImportant)
1550 break; 1553 break;
1551 1554
1552 if (ruleList->item(size - i - 1)->type() != CSSRule::STYLE_RULE) 1555 if (ruleList->item(size - i - 1)->type() != CSSRule::STYLE_RULE)
1553 continue; 1556 continue;
1554 1557
1555 CSSStyleRule* rule = toCSSStyleRule(ruleList->item(size - i - 1)); 1558 CSSStyleRule* rule = toCSSStyleRule(ruleList->item(size - i - 1));
1556 if (!rule) 1559 if (!rule)
1557 continue; 1560 continue;
1558 1561
1559 CSSStyleDeclaration* style = rule->style(); 1562 CSSStyleDeclaration* style = rule->style();
1560 if (!style) 1563 if (!style)
1561 continue; 1564 continue;
1562 1565
1563 if (style->getPropertyValue(longhand).isEmpty()) 1566 if (style->getPropertyValue(longhand).isEmpty())
1564 continue; 1567 continue;
1565 1568
1566 isImportant = style->getPropertyPriority(longhand) == "important"; 1569 isImportant = style->getPropertyPriority(longhand) == "important";
1567 if (isImportant || !foundStyle) 1570 if (isImportant || !foundStyle)
1568 foundStyle = style; 1571 foundStyle = style;
1569 } 1572 }
1570 1573
1571 return foundStyle.release(); 1574 return foundStyle.release();
1572 } 1575 }
1573 1576
1574 void InspectorCSSAgent::setCSSPropertyValue(ErrorString* errorString, Element* e lement, CSSPropertyID propertyId, const String& value) 1577 void InspectorCSSAgent::setCSSPropertyValue(ErrorString* errorString, Element* e lement, CSSStyleDeclaration* style, CSSPropertyID propertyId, const String& valu e, bool forceImportant)
dgozman 2015/09/03 20:42:14 RefPtrWillBeRawPtr<CSSStyleDeclaration>
sergeyv 2015/09/03 22:43:27 Done.
1575 { 1578 {
1576 Document* ownerDocument = element->ownerDocument();
1577 if (!ownerDocument->isActive()) {
1578 *errorString = "Can't edit a node from a non-active document";
1579 return;
1580 }
1581
1582 Vector<StylePropertyShorthand, 4> shorthands;
1583 getMatchingShorthandsForLonghand(propertyId, &shorthands);
1584
1585 String shorthand = shorthands.size() > 0 ? getPropertyNameString(shorthands [0].id()) : String();
1586 String longhand = getPropertyNameString(propertyId);
1587
1588 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = findEffectiveDeclaratio n(element, propertyId);
1589 CSSStyleDeclaration* inlineStyle = element->style();
1590 if (!foundStyle || !foundStyle->parentStyleSheet())
1591 foundStyle = inlineStyle;
1592
1593 if (!foundStyle) {
1594 *errorString = "Can't find a style to edit";
1595 return;
1596 }
1597
1598 InspectorStyleSheetBase* inspectorStyleSheet = nullptr; 1579 InspectorStyleSheetBase* inspectorStyleSheet = nullptr;
1599 RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = nullptr; 1580 RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = nullptr;
1600 if (foundStyle != inlineStyle) { 1581 // An absence of the parent rule means that given style is an inline style.
1601 InspectorStyleSheet* styleSheet = bindStyleSheet(foundStyle->parentStyl eSheet()); 1582 if (style->parentRule()) {
1583 InspectorStyleSheet* styleSheet = bindStyleSheet(style->parentStyleShee t());
dgozman 2015/09/03 20:42:14 nit: two spaces
sergeyv 2015/09/03 22:43:27 Done.
1602 inspectorStyleSheet = styleSheet; 1584 inspectorStyleSheet = styleSheet;
1603 sourceData = styleSheet->sourceDataForRule(foundStyle->parentRule()); 1585 sourceData = styleSheet->sourceDataForRule(style->parentRule());
1604 } 1586 } else {
1605
1606 if (!sourceData) {
1607 InspectorStyleSheetForInlineStyle* inlineStyleSheet = asInspectorStyleSh eet(element); 1587 InspectorStyleSheetForInlineStyle* inlineStyleSheet = asInspectorStyleSh eet(element);
1608 inspectorStyleSheet = inlineStyleSheet; 1588 inspectorStyleSheet = inlineStyleSheet;
1609 sourceData = inlineStyleSheet->ruleSourceData(); 1589 sourceData = inlineStyleSheet->ruleSourceData();
1610 } 1590 }
1611 1591
1612 if (!sourceData) { 1592 if (!sourceData) {
1613 *errorString = "Can't find a source to edit"; 1593 *errorString = "Can't find a source to edit";
1614 return; 1594 return;
1615 } 1595 }
1616 1596
1597 Vector<StylePropertyShorthand, 4> shorthands;
1598 getMatchingShorthandsForLonghand(propertyId, &shorthands);
1599
1600 String shorthand = shorthands.size() > 0 ? getPropertyNameString(shorthands [0].id()) : String();
1601 String longhand = getPropertyNameString(propertyId);
1602
1617 int foundIndex = -1; 1603 int foundIndex = -1;
1618 WillBeHeapVector<CSSPropertySourceData> properties = sourceData->styleSource Data->propertyData; 1604 WillBeHeapVector<CSSPropertySourceData> properties = sourceData->styleSource Data->propertyData;
1619 for (unsigned i = 0; i < properties.size(); ++i) { 1605 for (unsigned i = 0; i < properties.size(); ++i) {
1620 CSSPropertySourceData property = properties[properties.size() - i - 1]; 1606 CSSPropertySourceData property = properties[properties.size() - i - 1];
1621 String name = property.name; 1607 String name = property.name;
1622 if (property.disabled) 1608 if (property.disabled)
1623 continue; 1609 continue;
1624 1610
1625 if (name != shorthand && name != longhand) 1611 if (name != shorthand && name != longhand)
1626 continue; 1612 continue;
1627 1613
1628 if (property.important || foundIndex == -1) 1614 if (property.important || foundIndex == -1)
1629 foundIndex = properties.size() - i - 1; 1615 foundIndex = properties.size() - i - 1;
1630 1616
1631 if (property.important) 1617 if (property.important)
1632 break; 1618 break;
1633 } 1619 }
1634 1620
1635 SourceRange bodyRange = sourceData->ruleBodyRange; 1621 SourceRange bodyRange = sourceData->ruleBodyRange;
1636 String styleSheetText; 1622 String styleSheetText;
1637 inspectorStyleSheet->getText(&styleSheetText); 1623 inspectorStyleSheet->getText(&styleSheetText);
1638 String styleText = styleSheetText.substring(bodyRange.start, bodyRange.lengt h()); 1624 String styleText = styleSheetText.substring(bodyRange.start, bodyRange.lengt h());
1639 SourceRange changeRange; 1625 SourceRange changeRange;
1640 if (foundIndex == -1) { 1626 if (foundIndex == -1) {
1641 bool isImportant = inlineStyle->getPropertyPriority(longhand) == "import ant"; 1627 String newPropertyText = "\n" + longhand + ": " + value + (forceImportan t ? " !important" : "") + ";";
1642 String newPropertyText = "\n" + longhand + ": " + value + (isImportant ? " !important" : "") + ";";
1643 if (!styleText.isEmpty() && !styleText.stripWhiteSpace().endsWith(';')) 1628 if (!styleText.isEmpty() && !styleText.stripWhiteSpace().endsWith(';'))
1644 newPropertyText = ";" + newPropertyText; 1629 newPropertyText = ";" + newPropertyText;
1645 styleText.append(newPropertyText); 1630 styleText.append(newPropertyText);
1646 changeRange.start = bodyRange.end; 1631 changeRange.start = bodyRange.end;
1647 changeRange.end = bodyRange.end + newPropertyText.length(); 1632 changeRange.end = bodyRange.end + newPropertyText.length();
1648 } else { 1633 } else {
1649 CSSPropertySourceData declaration = properties[foundIndex]; 1634 CSSPropertySourceData declaration = properties[foundIndex];
1650 String newValueText; 1635 String newValueText;
1651 if (declaration.name == shorthand) 1636 if (declaration.name == shorthand)
1652 newValueText = createShorthandValue(ownerDocument, shorthand, declar ation.value, longhand, value); 1637 newValueText = createShorthandValue(element->ownerDocument(), shorth and, declaration.value, longhand, value);
1653 else 1638 else
1654 newValueText = value; 1639 newValueText = value;
1655 1640
1656 String newPropertyText = declaration.name + ": " + newValueText + (decla ration.important ? " !important" : "") + ";"; 1641 String newPropertyText = declaration.name + ": " + newValueText + (decla ration.important || forceImportant ? " !important" : "") + ";";
1657 styleText.replace(declaration.range.start - bodyRange.start, declaration .range.length(), newPropertyText); 1642 styleText.replace(declaration.range.start - bodyRange.start, declaration .range.length(), newPropertyText);
1658 changeRange.start = declaration.range.start; 1643 changeRange.start = declaration.range.start;
1659 changeRange.end = changeRange.start + newPropertyText.length(); 1644 changeRange.end = changeRange.start + newPropertyText.length();
1660 } 1645 }
1661 CSSStyleDeclaration* resultStyle = setStyleText(errorString, inspectorStyleS heet, bodyRange, styleText); 1646 CSSStyleDeclaration* resultStyle = setStyleText(errorString, inspectorStyleS heet, bodyRange, styleText);
1662 if (resultStyle) { 1647 if (resultStyle) {
1663 frontend()->layoutEditorChange(inspectorStyleSheet->id(), inspectorStyle Sheet->buildSourceRangeObject(changeRange)); 1648 frontend()->layoutEditorChange(inspectorStyleSheet->id(), inspectorStyle Sheet->buildSourceRangeObject(changeRange));
1664 frontend()->flush(); 1649 frontend()->flush();
1665 } 1650 }
1666 } 1651 }
1667 1652
1668 void InspectorCSSAgent::setEffectivePropertyValueForNode(ErrorString* errorStrin g, int nodeId, const String& propertyName, const String& value) 1653 void InspectorCSSAgent::setEffectivePropertyValueForNode(ErrorString* errorStrin g, int nodeId, const String& propertyName, const String& value)
1669 { 1654 {
1670 Element* element = elementForId(errorString, nodeId); 1655 Element* element = elementForId(errorString, nodeId);
dgozman 2015/09/03 20:42:14 TODO: move testing from CSSAgent to layout editor.
sergeyv 2015/09/03 22:43:27 Done.
1671 if (!element) 1656 if (!element)
1672 return; 1657 return;
1673 1658
1674 CSSPropertyID property = cssPropertyID(propertyName); 1659 CSSPropertyID property = cssPropertyID(propertyName);
1675 if (!property) { 1660 if (!property) {
1676 *errorString = "Invalid property name"; 1661 *errorString = "Invalid property name";
1677 return; 1662 return;
1678 } 1663 }
1679 setCSSPropertyValue(errorString, element, cssPropertyID(propertyName), value ); 1664
1665 Document* ownerDocument = element->ownerDocument();
1666 if (!ownerDocument->isActive()) {
1667 *errorString = "Can't edit a node from a non-active document";
1668 return;
1669 }
1670
1671 CSSPropertyID propertyId = cssPropertyID(propertyName);
1672 CSSStyleDeclaration* inlineStyle = element->style();
dgozman 2015/09/03 20:42:14 nit: two spaces
sergeyv 2015/09/03 22:43:27 Done.
1673 RefPtrWillBeRawPtr<CSSRuleList> ruleList = matchedRulesList(element);
1674 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = findEffectiveDeclaratio n(propertyId, ruleList.get(), inlineStyle);
1675 if (!foundStyle || !foundStyle->parentStyleSheet())
1676 foundStyle = inlineStyle;
1677
1678 if (!foundStyle) {
1679 *errorString = "Can't find a style to edit";
1680 return;
1681 }
1682
1683 setCSSPropertyValue(errorString, element, foundStyle.get(), propertyId, valu e);
1680 } 1684 }
1681 1685
1682 DEFINE_TRACE(InspectorCSSAgent) 1686 DEFINE_TRACE(InspectorCSSAgent)
1683 { 1687 {
1684 visitor->trace(m_domAgent); 1688 visitor->trace(m_domAgent);
1685 visitor->trace(m_pageAgent); 1689 visitor->trace(m_pageAgent);
1686 visitor->trace(m_resourceAgent); 1690 visitor->trace(m_resourceAgent);
1687 visitor->trace(m_resourceContentLoader); 1691 visitor->trace(m_resourceContentLoader);
1688 #if ENABLE(OILPAN) 1692 #if ENABLE(OILPAN)
1689 visitor->trace(m_idToInspectorStyleSheet); 1693 visitor->trace(m_idToInspectorStyleSheet);
1690 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); 1694 visitor->trace(m_idToInspectorStyleSheetForInlineStyle);
1691 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); 1695 visitor->trace(m_cssStyleSheetToInspectorStyleSheet);
1692 visitor->trace(m_documentToCSSStyleSheets); 1696 visitor->trace(m_documentToCSSStyleSheets);
1693 visitor->trace(m_invalidatedDocuments); 1697 visitor->trace(m_invalidatedDocuments);
1694 visitor->trace(m_nodeToInspectorStyleSheet); 1698 visitor->trace(m_nodeToInspectorStyleSheet);
1695 visitor->trace(m_documentToViaInspectorStyleSheet); 1699 visitor->trace(m_documentToViaInspectorStyleSheet);
1696 #endif 1700 #endif
1697 visitor->trace(m_inspectorUserAgentStyleSheet); 1701 visitor->trace(m_inspectorUserAgentStyleSheet);
1698 InspectorBaseAgent::trace(visitor); 1702 InspectorBaseAgent::trace(visitor);
1699 } 1703 }
1700 1704
1701 } // namespace blink 1705 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698