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 29 matching lines...) Expand all Loading... |
40 // |options| are taken from |Editor::replaceSelectionWithFragment()| with | 40 // |options| are taken from |Editor::replaceSelectionWithFragment()| with |
41 // |selectReplacement| and |smartReplace|. | 41 // |selectReplacement| and |smartReplace|. |
42 ReplaceSelectionCommand::CommandOptions options = | 42 ReplaceSelectionCommand::CommandOptions options = |
43 ReplaceSelectionCommand::PreventNesting | | 43 ReplaceSelectionCommand::PreventNesting | |
44 ReplaceSelectionCommand::SanitizeFragment | | 44 ReplaceSelectionCommand::SanitizeFragment | |
45 ReplaceSelectionCommand::SelectReplacement | | 45 ReplaceSelectionCommand::SelectReplacement | |
46 ReplaceSelectionCommand::SmartReplace; | 46 ReplaceSelectionCommand::SmartReplace; |
47 ReplaceSelectionCommand* command = | 47 ReplaceSelectionCommand* command = |
48 ReplaceSelectionCommand::create(document(), fragment, options); | 48 ReplaceSelectionCommand::create(document(), fragment, options); |
49 | 49 |
50 EXPECT_TRUE(command->apply(EditCommandSource::kMenuOrKeyBinding)) | 50 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; |
51 << "the replace command should have succeeded"; | |
52 EXPECT_EQ("foo", document().body()->innerHTML()) << "no DOM tree mutation"; | 51 EXPECT_EQ("foo", document().body()->innerHTML()) << "no DOM tree mutation"; |
53 } | 52 } |
54 | 53 |
55 // This is a regression test for https://crbug.com/668808 | 54 // This is a regression test for https://crbug.com/668808 |
56 TEST_F(ReplaceSelectionCommandTest, pasteSpanInText) { | 55 TEST_F(ReplaceSelectionCommandTest, pasteSpanInText) { |
57 document().setDesignMode("on"); | 56 document().setDesignMode("on"); |
58 setBodyContent("<b>text</b>"); | 57 setBodyContent("<b>text</b>"); |
59 | 58 |
60 Element* bElement = document().querySelector("b"); | 59 Element* bElement = document().querySelector("b"); |
61 LocalFrame* frame = document().frame(); | 60 LocalFrame* frame = document().frame(); |
62 frame->selection().setSelection( | 61 frame->selection().setSelection( |
63 SelectionInDOMTree::Builder() | 62 SelectionInDOMTree::Builder() |
64 .collapse(Position(bElement->firstChild(), 1)) | 63 .collapse(Position(bElement->firstChild(), 1)) |
65 .build()); | 64 .build()); |
66 | 65 |
67 DocumentFragment* fragment = document().createDocumentFragment(); | 66 DocumentFragment* fragment = document().createDocumentFragment(); |
68 fragment->parseHTML("<span><div>bar</div></span>", bElement); | 67 fragment->parseHTML("<span><div>bar</div></span>", bElement); |
69 | 68 |
70 ReplaceSelectionCommand::CommandOptions options = 0; | 69 ReplaceSelectionCommand::CommandOptions options = 0; |
71 ReplaceSelectionCommand* command = | 70 ReplaceSelectionCommand* command = |
72 ReplaceSelectionCommand::create(document(), fragment, options); | 71 ReplaceSelectionCommand::create(document(), fragment, options); |
73 | 72 |
74 EXPECT_TRUE(command->apply(EditCommandSource::kMenuOrKeyBinding)) | 73 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; |
75 << "the replace command should have succeeded"; | |
76 EXPECT_EQ("<b>t</b>bar<b>ext</b>", document().body()->innerHTML()) | 74 EXPECT_EQ("<b>t</b>bar<b>ext</b>", document().body()->innerHTML()) |
77 << "'bar' should have been inserted"; | 75 << "'bar' should have been inserted"; |
78 } | 76 } |
79 | 77 |
80 // This is a regression test for https://crbug.com/121163 | 78 // This is a regression test for https://crbug.com/121163 |
81 TEST_F(ReplaceSelectionCommandTest, styleTagsInPastedHeadIncludedInContent) { | 79 TEST_F(ReplaceSelectionCommandTest, styleTagsInPastedHeadIncludedInContent) { |
82 document().setDesignMode("on"); | 80 document().setDesignMode("on"); |
83 updateAllLifecyclePhases(); | 81 updateAllLifecyclePhases(); |
84 dummyPageHolder().frame().selection().setSelection( | 82 dummyPageHolder().frame().selection().setSelection( |
85 SelectionInDOMTree::Builder() | 83 SelectionInDOMTree::Builder() |
86 .collapse(Position(document().body(), 0)) | 84 .collapse(Position(document().body(), 0)) |
87 .build()); | 85 .build()); |
88 | 86 |
89 DocumentFragment* fragment = document().createDocumentFragment(); | 87 DocumentFragment* fragment = document().createDocumentFragment(); |
90 fragment->parseHTML( | 88 fragment->parseHTML( |
91 "<head><style>foo { bar: baz; }</style></head>" | 89 "<head><style>foo { bar: baz; }</style></head>" |
92 "<body><p>Text</p></body>", | 90 "<body><p>Text</p></body>", |
93 document().documentElement(), DisallowScriptingAndPluginContent); | 91 document().documentElement(), DisallowScriptingAndPluginContent); |
94 | 92 |
95 ReplaceSelectionCommand::CommandOptions options = 0; | 93 ReplaceSelectionCommand::CommandOptions options = 0; |
96 ReplaceSelectionCommand* command = | 94 ReplaceSelectionCommand* command = |
97 ReplaceSelectionCommand::create(document(), fragment, options); | 95 ReplaceSelectionCommand::create(document(), fragment, options); |
98 EXPECT_TRUE(command->apply(EditCommandSource::kMenuOrKeyBinding)) | 96 EXPECT_TRUE(command->apply()) << "the replace command should have succeeded"; |
99 << "the replace command should have succeeded"; | |
100 | 97 |
101 EXPECT_EQ( | 98 EXPECT_EQ( |
102 "<head><style>foo { bar: baz; }</style></head>" | 99 "<head><style>foo { bar: baz; }</style></head>" |
103 "<body><p>Text</p></body>", | 100 "<body><p>Text</p></body>", |
104 document().body()->innerHTML()) | 101 document().body()->innerHTML()) |
105 << "the STYLE and P elements should have been pasted into the body " | 102 << "the STYLE and P elements should have been pasted into the body " |
106 << "of the document"; | 103 << "of the document"; |
107 } | 104 } |
108 | 105 |
109 } // namespace blink | 106 } // namespace blink |
OLD | NEW |