| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/editing/commands/ReplaceSelectionCommand.h" | 5 #include "core/editing/commands/ReplaceSelectionCommand.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/HTMLNames.h" | 8 #include "core/HTMLNames.h" |
| 9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/DocumentFragment.h" | 10 #include "core/dom/DocumentFragment.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 namespace blink { | 23 namespace blink { |
| 24 | 24 |
| 25 class ReplaceSelectionCommandTest : public EditingTestBase {}; | 25 class ReplaceSelectionCommandTest : public EditingTestBase {}; |
| 26 | 26 |
| 27 // This is a regression test for https://crbug.com/619131 | 27 // This is a regression test for https://crbug.com/619131 |
| 28 TEST_F(ReplaceSelectionCommandTest, pastingEmptySpan) { | 28 TEST_F(ReplaceSelectionCommandTest, pastingEmptySpan) { |
| 29 document().setDesignMode("on"); | 29 document().setDesignMode("on"); |
| 30 setBodyContent("foo"); | 30 setBodyContent("foo"); |
| 31 | 31 |
| 32 LocalFrame* frame = document().frame(); | 32 LocalFrame* frame = document().frame(); |
| 33 frame->selection().setSelection( | 33 frame->selection().setSelection(SelectionInDOMTree::Builder() |
| 34 createVisibleSelection(Position(document().body(), 0))); | 34 .collapse(Position(document().body(), 0)) |
| 35 .build()); |
| 35 | 36 |
| 36 DocumentFragment* fragment = document().createDocumentFragment(); | 37 DocumentFragment* fragment = document().createDocumentFragment(); |
| 37 fragment->appendChild(document().createElement("span", ASSERT_NO_EXCEPTION)); | 38 fragment->appendChild(document().createElement("span", ASSERT_NO_EXCEPTION)); |
| 38 | 39 |
| 39 // |options| are taken from |Editor::replaceSelectionWithFragment()| with | 40 // |options| are taken from |Editor::replaceSelectionWithFragment()| with |
| 40 // |selectReplacement| and |smartReplace|. | 41 // |selectReplacement| and |smartReplace|. |
| 41 ReplaceSelectionCommand::CommandOptions options = | 42 ReplaceSelectionCommand::CommandOptions options = |
| 42 ReplaceSelectionCommand::PreventNesting | | 43 ReplaceSelectionCommand::PreventNesting | |
| 43 ReplaceSelectionCommand::SanitizeFragment | | 44 ReplaceSelectionCommand::SanitizeFragment | |
| 44 ReplaceSelectionCommand::SelectReplacement | | 45 ReplaceSelectionCommand::SelectReplacement | |
| 45 ReplaceSelectionCommand::SmartReplace; | 46 ReplaceSelectionCommand::SmartReplace; |
| 46 ReplaceSelectionCommand* command = | 47 ReplaceSelectionCommand* command = |
| 47 ReplaceSelectionCommand::create(document(), fragment, options); | 48 ReplaceSelectionCommand::create(document(), fragment, options); |
| 48 | 49 |
| 49 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; | 50 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; |
| 50 EXPECT_EQ("foo", document().body()->innerHTML()) << "no DOM tree mutation"; | 51 EXPECT_EQ("foo", document().body()->innerHTML()) << "no DOM tree mutation"; |
| 51 } | 52 } |
| 52 | 53 |
| 53 // This is a regression test for https://crbug.com/121163 | 54 // This is a regression test for https://crbug.com/121163 |
| 54 TEST_F(ReplaceSelectionCommandTest, styleTagsInPastedHeadIncludedInContent) { | 55 TEST_F(ReplaceSelectionCommandTest, styleTagsInPastedHeadIncludedInContent) { |
| 55 document().setDesignMode("on"); | 56 document().setDesignMode("on"); |
| 56 updateAllLifecyclePhases(); | 57 updateAllLifecyclePhases(); |
| 57 dummyPageHolder().frame().selection().setSelection( | 58 dummyPageHolder().frame().selection().setSelection( |
| 58 createVisibleSelection(Position(document().body(), 0))); | 59 SelectionInDOMTree::Builder() |
| 60 .collapse(Position(document().body(), 0)) |
| 61 .build()); |
| 59 | 62 |
| 60 DocumentFragment* fragment = document().createDocumentFragment(); | 63 DocumentFragment* fragment = document().createDocumentFragment(); |
| 61 fragment->parseHTML( | 64 fragment->parseHTML( |
| 62 "<head><style>foo { bar: baz; }</style></head>" | 65 "<head><style>foo { bar: baz; }</style></head>" |
| 63 "<body><p>Text</p></body>", | 66 "<body><p>Text</p></body>", |
| 64 document().documentElement(), DisallowScriptingAndPluginContent); | 67 document().documentElement(), DisallowScriptingAndPluginContent); |
| 65 | 68 |
| 66 ReplaceSelectionCommand::CommandOptions options = 0; | 69 ReplaceSelectionCommand::CommandOptions options = 0; |
| 67 ReplaceSelectionCommand* command = | 70 ReplaceSelectionCommand* command = |
| 68 ReplaceSelectionCommand::create(document(), fragment, options); | 71 ReplaceSelectionCommand::create(document(), fragment, options); |
| 69 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; | 72 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; |
| 70 | 73 |
| 71 EXPECT_EQ( | 74 EXPECT_EQ( |
| 72 "<head><style>foo { bar: baz; }</style></head>" | 75 "<head><style>foo { bar: baz; }</style></head>" |
| 73 "<body><p>Text</p></body>", | 76 "<body><p>Text</p></body>", |
| 74 document().body()->innerHTML()) | 77 document().body()->innerHTML()) |
| 75 << "the STYLE and P elements should have been pasted into the body " | 78 << "the STYLE and P elements should have been pasted into the body " |
| 76 << "of the document"; | 79 << "of the document"; |
| 77 } | 80 } |
| 78 | 81 |
| 79 } // namespace blink | 82 } // namespace blink |
| OLD | NEW |