1/* 2 * Copyright (C) 2011 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. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "DictationCommandIOS.h" 28 29#include "Document.h" 30#include "DocumentMarkerController.h" 31#include "Element.h" 32#include "Position.h" 33#include "Range.h" 34#include "SmartReplace.h" 35#include "TextIterator.h" 36#include "VisibleUnits.h" 37 38namespace WebCore { 39 40DictationCommandIOS::DictationCommandIOS(Document& document, PassOwnPtr<Vector<Vector<String> > > dictationPhrases, RetainPtr<id> metadata) 41 : CompositeEditCommand(document) 42 , m_dictationPhrases(dictationPhrases) 43 , m_metadata(metadata) 44{ 45} 46 47DictationCommandIOS::~DictationCommandIOS() 48{ 49} 50 51void DictationCommandIOS::doApply() 52{ 53 VisiblePosition insertionPosition(startingSelection().visibleStart()); 54 55 unsigned resultLength = 0; 56 size_t dictationPhraseCount = m_dictationPhrases->size(); 57 for (size_t i = 0; i < dictationPhraseCount; i++) { 58 59 const String& firstInterpretation = m_dictationPhrases->at(i)[0]; 60 resultLength += firstInterpretation.length(); 61 inputText(firstInterpretation, true); 62 63 const Vector<String>& interpretations = m_dictationPhrases->at(i); 64 65 if (interpretations.size() > 1) 66 document().markers().addDictationPhraseWithAlternativesMarker(endingSelection().toNormalizedRange().get(), interpretations); 67 68 setEndingSelection(VisibleSelection(endingSelection().visibleEnd())); 69 } 70 71 VisiblePosition afterResults(endingSelection().visibleEnd()); 72 73 Element* root = afterResults.rootEditableElement(); 74 // FIXME: Add the result marker using a Position cached before results are inserted, instead of relying on TextIterators. 75 RefPtr<Range> rangeToEnd = Range::create(document(), createLegacyEditingPosition((Node *)root, 0), afterResults.deepEquivalent()); 76 int endIndex = TextIterator::rangeLength(rangeToEnd.get(), true); 77 int startIndex = endIndex - resultLength; 78 79 if (startIndex >= 0) { 80 RefPtr<Range> resultRange = TextIterator::rangeFromLocationAndLength(document().documentElement(), startIndex, endIndex, true); 81 document().markers().addDictationResultMarker(resultRange.get(), m_metadata); 82 } 83} 84 85} // namespace WebCore 86