InspectorStyleTextEditor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include "config.h"
  25. #if ENABLE(INSPECTOR)
  26. #include "InspectorStyleTextEditor.h"
  27. #include "CSSPropertySourceData.h"
  28. #include "HTMLParserIdioms.h"
  29. #include "InspectorStyleSheet.h"
  30. namespace WebCore {
  31. InspectorStyleTextEditor::InspectorStyleTextEditor(Vector<InspectorStyleProperty>* allProperties, Vector<InspectorStyleProperty>* disabledProperties, const String& styleText, const NewLineAndWhitespace& format)
  32. : m_allProperties(allProperties)
  33. , m_disabledProperties(disabledProperties)
  34. , m_styleText(styleText)
  35. , m_format(format)
  36. {
  37. }
  38. void InspectorStyleTextEditor::insertProperty(unsigned index, const String& propertyText, unsigned styleBodyLength)
  39. {
  40. long propertyStart = 0;
  41. bool insertLast = true;
  42. if (index < m_allProperties->size()) {
  43. const InspectorStyleProperty& property = m_allProperties->at(index);
  44. if (property.hasSource) {
  45. propertyStart = property.sourceData.range.start;
  46. // If inserting before a disabled property, it should be shifted, too.
  47. insertLast = false;
  48. }
  49. }
  50. bool insertFirstInSource = true;
  51. for (unsigned i = 0, size = m_allProperties->size(); i < index && i < size; ++i) {
  52. const InspectorStyleProperty& property = m_allProperties->at(i);
  53. if (property.hasSource && !property.disabled) {
  54. insertFirstInSource = false;
  55. break;
  56. }
  57. }
  58. bool insertLastInSource = true;
  59. for (unsigned i = index, size = m_allProperties->size(); i < size; ++i) {
  60. const InspectorStyleProperty& property = m_allProperties->at(i);
  61. if (property.hasSource && !property.disabled) {
  62. insertLastInSource = false;
  63. break;
  64. }
  65. }
  66. String textToSet = propertyText;
  67. int formattingPrependOffset = 0;
  68. if (insertLast && !insertFirstInSource) {
  69. propertyStart = styleBodyLength;
  70. if (propertyStart && textToSet.length()) {
  71. const UChar* characters = m_styleText.characters();
  72. long curPos = propertyStart - 1; // The last position of style declaration, since propertyStart points past one.
  73. while (curPos && isHTMLSpace(characters[curPos]))
  74. --curPos;
  75. if (curPos && characters[curPos] != ';') {
  76. // Prepend a ";" to the property text if appending to a style declaration where
  77. // the last property has no trailing ";".
  78. textToSet.insert(";", 0);
  79. formattingPrependOffset = 1;
  80. }
  81. }
  82. }
  83. const String& formatLineFeed = m_format.first;
  84. const String& formatPropertyPrefix = m_format.second;
  85. if (insertLastInSource) {
  86. long formatPropertyPrefixLength = formatPropertyPrefix.length();
  87. if (!formattingPrependOffset && (propertyStart < formatPropertyPrefixLength || m_styleText.substring(propertyStart - formatPropertyPrefixLength, formatPropertyPrefixLength) != formatPropertyPrefix)) {
  88. textToSet.insert(formatPropertyPrefix, formattingPrependOffset);
  89. if (!propertyStart || !isHTMLLineBreak(m_styleText[propertyStart - 1]))
  90. textToSet.insert(formatLineFeed, formattingPrependOffset);
  91. }
  92. if (!isHTMLLineBreak(m_styleText[propertyStart]))
  93. textToSet.append(formatLineFeed);
  94. } else {
  95. String fullPrefix = formatLineFeed + formatPropertyPrefix;
  96. long fullPrefixLength = fullPrefix.length();
  97. textToSet.append(fullPrefix);
  98. if (insertFirstInSource && (propertyStart < fullPrefixLength || m_styleText.substring(propertyStart - fullPrefixLength, fullPrefixLength) != fullPrefix))
  99. textToSet.insert(fullPrefix, formattingPrependOffset);
  100. }
  101. m_styleText.insert(textToSet, propertyStart);
  102. // Recompute disabled property ranges after an inserted property.
  103. long propertyLengthDelta = textToSet.length();
  104. shiftDisabledProperties(disabledIndexByOrdinal(index, true), propertyLengthDelta);
  105. }
  106. void InspectorStyleTextEditor::replaceProperty(unsigned index, const String& newText)
  107. {
  108. ASSERT_WITH_SECURITY_IMPLICATION(index < m_allProperties->size());
  109. const InspectorStyleProperty& property = m_allProperties->at(index);
  110. long propertyStart = property.sourceData.range.start;
  111. long propertyEnd = property.sourceData.range.end;
  112. long oldLength = propertyEnd - propertyStart;
  113. long newLength = newText.length();
  114. long propertyLengthDelta = newLength - oldLength;
  115. if (!property.disabled) {
  116. SourceRange overwrittenRange;
  117. unsigned insertedLength;
  118. internalReplaceProperty(property, newText, &overwrittenRange, &insertedLength);
  119. propertyLengthDelta = static_cast<long>(insertedLength) - static_cast<long>(overwrittenRange.length());
  120. // Recompute subsequent disabled property ranges if acting on a non-disabled property.
  121. shiftDisabledProperties(disabledIndexByOrdinal(index, true), propertyLengthDelta);
  122. } else {
  123. long textLength = newText.length();
  124. unsigned disabledIndex = disabledIndexByOrdinal(index, false);
  125. if (!textLength) {
  126. // Delete disabled property.
  127. m_disabledProperties->remove(disabledIndex);
  128. } else {
  129. // Patch disabled property text.
  130. m_disabledProperties->at(disabledIndex).rawText = newText;
  131. }
  132. }
  133. }
  134. void InspectorStyleTextEditor::removeProperty(unsigned index)
  135. {
  136. replaceProperty(index, "");
  137. }
  138. void InspectorStyleTextEditor::enableProperty(unsigned index)
  139. {
  140. ASSERT(m_allProperties->at(index).disabled);
  141. unsigned disabledIndex = disabledIndexByOrdinal(index, false);
  142. ASSERT(disabledIndex != UINT_MAX);
  143. InspectorStyleProperty disabledProperty = m_disabledProperties->at(disabledIndex);
  144. m_disabledProperties->remove(disabledIndex);
  145. SourceRange removedRange;
  146. unsigned insertedLength;
  147. internalReplaceProperty(disabledProperty, disabledProperty.rawText, &removedRange, &insertedLength);
  148. shiftDisabledProperties(disabledIndex, static_cast<long>(insertedLength) - static_cast<long>(removedRange.length()));
  149. }
  150. void InspectorStyleTextEditor::disableProperty(unsigned index)
  151. {
  152. ASSERT(!m_allProperties->at(index).disabled);
  153. const InspectorStyleProperty& property = m_allProperties->at(index);
  154. InspectorStyleProperty disabledProperty(property);
  155. disabledProperty.setRawTextFromStyleDeclaration(m_styleText);
  156. disabledProperty.disabled = true;
  157. SourceRange removedRange;
  158. unsigned insertedLength;
  159. internalReplaceProperty(property, "", &removedRange, &insertedLength);
  160. // If some preceding formatting has been removed, move the property to the start of the removed range.
  161. if (property.sourceData.range.start > removedRange.start)
  162. disabledProperty.sourceData.range.start = removedRange.start;
  163. disabledProperty.sourceData.range.end = disabledProperty.sourceData.range.start;
  164. // Add disabled property at correct position.
  165. unsigned insertionIndex = disabledIndexByOrdinal(index, true);
  166. if (insertionIndex == UINT_MAX)
  167. m_disabledProperties->append(disabledProperty);
  168. else {
  169. m_disabledProperties->insert(insertionIndex, disabledProperty);
  170. long styleLengthDelta = -(static_cast<long>(removedRange.length()));
  171. shiftDisabledProperties(insertionIndex + 1, styleLengthDelta); // Property removed from text - shift these back.
  172. }
  173. }
  174. unsigned InspectorStyleTextEditor::disabledIndexByOrdinal(unsigned ordinal, bool canUseSubsequent)
  175. {
  176. unsigned disabledIndex = 0;
  177. for (unsigned i = 0, size = m_allProperties->size(); i < size; ++i) {
  178. if (m_allProperties->at(i).disabled) {
  179. if (i == ordinal || (canUseSubsequent && i > ordinal))
  180. return disabledIndex;
  181. ++disabledIndex;
  182. }
  183. }
  184. return UINT_MAX;
  185. }
  186. void InspectorStyleTextEditor::shiftDisabledProperties(unsigned fromIndex, long delta)
  187. {
  188. for (unsigned i = fromIndex, size = m_disabledProperties->size(); i < size; ++i) {
  189. SourceRange& range = m_disabledProperties->at(i).sourceData.range;
  190. range.start += delta;
  191. range.end += delta;
  192. }
  193. }
  194. void InspectorStyleTextEditor::internalReplaceProperty(const InspectorStyleProperty& property, const String& newText, SourceRange* removedRange, unsigned* insertedLength)
  195. {
  196. const SourceRange& range = property.sourceData.range;
  197. long replaceRangeStart = range.start;
  198. long replaceRangeEnd = range.end;
  199. const UChar* characters = m_styleText.characters();
  200. long newTextLength = newText.length();
  201. String finalNewText = newText;
  202. // Removing a property - remove preceding prefix.
  203. String fullPrefix = m_format.first + m_format.second;
  204. long fullPrefixLength = fullPrefix.length();
  205. if (!newTextLength && fullPrefixLength) {
  206. if (replaceRangeStart >= fullPrefixLength && m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) == fullPrefix)
  207. replaceRangeStart -= fullPrefixLength;
  208. } else if (newTextLength) {
  209. if (isHTMLLineBreak(newText.characters()[newTextLength - 1])) {
  210. // Coalesce newlines of the original and new property values (to avoid a lot of blank lines while incrementally applying property values).
  211. bool foundNewline = false;
  212. bool isLastNewline = false;
  213. int i;
  214. int textLength = m_styleText.length();
  215. for (i = replaceRangeEnd; i < textLength && isSpaceOrNewline(characters[i]); ++i) {
  216. isLastNewline = isHTMLLineBreak(characters[i]);
  217. if (isLastNewline)
  218. foundNewline = true;
  219. else if (foundNewline && !isLastNewline) {
  220. replaceRangeEnd = i;
  221. break;
  222. }
  223. }
  224. if (foundNewline && isLastNewline)
  225. replaceRangeEnd = i;
  226. }
  227. if (fullPrefixLength > replaceRangeStart || m_styleText.substring(replaceRangeStart - fullPrefixLength, fullPrefixLength) != fullPrefix)
  228. finalNewText.insert(fullPrefix, 0);
  229. }
  230. int replacedLength = replaceRangeEnd - replaceRangeStart;
  231. m_styleText.replace(replaceRangeStart, replacedLength, finalNewText);
  232. *removedRange = SourceRange(replaceRangeStart, replaceRangeEnd);
  233. *insertedLength = finalNewText.length();
  234. }
  235. } // namespace WebCore
  236. #endif // ENABLE(INSPECTOR)