Chromium Code Reviews| Index: Source/core/dom/Range.cpp |
| diff --git a/Source/core/dom/Range.cpp b/Source/core/dom/Range.cpp |
| index 75b7fd88993465bc3f2977676ec2844ad8ec8c91..e95e9bf2b9fc6f6a4a075c22a167c9e9beb60a8a 100644 |
| --- a/Source/core/dom/Range.cpp |
| +++ b/Source/core/dom/Range.cpp |
| @@ -40,9 +40,11 @@ |
| #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" |
| +#include "core/svg/SVGSVGElement.h" |
| #include "platform/geometry/FloatQuad.h" |
| #include "wtf/RefCountedLeakCounter.h" |
| #include "wtf/text/CString.h" |
| @@ -54,7 +56,6 @@ |
| namespace WebCore { |
| using namespace std; |
| -using namespace HTMLNames; |
| DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, rangeCounter, ("Range")); |
| @@ -981,13 +982,42 @@ 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* node = m_start.container(); |
| + |
| + // Step 1. |
| + RefPtrWillBeRawPtr<Element> element; |
| + if (!m_start.offset() && (node->isDocumentNode() || node->isDocumentFragment())) |
| + element = nullptr; |
| + else if (node->isElementNode()) |
| + element = toElement(node); |
| + else |
| + element = node->parentElement(); |
| + |
| + // Step 2. |
| + if (!element || isHTMLHtmlElement(element)) { |
| + Document& document = node->document(); |
| + |
| + if (document.isHTMLDocument() || document.isXHTMLDocument()) { |
| + // Optimization over spec: try to reuse the existing <body> element, if it is available. |
| + element = document.body(); |
| + if (!element) |
| + element = HTMLBodyElement::create(document); |
|
eseidel
2014/06/04 15:00:22
It's a bit odd to pass in a detached contextElemen
pwnall-personal
2014/06/05 22:59:50
FWIW, in my interpretation of the spec, the Range'
|
| + } else if (document.isSVGDocument()) { |
| + element = document.documentElement(); |
| + if (!element) |
| + element = SVGSVGElement::create(document); |
| + } |
| + } |
| + |
| + if (!element || (!element->isHTMLElement() && !element->isSVGElement())) { |
| + exceptionState.throwDOMException(NotSupportedError, "The range's container must be an HTML or SVG Element, Document, or DocumentFragment."); |
| return nullptr; |
| } |
| - RefPtrWillBeRawPtr<DocumentFragment> fragment = WebCore::createContextualFragment(markup, toHTMLElement(element), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState); |
| + // Steps 3, 4, 5. |
| + RefPtrWillBeRawPtr<DocumentFragment> fragment = WebCore::createContextualFragment(markup, element.get(), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState); |
| if (!fragment) |
| return nullptr; |