Index: Source/core/dom/Range.cpp |
diff --git a/Source/core/dom/Range.cpp b/Source/core/dom/Range.cpp |
index 9189f403bbb65bda280a75d7fa493fb648135f57..cb757774717a15dc42b40d99bb6d878ecfd0a4fc 100644 |
--- a/Source/core/dom/Range.cpp |
+++ b/Source/core/dom/Range.cpp |
@@ -40,6 +40,7 @@ |
#include "core/editing/VisibleUnits.h" |
#include "core/editing/markup.h" |
#include "core/events/ScopedEventQueue.h" |
+#include "core/html/HTMLBodyElement.h" |
#include "core/html/HTMLElement.h" |
#include "core/rendering/RenderBoxModelObject.h" |
#include "core/rendering/RenderText.h" |
@@ -54,7 +55,6 @@ |
namespace WebCore { |
using namespace std; |
-using namespace HTMLNames; |
DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, rangeCounter, ("Range")); |
@@ -979,13 +979,57 @@ String Range::text() const |
PassRefPtrWillBeRawPtr<DocumentFragment> Range::createContextualFragment(const String& markup, ExceptionState& exceptionState) |
{ |
- Node* element = m_start.container()->isElementNode() ? m_start.container() : m_start.container()->parentNode(); |
- if (!element || !element->isHTMLElement()) { |
- exceptionState.throwDOMException(NotSupportedError, "The range's container must be an HTML element."); |
+ // Algorithm: http://domparsing.spec.whatwg.org/#extensions-to-the-range-interface |
+ |
+ Node* startContainer = m_start.container(); |
+ if (!startContainer) { |
+ exceptionState.throwDOMException(InvalidStateError, "The range must have a container."); |
return nullptr; |
} |
- RefPtrWillBeRawPtr<DocumentFragment> fragment = WebCore::createContextualFragment(markup, toHTMLElement(element), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState); |
+ // Step 1. |
+ Node* element; |
+ if (!m_start.offset() && (m_start.container()->isDocumentNode() || m_start.container()->isDocumentFragment())) { |
+ element = nullptr; |
+ } else if (startContainer->isElementNode()) { |
+ element = startContainer; |
+ } else { |
+ element = startContainer->parentNode(); |
+ if (!element || !element->isElementNode()) { |
+ exceptionState.throwDOMException(NotSupportedError, "The range's container must be an Element, Document, or DocumentFragment."); |
+ return nullptr; |
+ } |
+ } |
+ ASSERT(!element || element->isElementNode()); |
+ |
+ // Step 2. |
+ RefPtrWillBeRawPtr<Element> fakeBody = nullptr; |
+ if (!element || isHTMLHtmlElement(element)) { |
+ Document& document = startContainer->document(); |
+ |
+ if (document.isSVGDocument()) { |
eseidel
2014/06/02 16:51:28
I would have just written this as a ternary to sav
pwnall-personal
2014/06/03 00:09:01
I was worried about that because the comment about
|
+ element = document.documentElement(); |
+ } else { |
+ // Try to use the existing <body> element, if it is available. |
+ element = document.body(); |
+ } |
+ |
+ if (!element) { |
+ // SVG documents always have an <svg> root element, should never need a fake node. |
+ ASSERT(document.isHTMLDocument() || (document.isXHTMLDocument() && !document.isSVGDocument())); |
eseidel
2014/06/02 16:51:28
Why not move this ASSERT into the document.documen
pwnall-personal
2014/06/03 00:09:01
I revised the asserts to express my assumptions th
|
+ fakeBody = HTMLBodyElement::create(document); |
+ element = fakeBody.get(); |
eseidel
2014/06/02 16:51:28
It's generally dangerous to put RefCounted objects
pwnall-personal
2014/06/03 00:09:01
Done and done. Thank you!
|
+ } |
+ } else { |
+ if (!element->isHTMLElement() && !element->isSVGElement()) { |
+ exceptionState.throwDOMException(NotSupportedError, "The range's container must be an Element, Document, or DocumentFragment."); |
+ return nullptr; |
+ } |
+ } |
+ ASSERT(element && (element->isHTMLElement() || element->isSVGElement())); |
+ |
+ // Steps 3, 4, 5. |
+ RefPtrWillBeRawPtr<DocumentFragment> fragment = WebCore::createContextualFragment(markup, toElement(element), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState); |
if (!fragment) |
return nullptr; |