OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 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 are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "core/dom/CSSSelectorWatch.h" |
| 33 |
| 34 #include "core/css/CSSParser.h" |
| 35 #include "core/css/CSSSelectorList.h" |
| 36 #include "core/css/StylePropertySet.h" |
| 37 #include "core/dom/Document.h" |
| 38 #include "core/dom/ScriptExecutionContext.h" |
| 39 #include "core/loader/FrameLoaderClient.h" |
| 40 #include "core/page/Frame.h" |
| 41 #include "core/rendering/style/StyleRareNonInheritedData.h" |
| 42 |
| 43 namespace WebCore { |
| 44 |
| 45 // The address of this string is important; its value is just documentation. |
| 46 static const char kSupplementName[] = "CSSSelectorWatch"; |
| 47 |
| 48 CSSSelectorWatch::CSSSelectorWatch(Document& document) |
| 49 : m_document(document) |
| 50 , m_callbackSelectorChangeTimer(this, &CSSSelectorWatch::callbackSelectorCha
ngeTimerFired) |
| 51 , m_timerExpirations(0) |
| 52 { |
| 53 } |
| 54 |
| 55 CSSSelectorWatch& CSSSelectorWatch::from(Document& document) |
| 56 { |
| 57 CSSSelectorWatch* watch = static_cast<CSSSelectorWatch*>(Supplement<ScriptEx
ecutionContext>::from(&document, kSupplementName)); |
| 58 if (!watch) { |
| 59 watch = new CSSSelectorWatch(document); |
| 60 Supplement<ScriptExecutionContext>::provideTo(&document, kSupplementName
, adoptPtr(watch)); |
| 61 } |
| 62 return *watch; |
| 63 } |
| 64 |
| 65 void CSSSelectorWatch::callbackSelectorChangeTimerFired(Timer<CSSSelectorWatch>*
) |
| 66 { |
| 67 // Should be ensured by updateSelectorMatches(): |
| 68 ASSERT(!m_addedSelectors.isEmpty() || !m_removedSelectors.isEmpty()); |
| 69 |
| 70 if (m_timerExpirations < 1) { |
| 71 m_timerExpirations++; |
| 72 m_callbackSelectorChangeTimer.startOneShot(0); |
| 73 return; |
| 74 } |
| 75 if (m_document.frame()) { |
| 76 Vector<String> addedSelectors; |
| 77 Vector<String> removedSelectors; |
| 78 copyToVector(m_addedSelectors, addedSelectors); |
| 79 copyToVector(m_removedSelectors, removedSelectors); |
| 80 m_document.frame()->loader()->client()->selectorMatchChanged(addedSelect
ors, removedSelectors); |
| 81 } |
| 82 m_addedSelectors.clear(); |
| 83 m_removedSelectors.clear(); |
| 84 m_timerExpirations = 0; |
| 85 } |
| 86 |
| 87 void CSSSelectorWatch::updateSelectorMatches(const Vector<String>& removedSelect
ors, const Vector<String>& addedSelectors) |
| 88 { |
| 89 bool shouldUpdateTimer = false; |
| 90 |
| 91 for (unsigned i = 0; i < removedSelectors.size(); ++i) { |
| 92 const String& selector = removedSelectors[i]; |
| 93 HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(
selector); |
| 94 if (count == m_matchingCallbackSelectors.end()) |
| 95 continue; |
| 96 --count->value; |
| 97 if (!count->value) { |
| 98 shouldUpdateTimer = true; |
| 99 |
| 100 m_matchingCallbackSelectors.remove(count); |
| 101 if (m_addedSelectors.contains(selector)) |
| 102 m_addedSelectors.remove(selector); |
| 103 else |
| 104 m_removedSelectors.add(selector); |
| 105 } |
| 106 } |
| 107 |
| 108 for (unsigned i = 0; i < addedSelectors.size(); ++i) { |
| 109 const String& selector = addedSelectors[i]; |
| 110 HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(
selector); |
| 111 if (count != m_matchingCallbackSelectors.end()) { |
| 112 ++count->value; |
| 113 continue; |
| 114 } |
| 115 shouldUpdateTimer = true; |
| 116 |
| 117 m_matchingCallbackSelectors.set(selector, 1); |
| 118 if (m_removedSelectors.contains(selector)) |
| 119 m_removedSelectors.remove(selector); |
| 120 else |
| 121 m_addedSelectors.add(selector); |
| 122 } |
| 123 |
| 124 if (!shouldUpdateTimer) |
| 125 return; |
| 126 |
| 127 if (m_removedSelectors.isEmpty() && m_addedSelectors.isEmpty()) { |
| 128 if (m_callbackSelectorChangeTimer.isActive()) { |
| 129 m_timerExpirations = 0; |
| 130 m_callbackSelectorChangeTimer.stop(); |
| 131 } |
| 132 } else { |
| 133 m_timerExpirations = 0; |
| 134 if (!m_callbackSelectorChangeTimer.isActive()) |
| 135 m_callbackSelectorChangeTimer.startOneShot(0); |
| 136 } |
| 137 } |
| 138 |
| 139 static bool allCompound(const CSSSelectorList& selectorList) |
| 140 { |
| 141 for (const CSSSelector* selector = selectorList.first(); selector; selector
= selectorList.next(selector)) { |
| 142 if (!selector->isCompound()) |
| 143 return false; |
| 144 } |
| 145 return true; |
| 146 } |
| 147 |
| 148 void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors) |
| 149 { |
| 150 m_watchedCallbackSelectors.clear(); |
| 151 CSSParserContext context(UASheetMode); |
| 152 CSSParser parser(context); |
| 153 |
| 154 const CSSProperty callbackProperty(CSSPropertyInternalCallback, CSSPrimitive
Value::createIdentifier(CSSValueInternalPresence)); |
| 155 const RefPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertyS
et::create(&callbackProperty, 1, UASheetMode); |
| 156 |
| 157 CSSSelectorList selectorList; |
| 158 for (unsigned i = 0; i < selectors.size(); ++i) { |
| 159 parser.parseSelector(selectors[i], selectorList); |
| 160 if (!selectorList.isValid()) |
| 161 continue; |
| 162 |
| 163 // Only accept Compound Selectors, since they're cheaper to match. |
| 164 if (!allCompound(selectorList)) |
| 165 continue; |
| 166 |
| 167 RefPtr<StyleRule> rule = StyleRule::create(); |
| 168 rule->wrapperAdoptSelectorList(selectorList); |
| 169 rule->setProperties(callbackPropertySet); |
| 170 m_watchedCallbackSelectors.append(rule.release()); |
| 171 } |
| 172 m_document.changedSelectorWatch(); |
| 173 } |
| 174 |
| 175 } // namespace WebCore |
OLD | NEW |