Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: Source/core/inspector/InspectorStyleSheet.cpp

Issue 1203133002: DevTools: never iterate over css source data and cssom rules in the same loop. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/inspector/InspectorStyleSheet.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 900
901 RefPtrWillBeRawPtr<CSSMediaRule> mediaRule = InspectorCSSAgent::asCSSMediaR ule(rule.get()); 901 RefPtrWillBeRawPtr<CSSMediaRule> mediaRule = InspectorCSSAgent::asCSSMediaR ule(rule.get());
902 mediaRule->media()->setMediaText(text); 902 mediaRule->media()->setMediaText(text);
903 903
904 replaceText(sourceData->ruleHeaderRange, text, newRange, oldText); 904 replaceText(sourceData->ruleHeaderRange, text, newRange, oldText);
905 onStyleSheetTextChanged(); 905 onStyleSheetTextChanged();
906 906
907 return mediaRule; 907 return mediaRule;
908 } 908 }
909 909
910 unsigned InspectorStyleSheet::ruleIndexBySourceRange(CSSMediaRule* parentMediaRu le, const SourceRange& sourceRange) 910 RefPtrWillBeRawPtr<CSSRuleSourceData> InspectorStyleSheet::ruleAfterSourceRange( const SourceRange& sourceRange)
lushnikov 2015/06/25 15:33:08 ruleAfterSourceRange is a confusing name given its
pfeldman 2015/06/25 16:10:36 Acknowledged.
911 { 911 {
912 ASSERT(m_sourceData); 912 ASSERT(m_sourceData);
913 unsigned index = 0; 913 unsigned index = 0;
914 for (size_t i = 0; i < m_flatRules.size() && i < m_sourceData->size(); ++i) { 914 for (; index < m_sourceData->size(); ++index) {
915 RefPtrWillBeRawPtr<CSSRule> rule = m_flatRules.at(i); 915 RefPtrWillBeRawPtr<CSSRuleSourceData> sd = m_sourceData->at(index);
916 if (rule->parentRule() != parentMediaRule) 916 if (sd->ruleHeaderRange.start >= sourceRange.end)
917 continue; 917 break;
918 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i);
919 if (ruleSourceData->ruleBodyRange.end < sourceRange.start)
920 ++index;
921 } 918 }
922 return index; 919 return index < m_sourceData->size() ? m_sourceData->at(index) : nullptr;
923 } 920 }
924 921
925 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleInStyleSheet(const SourceRange & sourceRange, const String& ruleText, ExceptionState& exceptionState) 922 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleInStyleSheet(CSSRule* insertBe fore, const String& ruleText, ExceptionState& exceptionState)
lushnikov 2015/06/25 15:33:08 isn't insertBefore a CSSStyleRule actually?
pfeldman 2015/06/25 16:10:36 This can me a media rule.
926 { 923 {
927 unsigned index = ruleIndexBySourceRange(nullptr, sourceRange); 924 unsigned index = 0;
925 for (; index < m_pageStyleSheet->length(); ++index) {
926 CSSRule* rule = m_pageStyleSheet->item(index);
927 if (rule == insertBefore)
928 break;
929 }
lushnikov 2015/06/25 15:33:07 let's ASSERT(!insertBefore || index < length())
pfeldman 2015/06/25 16:10:36 It can happen when inserting into media rule or up
930
928 m_pageStyleSheet->insertRule(ruleText, index, exceptionState); 931 m_pageStyleSheet->insertRule(ruleText, index, exceptionState);
929 CSSRule* rule = m_pageStyleSheet->item(index); 932 CSSRule* rule = m_pageStyleSheet->item(index);
930 CSSStyleRule* styleRule = InspectorCSSAgent::asCSSStyleRule(rule); 933 CSSStyleRule* styleRule = InspectorCSSAgent::asCSSStyleRule(rule);
931 if (!styleRule) { 934 if (!styleRule) {
932 m_pageStyleSheet->deleteRule(index, ASSERT_NO_EXCEPTION); 935 m_pageStyleSheet->deleteRule(index, ASSERT_NO_EXCEPTION);
933 exceptionState.throwDOMException(SyntaxError, "The rule '" + ruleText + "' could not be added in style sheet."); 936 exceptionState.throwDOMException(SyntaxError, "The rule '" + ruleText + "' could not be added in style sheet.");
934 return nullptr; 937 return nullptr;
935 } 938 }
936 return styleRule; 939 return styleRule;
937 } 940 }
938 941
939 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleInMediaRule(CSSMediaRule* medi aRule, const SourceRange& sourceRange, const String& ruleText, ExceptionState& e xceptionState) 942 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleInMediaRule(CSSMediaRule* medi aRule, CSSRule* insertBefore, const String& ruleText, ExceptionState& exceptionS tate)
lushnikov 2015/06/25 15:33:08 ditto about insertBefore type
pfeldman 2015/06/25 16:10:36 Acknowledged.
940 { 943 {
941 unsigned index = ruleIndexBySourceRange(mediaRule, sourceRange); 944 unsigned index = 0;
945 for (; index < mediaRule->length(); ++index) {
946 CSSRule* rule = mediaRule->item(index);
947 if (rule == insertBefore)
948 break;
949 }
lushnikov 2015/06/25 15:33:07 ditto
pfeldman 2015/06/25 16:10:36 Acknowledged.
950
942 mediaRule->insertRule(ruleText, index, exceptionState); 951 mediaRule->insertRule(ruleText, index, exceptionState);
943 CSSRule* rule = mediaRule->item(index); 952 CSSRule* rule = mediaRule->item(index);
944 CSSStyleRule* styleRule = InspectorCSSAgent::asCSSStyleRule(rule); 953 CSSStyleRule* styleRule = InspectorCSSAgent::asCSSStyleRule(rule);
945 if (!styleRule) { 954 if (!styleRule) {
946 mediaRule->deleteRule(index, ASSERT_NO_EXCEPTION); 955 mediaRule->deleteRule(index, ASSERT_NO_EXCEPTION);
947 exceptionState.throwDOMException(SyntaxError, "The rule '" + ruleText + "' could not be added in media rule."); 956 exceptionState.throwDOMException(SyntaxError, "The rule '" + ruleText + "' could not be added in media rule.");
948 return nullptr; 957 return nullptr;
949 } 958 }
950 return styleRule; 959 return styleRule;
951 } 960 }
952 961
953 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleBySourceRange(const SourceRang e& sourceRange, const String& ruleText, ExceptionState& exceptionState) 962 CSSStyleRule* InspectorStyleSheet::insertCSSOMRuleBySourceRange(const SourceRang e& sourceRange, const String& ruleText, ExceptionState& exceptionState)
954 { 963 {
955 ASSERT(m_sourceData); 964 ASSERT(m_sourceData);
956 965
957 int containingRuleIndex = -1; 966 RefPtrWillBeRawPtr<CSSRuleSourceData> containingRuleSourceData;
958 unsigned containingRuleLength = 0;
959 for (size_t i = 0; i < m_sourceData->size(); ++i) { 967 for (size_t i = 0; i < m_sourceData->size(); ++i) {
960 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i); 968 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i);
961 if (ruleSourceData->ruleHeaderRange.start < sourceRange.start && sourceR ange.start < ruleSourceData->ruleBodyRange.start) { 969 if (ruleSourceData->ruleHeaderRange.start < sourceRange.start && sourceR ange.start < ruleSourceData->ruleBodyRange.start) {
962 exceptionState.throwDOMException(NotFoundError, "Cannot insert rule inside rule selector."); 970 exceptionState.throwDOMException(NotFoundError, "Cannot insert rule inside rule selector.");
963 return nullptr; 971 return nullptr;
964 } 972 }
965 if (sourceRange.start < ruleSourceData->ruleBodyRange.start || ruleSourc eData->ruleBodyRange.end < sourceRange.start) 973 if (sourceRange.start < ruleSourceData->ruleBodyRange.start || ruleSourc eData->ruleBodyRange.end < sourceRange.start)
966 continue; 974 continue;
967 if (containingRuleIndex == -1 || containingRuleLength > ruleSourceData-> ruleBodyRange.length()) { 975 if (!containingRuleSourceData || containingRuleSourceData->ruleBodyRange .length() > ruleSourceData->ruleBodyRange.length())
968 containingRuleIndex = i; 976 containingRuleSourceData = ruleSourceData;
969 containingRuleLength = ruleSourceData->ruleBodyRange.length();
970 }
971 } 977 }
972 if (containingRuleIndex == -1)
973 return insertCSSOMRuleInStyleSheet(sourceRange, ruleText, exceptionState );
974 978
975 RefPtrWillBeRawPtr<CSSRule> rule = containingRuleIndex < static_cast<int>(m_ flatRules.size()) ? m_flatRules.at(containingRuleIndex) : nullptr; 979 RefPtrWillBeRawPtr<CSSRuleSourceData> insertBefore = ruleAfterSourceRange(so urceRange);
980 RefPtrWillBeRawPtr<CSSRule> insertBeforeRule = ruleForSourceData(insertBefor e.get());
981
982 if (!containingRuleSourceData)
983 return insertCSSOMRuleInStyleSheet(insertBeforeRule.get(), ruleText, exc eptionState);
984
985 RefPtrWillBeRawPtr<CSSRule> rule = ruleForSourceData(containingRuleSourceDat a.get());
976 if (!rule || rule->type() != CSSRule::MEDIA_RULE) { 986 if (!rule || rule->type() != CSSRule::MEDIA_RULE) {
977 exceptionState.throwDOMException(NotFoundError, "Cannot insert rule in n on-media rule."); 987 exceptionState.throwDOMException(NotFoundError, "Cannot insert rule in n on-media rule.");
978 return nullptr; 988 return nullptr;
979 } 989 }
980 990
981 return insertCSSOMRuleInMediaRule(toCSSMediaRule(rule.get()), sourceRange, r uleText, exceptionState); 991 return insertCSSOMRuleInMediaRule(toCSSMediaRule(rule.get()), insertBeforeRu le.get(), ruleText, exceptionState);
982 } 992 }
983 993
984 RefPtrWillBeRawPtr<CSSStyleRule> InspectorStyleSheet::addRule(const String& rule Text, const SourceRange& location, SourceRange* addedRange, ExceptionState& exce ptionState) 994 RefPtrWillBeRawPtr<CSSStyleRule> InspectorStyleSheet::addRule(const String& rule Text, const SourceRange& location, SourceRange* addedRange, ExceptionState& exce ptionState)
985 { 995 {
986 if (location.start != location.end) { 996 if (location.start != location.end) {
987 exceptionState.throwDOMException(NotFoundError, "Source range must be co llapsed."); 997 exceptionState.throwDOMException(NotFoundError, "Source range must be co llapsed.");
988 return nullptr; 998 return nullptr;
989 } 999 }
990 1000
991 if (!verifyRuleText(m_pageStyleSheet->ownerDocument(), ruleText)) { 1001 if (!verifyRuleText(m_pageStyleSheet->ownerDocument(), ruleText)) {
(...skipping 18 matching lines...) Expand all
1010 } 1020 }
1011 1021
1012 bool InspectorStyleSheet::deleteRule(const SourceRange& range, ExceptionState& e xceptionState) 1022 bool InspectorStyleSheet::deleteRule(const SourceRange& range, ExceptionState& e xceptionState)
1013 { 1023 {
1014 if (!m_sourceData) { 1024 if (!m_sourceData) {
1015 exceptionState.throwDOMException(NotFoundError, "Style is read-only."); 1025 exceptionState.throwDOMException(NotFoundError, "Style is read-only.");
1016 return false; 1026 return false;
1017 } 1027 }
1018 1028
1019 // Find index of CSSRule that entirely belongs to the range. 1029 // Find index of CSSRule that entirely belongs to the range.
1020 RefPtrWillBeRawPtr<CSSRule> rule = nullptr; 1030 RefPtrWillBeRawPtr<CSSRuleSourceData> foundData = nullptr;
1021 unsigned containingRuleLength = 0;
1022 1031
1023 for (size_t i = 0; i < m_flatRules.size() && i < m_sourceData->size(); ++i) { 1032 for (size_t i = 0; i < m_sourceData->size(); ++i) {
1024 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i); 1033 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i);
1025 unsigned ruleStart = ruleSourceData->ruleHeaderRange.start; 1034 unsigned ruleStart = ruleSourceData->ruleHeaderRange.start;
1026 unsigned ruleEnd = ruleSourceData->ruleBodyRange.end + 1; 1035 unsigned ruleEnd = ruleSourceData->ruleBodyRange.end + 1;
1027 bool startBelongs = ruleStart >= range.start && ruleStart < range.end; 1036 bool startBelongs = ruleStart >= range.start && ruleStart < range.end;
1028 bool endBelongs = ruleEnd > range.start && ruleEnd <= range.end; 1037 bool endBelongs = ruleEnd > range.start && ruleEnd <= range.end;
1029 1038
1030 if (startBelongs != endBelongs) 1039 if (startBelongs != endBelongs)
1031 break; 1040 break;
1032 if (!startBelongs) 1041 if (!startBelongs)
1033 continue; 1042 continue;
1034 if (!rule || containingRuleLength > ruleSourceData->ruleBodyRange.length ()) { 1043 if (!foundData || foundData->ruleBodyRange.length() > ruleSourceData->ru leBodyRange.length())
1035 containingRuleLength = ruleSourceData->ruleBodyRange.length(); 1044 foundData = ruleSourceData;
1036 rule = m_flatRules.at(i).get();
1037 }
1038 } 1045 }
1046 RefPtrWillBeRawPtr<CSSRule> rule = ruleForSourceData(foundData.get());
1039 if (!rule) { 1047 if (!rule) {
1040 exceptionState.throwDOMException(NotFoundError, "No style rule could be found in given range."); 1048 exceptionState.throwDOMException(NotFoundError, "No style rule could be found in given range.");
1041 return false; 1049 return false;
1042 } 1050 }
1043 CSSStyleSheet* styleSheet = rule->parentStyleSheet(); 1051 CSSStyleSheet* styleSheet = rule->parentStyleSheet();
1044 if (!styleSheet) { 1052 if (!styleSheet) {
1045 exceptionState.throwDOMException(NotFoundError, "No parent stylesheet co uld be found."); 1053 exceptionState.throwDOMException(NotFoundError, "No parent stylesheet co uld be found.");
1046 return false; 1054 return false;
1047 } 1055 }
1048 CSSRule* parentRule = rule->parentRule(); 1056 CSSRule* parentRule = rule->parentRule();
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1345 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i); 1353 RefPtrWillBeRawPtr<CSSRuleSourceData> ruleSourceData = m_sourceData->at( i);
1346 if (ruleSourceData->ruleBodyRange.start == sourceRange.start && ruleSour ceData->ruleBodyRange.end == sourceRange.end) { 1354 if (ruleSourceData->ruleBodyRange.start == sourceRange.start && ruleSour ceData->ruleBodyRange.end == sourceRange.end) {
1347 return ruleSourceData; 1355 return ruleSourceData;
1348 } 1356 }
1349 } 1357 }
1350 return nullptr; 1358 return nullptr;
1351 } 1359 }
1352 1360
1353 RefPtrWillBeRawPtr<CSSRule> InspectorStyleSheet::ruleForSourceData(CSSRuleSource Data* sourceData) 1361 RefPtrWillBeRawPtr<CSSRule> InspectorStyleSheet::ruleForSourceData(CSSRuleSource Data* sourceData)
1354 { 1362 {
1355 if (!m_sourceData) 1363 if (!m_sourceData || !sourceData)
1356 return nullptr; 1364 return nullptr;
1357 for (size_t i = 0; i < m_sourceData->size(); ++i) { 1365 for (size_t i = 0; i < m_sourceData->size(); ++i) {
1358 if (m_sourceData->at(i).get() == sourceData) 1366 if (m_sourceData->at(i).get() == sourceData)
1359 return i < m_flatRules.size() ? m_flatRules.at(i) : nullptr; 1367 return i < m_flatRules.size() ? m_flatRules.at(i) : nullptr;
1360 } 1368 }
1361 return nullptr; 1369 return nullptr;
1362 } 1370 }
1363 1371
1364 RefPtrWillBeRawPtr<CSSRuleSourceData> InspectorStyleSheet::sourceDataForRule(CSS Rule* rule) 1372 RefPtrWillBeRawPtr<CSSRuleSourceData> InspectorStyleSheet::sourceDataForRule(CSS Rule* rule)
1365 { 1373 {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1491 } 1499 }
1492 1500
1493 DEFINE_TRACE(InspectorStyleSheetForInlineStyle) 1501 DEFINE_TRACE(InspectorStyleSheetForInlineStyle)
1494 { 1502 {
1495 visitor->trace(m_element); 1503 visitor->trace(m_element);
1496 visitor->trace(m_inspectorStyle); 1504 visitor->trace(m_inspectorStyle);
1497 InspectorStyleSheetBase::trace(visitor); 1505 InspectorStyleSheetBase::trace(visitor);
1498 } 1506 }
1499 1507
1500 } // namespace blink 1508 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/inspector/InspectorStyleSheet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698