Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorStyleTextEditor.cpp
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(Vector<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::removeProperty(unsigned index)
114 {
115     replaceProperty(index, "");
116 }
117
118 void InspectorStyleTextEditor::internalReplaceProperty(const InspectorStyleProperty& property, const String& newText)
119 {
120     const SourceRange& range = property.sourceData.range;
121     long replaceRangeStart = range.start;
122     long replaceRangeEnd = range.end;
123     long newTextLength = newText.length();
124     String finalNewText = newText;
125
126     // Removing a property - remove preceding prefix.
127     String fullPrefix = m_format.first + m_format.second;
128     long fullPrefixLength = fullPrefix.length();
129     if (!newTextLength && fullPrefixLength) {
130         if (replaceRangeStart >= fullPrefixLength && m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) == fullPrefix)
131             replaceRangeStart -= fullPrefixLength;
132     } else if (newTextLength) {
133         if (isHTMLLineBreak(newText[newTextLength - 1])) {
134             // Coalesce newlines of the original and new property values (to avoid a lot of blank lines while incrementally applying property values).
135             bool foundNewline = false;
136             bool isLastNewline = false;
137             int i;
138             int textLength = m_styleText.length();
139             for (i = replaceRangeEnd; i < textLength && isSpaceOrNewline(m_styleText[i]); ++i) {
140                 isLastNewline = isHTMLLineBreak(m_styleText[i]);
141                 if (isLastNewline)
142                     foundNewline = true;
143                 else if (foundNewline && !isLastNewline) {
144                     replaceRangeEnd = i;
145                     break;
146                 }
147             }
148             if (foundNewline && isLastNewline)
149                 replaceRangeEnd = i;
150         }
151
152         if (fullPrefixLength > replaceRangeStart || m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) != fullPrefix)
153             finalNewText.insert(fullPrefix, 0);
154     }
155
156     int replacedLength = replaceRangeEnd - replaceRangeStart;
157     m_styleText.replace(replaceRangeStart, replacedLength, finalNewText);
158 }
159
160 } // namespace WebCore
161