OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
| 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All |
| 7 * rights reserved. |
| 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. |
| 9 * (http://www.torchmobile.com/) |
| 10 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. |
| 11 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
| 12 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. |
| 13 * |
| 14 * This library is free software; you can redistribute it and/or |
| 15 * modify it under the terms of the GNU Library General Public |
| 16 * License as published by the Free Software Foundation; either |
| 17 * version 2 of the License, or (at your option) any later version. |
| 18 * |
| 19 * This library is distributed in the hope that it will be useful, |
| 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 * Library General Public License for more details. |
| 23 * |
| 24 * You should have received a copy of the GNU Library General Public License |
| 25 * along with this library; see the file COPYING.LIB. If not, write to |
| 26 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 27 * Boston, MA 02110-1301, USA. |
| 28 */ |
| 29 |
| 30 #include "core/dom/Document.h" |
| 31 |
| 32 #include "core/editing/Editor.h" |
| 33 #include "core/events/ScopedEventQueue.h" |
| 34 #include "core/html/TextControlElement.h" |
| 35 #include "core/inspector/ConsoleMessage.h" |
| 36 #include "platform/Histogram.h" |
| 37 #include "wtf/AutoReset.h" |
| 38 #include "wtf/StdLibExtras.h" |
| 39 |
| 40 namespace blink { |
| 41 |
| 42 namespace { |
| 43 |
| 44 Editor::Command command(Document* document, const String& commandName) { |
| 45 LocalFrame* frame = document->frame(); |
| 46 if (!frame || frame->document() != document) |
| 47 return Editor::Command(); |
| 48 |
| 49 document->updateStyleAndLayoutTree(); |
| 50 return frame->editor().createCommandFromDOM(commandName); |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 bool Document::execCommand(const String& commandName, |
| 56 bool, |
| 57 const String& value, |
| 58 ExceptionState& exceptionState) { |
| 59 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 60 exceptionState.throwDOMException( |
| 61 InvalidStateError, "execCommand is only supported on HTML documents."); |
| 62 return false; |
| 63 } |
| 64 if (focusedElement() && isTextControlElement(*focusedElement())) |
| 65 UseCounter::count(*this, UseCounter::ExecCommandOnInputOrTextarea); |
| 66 |
| 67 // We don't allow recursive |execCommand()| to protect against attack code. |
| 68 // Recursive call of |execCommand()| could be happened by moving iframe |
| 69 // with script triggered by insertion, e.g. <iframe src="javascript:..."> |
| 70 // <iframe onload="...">. This usage is valid as of the specification |
| 71 // although, it isn't common use case, rather it is used as attack code. |
| 72 if (m_isRunningExecCommand) { |
| 73 String message = |
| 74 "We don't execute document.execCommand() this time, because it is " |
| 75 "called recursively."; |
| 76 addConsoleMessage( |
| 77 ConsoleMessage::create(JSMessageSource, WarningMessageLevel, message)); |
| 78 return false; |
| 79 } |
| 80 AutoReset<bool> executeScope(&m_isRunningExecCommand, true); |
| 81 |
| 82 // Postpone DOM mutation events, which can execute scripts and change |
| 83 // DOM tree against implementation assumption. |
| 84 EventQueueScope eventQueueScope; |
| 85 Editor::tidyUpHTMLStructure(*this); |
| 86 Editor::Command editorCommand = command(this, commandName); |
| 87 |
| 88 DEFINE_STATIC_LOCAL(SparseHistogram, editorCommandHistogram, |
| 89 ("WebCore.Document.execCommand")); |
| 90 editorCommandHistogram.sample(editorCommand.idForHistogram()); |
| 91 return editorCommand.execute(value); |
| 92 } |
| 93 |
| 94 bool Document::queryCommandEnabled(const String& commandName, |
| 95 ExceptionState& exceptionState) { |
| 96 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 97 exceptionState.throwDOMException( |
| 98 InvalidStateError, |
| 99 "queryCommandEnabled is only supported on HTML documents."); |
| 100 return false; |
| 101 } |
| 102 |
| 103 return command(this, commandName).isEnabled(); |
| 104 } |
| 105 |
| 106 bool Document::queryCommandIndeterm(const String& commandName, |
| 107 ExceptionState& exceptionState) { |
| 108 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 109 exceptionState.throwDOMException( |
| 110 InvalidStateError, |
| 111 "queryCommandIndeterm is only supported on HTML documents."); |
| 112 return false; |
| 113 } |
| 114 |
| 115 return command(this, commandName).state() == MixedTriState; |
| 116 } |
| 117 |
| 118 bool Document::queryCommandState(const String& commandName, |
| 119 ExceptionState& exceptionState) { |
| 120 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 121 exceptionState.throwDOMException( |
| 122 InvalidStateError, |
| 123 "queryCommandState is only supported on HTML documents."); |
| 124 return false; |
| 125 } |
| 126 |
| 127 return command(this, commandName).state() == TrueTriState; |
| 128 } |
| 129 |
| 130 bool Document::queryCommandSupported(const String& commandName, |
| 131 ExceptionState& exceptionState) { |
| 132 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 133 exceptionState.throwDOMException( |
| 134 InvalidStateError, |
| 135 "queryCommandSupported is only supported on HTML documents."); |
| 136 return false; |
| 137 } |
| 138 |
| 139 return command(this, commandName).isSupported(); |
| 140 } |
| 141 |
| 142 String Document::queryCommandValue(const String& commandName, |
| 143 ExceptionState& exceptionState) { |
| 144 if (!isHTMLDocument() && !isXHTMLDocument()) { |
| 145 exceptionState.throwDOMException( |
| 146 InvalidStateError, |
| 147 "queryCommandValue is only supported on HTML documents."); |
| 148 return ""; |
| 149 } |
| 150 |
| 151 return command(this, commandName).value(); |
| 152 } |
| 153 |
| 154 } // namespace blink |
OLD | NEW |