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

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: Rebase 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<CSSStyleDeclaration> InspectorCSSAgent::findEffectiveDecl aration(Element* element, CSSPropertyID propertyId)
dgozman 2015/09/02 22:26:16 Can we kill this function?
sergeyv 2015/09/03 00:52:28 Done.
1519 { 1519 {
1520 RefPtrWillBeRawPtr<CSSRuleList> ruleList = matchedRulesList(element);
1521 return findEffectiveDeclaration(propertyId, ruleList.get(), element->pseudoI d() ? nullptr : element->style());
dgozman 2015/09/02 22:26:17 Can we always call element->style()?
sergeyv 2015/09/03 00:52:27 Done.
1522 }
1523
1524 PassRefPtrWillBeRawPtr<CSSRuleList> InspectorCSSAgent::matchedRulesList(Element* element)
1525 {
1520 PseudoId elementPseudoId = element->pseudoId(); 1526 PseudoId elementPseudoId = element->pseudoId();
1521 CSSStyleDeclaration* inlineStyle = element->style();
1522
1523 if (elementPseudoId) { 1527 if (elementPseudoId) {
1524 element = element->parentOrShadowHostElement(); 1528 element = element->parentOrShadowHostElement();
1525 if (!element) 1529 if (!element)
1526 return nullptr; 1530 return nullptr;
1527 } 1531 }
1528 1532
1529 Document* ownerDocument = element->ownerDocument(); 1533 Document* ownerDocument = element->ownerDocument();
1530 // A non-active document has no styles. 1534 // A non-active document has no styles.
1531 if (!ownerDocument->isActive()) 1535 if (!ownerDocument->isActive())
1532 return nullptr; 1536 return nullptr;
1533 1537
1538 StyleResolver& styleResolver = ownerDocument->ensureStyleResolver();
1539 element->updateDistribution();
1540 return styleResolver.pseudoCSSRulesForElement(element, elementPseudoId, Styl eResolver::AllCSSRules);
1541 }
1542
1543 PassRefPtrWillBeRawPtr<CSSStyleDeclaration> InspectorCSSAgent::findEffectiveDecl aration(CSSPropertyID propertyId, CSSRuleList* ruleList, CSSStyleDeclaration* in lineStyle)
1544 {
1545 if (!ruleList && !inlineStyle)
1546 return nullptr;
1547
1534 String longhand = getPropertyNameString(propertyId); 1548 String longhand = getPropertyNameString(propertyId);
1535
1536 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = nullptr; 1549 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = nullptr;
1537 bool isImportant = false; 1550 bool isImportant = false;
1538 1551
1539 if (inlineStyle && !inlineStyle->getPropertyValue(longhand).isEmpty()) { 1552 if (inlineStyle && !inlineStyle->getPropertyValue(longhand).isEmpty()) {
1540 foundStyle = inlineStyle; 1553 foundStyle = inlineStyle;
1541 isImportant = inlineStyle->getPropertyPriority(longhand) == "important"; 1554 isImportant = inlineStyle->getPropertyPriority(longhand) == "important";
1542 } 1555 }
1543 1556
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 ) { 1557 for (unsigned i = 0, size = ruleList ? ruleList->length() : 0; i < size; ++i ) {
1549 if (isImportant) 1558 if (isImportant)
1550 break; 1559 break;
1551 1560
1552 if (ruleList->item(size - i - 1)->type() != CSSRule::STYLE_RULE) 1561 if (ruleList->item(size - i - 1)->type() != CSSRule::STYLE_RULE)
1553 continue; 1562 continue;
1554 1563
1555 CSSStyleRule* rule = toCSSStyleRule(ruleList->item(size - i - 1)); 1564 CSSStyleRule* rule = toCSSStyleRule(ruleList->item(size - i - 1));
1556 if (!rule) 1565 if (!rule)
1557 continue; 1566 continue;
1558 1567
1559 CSSStyleDeclaration* style = rule->style(); 1568 CSSStyleDeclaration* style = rule->style();
1560 if (!style) 1569 if (!style)
1561 continue; 1570 continue;
1562 1571
1563 if (style->getPropertyValue(longhand).isEmpty()) 1572 if (style->getPropertyValue(longhand).isEmpty())
1564 continue; 1573 continue;
1565 1574
1566 isImportant = style->getPropertyPriority(longhand) == "important"; 1575 isImportant = style->getPropertyPriority(longhand) == "important";
1567 if (isImportant || !foundStyle) 1576 if (isImportant || !foundStyle)
1568 foundStyle = style; 1577 foundStyle = style;
1569 } 1578 }
1570 1579
1571 return foundStyle.release(); 1580 return foundStyle.release();
1572 } 1581 }
1573 1582
1574 void InspectorCSSAgent::setCSSPropertyValue(ErrorString* errorString, Element* e lement, CSSPropertyID propertyId, const String& value) 1583 void InspectorCSSAgent::setCSSPropertyValue(ErrorString* errorString, Element* e lement, CSSPropertyID propertyId, const String& value)
lushnikov 2015/09/02 23:00:43 can we rename? there are 3 setCSSPropertyValue.. m
dgozman 2015/09/02 23:03:11 Inline this into it's single usage.
sergeyv 2015/09/03 00:52:27 Done.
1575 { 1584 {
1576 Document* ownerDocument = element->ownerDocument(); 1585 Document* ownerDocument = element->ownerDocument();
1577 if (!ownerDocument->isActive()) { 1586 if (!ownerDocument->isActive()) {
1578 *errorString = "Can't edit a node from a non-active document"; 1587 *errorString = "Can't edit a node from a non-active document";
1579 return; 1588 return;
1580 } 1589 }
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); 1590 RefPtrWillBeRawPtr<CSSStyleDeclaration> foundStyle = findEffectiveDeclaratio n(element, propertyId);
1589 CSSStyleDeclaration* inlineStyle = element->style(); 1591 CSSStyleDeclaration* inlineStyle = element->style();
1590 if (!foundStyle || !foundStyle->parentStyleSheet()) 1592 if (!foundStyle || !foundStyle->parentStyleSheet())
1591 foundStyle = inlineStyle; 1593 foundStyle = inlineStyle;
1592 1594
1593 if (!foundStyle) { 1595 if (!foundStyle) {
1594 *errorString = "Can't find a style to edit"; 1596 *errorString = "Can't find a style to edit";
1595 return; 1597 return;
1596 } 1598 }
1597 1599
1600 setCSSPropertyValue(errorString, element, foundStyle.get(), propertyId, valu e);
1601 }
1602
1603 void InspectorCSSAgent::setCSSPropertyValue(ErrorString* errorString, Element* e lement, CSSStyleDeclaration* style, CSSPropertyID propertyId, const String& valu e, bool forceImportant)
1604 {
1598 InspectorStyleSheetBase* inspectorStyleSheet = nullptr; 1605 InspectorStyleSheetBase* inspectorStyleSheet = nullptr;
1599 RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = nullptr; 1606 RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = nullptr;
1600 if (foundStyle != inlineStyle) { 1607 if (style->parentRule()) {
dgozman 2015/09/02 22:26:16 Let's comment that this means inline style.
sergeyv 2015/09/03 00:52:27 Done.
1601 InspectorStyleSheet* styleSheet = bindStyleSheet(foundStyle->parentStyl eSheet()); 1608 InspectorStyleSheet* styleSheet = bindStyleSheet(style->parentStyleShee t());
1602 inspectorStyleSheet = styleSheet; 1609 inspectorStyleSheet = styleSheet;
1603 sourceData = styleSheet->sourceDataForRule(foundStyle->parentRule()); 1610 sourceData = styleSheet->sourceDataForRule(style->parentRule());
1604 } 1611 } else {
1605
1606 if (!sourceData) {
1607 InspectorStyleSheetForInlineStyle* inlineStyleSheet = asInspectorStyleSh eet(element); 1612 InspectorStyleSheetForInlineStyle* inlineStyleSheet = asInspectorStyleSh eet(element);
1608 inspectorStyleSheet = inlineStyleSheet; 1613 inspectorStyleSheet = inlineStyleSheet;
1609 sourceData = inlineStyleSheet->ruleSourceData(); 1614 sourceData = inlineStyleSheet->ruleSourceData();
1610 } 1615 }
1611 1616
1612 if (!sourceData) { 1617 if (!sourceData) {
1613 *errorString = "Can't find a source to edit"; 1618 *errorString = "Can't find a source to edit";
1614 return; 1619 return;
1615 } 1620 }
1616 1621
1622 Vector<StylePropertyShorthand, 4> shorthands;
1623 getMatchingShorthandsForLonghand(propertyId, &shorthands);
1624
1625 String shorthand = shorthands.size() > 0 ? getPropertyNameString(shorthands [0].id()) : String();
1626 String longhand = getPropertyNameString(propertyId);
1627
1617 int foundIndex = -1; 1628 int foundIndex = -1;
1618 WillBeHeapVector<CSSPropertySourceData> properties = sourceData->styleSource Data->propertyData; 1629 WillBeHeapVector<CSSPropertySourceData> properties = sourceData->styleSource Data->propertyData;
1619 for (unsigned i = 0; i < properties.size(); ++i) { 1630 for (unsigned i = 0; i < properties.size(); ++i) {
1620 CSSPropertySourceData property = properties[properties.size() - i - 1]; 1631 CSSPropertySourceData property = properties[properties.size() - i - 1];
1621 String name = property.name; 1632 String name = property.name;
1622 if (property.disabled) 1633 if (property.disabled)
1623 continue; 1634 continue;
1624 1635
1625 if (name != shorthand && name != longhand) 1636 if (name != shorthand && name != longhand)
1626 continue; 1637 continue;
1627 1638
1628 if (property.important || foundIndex == -1) 1639 if (property.important || foundIndex == -1)
1629 foundIndex = properties.size() - i - 1; 1640 foundIndex = properties.size() - i - 1;
1630 1641
1631 if (property.important) 1642 if (property.important)
1632 break; 1643 break;
1633 } 1644 }
1634 1645
1635 SourceRange bodyRange = sourceData->ruleBodyRange; 1646 SourceRange bodyRange = sourceData->ruleBodyRange;
1636 String styleSheetText; 1647 String styleSheetText;
1637 inspectorStyleSheet->getText(&styleSheetText); 1648 inspectorStyleSheet->getText(&styleSheetText);
1638 String styleText = styleSheetText.substring(bodyRange.start, bodyRange.lengt h()); 1649 String styleText = styleSheetText.substring(bodyRange.start, bodyRange.lengt h());
1639 SourceRange changeRange; 1650 SourceRange changeRange;
1640 if (foundIndex == -1) { 1651 if (foundIndex == -1) {
1641 bool isImportant = inlineStyle->getPropertyPriority(longhand) == "import ant"; 1652 String newPropertyText = "\n" + longhand + ": " + value + (forceImportan t ? " !important" : "") + ";";
1642 String newPropertyText = "\n" + longhand + ": " + value + (isImportant ? " !important" : "") + ";";
1643 if (!styleText.isEmpty() && !styleText.stripWhiteSpace().endsWith(';')) 1653 if (!styleText.isEmpty() && !styleText.stripWhiteSpace().endsWith(';'))
1644 newPropertyText = ";" + newPropertyText; 1654 newPropertyText = ";" + newPropertyText;
1645 styleText.append(newPropertyText); 1655 styleText.append(newPropertyText);
1646 changeRange.start = bodyRange.end; 1656 changeRange.start = bodyRange.end;
1647 changeRange.end = bodyRange.end + newPropertyText.length(); 1657 changeRange.end = bodyRange.end + newPropertyText.length();
1648 } else { 1658 } else {
1649 CSSPropertySourceData declaration = properties[foundIndex]; 1659 CSSPropertySourceData declaration = properties[foundIndex];
1650 String newValueText; 1660 String newValueText;
1651 if (declaration.name == shorthand) 1661 if (declaration.name == shorthand)
1652 newValueText = createShorthandValue(ownerDocument, shorthand, declar ation.value, longhand, value); 1662 newValueText = createShorthandValue(element->ownerDocument(), shorth and, declaration.value, longhand, value);
1653 else 1663 else
1654 newValueText = value; 1664 newValueText = value;
1655 1665
1656 String newPropertyText = declaration.name + ": " + newValueText + (decla ration.important ? " !important" : "") + ";"; 1666 String newPropertyText = declaration.name + ": " + newValueText + (decla ration.important || forceImportant ? " !important" : "") + ";";
1657 styleText.replace(declaration.range.start - bodyRange.start, declaration .range.length(), newPropertyText); 1667 styleText.replace(declaration.range.start - bodyRange.start, declaration .range.length(), newPropertyText);
1658 changeRange.start = declaration.range.start; 1668 changeRange.start = declaration.range.start;
1659 changeRange.end = changeRange.start + newPropertyText.length(); 1669 changeRange.end = changeRange.start + newPropertyText.length();
1660 } 1670 }
1661 CSSStyleDeclaration* resultStyle = setStyleText(errorString, inspectorStyleS heet, bodyRange, styleText); 1671 CSSStyleDeclaration* resultStyle = setStyleText(errorString, inspectorStyleS heet, bodyRange, styleText);
1662 if (resultStyle) { 1672 if (resultStyle) {
1663 frontend()->layoutEditorChange(inspectorStyleSheet->id(), inspectorStyle Sheet->buildSourceRangeObject(changeRange)); 1673 frontend()->layoutEditorChange(inspectorStyleSheet->id(), inspectorStyle Sheet->buildSourceRangeObject(changeRange));
1664 frontend()->flush(); 1674 frontend()->flush();
1665 } 1675 }
1666 } 1676 }
1667 1677
1668 void InspectorCSSAgent::setEffectivePropertyValueForNode(ErrorString* errorStrin g, int nodeId, const String& propertyName, const String& value) 1678 void InspectorCSSAgent::setEffectivePropertyValueForNode(ErrorString* errorStrin g, int nodeId, const String& propertyName, const String& value)
1669 { 1679 {
1670 Element* element = elementForId(errorString, nodeId); 1680 Element* element = elementForId(errorString, nodeId);
1671 if (!element) 1681 if (!element)
1672 return; 1682 return;
1673 1683
1674 CSSPropertyID property = cssPropertyID(propertyName); 1684 CSSPropertyID property = cssPropertyID(propertyName);
1675 if (!property) { 1685 if (!property) {
1676 *errorString = "Invalid property name"; 1686 *errorString = "Invalid property name";
1677 return; 1687 return;
1678 } 1688 }
1679 setCSSPropertyValue(errorString, element, cssPropertyID(propertyName), value ); 1689 setCSSPropertyValue(errorString, element, cssPropertyID(propertyName), value );
1680 } 1690 }
1681 1691
1692 void InspectorCSSAgent::setCSSPropertyValueInRule(ErrorString* errorString, Elem ent* element, CSSStyleRule* rule, CSSPropertyID propertyId, const String& value)
dgozman 2015/09/02 22:26:16 This one can move to layout editor.
sergeyv 2015/09/03 00:52:28 Done.
1693 {
1694 RefPtrWillBeRawPtr<CSSRuleList> ruleList = matchedRulesList(element);
1695 RefPtrWillBeRawPtr<CSSStyleDeclaration> effectiveDeclaration = findEffective Declaration(propertyId, ruleList.get(), element->pseudoId() ? nullptr : element- >style());
1696
1697 CSSStyleRule* effectiveRule = nullptr;
1698 if (effectiveDeclaration->parentRule() && effectiveDeclaration->parentRule() ->type() == CSSRule::STYLE_RULE)
lushnikov 2015/09/02 23:00:43 effectiveDeclaration &&
sergeyv 2015/09/03 00:52:28 Done.
1699 effectiveRule = toCSSStyleRule(effectiveDeclaration->parentRule());
1700
1701 String longhand = getPropertyNameString(propertyId);
1702 bool forceImportant = effectiveDeclaration && effectiveDeclaration->getPrope rtyPriority(longhand) == "important";
1703
1704 if (effectiveDeclaration && rule) {
1705 int effectiveRuleIndex = -1;
1706 int ruleIndex = -1;
1707 for (unsigned i = 0, size = ruleList ? ruleList->length() : 0; i < size; ++i) {
1708 CSSRule* currentRule = ruleList->item(size - i - 1);
1709 if (currentRule == effectiveRule)
1710 effectiveRuleIndex = i;
1711 if (currentRule == rule)
1712 ruleIndex = i;
1713 }
1714 forceImportant |= effectiveRuleIndex < ruleIndex;
1715 }
1716
1717 setCSSPropertyValue(errorString, element, rule ? rule->style() : element->st yle(), propertyId, value, forceImportant);
1718 }
1719
1682 DEFINE_TRACE(InspectorCSSAgent) 1720 DEFINE_TRACE(InspectorCSSAgent)
1683 { 1721 {
1684 visitor->trace(m_domAgent); 1722 visitor->trace(m_domAgent);
1685 visitor->trace(m_pageAgent); 1723 visitor->trace(m_pageAgent);
1686 visitor->trace(m_resourceAgent); 1724 visitor->trace(m_resourceAgent);
1687 visitor->trace(m_resourceContentLoader); 1725 visitor->trace(m_resourceContentLoader);
1688 #if ENABLE(OILPAN) 1726 #if ENABLE(OILPAN)
1689 visitor->trace(m_idToInspectorStyleSheet); 1727 visitor->trace(m_idToInspectorStyleSheet);
1690 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); 1728 visitor->trace(m_idToInspectorStyleSheetForInlineStyle);
1691 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); 1729 visitor->trace(m_cssStyleSheetToInspectorStyleSheet);
1692 visitor->trace(m_documentToCSSStyleSheets); 1730 visitor->trace(m_documentToCSSStyleSheets);
1693 visitor->trace(m_invalidatedDocuments); 1731 visitor->trace(m_invalidatedDocuments);
1694 visitor->trace(m_nodeToInspectorStyleSheet); 1732 visitor->trace(m_nodeToInspectorStyleSheet);
1695 visitor->trace(m_documentToViaInspectorStyleSheet); 1733 visitor->trace(m_documentToViaInspectorStyleSheet);
1696 #endif 1734 #endif
1697 visitor->trace(m_inspectorUserAgentStyleSheet); 1735 visitor->trace(m_inspectorUserAgentStyleSheet);
1698 InspectorBaseAgent::trace(visitor); 1736 InspectorBaseAgent::trace(visitor);
1699 } 1737 }
1700 1738
1701 } // namespace blink 1739 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698