Index: Source/core/dom/CSSSelectorWatch.cpp |
diff --git a/Source/core/dom/CSSSelectorWatch.cpp b/Source/core/dom/CSSSelectorWatch.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..94eb34e9c8c50f7d4a563e9e8e2280a8fe0cea41 |
--- /dev/null |
+++ b/Source/core/dom/CSSSelectorWatch.cpp |
@@ -0,0 +1,145 @@ |
+/* |
+ * Copyright (C) 2013 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "config.h" |
+#include "core/dom/CSSSelectorWatch.h" |
+ |
+#include "core/css/CSSParser.h" |
+#include "core/css/CSSSelectorList.h" |
+#include "core/css/StylePropertySet.h" |
+#include "core/css/StyleRule.h" |
+#include "core/dom/Document.h" |
+#include "core/dom/ScriptExecutionContext.h" |
+#include "core/loader/FrameLoaderClient.h" |
+#include "core/page/Frame.h" |
+#include "core/rendering/style/StyleRareNonInheritedData.h" |
+ |
+namespace WebCore { |
+ |
+// The address of this string is important; its value is just documentation. |
+static const char kSupplementName[] = "CSSSelectorWatch"; |
+ |
+CSSSelectorWatch::CSSSelectorWatch(Document* document) |
+ : m_document(document) |
+ , m_callbackSelectorChangeTimer(this, &CSSSelectorWatch::callbackSelectorChangeTimerFired) |
+ , m_weakThisFactory(this) |
+{ |
+} |
+ |
+CSSSelectorWatch::~CSSSelectorWatch() |
+{ |
+} |
esprehn
2013/07/17 07:14:32
We often just leave this in the header like { }
Jeffrey Yasskin
2013/08/02 01:34:07
'k.
|
+ |
+CSSSelectorWatch* CSSSelectorWatch::from(Document* document) |
+{ |
+ CSSSelectorWatch* watch = static_cast<CSSSelectorWatch*>(Supplement<ScriptExecutionContext>::from(document, kSupplementName)); |
+ if (!watch) { |
+ watch = new CSSSelectorWatch(document); |
+ Supplement<ScriptExecutionContext>::provideTo(document, kSupplementName, adoptPtr(watch)); |
+ } |
+ return watch; |
+} |
+ |
+void CSSSelectorWatch::callbackSelectorChangeTimerFired(Timer<CSSSelectorWatch>*) |
+{ |
+ if (m_addedSelectors.isEmpty() && m_removedSelectors.isEmpty()) |
+ return; |
+ if (m_document->frame()) { |
+ Vector<String> addedSelectors, removedSelectors; |
+ copyToVector(m_addedSelectors, addedSelectors); |
+ copyToVector(m_removedSelectors, removedSelectors); |
esprehn
2013/07/17 07:14:32
Use swap() instead.
Jeffrey Yasskin
2013/08/02 01:34:07
m_removedSelectors is a HashSet, while removedSele
|
+ m_document->frame()->loader()->client()->selectorMatchChanged(addedSelectors, removedSelectors); |
+ } |
+ m_addedSelectors.clear(); |
+ m_removedSelectors.clear(); |
esprehn
2013/07/17 07:14:32
remove once you swap()
|
+} |
+ |
+void CSSSelectorWatch::addSelectorMatch(const String& selector, StyleRareNonInheritedData* data) |
+{ |
+ // Set m_selectorWatch here to avoid exposing the weakFactory outside of CSSSelectorWatch. |
+ data->m_selectorWatch = m_weakThisFactory.createWeakPtr(); |
+ |
+ HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(selector); |
+ if (count != m_matchingCallbackSelectors.end()) { |
+ ++count->value; |
+ return; |
+ } |
+ if (!m_callbackSelectorChangeTimer.isActive()) |
+ m_callbackSelectorChangeTimer.startOneShot(0); |
+ |
+ m_matchingCallbackSelectors.set(selector, 1); |
+ if (m_removedSelectors.contains(selector)) |
+ m_removedSelectors.remove(selector); |
+ else |
+ m_addedSelectors.add(selector); |
+} |
+ |
+void CSSSelectorWatch::removeSelectorMatch(const String& selector) |
+{ |
+ HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(selector); |
+ if (count == m_matchingCallbackSelectors.end()) |
+ return; |
+ --count->value; |
+ if (!count->value) { |
+ if (!m_callbackSelectorChangeTimer.isActive()) |
+ m_callbackSelectorChangeTimer.startOneShot(0); |
+ |
+ m_matchingCallbackSelectors.remove(count); |
+ if (m_addedSelectors.contains(selector)) |
+ m_addedSelectors.remove(selector); |
+ else |
+ m_removedSelectors.add(selector); |
+ } |
+} |
+ |
+void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors) |
+{ |
+ m_watchedCallbackSelectors.clear(); |
+ CSSParserContext context(UASheetMode); |
+ CSSParser parser(context); |
+ |
+ const CSSProperty callbackProperty(CSSPropertyInternalCallback, CSSPrimitiveValue::createIdentifier(CSSValueInternalPresence)); |
+ const RefPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertySet::create(&callbackProperty, 1, UASheetMode); |
+ |
+ CSSSelectorList selectorList; |
+ for (unsigned i = 0; i < selectors.size(); ++i) { |
+ RefPtr<StyleRule> rule = StyleRule::create(i); |
+ |
+ parser.parseSelector(selectors[i], selectorList); |
esprehn
2013/07/17 07:14:32
What about invalid selectors?
Jeffrey Yasskin
2013/08/02 01:34:07
Fixed this by omitting invalid selectors, and test
|
+ rule->wrapperAdoptSelectorList(selectorList); |
+ |
+ rule->setProperties(callbackPropertySet); |
+ |
+ m_watchedCallbackSelectors.append(rule.release()); |
+ } |
+ m_document->styleResolverChanged(DeferRecalcStyle); |
+} |
+ |
+} // namespace WebCore |