Changeset 90619 in webkit


Ignore:
Timestamp:
Jul 8, 2011 4:32:40 AM (13 years ago)
Author:
apavlov@chromium.org
Message:

Web Inspector: Adding CSS properties results in messy style rules
https://bugs.webkit.org/show_bug.cgi?id=63622

Reviewed by Pavel Feldman.

Source/WebCore:

Test: inspector/styles/styles-formatting.html

  • CMakeLists.txt:
  • GNUmakefile.list.am:
  • WebCore.gypi:
  • WebCore.pro:
  • WebCore.vcproj/WebCore.vcproj:
  • WebCore.xcodeproj/project.pbxproj:
  • css/CSSPropertySourceData.cpp:

(WebCore::SourceRange::length):

  • css/CSSPropertySourceData.h:
  • inspector/InspectorStyleSheet.cpp:

(WebCore::InspectorStyle::InspectorStyle):
(WebCore::InspectorStyle::setPropertyText):
(WebCore::InspectorStyle::toggleProperty):
(WebCore::InspectorStyle::applyStyleText):
(WebCore::InspectorStyle::newLineAndWhitespaceDelimiters):

  • inspector/InspectorStyleSheet.h:
  • inspector/InspectorStyleTextEditor.cpp: Added.

(WebCore::InspectorStyleTextEditor::InspectorStyleTextEditor):
(WebCore::InspectorStyleTextEditor::insertProperty):
(WebCore::InspectorStyleTextEditor::replaceProperty):
(WebCore::InspectorStyleTextEditor::removeProperty):
(WebCore::InspectorStyleTextEditor::enableProperty):
(WebCore::InspectorStyleTextEditor::disableProperty):
(WebCore::InspectorStyleTextEditor::disabledIndexByOrdinal):
(WebCore::InspectorStyleTextEditor::shiftDisabledProperties):
(WebCore::InspectorStyleTextEditor::internalReplaceProperty):

  • inspector/InspectorStyleTextEditor.h: Added.

(WebCore::InspectorStyleTextEditor::styleText):

LayoutTests:

  • inspector/styles/styles-formatting-expected.txt: Added.
  • inspector/styles/styles-formatting.html: Added.
  • inspector/styles/styles-new-API-expected.txt:
  • inspector/styles/styles-new-API.html:
Location:
trunk
Files:
4 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r90617 r90619  
     12011-07-07  Alexander Pavlov  <apavlov@chromium.org>
     2
     3        Web Inspector: Adding CSS properties results in messy style rules
     4        https://bugs.webkit.org/show_bug.cgi?id=63622
     5
     6        Reviewed by Pavel Feldman.
     7
     8        * inspector/styles/styles-formatting-expected.txt: Added.
     9        * inspector/styles/styles-formatting.html: Added.
     10        * inspector/styles/styles-new-API-expected.txt:
     11        * inspector/styles/styles-new-API.html:
     12
    1132011-07-08  Alexander Pavlov  <apavlov@chromium.org>
    214
  • trunk/LayoutTests/inspector/styles/styles-new-API-expected.txt

    r86754 r90619  
    311311=== After style text set ===
    312312
    313 StyleSheet: 'html *, body[foo="bar"] {        color: white; background: black;}body {    text-align: justify;}'
     313StyleSheet: 'html *, body[foo="bar"] {
     314    color: white; background: black;
     315}body {    text-align: justify;}'
    314316
    315317rule
    316318html *, body[foo="bar"]: []
    317 ['color':'white'] @[8-21] active
    318 ['background':'black'] @[22-40] active
     319['color':'white'] @[5-18] active
     320['background':'black'] @[19-37] active
    319321['background-image':'initial'] @[undefined-undefined] style
    320322['background-repeat-x':'initial'] @[undefined-undefined] style
     
    363365rule
    364366html *, body[foo="bar"]: []
    365 ['color':'white'] @[8-21] active
    366 ['background':'black'] @[22-40] active
     367['color':'white'] @[5-18] active
     368['background':'black'] @[19-37] active
    367369['background-image':'initial'] @[undefined-undefined] style
    368370['background-repeat-x':'initial'] @[undefined-undefined] style
     
    381383rule
    382384body: [inspector]
    383 ['font-family':'serif'] @[0-19] active
     385['font-family':'serif'] @[5-24] active
    384386
    385387rule
     
    401403['background-color':'green' is-important] @[undefined-undefined] style
    402404
    403 === After two properties disabled, one property added, one property removed ===
     405=== After property manipulations ===
    404406
    405407style
    406408raw style
    407409[text='height : 100% ;'] disabled
    408 ['border':'1px solid'] @[6-24] active
    409 ['border-width':'2px'] @[25-43] inactive
    410 ['googles':'abra' non-parsed] @[45-59] active
    411 ['foo':'.bar' non-parsed] @[60-70] active
    412 ['-moz-goog':'1***' non-parsed] @[71-87] active
     410['border':'1px solid'] @[1-19] active
     411['border-width':'2px'] @[20-38] inactive
     412['googles':'abra' non-parsed] @[40-54] active
     413['foo':'.bar' non-parsed] @[55-65] active
     414['-moz-goog':'1***' non-parsed] @[66-82] active
    413415['font-size':'12px'] @[88-104] active
    414 ['border-width':'1px'] @[104-122] active
     416['border-width':'1px'] @[109-127] active
    415417[text='font-size: 14px;'] disabled
    416418['border-top-style':'solid'] @[undefined-undefined] style
     
    429431raw style
    430432[text='height : 100% ;'] disabled
    431 ['border':'1px solid'] @[6-24] active
    432 ['border-width':'2px'] @[25-43] inactive
    433 ['googles':'abra' non-parsed] @[45-59] active
    434 ['foo':'.bar' non-parsed] @[60-70] active
    435 ['-moz-goog':'1***' non-parsed] @[71-87] active
     433['border':'1px solid'] @[1-19] active
     434['border-width':'2px'] @[20-38] inactive
     435['googles':'abra' non-parsed] @[40-54] active
     436['foo':'.bar' non-parsed] @[55-65] active
     437['-moz-goog':'1***' non-parsed] @[66-82] active
    436438['font-size':'12px'] @[88-104] inactive
    437 ['border-width':'1px'] @[104-122] active
    438 ['font-size':'14px'] @[123-139] active
     439['border-width':'1px'] @[109-127] active
     440['font-size':'14px'] @[132-148] active
    439441['border-top-style':'solid'] @[undefined-undefined] style
    440442['border-right-style':'solid'] @[undefined-undefined] style
  • trunk/LayoutTests/inspector/styles/styles-new-API.html

    r89922 r90619  
    199199            }
    200200            InspectorTest.addResult("");
    201             InspectorTest.addResult("=== After two properties disabled, one property added, one property removed ===");
     201            InspectorTest.addResult("=== After property manipulations ===");
    202202            dumpRuleOrStyle(style);
    203203            test_enableProperty(style.styleId);
     
    219219            // border-width: 0px;
    220220
    221             var style = styles.matchedCSSRules[1].style;
    222             CSSAgent.toggleProperty(style.styleId, 0, true); // height: 100%
    223             CSSAgent.toggleProperty(style.styleId, 7, true); // border-width: 0px
    224             CSSAgent.setPropertyText(style.styleId, 7, "font-size: 12px;", false);
    225             CSSAgent.setPropertyText(style.styleId, 9, "font-size: 14px;", false);
    226             CSSAgent.toggleProperty(style.styleId, 9, true); // font-size: 14px
    227             CSSAgent.setPropertyText(style.styleId, 8, "border-width: 1px;", true);
    228             CSSAgent.toggleProperty(style.styleId, 8, false); // border-width: 1px
     221            var styleId = styles.matchedCSSRules[1].style.styleId;
     222            CSSAgent.toggleProperty(styleId, 0, true); // height: 100%
     223            CSSAgent.toggleProperty(styleId, 7, true); // border-width: 0px
     224            CSSAgent.setPropertyText(styleId, 7, "font-size: 12px;", false);
     225            CSSAgent.setPropertyText(styleId, 9, "font-size: 14px;", false);
     226            CSSAgent.toggleProperty(styleId, 9, true); // font-size: 14px
     227            CSSAgent.setPropertyText(styleId, 8, "border-width: 1px;", true);
     228            CSSAgent.toggleProperty(styleId, 8, false); // border-width: 1px
    229229            // height : 100% ; [d]
    230230            // border: 1px solid;
     
    238238            // font-size: 14px; [d]
    239239
    240             CSSAgent.setPropertyText(style.styleId, 3, "", true, didDisableProperty);
     240            CSSAgent.setPropertyText(styleId, 3, "", true, didDisableProperty);
    241241        }
    242242
  • trunk/Source/WebCore/CMakeLists.txt

    r90531 r90619  
    883883    inspector/InspectorState.cpp
    884884    inspector/InspectorStyleSheet.cpp
     885    inspector/InspectorStyleTextEditor.cpp
    885886    inspector/InspectorTimelineAgent.cpp
    886887    inspector/InspectorValues.cpp
  • trunk/Source/WebCore/ChangeLog

    r90617 r90619  
     12011-07-07  Alexander Pavlov  <apavlov@chromium.org>
     2
     3        Web Inspector: Adding CSS properties results in messy style rules
     4        https://bugs.webkit.org/show_bug.cgi?id=63622
     5
     6        Reviewed by Pavel Feldman.
     7
     8        Test: inspector/styles/styles-formatting.html
     9
     10        * CMakeLists.txt:
     11        * GNUmakefile.list.am:
     12        * WebCore.gypi:
     13        * WebCore.pro:
     14        * WebCore.vcproj/WebCore.vcproj:
     15        * WebCore.xcodeproj/project.pbxproj:
     16        * css/CSSPropertySourceData.cpp:
     17        (WebCore::SourceRange::length):
     18        * css/CSSPropertySourceData.h:
     19        * inspector/InspectorStyleSheet.cpp:
     20        (WebCore::InspectorStyle::InspectorStyle):
     21        (WebCore::InspectorStyle::setPropertyText):
     22        (WebCore::InspectorStyle::toggleProperty):
     23        (WebCore::InspectorStyle::applyStyleText):
     24        (WebCore::InspectorStyle::newLineAndWhitespaceDelimiters):
     25        * inspector/InspectorStyleSheet.h:
     26        * inspector/InspectorStyleTextEditor.cpp: Added.
     27        (WebCore::InspectorStyleTextEditor::InspectorStyleTextEditor):
     28        (WebCore::InspectorStyleTextEditor::insertProperty):
     29        (WebCore::InspectorStyleTextEditor::replaceProperty):
     30        (WebCore::InspectorStyleTextEditor::removeProperty):
     31        (WebCore::InspectorStyleTextEditor::enableProperty):
     32        (WebCore::InspectorStyleTextEditor::disableProperty):
     33        (WebCore::InspectorStyleTextEditor::disabledIndexByOrdinal):
     34        (WebCore::InspectorStyleTextEditor::shiftDisabledProperties):
     35        (WebCore::InspectorStyleTextEditor::internalReplaceProperty):
     36        * inspector/InspectorStyleTextEditor.h: Added.
     37        (WebCore::InspectorStyleTextEditor::styleText):
     38
    1392011-07-08  Alexander Pavlov  <apavlov@chromium.org>
    240
  • trunk/Source/WebCore/GNUmakefile.list.am

    r90269 r90619  
    20062006        Source/WebCore/inspector/InspectorStyleSheet.cpp \
    20072007        Source/WebCore/inspector/InspectorStyleSheet.h \
     2008        Source/WebCore/inspector/InspectorStyleTextEditor.cpp \
     2009        Source/WebCore/inspector/InspectorStyleTextEditor.h \
    20082010        Source/WebCore/inspector/InspectorTimelineAgent.cpp \
    20092011        Source/WebCore/inspector/InspectorTimelineAgent.h \
  • trunk/Source/WebCore/WebCore.gypi

    r90397 r90619  
    33183318            'inspector/InspectorStyleSheet.cpp',
    33193319            'inspector/InspectorStyleSheet.h',
     3320            'inspector/InspectorStyleTextEditor.cpp',
     3321            'inspector/InspectorStyleTextEditor.h',
    33203322            'inspector/InspectorTimelineAgent.cpp',
    33213323            'inspector/InspectorTimelineAgent.h',
  • trunk/Source/WebCore/WebCore.pro

    r90450 r90619  
    836836    inspector/InspectorState.cpp \
    837837    inspector/InspectorStyleSheet.cpp \
     838    inspector/InspectorStyleTextEditor.cpp \
    838839    inspector/InspectorTimelineAgent.cpp \
    839840    inspector/InspectorValues.cpp \
     
    17951796    inspector/InspectorState.h \
    17961797    inspector/InspectorStyleSheet.h \
     1798    inspector/InspectorStyleTextEditor.h \
    17971799    inspector/InspectorTimelineAgent.h \
    17981800    inspector/InspectorWorkerAgent.h \
  • trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj

    r90365 r90619  
    6775567755                        </File>
    6775667756                        <File
     67757                                RelativePath="..\inspector\InspectorStyleTextEditor.cpp"
     67758                                >
     67759                        </File>
     67760                        <File
     67761                                RelativePath="..\inspector\InspectorStyleTextEditor.h"
     67762                                >
     67763                        </File>
     67764                        <File
    6775767765                                RelativePath="..\inspector\InspectorTimelineAgent.cpp"
    6775867766                                >
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r90269 r90619  
    17611761                81BE20D311F4BC3200915DFA /* JSIDBCursor.h in Headers */ = {isa = PBXBuildFile; fileRef = 81BE20A811F4B66F00915DFA /* JSIDBCursor.h */; };
    17621762                81F65FF613788FAA00FF6F2D /* DragState.h in Headers */ = {isa = PBXBuildFile; fileRef = 81F65FF513788FAA00FF6F2D /* DragState.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1763                82889B4C13C62392009A6156 /* InspectorStyleTextEditor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 82889B4A13C62392009A6156 /* InspectorStyleTextEditor.cpp */; };
     1764                82889B4D13C62392009A6156 /* InspectorStyleTextEditor.h in Headers */ = {isa = PBXBuildFile; fileRef = 82889B4B13C62392009A6156 /* InspectorStyleTextEditor.h */; };
    17631765                82AB1743124B99EC00C5069D /* InspectorCSSAgent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 82AB1741124B99EC00C5069D /* InspectorCSSAgent.cpp */; };
    17641766                82AB1744124B99EC00C5069D /* InspectorCSSAgent.h in Headers */ = {isa = PBXBuildFile; fileRef = 82AB1742124B99EC00C5069D /* InspectorCSSAgent.h */; };
     
    83298331                81BE20A811F4B66F00915DFA /* JSIDBCursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSIDBCursor.h; sourceTree = "<group>"; };
    83308332                81F65FF513788FAA00FF6F2D /* DragState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DragState.h; sourceTree = "<group>"; };
     8333                82889B4A13C62392009A6156 /* InspectorStyleTextEditor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorStyleTextEditor.cpp; sourceTree = "<group>"; };
     8334                82889B4B13C62392009A6156 /* InspectorStyleTextEditor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorStyleTextEditor.h; sourceTree = "<group>"; };
    83318335                82AB1741124B99EC00C5069D /* InspectorCSSAgent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorCSSAgent.cpp; sourceTree = "<group>"; };
    83328336                82AB1742124B99EC00C5069D /* InspectorCSSAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorCSSAgent.h; sourceTree = "<group>"; };
     
    1341713421                                82AB176F125C826700C5069D /* InspectorStyleSheet.cpp */,
    1341813422                                82AB1770125C826700C5069D /* InspectorStyleSheet.h */,
     13423                                82889B4A13C62392009A6156 /* InspectorStyleTextEditor.cpp */,
     13424                                82889B4B13C62392009A6156 /* InspectorStyleTextEditor.h */,
    1341913425                                754133A9102E00F400075D00 /* InspectorTimelineAgent.cpp */,
    1342013426                                754133A7102E00E800075D00 /* InspectorTimelineAgent.h */,
     
    2308923095                                431A2F9C13B6F2B0007791E4 /* SVGAnimatedNumberOptionalNumber.h in Headers */,
    2309023096                                A5ABB78713B904BC00F197E3 /* LineBreakIteratorPoolICU.h in Headers */,
     23097                                82889B4D13C62392009A6156 /* InspectorStyleTextEditor.h in Headers */,
    2309123098                        );
    2309223099                        runOnlyForDeploymentPostprocessing = 0;
     
    2585625863                                431A308813B8F978007791E4 /* SVGAnimatedBoolean.cpp in Sources */,
    2585725864                                43F6FD9613BCD0B100224052 /* SVGAnimatedInteger.cpp in Sources */,
     25865                                82889B4C13C62392009A6156 /* InspectorStyleTextEditor.cpp in Sources */,
    2585825866                        );
    2585925867                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/Source/WebCore/css/CSSPropertySourceData.cpp

    r69196 r90619  
    5353    , end(end)
    5454{
     55}
     56
     57unsigned SourceRange::length() const
     58{
     59    return end - start;
    5560}
    5661
  • trunk/Source/WebCore/css/CSSPropertySourceData.h

    r69196 r90619  
    4545    SourceRange();
    4646    SourceRange(unsigned start, unsigned end);
     47    unsigned length() const;
    4748
    4849    unsigned start;
  • trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp

    r89449 r90619  
    149149    , m_style(style)
    150150    , m_parentStyleSheet(parentStyleSheet)
     151    , m_formatAcquired(false)
    151152{
    152153    ASSERT(m_style);
     
    176177
    177178// This method does the following preprocessing of |propertyText| with |overwrite| == false and |index| past the last active property:
    178 // - If the last property (if present) has no subsequent whitespace in the style declaration, a space is prepended to |propertyText|.
    179179// - If the last property (if present) has no closing ";", the ";" is prepended to the current |propertyText| value.
     180// - A heuristic formatting is attempted to retain the style structure.
    180181//
    181182// The propertyText (if not empty) is checked to be a valid style declaration (containing at least one property). If not,
     
    184185{
    185186    ASSERT(m_parentStyleSheet);
     187    DEFINE_STATIC_LOCAL(String, bogusPropertyName, ("-webkit-boguz-propertee"));
     188
    186189    if (!m_parentStyleSheet->ensureParsedDataReady()) {
    187190        *errorString = "Internal error: no stylesheet parsed data available";
     
    191194    Vector<InspectorStyleProperty> allProperties;
    192195    populateAllProperties(&allProperties);
    193 
    194     unsigned propertyStart = 0; // Need to initialize to make the compiler happy.
    195     long propertyLengthDelta;
    196196
    197197    if (propertyText.stripWhiteSpace().length()) {
     
    199199        CSSParser p;
    200200        RefPtr<CSSStyleSourceData> sourceData = CSSStyleSourceData::create();
    201         p.parseDeclaration(tempMutableStyle.get(), propertyText + " -webkit-boguz-propertee: none", &sourceData);
     201        p.parseDeclaration(tempMutableStyle.get(), propertyText + " " + bogusPropertyName + ": none", &sourceData);
    202202        Vector<CSSPropertySourceData>& propertyData = sourceData->propertyData;
    203203        unsigned propertyCount = propertyData.size();
     
    210210
    211211        // Check for a proper propertyText termination (the parser could at least restore to the PROPERTY_NAME state).
    212         if (propertyData.at(propertyCount - 1).name != "-webkit-boguz-propertee") {
     212        if (propertyData.at(propertyCount - 1).name != bogusPropertyName) {
    213213            *errorString = "Invalid property value";
    214214            return false;
     
    216216    }
    217217
    218     if (overwrite) {
    219         ASSERT(index < allProperties.size());
    220         InspectorStyleProperty& property = allProperties.at(index);
    221         propertyStart = property.sourceData.range.start;
    222         unsigned propertyEnd = property.sourceData.range.end;
    223         unsigned oldLength = propertyEnd - propertyStart;
    224         unsigned newLength = propertyText.length();
    225         propertyLengthDelta = newLength - oldLength;
    226 
    227         if (!property.disabled) {
    228             bool success = replacePropertyInStyleText(property, propertyText);
    229             if (!success) {
    230                 *errorString = "Internal error: could not replace property value";
    231                 return false;
    232             }
    233         } else {
    234             unsigned textLength = propertyText.length();
    235             unsigned disabledIndex = disabledIndexByOrdinal(index, false, allProperties);
    236             if (!textLength) {
    237                 // Delete disabled property.
    238                 m_disabledProperties.remove(disabledIndex);
    239             } else {
    240                 // Patch disabled property text.
    241                 m_disabledProperties.at(disabledIndex).rawText = propertyText;
    242             }
    243 
    244             // We should not shift subsequent disabled properties when altering a disabled property.
    245             return true;
    246         }
    247     } else {
    248         // Insert at index.
    249         RefPtr<CSSRuleSourceData> sourceData = m_parentStyleSheet->ruleSourceDataFor(m_style.get());
    250         if (!sourceData) {
    251             *errorString = "Internal error: no CSS rule source found";
    252             return false;
    253         }
    254         String text;
    255         bool success = styleText(&text);
    256         if (!success) {
    257             *errorString = "Internal error: could not fetch style text";
    258             return false;
    259         }
    260         propertyLengthDelta = propertyText.length();
    261 
    262         bool insertLast = true;
    263         if (index < allProperties.size()) {
    264             InspectorStyleProperty& property = allProperties.at(index);
    265             if (property.hasSource) {
    266                 propertyStart = property.sourceData.range.start;
    267                 // If inserting before a disabled property, it should be shifted, too.
    268                 insertLast = false;
    269             }
    270         }
    271 
    272         String textToSet = propertyText;
    273         if (insertLast) {
    274             propertyStart = sourceData->styleSourceData->styleBodyRange.end - sourceData->styleSourceData->styleBodyRange.start;
    275             if (propertyStart && propertyText.length()) {
    276                 const UChar* characters = text.characters();
    277 
    278                 unsigned curPos = propertyStart - 1; // The last position of style declaration, since propertyStart points past one.
    279                 while (curPos && isHTMLSpace(characters[curPos]))
    280                     --curPos;
    281                 if (curPos && characters[curPos] != ';') {
    282                     // Prepend a ";" to the property text if appending to a style declaration where
    283                     // the last property has no trailing ";".
    284                     textToSet.insert("; ", 0);
    285                 } else if (!isHTMLSpace(characters[propertyStart - 1])) {
    286                     // Prepend a " " if the last declaration character is not an HTML space.
    287                     textToSet.insert(" ", 0);
    288                 }
    289             }
    290         }
    291 
    292         text.insert(textToSet, propertyStart);
    293         m_parentStyleSheet->setStyleText(m_style.get(), text);
    294     }
    295 
    296     // Recompute subsequent disabled property ranges if acting on a non-disabled property.
    297     shiftDisabledProperties(disabledIndexByOrdinal(index, true, allProperties), propertyLengthDelta);
    298 
    299     return true;
     218    RefPtr<CSSRuleSourceData> sourceData = m_parentStyleSheet->ruleSourceDataFor(m_style.get());
     219    if (!sourceData) {
     220        *errorString = "Internal error: no CSS rule source found";
     221        return false;
     222    }
     223
     224    String text;
     225    bool success = styleText(&text);
     226    if (!success) {
     227        *errorString = "Internal error: could not fetch style text";
     228        return false;
     229    }
     230
     231    InspectorStyleTextEditor editor(&allProperties, &m_disabledProperties, text, newLineAndWhitespaceDelimiters());
     232    if (overwrite)
     233        editor.replaceProperty(index, propertyText);
     234    else
     235        editor.insertProperty(index, propertyText, sourceData->styleSourceData->styleBodyRange.length());
     236
     237    return applyStyleText(editor.styleText());
    300238}
    301239
     
    307245        return false;
    308246    }
     247
    309248    RefPtr<CSSRuleSourceData> sourceData = m_parentStyleSheet->ruleSourceDataFor(m_style.get());
    310249    if (!sourceData) {
    311250        *errorString = "Internal error: No source data for the style found";
     251        return false;
     252    }
     253
     254    String text;
     255    bool success = styleText(&text);
     256    if (!success) {
     257        *errorString = "Internal error: could not fetch style text";
    312258        return false;
    313259    }
     
    324270        return true; // Idempotent operation.
    325271
    326     bool success;
    327     if (!disable)
    328         success = enableProperty(index, allProperties);
     272    InspectorStyleTextEditor editor(&allProperties, &m_disabledProperties, text, newLineAndWhitespaceDelimiters());
     273    if (disable)
     274        editor.disableProperty(index);
    329275    else
    330         success = disableProperty(index, allProperties);
    331 
    332     return success;
    333 }
    334 
    335 // static
    336 unsigned InspectorStyle::disabledIndexByOrdinal(unsigned ordinal, bool canUseSubsequent, Vector<InspectorStyleProperty>& allProperties)
    337 {
    338     unsigned disabledIndex = 0;
    339     for (unsigned i = 0, size = allProperties.size(); i < size; ++i) {
    340         InspectorStyleProperty& property = allProperties.at(i);
    341         if (property.disabled) {
    342             if (i == ordinal || (canUseSubsequent && i > ordinal))
    343                 return disabledIndex;
    344             ++disabledIndex;
    345         }
    346     }
    347 
    348     return UINT_MAX;
     276        editor.enableProperty(index);
     277
     278    return applyStyleText(editor.styleText());
    349279}
    350280
     
    364294    *result = styleSheetText.substring(bodyRange.start, bodyRange.end - bodyRange.start);
    365295    return true;
    366 }
    367 
    368 bool InspectorStyle::disableProperty(unsigned indexToDisable, Vector<InspectorStyleProperty>& allProperties)
    369 {
    370     // Precondition: |indexToEnable| points to an enabled property.
    371     const InspectorStyleProperty& property = allProperties.at(indexToDisable);
    372     unsigned propertyStart = property.sourceData.range.start;
    373     InspectorStyleProperty disabledProperty(property);
    374     String oldStyleText;
    375     bool success = styleText(&oldStyleText);
    376     if (!success)
    377         return false;
    378     disabledProperty.setRawTextFromStyleDeclaration(oldStyleText);
    379     disabledProperty.disabled = true;
    380     disabledProperty.sourceData.range.end = propertyStart;
    381     // This may have to be negated below.
    382     long propertyLength = property.sourceData.range.end - propertyStart;
    383     success = replacePropertyInStyleText(property, "");
    384     if (!success)
    385         return false;
    386 
    387     // Add disabled property at correct position.
    388     unsigned insertionIndex = disabledIndexByOrdinal(indexToDisable, true, allProperties);
    389     if (insertionIndex == UINT_MAX)
    390         m_disabledProperties.append(disabledProperty);
    391     else {
    392         m_disabledProperties.insert(insertionIndex, disabledProperty);
    393         shiftDisabledProperties(insertionIndex + 1, -propertyLength); // Property removed from text - shift these back.
    394     }
    395     return true;
    396 }
    397 
    398 bool InspectorStyle::enableProperty(unsigned indexToEnable, Vector<InspectorStyleProperty>& allProperties)
    399 {
    400     // Precondition: |indexToEnable| points to a disabled property.
    401     unsigned disabledIndex = disabledIndexByOrdinal(indexToEnable, false, allProperties);
    402     if (disabledIndex == UINT_MAX)
    403         return false;
    404 
    405     InspectorStyleProperty disabledProperty = m_disabledProperties.at(disabledIndex);
    406     m_disabledProperties.remove(disabledIndex);
    407     bool success = replacePropertyInStyleText(disabledProperty, disabledProperty.rawText);
    408     if (success)
    409         shiftDisabledProperties(disabledIndex, disabledProperty.rawText.length());
    410     return success;
    411296}
    412297
     
    547432}
    548433
    549 
    550 void InspectorStyle::shiftDisabledProperties(unsigned fromIndex, long delta)
    551 {
    552     for (unsigned i = fromIndex, size = m_disabledProperties.size(); i < size; ++i) {
    553         SourceRange& range = m_disabledProperties.at(i).sourceData.range;
    554         range.start += delta;
    555         range.end += delta;
    556     }
    557 }
    558 
    559 bool InspectorStyle::replacePropertyInStyleText(const InspectorStyleProperty& property, const String& newText)
    560 {
    561     // Precondition: m_parentStyleSheet->ensureParsedDataReady() has been called successfully.
    562     String text;
    563     bool success = styleText(&text);
    564     if (!success)
    565         return false;
    566     const SourceRange& range = property.sourceData.range;
    567     text.replace(range.start, range.end - range.start, newText);
    568     success = m_parentStyleSheet->setStyleText(m_style.get(), text);
    569     return success;
     434bool InspectorStyle::applyStyleText(const String& text)
     435{
     436    return m_parentStyleSheet->setStyleText(m_style.get(), text);
    570437}
    571438
     
    621488}
    622489
     490NewLineAndWhitespace& InspectorStyle::newLineAndWhitespaceDelimiters() const
     491{
     492    DEFINE_STATIC_LOCAL(String, defaultPrefix, ("    "));
     493
     494    if (m_formatAcquired)
     495        return m_format;
     496
     497    RefPtr<CSSRuleSourceData> sourceData = (m_parentStyleSheet && m_parentStyleSheet->ensureParsedDataReady()) ? m_parentStyleSheet->ruleSourceDataFor(m_style.get()) : 0;
     498    Vector<CSSPropertySourceData>* sourcePropertyData = sourceData ? &(sourceData->styleSourceData->propertyData) : 0;
     499    int propertyCount;
     500    if (!sourcePropertyData || !(propertyCount = sourcePropertyData->size())) {
     501        m_format.first = "\n";
     502        m_format.second = defaultPrefix;
     503        return m_format; // Do not remember the default formatting and attempt to acquire it later.
     504    }
     505
     506    String text;
     507    bool success = styleText(&text);
     508    ASSERT_UNUSED(success, success);
     509
     510    m_formatAcquired = true;
     511
     512    String formatLineFeed = "";
     513    String formatPropertyPrefix = "";
     514    String prefix;
     515    String candidatePrefix = defaultPrefix;
     516    int scanStart = 0;
     517    int propertyIndex = 0;
     518    bool isFullPrefixScanned = false;
     519    bool lineFeedTerminated = false;
     520    const UChar* characters = text.characters();
     521    while (propertyIndex < propertyCount) {
     522        const WebCore::CSSPropertySourceData& currentProperty = sourcePropertyData->at(propertyIndex++);
     523
     524        bool processNextProperty = false;
     525        int scanEnd = currentProperty.range.start;
     526        for (int i = scanStart; i < scanEnd; ++i) {
     527            UChar ch = characters[i];
     528            bool isLineFeed = isHTMLLineBreak(ch);
     529            if (isLineFeed) {
     530                if (!lineFeedTerminated)
     531                    formatLineFeed.append(ch);
     532            } else if (isHTMLSpace(ch))
     533                prefix.append(ch);
     534            else {
     535                candidatePrefix = prefix;
     536                prefix = "";
     537                scanStart = currentProperty.range.end;
     538                ++propertyIndex;
     539                processNextProperty = true;
     540                break;
     541            }
     542            if (!isLineFeed && formatLineFeed.length())
     543                lineFeedTerminated = true;
     544        }
     545        if (!processNextProperty) {
     546            isFullPrefixScanned = true;
     547            break;
     548        }
     549    }
     550
     551    m_format.first = formatLineFeed;
     552    m_format.second = isFullPrefixScanned ? prefix : candidatePrefix;
     553    return m_format;
     554}
     555
    623556PassRefPtr<InspectorStyleSheet> InspectorStyleSheet::create(const String& id, PassRefPtr<CSSStyleSheet> pageStyleSheet, const String& origin, const String& documentURL)
    624557{
  • trunk/Source/WebCore/inspector/InspectorStyleSheet.h

    r89224 r90619  
    2727
    2828#include "CSSPropertySourceData.h"
     29#include "InspectorStyleTextEditor.h"
    2930#include "InspectorValues.h"
    3031#include "PlatformString.h"
     
    136137    InspectorStyle(const InspectorCSSId& styleId, PassRefPtr<CSSStyleDeclaration> style, InspectorStyleSheet* parentStyleSheet);
    137138
    138     static unsigned disabledIndexByOrdinal(unsigned ordinal, bool canUseSubsequent, Vector<InspectorStyleProperty>& allProperties);
    139 
    140139    bool styleText(String* result) const;
    141     bool disableProperty(unsigned indexToDisable, Vector<InspectorStyleProperty>& allProperties);
    142     bool enableProperty(unsigned indexToEnable, Vector<InspectorStyleProperty>& allProperties);
    143140    bool populateAllProperties(Vector<InspectorStyleProperty>* result) const;
    144141    void populateObjectWithStyleProperties(InspectorObject* result) const;
    145     void shiftDisabledProperties(unsigned fromIndex, long offset);
    146     bool replacePropertyInStyleText(const InspectorStyleProperty& property, const String& newText);
     142    bool applyStyleText(const String&);
    147143    String shorthandValue(const String& shorthandProperty) const;
    148144    String shorthandPriority(const String& shorthandProperty) const;
    149145    Vector<String> longhandProperties(const String& shorthandProperty) const;
     146    NewLineAndWhitespace& newLineAndWhitespaceDelimiters() const;
    150147
    151148    InspectorCSSId m_styleId;
     
    153150    InspectorStyleSheet* m_parentStyleSheet;
    154151    Vector<InspectorStyleProperty> m_disabledProperties;
     152    mutable std::pair<String, String> m_format;
     153    mutable bool m_formatAcquired;
    155154};
    156155
Note: See TracChangeset for help on using the changeset viewer.