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
InspectorStyleTextEditor(Vector<InspectorStyleProperty> * allProperties,const String & styleText,const NewLineAndWhitespace & format)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
insertProperty(unsigned index,const String & propertyText,unsigned styleBodyLength)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.append(formatLineFeed);
97 } else {
98 String fullPrefix = formatLineFeed + formatPropertyPrefix;
99 long fullPrefixLength = fullPrefix.length();
100 textToSet.append(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
replaceProperty(unsigned index,const String & newText)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
removeProperty(unsigned index)113 void InspectorStyleTextEditor::removeProperty(unsigned index)
114 {
115 replaceProperty(index, "");
116 }
117
enableProperty(unsigned index)118 void InspectorStyleTextEditor::enableProperty(unsigned index)
119 {
120 InspectorStyleProperty& disabledProperty = m_allProperties->at(index);
121 ASSERT(disabledProperty.sourceData.disabled);
122 internalReplaceProperty(disabledProperty, disabledProperty.rawText.substring(2, disabledProperty.rawText.length() - 4).stripWhiteSpace());
123 }
124
disableProperty(unsigned index)125 void InspectorStyleTextEditor::disableProperty(unsigned index)
126 {
127 ASSERT(!m_allProperties->at(index).sourceData.disabled);
128
129 InspectorStyleProperty& property = m_allProperties->at(index);
130 property.setRawTextFromStyleDeclaration(m_styleText);
131 property.sourceData.disabled = true;
132
133 internalReplaceProperty(property, "/* " + property.rawText + " */");
134 }
135
internalReplaceProperty(const InspectorStyleProperty & property,const String & newText)136 void InspectorStyleTextEditor::internalReplaceProperty(const InspectorStyleProperty& property, const String& newText)
137 {
138 const SourceRange& range = property.sourceData.range;
139 long replaceRangeStart = range.start;
140 long replaceRangeEnd = range.end;
141 long newTextLength = newText.length();
142 String finalNewText = newText;
143
144 // Removing a property - remove preceding prefix.
145 String fullPrefix = m_format.first + m_format.second;
146 long fullPrefixLength = fullPrefix.length();
147 if (!newTextLength && fullPrefixLength) {
148 if (replaceRangeStart >= fullPrefixLength && m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) == fullPrefix)
149 replaceRangeStart -= fullPrefixLength;
150 } else if (newTextLength) {
151 if (isHTMLLineBreak(newText[newTextLength - 1])) {
152 // Coalesce newlines of the original and new property values (to avoid a lot of blank lines while incrementally applying property values).
153 bool foundNewline = false;
154 bool isLastNewline = false;
155 int i;
156 int textLength = m_styleText.length();
157 for (i = replaceRangeEnd; i < textLength && isSpaceOrNewline(m_styleText[i]); ++i) {
158 isLastNewline = isHTMLLineBreak(m_styleText[i]);
159 if (isLastNewline)
160 foundNewline = true;
161 else if (foundNewline && !isLastNewline) {
162 replaceRangeEnd = i;
163 break;
164 }
165 }
166 if (foundNewline && isLastNewline)
167 replaceRangeEnd = i;
168 }
169
170 if (fullPrefixLength > replaceRangeStart || m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) != fullPrefix)
171 finalNewText.insert(fullPrefix, 0);
172 }
173
174 int replacedLength = replaceRangeEnd - replaceRangeStart;
175 m_styleText.replace(replaceRangeStart, replacedLength, finalNewText);
176 }
177
178 } // namespace WebCore
179
180