Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2011, 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 
     25 #include "config.h"
     26 #include "core/inspector/InspectorStyleTextEditor.h"
     27 
     28 #include "core/css/CSSPropertySourceData.h"
     29 #include "core/html/parser/HTMLParserIdioms.h"
     30 #include "core/inspector/InspectorStyleSheet.h"
     31 
     32 namespace WebCore {
     33 
     34 InspectorStyleTextEditor::InspectorStyleTextEditor(WillBeHeapVector<InspectorStyleProperty>* allProperties, const String& styleText, const NewLineAndWhitespace& format)
     35     : m_allProperties(allProperties)
     36     , m_styleText(styleText)
     37     , m_format(format)
     38 {
     39 }
     40 
     41 void InspectorStyleTextEditor::insertProperty(unsigned index, const String& propertyText, unsigned styleBodyLength)
     42 {
     43     long propertyStart = 0;
     44 
     45     bool insertLast = true;
     46     if (index < m_allProperties->size()) {
     47         const InspectorStyleProperty& property = m_allProperties->at(index);
     48         if (property.hasSource) {
     49             propertyStart = property.sourceData.range.start;
     50             // If inserting before a disabled property, it should be shifted, too.
     51             insertLast = false;
     52         }
     53     }
     54 
     55     bool insertFirstInSource = !m_allProperties->size() || !m_allProperties->at(0).hasSource;
     56     bool insertLastInSource = true;
     57     for (unsigned i = index, size = m_allProperties->size(); i < size; ++i) {
     58         const InspectorStyleProperty& property = m_allProperties->at(i);
     59         if (property.hasSource) {
     60             insertLastInSource = false;
     61             break;
     62         }
     63     }
     64 
     65     String textToSet = propertyText;
     66 
     67     int formattingPrependOffset = 0;
     68     if (insertLast && !insertFirstInSource) {
     69         propertyStart = styleBodyLength;
     70         if (propertyStart && textToSet.length()) {
     71             long curPos = propertyStart - 1; // The last position of style declaration, since propertyStart points past one.
     72             while (curPos && isHTMLSpace<UChar>(m_styleText[curPos]))
     73                 --curPos;
     74             if (curPos) {
     75                 bool terminated = m_styleText[curPos] == ';' || (m_styleText[curPos] == '/' && m_styleText[curPos - 1] == '*');
     76                 if (!terminated) {
     77                     // Prepend a ";" to the property text if appending to a style declaration where
     78                     // the last property has no trailing ";".
     79                     textToSet.insert(";", 0);
     80                     formattingPrependOffset = 1;
     81                 }
     82             }
     83         }
     84     }
     85 
     86     const String& formatLineFeed = m_format.first;
     87     const String& formatPropertyPrefix = m_format.second;
     88     if (insertLastInSource) {
     89         long formatPropertyPrefixLength = formatPropertyPrefix.length();
     90         if (!formattingPrependOffset && (propertyStart < formatPropertyPrefixLength || m_styleText.substring(propertyStart - formatPropertyPrefixLength, formatPropertyPrefixLength) != formatPropertyPrefix)) {
     91             textToSet.insert(formatPropertyPrefix, formattingPrependOffset);
     92             if (!propertyStart || !isHTMLLineBreak(m_styleText[propertyStart - 1]))
     93                 textToSet.insert(formatLineFeed, formattingPrependOffset);
     94         }
     95         if (!isHTMLLineBreak(m_styleText[propertyStart]))
     96             textToSet = textToSet + formatLineFeed;
     97     } else {
     98         String fullPrefix = formatLineFeed + formatPropertyPrefix;
     99         long fullPrefixLength = fullPrefix.length();
    100         textToSet = textToSet + fullPrefix;
    101         if (insertFirstInSource && (propertyStart < fullPrefixLength || m_styleText.substring(propertyStart - fullPrefixLength, fullPrefixLength) != fullPrefix))
    102             textToSet.insert(fullPrefix, formattingPrependOffset);
    103     }
    104     m_styleText.insert(textToSet, propertyStart);
    105 }
    106 
    107 void InspectorStyleTextEditor::replaceProperty(unsigned index, const String& newText)
    108 {
    109     ASSERT_WITH_SECURITY_IMPLICATION(index < m_allProperties->size());
    110     internalReplaceProperty(m_allProperties->at(index), newText);
    111 }
    112 
    113 void InspectorStyleTextEditor::internalReplaceProperty(const InspectorStyleProperty& property, const String& newText)
    114 {
    115     const SourceRange& range = property.sourceData.range;
    116     long replaceRangeStart = range.start;
    117     long replaceRangeEnd = range.end;
    118     long newTextLength = newText.length();
    119     String finalNewText = newText;
    120 
    121     // Removing a property - remove preceding prefix.
    122     String fullPrefix = m_format.first + m_format.second;
    123     long fullPrefixLength = fullPrefix.length();
    124     if (!newTextLength && fullPrefixLength) {
    125         if (replaceRangeStart >= fullPrefixLength && m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) == fullPrefix)
    126             replaceRangeStart -= fullPrefixLength;
    127     } else if (newTextLength) {
    128         if (isHTMLLineBreak(newText[newTextLength - 1])) {
    129             // Coalesce newlines of the original and new property values (to avoid a lot of blank lines while incrementally applying property values).
    130             bool foundNewline = false;
    131             bool isLastNewline = false;
    132             int i;
    133             int textLength = m_styleText.length();
    134             for (i = replaceRangeEnd; i < textLength && isSpaceOrNewline(m_styleText[i]); ++i) {
    135                 isLastNewline = isHTMLLineBreak(m_styleText[i]);
    136                 if (isLastNewline)
    137                     foundNewline = true;
    138                 else if (foundNewline && !isLastNewline) {
    139                     replaceRangeEnd = i;
    140                     break;
    141                 }
    142             }
    143             if (foundNewline && isLastNewline)
    144                 replaceRangeEnd = i;
    145         }
    146 
    147         if (fullPrefixLength > replaceRangeStart || m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) != fullPrefix)
    148             finalNewText.insert(fullPrefix, 0);
    149     }
    150 
    151     int replacedLength = replaceRangeEnd - replaceRangeStart;
    152     m_styleText.replace(replaceRangeStart, replacedLength, finalNewText);
    153 }
    154 
    155 } // namespace WebCore
    156 
    157