LEFT | RIGHT |
1 /* | 1 /* |
2 * Copyright (C) 2016 Apple Inc. All rights reserved. | 2 * Copyright (C) 2016 Apple 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 21 matching lines...) Expand all Loading... |
32 #include "CSSPropertyParser.h" | 32 #include "CSSPropertyParser.h" |
33 #include "CSSPropertyParserHelpers.h" | 33 #include "CSSPropertyParserHelpers.h" |
34 #include "CSSTokenizer.h" | 34 #include "CSSTokenizer.h" |
35 #include "DOMWindow.h" | 35 #include "DOMWindow.h" |
36 #include "Element.h" | 36 #include "Element.h" |
37 #include "ExceptionCode.h" | 37 #include "ExceptionCode.h" |
38 #include "IntersectionObserverCallback.h" | 38 #include "IntersectionObserverCallback.h" |
39 #include "IntersectionObserverEntry.h" | 39 #include "IntersectionObserverEntry.h" |
40 #include "Logging.h" | 40 #include "Logging.h" |
41 #include "StyleResolver.h" | 41 #include "StyleResolver.h" |
42 #include <wtf/text/TextStream.h> | |
43 #include <wtf/Vector.h> | |
44 #include <algorithm> | 42 #include <algorithm> |
45 #include <functional> | 43 #include <functional> |
| 44 #include <wtf/Vector.h> |
| 45 #include <wtf/text/TextStream.h> |
46 | 46 |
47 namespace WebCore { | 47 namespace WebCore { |
48 | 48 |
49 static bool isThresholdWithinRange(double threshold) { | 49 static bool isThresholdWithinRange(double threshold) |
50 return threshold >= 0.0 && threshold <= 1.0; | 50 { |
| 51 return threshold >= 0 && threshold <= 1; |
51 } | 52 } |
52 | 53 |
53 static ExceptionOr<LengthBox> parseRootMargin(String& rootMargin) | 54 static ExceptionOr<LengthBox> parseRootMargin(String& rootMargin) |
54 { | 55 { |
55 CSSTokenizer tokenizer(rootMargin); | 56 CSSTokenizer tokenizer(rootMargin); |
56 auto tokenRange = tokenizer.tokenRange(); | 57 auto tokenRange = tokenizer.tokenRange(); |
57 Vector<Length, 4> margins; | 58 Vector<Length, 4> margins; |
58 while (!tokenRange.atEnd()) { | 59 while (!tokenRange.atEnd()) { |
59 if (margins.size() == 4) | 60 if (margins.size() == 4) |
60 return Exception { SyntaxError, "Failed to construct 'IntersectionOb
server': Extra text found at the end of rootMargin." }; | 61 return Exception { SyntaxError, "Failed to construct 'IntersectionOb
server': Extra text found at the end of rootMargin." }; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 if (rootMarginOrException.hasException()) | 114 if (rootMarginOrException.hasException()) |
114 return rootMarginOrException.releaseException(); | 115 return rootMarginOrException.releaseException(); |
115 | 116 |
116 bool foundOutOfRangeThreshold = false; | 117 bool foundOutOfRangeThreshold = false; |
117 if (WTF::holds_alternative<double>(init.threshold)) | 118 if (WTF::holds_alternative<double>(init.threshold)) |
118 foundOutOfRangeThreshold = !isThresholdWithinRange(WTF::get<double>(init
.threshold)); | 119 foundOutOfRangeThreshold = !isThresholdWithinRange(WTF::get<double>(init
.threshold)); |
119 else { | 120 else { |
120 for (auto& threshold : WTF::get<Vector<double>>(init.threshold)) | 121 for (auto& threshold : WTF::get<Vector<double>>(init.threshold)) |
121 foundOutOfRangeThreshold |= !isThresholdWithinRange(threshold); | 122 foundOutOfRangeThreshold |= !isThresholdWithinRange(threshold); |
122 } | 123 } |
123 if (foundOutOfRangeThreshold) { | 124 if (foundOutOfRangeThreshold) |
124 return Exception { RangeError, "Failed to construct 'IntersectionObserve
r': all thresholds must lie in the range [0.0, 1.0]." }; | 125 return Exception { RangeError, "Failed to construct 'IntersectionObserve
r': all thresholds must lie in the range [0.0, 1.0]." }; |
125 } | |
126 | 126 |
127 return adoptRef(*new IntersectionObserver(document, WTFMove(callback), WTFMo
ve(init), rootMarginOrException.releaseReturnValue())); | 127 return adoptRef(*new IntersectionObserver(document, WTFMove(callback), WTFMo
ve(init), rootMarginOrException.releaseReturnValue())); |
128 } | 128 } |
129 | 129 |
130 // FIXME: need to stop observing if the root is deleted. | 130 // FIXME: need to stop observing if the root is deleted. |
131 IntersectionObserver::IntersectionObserver(Document& document, Ref<IntersectionO
bserverCallback>&& callback, Init&& init, LengthBox&& marginBox) | 131 IntersectionObserver::IntersectionObserver(Document& document, Ref<IntersectionO
bserverCallback>&& callback, Init&& init, LengthBox&& marginBox) |
132 : m_document(document) | 132 : m_document(document) |
133 , m_root(init.root) | 133 , m_root(init.root) |
134 , m_rootMargin(WTFMove(init.rootMargin)) | 134 , m_rootMargin(WTFMove(init.rootMargin)) |
135 , m_callback(WTFMove(callback)) | 135 , m_callback(WTFMove(callback)) |
136 , m_marginBox(marginBox) | 136 , m_marginBox(marginBox) |
137 { | 137 { |
138 if (WTF::holds_alternative<double>(init.threshold)) | 138 if (WTF::holds_alternative<double>(init.threshold)) |
139 m_thresholds.append(WTF::get<double>(init.threshold)); | 139 m_thresholds.append(WTF::get<double>(init.threshold)); |
140 else | 140 else |
141 m_thresholds = WTF::get<Vector<double>>(WTFMove(init.threshold)); | 141 m_thresholds = WTF::get<Vector<double>>(WTFMove(init.threshold)); |
142 std::sort(m_thresholds.begin(), m_thresholds.end(), std::less<double>()); | 142 std::sort(m_thresholds.begin(), m_thresholds.end(), std::less<double>()); |
143 } | 143 } |
144 | 144 |
145 IntersectionObserver::~IntersectionObserver() | 145 IntersectionObserver::~IntersectionObserver() |
146 { | 146 { |
147 disconnect(); | 147 disconnect(); |
148 } | 148 } |
149 | 149 |
150 ExceptionOr<void> IntersectionObserver::observe(Element& element) | 150 ExceptionOr<void> IntersectionObserver::observe(Element& element) |
151 { | 151 { |
152 if (&element.document() != &m_document) { | 152 if (m_root && &element.document() != &m_document) { |
| 153 // FIXME: Should we be raising an exception or just silently doing nothi
ng (which is what the spec wants and what other browsers do). |
153 m_document.domWindow()->printErrorMessage("IntersectionObserver.observe(
) on an element from another document is not allowed."); | 154 m_document.domWindow()->printErrorMessage("IntersectionObserver.observe(
) on an element from another document is not allowed."); |
154 return Exception { WrongDocumentError }; | 155 return Exception { WrongDocumentError }; |
155 } | 156 } |
156 | 157 |
157 element.startObservingIntersections(*this); | 158 element.startObservingIntersections(*this); |
158 return { }; | 159 return { }; |
159 } | 160 } |
160 | 161 |
161 void IntersectionObserver::unobserve(Element& element) | 162 void IntersectionObserver::unobserve(Element& element) |
162 { | 163 { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return; | 207 return; |
207 | 208 |
208 LOG(IntersectionObserver, "IntersectionObserver %p callback(): %lu records",
this, m_queuedEntries.size()); | 209 LOG(IntersectionObserver, "IntersectionObserver %p callback(): %lu records",
this, m_queuedEntries.size()); |
209 | 210 |
210 m_callback->handleEvent(takeRecords(), *this); | 211 m_callback->handleEvent(takeRecords(), *this); |
211 } | 212 } |
212 | 213 |
213 } // namespace WebCore | 214 } // namespace WebCore |
214 | 215 |
215 #endif // ENABLE(INTERSECTION_OBSERVER) | 216 #endif // ENABLE(INTERSECTION_OBSERVER) |
LEFT | RIGHT |