Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, 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 are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/editing/markers/DocumentMarkerController.h" | 31 #include "core/editing/markers/DocumentMarkerController.h" |
| 32 | 32 |
| 33 #include <memory> | |
| 33 #include "bindings/core/v8/ExceptionState.h" | 34 #include "bindings/core/v8/ExceptionState.h" |
| 34 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
| 35 #include "core/dom/Range.h" | 36 #include "core/dom/Range.h" |
| 36 #include "core/dom/Text.h" | 37 #include "core/dom/Text.h" |
| 37 #include "core/editing/EphemeralRange.h" | 38 #include "core/editing/EphemeralRange.h" |
| 39 #include "core/editing/PlainTextRange.h" | |
|
Xiaocheng
2017/04/10 21:32:28
Could you set marker ranges directly, so that (at
| |
| 38 #include "core/editing/markers/RenderedDocumentMarker.h" | 40 #include "core/editing/markers/RenderedDocumentMarker.h" |
| 39 #include "core/html/HTMLElement.h" | 41 #include "core/html/HTMLElement.h" |
| 40 #include "core/testing/DummyPageHolder.h" | 42 #include "core/testing/DummyPageHolder.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 42 #include "wtf/PassRefPtr.h" | 44 #include "wtf/PassRefPtr.h" |
| 43 #include "wtf/RefPtr.h" | 45 #include "wtf/RefPtr.h" |
| 44 #include <memory> | |
| 45 | 46 |
| 46 namespace blink { | 47 namespace blink { |
| 47 | 48 |
| 48 class DocumentMarkerControllerTest : public ::testing::Test { | 49 class DocumentMarkerControllerTest : public ::testing::Test { |
| 49 protected: | 50 protected: |
| 50 DocumentMarkerControllerTest() | 51 DocumentMarkerControllerTest() |
| 51 : dummy_page_holder_(DummyPageHolder::Create(IntSize(800, 600))) {} | 52 : dummy_page_holder_(DummyPageHolder::Create(IntSize(800, 600))) {} |
| 52 | 53 |
| 53 Document& GetDocument() const { return dummy_page_holder_->GetDocument(); } | 54 Document& GetDocument() const { return dummy_page_holder_->GetDocument(); } |
| 54 DocumentMarkerController& MarkerController() const { | 55 DocumentMarkerController& MarkerController() const { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 // Try to make active a marker that doesn't exist. | 258 // Try to make active a marker that doesn't exist. |
| 258 EXPECT_FALSE(MarkerController().SetMarkersActive(range, true)); | 259 EXPECT_FALSE(MarkerController().SetMarkersActive(range, true)); |
| 259 | 260 |
| 260 // Add a marker and try it once more. | 261 // Add a marker and try it once more. |
| 261 MarkerController().AddTextMatchMarker(range, | 262 MarkerController().AddTextMatchMarker(range, |
| 262 DocumentMarker::MatchStatus::kInactive); | 263 DocumentMarker::MatchStatus::kInactive); |
| 263 EXPECT_EQ(1u, MarkerController().Markers().size()); | 264 EXPECT_EQ(1u, MarkerController().Markers().size()); |
| 264 EXPECT_TRUE(MarkerController().SetMarkersActive(range, true)); | 265 EXPECT_TRUE(MarkerController().SetMarkersActive(range, true)); |
| 265 } | 266 } |
| 266 | 267 |
| 268 TEST_F(DocumentMarkerControllerTest, | |
| 269 RemoveStartOfMarkerDoRemovePartiallyOverlapping) { | |
| 270 SetBodyInnerHTML("<b>abc</b>"); | |
| 271 GetDocument().UpdateStyleAndLayout(); | |
| 272 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 273 | |
| 274 // Add marker under "abc" | |
| 275 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 276 GetDocument().Markers().AddTextMatchMarker( | |
| 277 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 278 | |
| 279 // Remove markers that overlap "a" | |
| 280 marker_range = PlainTextRange(0, 1).CreateRange(*b_element); | |
| 281 GetDocument().Markers().RemoveMarkers( | |
| 282 marker_range, DocumentMarker::AllMarkers(), | |
| 283 DocumentMarkerController::kRemovePartiallyOverlappingMarker); | |
| 284 | |
| 285 EXPECT_EQ(0u, MarkerController().Markers().size()); | |
| 286 } | |
| 287 | |
| 288 TEST_F(DocumentMarkerControllerTest, | |
| 289 RemoveStartOfMarkerDontRemovePartiallyOverlapping) { | |
| 290 SetBodyInnerHTML("<b>abc</b>"); | |
| 291 GetDocument().UpdateStyleAndLayout(); | |
| 292 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 293 | |
| 294 // Add marker under "abc" | |
| 295 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 296 GetDocument().Markers().AddTextMatchMarker( | |
| 297 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 298 | |
| 299 // Remove portion of marker that overlaps "a" | |
| 300 marker_range = PlainTextRange(0, 1).CreateRange(*b_element); | |
| 301 GetDocument().Markers().RemoveMarkers( | |
| 302 marker_range, DocumentMarker::AllMarkers(), | |
| 303 DocumentMarkerController::kDoNotRemovePartiallyOverlappingMarker); | |
| 304 | |
| 305 EXPECT_EQ(1u, MarkerController().Markers().size()); | |
| 306 | |
| 307 EXPECT_EQ(1u, MarkerController().Markers()[0]->StartOffset()); | |
| 308 EXPECT_EQ(3u, MarkerController().Markers()[0]->EndOffset()); | |
| 309 } | |
| 310 | |
| 311 TEST_F(DocumentMarkerControllerTest, | |
| 312 RemoveMiddleOfMarkerDoRemovePartiallyOverlapping) { | |
| 313 SetBodyInnerHTML("<b>abc</b>"); | |
| 314 GetDocument().UpdateStyleAndLayout(); | |
| 315 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 316 | |
| 317 // Add marker under "abc" | |
| 318 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 319 GetDocument().Markers().AddTextMatchMarker( | |
| 320 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 321 | |
| 322 // Remove markers that overlap "b" | |
| 323 marker_range = PlainTextRange(1, 2).CreateRange(*b_element); | |
| 324 GetDocument().Markers().RemoveMarkers( | |
| 325 marker_range, DocumentMarker::AllMarkers(), | |
| 326 DocumentMarkerController::kRemovePartiallyOverlappingMarker); | |
| 327 | |
| 328 EXPECT_EQ(0u, MarkerController().Markers().size()); | |
| 329 } | |
| 330 | |
| 331 TEST_F(DocumentMarkerControllerTest, | |
| 332 RemoveMiddleOfMarkerDontRemovePartiallyOverlapping) { | |
| 333 SetBodyInnerHTML("<b>abc</b>"); | |
| 334 GetDocument().UpdateStyleAndLayout(); | |
| 335 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 336 | |
| 337 // Add marker under "abc" | |
| 338 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 339 GetDocument().Markers().AddTextMatchMarker( | |
| 340 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 341 | |
| 342 // Remove portion of marker that overlaps "b" | |
| 343 marker_range = PlainTextRange(1, 2).CreateRange(*b_element); | |
| 344 GetDocument().Markers().RemoveMarkers( | |
| 345 marker_range, DocumentMarker::AllMarkers(), | |
| 346 DocumentMarkerController::kDoNotRemovePartiallyOverlappingMarker); | |
| 347 | |
| 348 EXPECT_EQ(2u, MarkerController().Markers().size()); | |
| 349 | |
| 350 EXPECT_EQ(0u, MarkerController().Markers()[0]->StartOffset()); | |
| 351 EXPECT_EQ(1u, MarkerController().Markers()[0]->EndOffset()); | |
| 352 | |
| 353 EXPECT_EQ(2u, MarkerController().Markers()[1]->StartOffset()); | |
| 354 EXPECT_EQ(3u, MarkerController().Markers()[1]->EndOffset()); | |
| 355 } | |
| 356 | |
| 357 TEST_F(DocumentMarkerControllerTest, | |
| 358 RemoveEndOfMarkerDoRemovePartiallyOverlapping) { | |
| 359 SetBodyInnerHTML("<b>abc</b>"); | |
| 360 GetDocument().UpdateStyleAndLayout(); | |
| 361 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 362 | |
| 363 // Add marker under "abc" | |
| 364 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 365 GetDocument().Markers().AddTextMatchMarker( | |
| 366 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 367 | |
| 368 // Remove markers that overlap "c" | |
| 369 marker_range = PlainTextRange(2, 3).CreateRange(*b_element); | |
| 370 GetDocument().Markers().RemoveMarkers( | |
| 371 marker_range, DocumentMarker::AllMarkers(), | |
| 372 DocumentMarkerController::kRemovePartiallyOverlappingMarker); | |
| 373 | |
| 374 EXPECT_EQ(0u, MarkerController().Markers().size()); | |
| 375 } | |
| 376 | |
| 377 TEST_F(DocumentMarkerControllerTest, | |
| 378 RemoveEndOfMarkerDontRemovePartiallyOverlapping) { | |
| 379 SetBodyInnerHTML("<b>abc</b>"); | |
| 380 GetDocument().UpdateStyleAndLayout(); | |
| 381 Element* b_element = ToElement(GetDocument().body()->FirstChild()); | |
| 382 | |
| 383 // Add marker under "abc" | |
| 384 EphemeralRange marker_range = PlainTextRange(0, 3).CreateRange(*b_element); | |
| 385 GetDocument().Markers().AddTextMatchMarker( | |
| 386 marker_range, DocumentMarker::MatchStatus::kInactive); | |
| 387 | |
| 388 // Remove portion of marker that overlaps "c" | |
| 389 marker_range = PlainTextRange(2, 3).CreateRange(*b_element); | |
| 390 GetDocument().Markers().RemoveMarkers( | |
| 391 marker_range, DocumentMarker::AllMarkers(), | |
| 392 DocumentMarkerController::kDoNotRemovePartiallyOverlappingMarker); | |
| 393 | |
| 394 EXPECT_EQ(1u, MarkerController().Markers().size()); | |
| 395 | |
| 396 EXPECT_EQ(0u, MarkerController().Markers()[0]->StartOffset()); | |
| 397 EXPECT_EQ(2u, MarkerController().Markers()[0]->EndOffset()); | |
| 398 } | |
| 399 | |
| 267 } // namespace blink | 400 } // namespace blink |
| OLD | NEW |