Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Unified Diff: third_party/WebKit/Source/core/editing/Editor.cpp

Issue 1637323002: Editing: Tidy up HTML document structure before execCommand(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert the Node.cpp change Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/editing/Editor.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/editing/Editor.cpp
diff --git a/third_party/WebKit/Source/core/editing/Editor.cpp b/third_party/WebKit/Source/core/editing/Editor.cpp
index 6ac01cdf4159227f26899b2694297968b60a8101..d090fb18ce08bf29fb5183272edf712f343c3c32 100644
--- a/third_party/WebKit/Source/core/editing/Editor.cpp
+++ b/third_party/WebKit/Source/core/editing/Editor.cpp
@@ -38,6 +38,7 @@
#include "core/css/StylePropertySet.h"
#include "core/dom/AXObjectCache.h"
#include "core/dom/DocumentFragment.h"
+#include "core/dom/ElementTraversal.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/ParserContentPolicy.h"
#include "core/dom/Text.h"
@@ -68,12 +69,15 @@
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/frame/UseCounter.h"
+#include "core/html/HTMLBodyElement.h"
#include "core/html/HTMLCanvasElement.h"
+#include "core/html/HTMLHtmlElement.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLTextAreaElement.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/input/EventHandler.h"
+#include "core/inspector/ConsoleMessage.h"
#include "core/layout/HitTestResult.h"
#include "core/layout/LayoutImage.h"
#include "core/loader/EmptyClients.h"
@@ -1303,6 +1307,49 @@ void Editor::toggleOverwriteModeEnabled()
frame().selection().setShouldShowBlockCursor(m_overwriteModeEnabled);
}
+void Editor::tidyUpHTMLStructure(Document& document)
+{
+ // hasEditableStyle() needs up-to-date ComputedStyle.
+ document.updateLayoutTreeIfNeeded();
+ bool needsValidStructure = document.hasEditableStyle() || (document.documentElement() && document.documentElement()->hasEditableStyle());
+ if (!needsValidStructure)
+ return;
+ RefPtrWillBeRawPtr<Element> existingHead = nullptr;
+ RefPtrWillBeRawPtr<Element> existingBody = nullptr;
+ Element* currentRoot = document.documentElement();
+ if (currentRoot) {
+ if (isHTMLHtmlElement(currentRoot))
+ return;
+ if (isHTMLHeadElement(currentRoot))
+ existingHead = currentRoot;
+ else if (isHTMLBodyElement(currentRoot))
+ existingBody = currentRoot;
+ else if (isHTMLFrameSetElement(currentRoot))
+ existingBody = currentRoot;
+ }
+ // We ensure only "the root is <html>."
+ // documentElement as rootEditableElement is problematic. So we move
+ // non-<html> root elements under <body>, and the <body> works as
+ // rootEditableElement.
+ document.addConsoleMessage(ConsoleMessage::create(JSMessageSource, WarningMessageLevel, "document.execCommand() doesn't work with an invalid HTML structure. It is corrected automatically."));
+
+ RefPtrWillBeRawPtr<Element> root = HTMLHtmlElement::create(document);
+ if (existingHead)
+ root->appendChild(existingHead.release());
+ RefPtrWillBeRawPtr<Element> body = nullptr;
+ if (existingBody)
+ body = existingBody.release();
+ else
+ body = HTMLBodyElement::create(document);
+ if (document.documentElement())
+ body->appendChild(document.documentElement());
+ root->appendChild(body.release());
+ ASSERT(!document.documentElement());
+ document.appendChild(root.release());
+
+ // TODO(tkent): Should we check and move Text node children of <html>?
+}
+
DEFINE_TRACE(Editor)
{
visitor->trace(m_frame);
« no previous file with comments | « third_party/WebKit/Source/core/editing/Editor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698