Fix the incorrect behavior touch sound on single tap event.
[framework/web/webkit-efl.git] / Source / WebCore / editing / DictationCommand.cpp
1 /*
2  * Copyright (C) 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "DictationCommand.h"
28
29 #include "AlternativeTextController.h"
30 #include "Document.h"
31 #include "DocumentMarker.h"
32 #include "DocumentMarkerController.h"
33 #include "Frame.h"
34 #include "InsertParagraphSeparatorCommand.h"
35 #include "InsertTextCommand.h"
36 #include "Text.h"
37
38 namespace WebCore {
39
40 class DictationCommandLineOperation {
41 public:
42     DictationCommandLineOperation(DictationCommand* dictationCommand)
43     : m_dictationCommand(dictationCommand)
44     { }
45     
46     void operator()(size_t lineOffset, size_t lineLength, bool isLastLine) const
47     {
48         if (lineLength > 0)
49             m_dictationCommand->insertTextRunWithoutNewlines(lineOffset, lineLength);
50         if (!isLastLine)
51             m_dictationCommand->insertParagraphSeparator();
52     }
53 private:
54     DictationCommand* m_dictationCommand;
55 };
56
57 class DictationMarkerSupplier : public TextInsertionMarkerSupplier {
58 public:
59     static PassRefPtr<DictationMarkerSupplier> create(const Vector<DictationAlternative>& alternatives)
60     {
61         return adoptRef(new DictationMarkerSupplier(alternatives));
62     }
63
64     virtual void addMarkersToTextNode(Text* textNode, unsigned offsetOfInsertion, const String& textToBeInserted)
65     {
66         Document* document = textNode->document();
67         DocumentMarkerController* markerController =document->markers();
68         for (size_t i = 0; i < m_alternatives.size(); ++i) {
69             const DictationAlternative& alternative = m_alternatives[i];
70             markerController->addMarkerToNode(textNode, alternative.rangeStart + offsetOfInsertion, alternative.rangeLength, DocumentMarker::DictationAlternatives, DictationMarkerDetails::create(textToBeInserted.substring(alternative.rangeStart, alternative.rangeLength), alternative.dictationContext));
71             markerController->addMarkerToNode(textNode, alternative.rangeStart + offsetOfInsertion, alternative.rangeLength, DocumentMarker::SpellCheckingExemption);
72         }
73     }
74
75 protected:
76     DictationMarkerSupplier(const Vector<DictationAlternative>& alternatives)
77     : m_alternatives(alternatives)
78     {
79     }
80 private:
81     Vector<DictationAlternative> m_alternatives;
82 };
83
84 DictationCommand::DictationCommand(Document* document, const String& text, const Vector<DictationAlternative>& alternatives)
85     : TextInsertionBaseCommand(document)
86     , m_textToInsert(text)
87     , m_alternatives(alternatives)
88 {
89 }
90
91 void DictationCommand::insertText(Document* document, const String& text, const Vector<DictationAlternative>& alternatives, const VisibleSelection& selectionForInsertion)
92 {
93     RefPtr<Frame> frame = document->frame();
94     ASSERT(frame);
95
96     VisibleSelection currentSelection = frame->selection()->selection();
97
98     String newText = dispatchBeforeTextInsertedEvent(text, selectionForInsertion, false);
99
100     RefPtr<DictationCommand> cmd;
101     if (newText == text)
102         cmd = DictationCommand::create(document, newText, alternatives);
103     else
104         // If the text was modified before insertion, the location of dictation alternatives
105         // will not be valid anymore. We will just drop the alternatives.
106         cmd = DictationCommand::create(document, newText, Vector<DictationAlternative>());
107     applyTextInsertionCommand(frame.get(), cmd, selectionForInsertion, currentSelection);
108 }
109
110 void DictationCommand::doApply()
111 {
112     DictationCommandLineOperation operation(this);
113     forEachLineInString(m_textToInsert, operation);
114 }
115
116 void DictationCommand::insertTextRunWithoutNewlines(size_t lineStart, size_t lineLength)
117 {
118     Vector<DictationAlternative> alternativesInLine;
119     collectDictationAlternativesInRange(lineStart, lineLength, alternativesInLine);
120     RefPtr<InsertTextCommand> command = InsertTextCommand::createWithMarkerSupplier(document(), m_textToInsert.substring(lineStart, lineLength), DictationMarkerSupplier::create(alternativesInLine));
121     applyCommandToComposite(command, endingSelection());
122 }
123
124 void DictationCommand::insertParagraphSeparator()
125 {
126     if (!canAppendNewLineFeedToSelection(endingSelection()))
127         return;
128
129     applyCommandToComposite(InsertParagraphSeparatorCommand::create(document()));
130 }
131
132 void DictationCommand::collectDictationAlternativesInRange(size_t rangeStart, size_t rangeLength, Vector<DictationAlternative>& alternatives)
133 {
134     for (size_t i = 0; i < m_alternatives.size(); ++i) {
135         const DictationAlternative& alternative = m_alternatives[i];
136         if (alternative.rangeStart >= rangeStart && (alternative.rangeStart + alternative.rangeLength) <= rangeStart + rangeLength)
137             alternatives.append(DictationAlternative(alternative.rangeStart - rangeStart, alternative.rangeLength, alternative.dictationContext));
138     }
139
140 }
141
142 }