#include "config.h"
#include "JSEditor.h"
#include "CSSPropertyNames.h"
#include "CreateLinkCommand.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "FormatBlockCommand.h"
#include "Frame.h"
#include "HTMLNames.h"
#include "HTMLImageElement.h"
#include "IndentOutdentCommand.h"
#include "InsertListCommand.h"
#include "ReplaceSelectionCommand.h"
#include "SelectionController.h"
#include "TypingCommand.h"
#include "UnlinkCommand.h"
#include "htmlediting.h"
#include "markup.h"
namespace WebCore {
using namespace HTMLNames;
class Document;
namespace {
bool supportsPasteCommand = false;
struct CommandImp {
bool (*execFn)(Frame *frame, bool userInterface, const String &value);
bool (*enabledFn)(Frame *frame);
Frame::TriState (*stateFn)(Frame *frame);
String (*valueFn)(Frame *frame);
};
typedef HashMap<StringImpl *, const CommandImp *, CaseInsensitiveHash> CommandMap;
CommandMap *createCommandDictionary();
const CommandImp *commandImp(const String &command)
{
static CommandMap *commandDictionary = createCommandDictionary();
return commandDictionary->get(command.impl());
}
}
bool JSEditor::execCommand(const String &command, bool userInterface, const String &value)
{
const CommandImp *cmd = commandImp(command);
if (!cmd)
return false;
Frame *frame = m_doc->frame();
if (!frame)
return false;
m_doc->updateLayoutIgnorePendingStylesheets();
return cmd->enabledFn(frame) && cmd->execFn(frame, userInterface, value);
}
bool JSEditor::queryCommandEnabled(const String &command)
{
const CommandImp *cmd = commandImp(command);
if (!cmd)
return false;
Frame *frame = m_doc->frame();
if (!frame)
return false;
m_doc->updateLayoutIgnorePendingStylesheets();
return cmd->enabledFn(frame);
}
bool JSEditor::queryCommandIndeterm(const String &command)
{
const CommandImp *cmd = commandImp(command);
if (!cmd)
return false;
Frame *frame = m_doc->frame();
if (!frame)
return false;
m_doc->updateLayoutIgnorePendingStylesheets();
return cmd->stateFn(frame) == Frame::mixedTriState;
}
bool JSEditor::queryCommandState(const String &command)
{
const CommandImp *cmd = commandImp(command);
if (!cmd)
return false;
Frame *frame = m_doc->frame();
if (!frame)
return false;
m_doc->updateLayoutIgnorePendingStylesheets();
return cmd->stateFn(frame) != Frame::falseTriState;
}
bool JSEditor::queryCommandSupported(const String &command)
{
if (!supportsPasteCommand && command.lower() == "paste")
return false;
return commandImp(command) != 0;
}
String JSEditor::queryCommandValue(const String &command)
{
const CommandImp *cmd = commandImp(command);
if (!cmd)
return String();
Frame *frame = m_doc->frame();
if (!frame)
return String();
m_doc->updateLayoutIgnorePendingStylesheets();
return cmd->valueFn(frame);
}
void JSEditor::setSupportsPasteCommand(bool flag)
{
supportsPasteCommand = flag;
}
namespace {
bool execStyleChange(Frame *frame, int propertyID, const String &propertyValue)
{
RefPtr<CSSMutableStyleDeclaration> style = new CSSMutableStyleDeclaration;
style->setProperty(propertyID, propertyValue);
frame->applyStyle(style.get());
return true;
}
bool execStyleChange(Frame *frame, int propertyID, const char *propertyValue)
{
return execStyleChange(frame, propertyID, String(propertyValue));
}
Frame::TriState stateStyle(Frame *frame, int propertyID, const char *desiredValue)
{
RefPtr<CSSMutableStyleDeclaration> style = new CSSMutableStyleDeclaration;
style->setProperty(propertyID, desiredValue);
return frame->selectionHasStyle(style.get());
}
bool selectionStartHasStyle(Frame *frame, int propertyID, const char *desiredValue)
{
RefPtr<CSSMutableStyleDeclaration> style = new CSSMutableStyleDeclaration;
style->setProperty(propertyID, desiredValue);
return frame->selectionStartHasStyle(style.get());
}
String valueStyle(Frame *frame, int propertyID)
{
return frame->selectionStartStylePropertyValue(propertyID);
}
bool execBackColor(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_BACKGROUND_COLOR, value);
}
bool execBold(Frame *frame, bool userInterface, const String &value)
{
bool isBold = selectionStartHasStyle(frame, CSS_PROP_FONT_WEIGHT, "bold");
return execStyleChange(frame, CSS_PROP_FONT_WEIGHT, isBold ? "normal" : "bold");
}
bool execCopy(Frame *frame, bool userInterface, const String &value)
{
frame->copyToPasteboard();
return true;
}
bool execCreateLink(Frame* frame, bool userInterface, const String& value)
{
if (userInterface)
LOG_ERROR("A dialog box for link creation is not yet implemented.\n");
if (value.isEmpty())
return false;
EditCommandPtr(new CreateLinkCommand(frame->document(), value)).apply();
return true;
}
bool execCut(Frame *frame, bool userInterface, const String &value)
{
frame->cutToPasteboard();
return true;
}
bool execDelete(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::deleteKeyPressed(frame->document(), frame->selectionGranularity() == WordGranularity);
return true;
}
bool execFindString(Frame *frame, bool userInterface, const String &value)
{
return frame->findString(value, true, false, true);
}
bool execForwardDelete(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::forwardDeleteKeyPressed(frame->document());
return true;
}
bool execFontName(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_FONT_FAMILY, value);
}
bool execFontSize(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_FONT_SIZE, value);
}
bool execFontSizeDelta(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP__WEBKIT_FONT_SIZE_DELTA, value);
}
bool execForeColor(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_COLOR, value);
}
bool execFormatBlock(Frame *frame, bool userInterface, const String &value)
{
String tagName = value.lower();
if (tagName[0] == '<' && tagName[tagName.length() - 1] == '>')
tagName = tagName.substring(1, tagName.length() - 2);
if (!validBlockTag(tagName))
return false;
EditCommandPtr(new FormatBlockCommand(frame->document(), tagName)).apply();
return true;
}
bool execInsertHorizontalRule(Frame* frame, bool userInterface, const String& value)
{
RefPtr<HTMLElement> hr = new HTMLElement(hrTag, frame->document());
hr->setId(value);
RefPtr<DocumentFragment> fragment = new DocumentFragment(frame->document());
ExceptionCode ec = 0;
fragment->appendChild(hr, ec);
if (ec)
return false;
EditCommandPtr(new ReplaceSelectionCommand(frame->document(), fragment.get(), false, false, false, false, EditActionUnspecified)).apply();
return true;
}
bool execInsertHTML(Frame* frame, bool userInterface, const String& value)
{
DeprecatedString baseURL = "";
bool selectReplacement = false;
RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), value.deprecatedString(), baseURL);
EditCommandPtr cmd(new ReplaceSelectionCommand(frame->document(), fragment.get(), selectReplacement));
cmd.apply();
return true;
}
bool execInsertImage(Frame* frame, bool userInterface, const String& value)
{
if (userInterface)
LOG_ERROR("A dialog box for image insertion is not yet implemented.\n");
RefPtr<HTMLImageElement> image = new HTMLImageElement(imgTag, frame->document());
image->setSrc(value);
RefPtr<DocumentFragment> fragment = new DocumentFragment(frame->document());
ExceptionCode ec = 0;
fragment->appendChild(image, ec);
if (ec)
return false;
EditCommandPtr(new ReplaceSelectionCommand(frame->document(), fragment.get(), false)).apply();
return true;
}
bool execIndent(Frame *frame, bool userInterface, const String &value)
{
EditCommandPtr(new IndentOutdentCommand(frame->document(), IndentOutdentCommand::Indent)).apply();
return true;
}
bool execInsertLineBreak(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::insertLineBreak(frame->document());
return true;
}
bool execInsertParagraph(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::insertParagraphSeparator(frame->document());
return true;
}
bool execInsertNewlineInQuotedContent(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::insertParagraphSeparatorInQuotedContent(frame->document());
return true;
}
bool execInsertText(Frame *frame, bool userInterface, const String &value)
{
TypingCommand::insertText(frame->document(), value);
return true;
}
bool execInsertUnorderedList(Frame *frame, bool userInterface, const String &value)
{
EditCommandPtr(new InsertListCommand(frame->document(), InsertListCommand::UnorderedListType, value)).apply();
return true;
}
bool execInsertOrderedList(Frame *frame, bool userInterface, const String &value)
{
EditCommandPtr(new InsertListCommand(frame->document(), InsertListCommand::OrderedListType, value)).apply();
return true;
}
bool execItalic(Frame *frame, bool userInterface, const String &value)
{
bool isItalic = selectionStartHasStyle(frame, CSS_PROP_FONT_STYLE, "italic");
return execStyleChange(frame, CSS_PROP_FONT_STYLE, isItalic ? "normal" : "italic");
}
bool execJustifyCenter(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_TEXT_ALIGN, "center");
}
bool execJustifyFull(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_TEXT_ALIGN, "justify");
}
bool execJustifyLeft(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_TEXT_ALIGN, "left");
}
bool execJustifyRight(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_TEXT_ALIGN, "right");
}
bool execOutdent(Frame *frame, bool userInterface, const String &value)
{
EditCommandPtr(new IndentOutdentCommand(frame->document(), IndentOutdentCommand::Outdent)).apply();
return true;
}
bool execPaste(Frame *frame, bool userInterface, const String &value)
{
frame->pasteFromPasteboard();
return true;
}
bool execPasteAndMatchStyle(Frame *frame, bool userInterface, const String &value)
{
frame->pasteAndMatchStyle();
return true;
}
bool execPrint(Frame *frame, bool userInterface, const String &value)
{
frame->print();
return true;
}
bool execRedo(Frame *frame, bool userInterface, const String &value)
{
frame->redo();
return true;
}
bool execRemoveFormat(Frame* frame, bool userInterface, const String& value)
{
RefPtr<DocumentFragment> fragment = createFragmentFromText(frame->selection().toRange().get(), frame->selection().toString());
EditCommandPtr(new ReplaceSelectionCommand(frame->document(), fragment.get(), false, false, false, false, EditActionUnspecified)).apply();
return true;
}
bool execSelectAll(Frame *frame, bool userInterface, const String &value)
{
frame->selectAll();
return true;
}
bool execStrikethrough(Frame *frame, bool userInterface, const String &value)
{
bool isStrikethrough = selectionStartHasStyle(frame, CSS_PROP__WEBKIT_TEXT_DECORATIONS_IN_EFFECT, "line-through");
return execStyleChange(frame, CSS_PROP__WEBKIT_TEXT_DECORATIONS_IN_EFFECT, isStrikethrough ? "none" : "line-through");
}
bool execSubscript(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_VERTICAL_ALIGN, "sub");
}
bool execSuperscript(Frame *frame, bool userInterface, const String &value)
{
return execStyleChange(frame, CSS_PROP_VERTICAL_ALIGN, "super");
}
bool execTranspose(Frame *frame, bool userInterface, const String &value)
{
frame->transpose();
return true;
}
bool execUnderline(Frame *frame, bool userInterface, const String &value)
{
bool isUnderlined = selectionStartHasStyle(frame, CSS_PROP__WEBKIT_TEXT_DECORATIONS_IN_EFFECT, "underline");
return execStyleChange(frame, CSS_PROP__WEBKIT_TEXT_DECORATIONS_IN_EFFECT, isUnderlined ? "none" : "underline");
}
bool execUndo(Frame *frame, bool userInterface, const String &value)
{
frame->undo();
return true;
}
bool execUnlink(Frame *frame, bool userInterface, const String &value)
{
EditCommandPtr(new UnlinkCommand(frame->document())).apply();
return true;
}
bool execUnselect(Frame *frame, bool userInterface, const String &value)
{
frame->setSelection(SelectionController());
return true;
}
bool enabled(Frame *frame)
{
return true;
}
bool enabledAnyCaret(Frame *frame)
{
return frame->selection().isCaret() && frame->selection().isContentEditable();
}
bool enabledAnySelection(Frame *frame)
{
return frame->selection().isCaretOrRange();
}
bool enabledAnyEditableSelection(Frame *frame)
{
return frame->selection().isCaretOrRange() && frame->selection().isContentEditable();
}
bool enabledAnyRichlyEditableSelection(Frame *frame)
{
return frame->selection().isCaretOrRange() && frame->selection().isContentRichlyEditable();
}
bool enabledPaste(Frame *frame)
{
return supportsPasteCommand && frame->canPaste();
}
bool enabledPasteAndMatchStyle(Frame *frame)
{
return supportsPasteCommand && frame->canPaste();
}
bool enabledAnyRangeSelection(Frame *frame)
{
return frame->selection().isRange();
}
bool enabledAnyEditableRangeSelection(Frame *frame)
{
return frame->selection().isRange() && frame->selection().isContentEditable();
}
bool enabledAnyRichlyEditableRangeSelection(Frame *frame)
{
return frame->selection().isRange() && frame->selection().isContentRichlyEditable();
}
bool enabledRedo(Frame *frame)
{
return frame->canRedo();
}
bool enabledUndo(Frame *frame)
{
return frame->canUndo();
}
Frame::TriState stateNone(Frame *frame)
{
return Frame::falseTriState;
}
Frame::TriState stateBold(Frame *frame)
{
return stateStyle(frame, CSS_PROP_FONT_WEIGHT, "bold");
}
Frame::TriState stateItalic(Frame *frame)
{
return stateStyle(frame, CSS_PROP_FONT_STYLE, "italic");
}
Frame::TriState stateList(Frame *frame)
{
return frame->selectionListState();
}
Frame::TriState stateStrikethrough(Frame *frame)
{
return stateStyle(frame, CSS_PROP_TEXT_DECORATION, "line-through");
}
Frame::TriState stateSubscript(Frame *frame)
{
return stateStyle(frame, CSS_PROP_VERTICAL_ALIGN, "sub");
}
Frame::TriState stateSuperscript(Frame *frame)
{
return stateStyle(frame, CSS_PROP_VERTICAL_ALIGN, "super");
}
Frame::TriState stateUnderline(Frame *frame)
{
return stateStyle(frame, CSS_PROP_TEXT_DECORATION, "underline");
}
String valueNull(Frame *frame)
{
return String();
}
String valueBackColor(Frame *frame)
{
return valueStyle(frame, CSS_PROP_BACKGROUND_COLOR);
}
String valueFontName(Frame *frame)
{
return valueStyle(frame, CSS_PROP_FONT_FAMILY);
}
String valueFontSize(Frame *frame)
{
return valueStyle(frame, CSS_PROP_FONT_SIZE);
}
String valueFontSizeDelta(Frame *frame)
{
return valueStyle(frame, CSS_PROP__WEBKIT_FONT_SIZE_DELTA);
}
String valueForeColor(Frame *frame)
{
return valueStyle(frame, CSS_PROP_COLOR);
}
CommandMap *createCommandDictionary()
{
struct EditorCommand { const char *name; CommandImp imp; };
static const EditorCommand commands[] = {
{ "BackColor", { execBackColor, enabledAnyRangeSelection, stateNone, valueBackColor } },
{ "Bold", { execBold, enabledAnyRichlyEditableSelection, stateBold, valueNull } },
{ "Copy", { execCopy, enabledAnyRangeSelection, stateNone, valueNull } },
{ "CreateLink", { execCreateLink, enabledAnyRichlyEditableRangeSelection, stateNone, valueNull } },
{ "Cut", { execCut, enabledAnyEditableRangeSelection, stateNone, valueNull } },
{ "Delete", { execDelete, enabledAnyEditableSelection, stateNone, valueNull } },
{ "FindString", { execFindString, enabled, stateNone, valueNull } },
{ "FontName", { execFontName, enabledAnySelection, stateNone, valueFontName } },
{ "FontSize", { execFontSize, enabledAnySelection, stateNone, valueFontSize } },
{ "FontSizeDelta", { execFontSizeDelta, enabledAnySelection, stateNone, valueFontSizeDelta } },
{ "ForeColor", { execForeColor, enabledAnySelection, stateNone, valueForeColor } },
{ "FormatBlock", { execFormatBlock, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "ForwardDelete", { execForwardDelete, enabledAnyEditableSelection, stateNone, valueNull } },
{ "Indent", { execIndent, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "InsertHorizontalRule", { execInsertHorizontalRule, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "InsertHTML", { execInsertHTML, enabledAnyEditableSelection, stateNone, valueNull } },
{ "InsertImage", { execInsertImage, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "InsertLineBreak", { execInsertLineBreak, enabledAnyEditableSelection, stateNone, valueNull } },
{ "InsertOrderedList", { execInsertOrderedList, enabledAnyRichlyEditableSelection, stateList, valueNull } },
{ "InsertParagraph", { execInsertParagraph, enabledAnyEditableSelection, stateNone, valueNull } },
{ "InsertNewlineInQuotedContent", { execInsertNewlineInQuotedContent, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "InsertText", { execInsertText, enabledAnyEditableSelection, stateNone, valueNull } },
{ "InsertUnorderedList", { execInsertUnorderedList, enabledAnyRichlyEditableSelection, stateList, valueNull } },
{ "Italic", { execItalic, enabledAnyRichlyEditableSelection, stateItalic, valueNull } },
{ "JustifyCenter", { execJustifyCenter, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "JustifyFull", { execJustifyFull, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "JustifyLeft", { execJustifyLeft, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "JustifyNone", { execJustifyLeft, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "JustifyRight", { execJustifyRight, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "Outdent", { execOutdent, enabledAnyRichlyEditableSelection, stateNone, valueNull } },
{ "Paste", { execPaste, enabledPaste, stateNone, valueNull } },
{ "PasteAndMatchStyle", { execPasteAndMatchStyle, enabledPasteAndMatchStyle, stateNone, valueNull } },
{ "Print", { execPrint, enabled, stateNone, valueNull } },
{ "Redo", { execRedo, enabledRedo, stateNone, valueNull } },
{ "RemoveFormat", { execRemoveFormat, enabledAnyEditableRangeSelection, stateNone, valueNull } },
{ "SelectAll", { execSelectAll, enabled, stateNone, valueNull } },
{ "Strikethrough", { execStrikethrough, enabledAnyRichlyEditableSelection, stateStrikethrough, valueNull } },
{ "Subscript", { execSubscript, enabledAnyRichlyEditableSelection, stateSubscript, valueNull } },
{ "Superscript", { execSuperscript, enabledAnyRichlyEditableSelection, stateSuperscript, valueNull } },
{ "Transpose", { execTranspose, enabledAnyCaret, stateNone, valueNull } },
{ "Underline", { execUnderline, enabledAnyRichlyEditableSelection, stateUnderline, valueNull } },
{ "Undo", { execUndo, enabledUndo, stateNone, valueNull } },
{ "Unlink", { execUnlink, enabledAnyRichlyEditableRangeSelection, stateNone, valueNull } },
{ "Unselect", { execUnselect, enabledAnySelection, stateNone, valueNull } }
};
CommandMap *commandMap = new CommandMap;
const int numCommands = sizeof(commands) / sizeof(commands[0]);
for (int i = 0; i < numCommands; ++i) {
StringImpl *name = new StringImpl(commands[i].name);
name->ref();
commandMap->set(name, &commands[i].imp);
}
#ifndef NDEBUG
supportsPasteCommand = true;
#endif
return commandMap;
}
}
}