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

Side by Side Diff: Source/core/dom/Range.cpp

Issue 115693010: Fix Range.createContextualFragment for non-Element contexts. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Minor cleanup, more testing. Created 7 years 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * (C) 1999 Lars Knoll (knoll@kde.org) 2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Gunnstein Lye (gunnstein@netcom.no) 3 * (C) 2000 Gunnstein Lye (gunnstein@netcom.no)
4 * (C) 2000 Frederik Holljen (frederik.holljen@hig.no) 4 * (C) 2000 Frederik Holljen (frederik.holljen@hig.no)
5 * (C) 2001 Peter Kelly (pmk@post.com) 5 * (C) 2001 Peter Kelly (pmk@post.com)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved. 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved.
7 * Copyright (C) 2011 Motorola Mobility. All rights reserved. 7 * Copyright (C) 2011 Motorola Mobility. All rights reserved.
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either 11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version. 12 * version 2 of the License, or (at your option) any later version.
13 * 13 *
14 * This library is distributed in the hope that it will be useful, 14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details. 17 * Library General Public License for more details.
18 * 18 *
19 * You should have received a copy of the GNU Library General Public License 19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to 20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA. 22 * Boston, MA 02110-1301, USA.
23 */ 23 */
24 24
25 #include "config.h" 25 #include "config.h"
26 #include "core/dom/Range.h" 26 #include "core/dom/Range.h"
27 27
28 #include "HTMLNames.h"
28 #include "bindings/v8/ExceptionState.h" 29 #include "bindings/v8/ExceptionState.h"
29 #include "core/dom/ClientRect.h" 30 #include "core/dom/ClientRect.h"
30 #include "core/dom/ClientRectList.h" 31 #include "core/dom/ClientRectList.h"
31 #include "core/dom/DocumentFragment.h" 32 #include "core/dom/DocumentFragment.h"
32 #include "core/dom/ExceptionCode.h" 33 #include "core/dom/ExceptionCode.h"
33 #include "core/dom/Node.h" 34 #include "core/dom/Node.h"
34 #include "core/dom/NodeTraversal.h" 35 #include "core/dom/NodeTraversal.h"
35 #include "core/dom/NodeWithIndex.h" 36 #include "core/dom/NodeWithIndex.h"
36 #include "core/dom/ProcessingInstruction.h" 37 #include "core/dom/ProcessingInstruction.h"
37 #include "core/events/ScopedEventQueue.h"
38 #include "core/dom/Text.h" 38 #include "core/dom/Text.h"
39 #include "core/editing/TextIterator.h" 39 #include "core/editing/TextIterator.h"
40 #include "core/editing/VisiblePosition.h" 40 #include "core/editing/VisiblePosition.h"
41 #include "core/editing/VisibleUnits.h" 41 #include "core/editing/VisibleUnits.h"
42 #include "core/editing/markup.h" 42 #include "core/editing/markup.h"
43 #include "core/events/ScopedEventQueue.h"
44 #include "core/html/HTMLBodyElement.h"
43 #include "core/html/HTMLElement.h" 45 #include "core/html/HTMLElement.h"
44 #include "core/rendering/RenderBoxModelObject.h" 46 #include "core/rendering/RenderBoxModelObject.h"
45 #include "core/rendering/RenderText.h" 47 #include "core/rendering/RenderText.h"
48 #include "core/svg/SVGDocument.h"
49 #include "core/svg/SVGElement.h"
50 #include "core/svg/SVGSVGElement.h"
46 #include "platform/geometry/FloatQuad.h" 51 #include "platform/geometry/FloatQuad.h"
47 #include "wtf/RefCountedLeakCounter.h" 52 #include "wtf/RefCountedLeakCounter.h"
48 #include "wtf/text/CString.h" 53 #include "wtf/text/CString.h"
49 #include "wtf/text/StringBuilder.h" 54 #include "wtf/text/StringBuilder.h"
50 #ifndef NDEBUG 55 #ifndef NDEBUG
51 #include <stdio.h> 56 #include <stdio.h>
52 #endif 57 #endif
53 58
54 namespace WebCore { 59 namespace WebCore {
55 60
(...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after
1096 1101
1097 // We need to update layout, since plainText uses line boxes in the render t ree. 1102 // We need to update layout, since plainText uses line boxes in the render t ree.
1098 // FIXME: As with innerText, we'd like this to work even if there are no ren der objects. 1103 // FIXME: As with innerText, we'd like this to work even if there are no ren der objects.
1099 m_start.container()->document().updateLayout(); 1104 m_start.container()->document().updateLayout();
1100 1105
1101 return plainText(this); 1106 return plainText(this);
1102 } 1107 }
1103 1108
1104 PassRefPtr<DocumentFragment> Range::createContextualFragment(const String& marku p, ExceptionState& exceptionState) 1109 PassRefPtr<DocumentFragment> Range::createContextualFragment(const String& marku p, ExceptionState& exceptionState)
1105 { 1110 {
1106 if (!m_start.container()) { 1111 // Algorithm: http://domparsing.spec.whatwg.org/#extensions-to-the-range-int erface
1112
1113 Node* startContainer = m_start.container();
1114 if (!startContainer) {
1107 exceptionState.throwDOMException(InvalidStateError, "The range has no co ntainer. Perhaps 'detatch()' has been invoked on this object?"); 1115 exceptionState.throwDOMException(InvalidStateError, "The range has no co ntainer. Perhaps 'detatch()' has been invoked on this object?");
1108 return 0; 1116 return 0;
1109 } 1117 }
1110 1118
1111 Node* element = m_start.container()->isElementNode() ? m_start.container() : m_start.container()->parentNode(); 1119 // Step 1.
1112 if (!element || !element->isHTMLElement()) { 1120 Node* element;
1113 exceptionState.throwDOMException(NotSupportedError, "The range's contain er must be an HTML element."); 1121 if (!m_start.offset() && (m_start.container()->isDocumentNode() || m_start.c ontainer()->isDocumentFragment())) {
1114 return 0; 1122 element = 0;
1123 } else if (startContainer->isElementNode()) {
1124 element = startContainer;
1125 } else {
1126 element = startContainer->parentNode();
1127 if (!element || !element->isElementNode()) {
1128 exceptionState.throwDOMException(NotSupportedError, "The range's con tainer must be an Element, Document, or DocumentFragment.");
1129 return 0;
1130 }
1115 } 1131 }
1132 ASSERT(!element || element->isElementNode());
1116 1133
1117 RefPtr<DocumentFragment> fragment = WebCore::createContextualFragment(markup , toHTMLElement(element), AllowScriptingContentAndDoNotMarkAlreadyStarted, excep tionState); 1134 // Step 2.
1135 RefPtr<Element> fakeBody = 0;
1136 if (!element || (element->isHTMLElement() && toHTMLElement(element)->hasLoca lName(HTMLNames::htmlTag))) {
1137 Document& document = startContainer->document();
1138 // Try to use the existing <body> element, if it is available.
1139 if (document.isHTMLDocument() || document.isXHTMLDocument())
1140 element = document.body();
1141 else if (document.isSVGDocument())
1142 element = toSVGDocument(document).rootElement();
1143
1144 if (!element) {
1145 // SVG documents always have an <svg> root element, should never nee d a fake node.
1146 ASSERT(document.isHTMLDocument() || document.isXHTMLDocument());
1147 fakeBody = HTMLBodyElement::create(document);
1148 element = fakeBody.get();
1149 }
1150 } else {
1151 if (!element->isHTMLElement() && !element->isSVGElement()) {
1152 exceptionState.throwDOMException(NotSupportedError, "The range's con tainer must be an Element, Document, or DocumentFragment.");
1153 return 0;
1154 }
1155 }
1156 ASSERT(element && (element->isHTMLElement() || element->isSVGElement()));
yosin_UTC9 2014/05/29 03:53:06 Is this ASSERT always true? We can set any node to
pwnall-personal 2014/06/02 06:57:57 The patch uses document.body() for HTML/XHTML docu
1157
1158 // Steps 3, 4, 5.
1159 RefPtr<DocumentFragment> fragment;
1160 if (element->isHTMLElement())
1161 fragment = WebCore::createContextualFragment(markup, toHTMLElement(eleme nt), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState);
1162 else
1163 fragment = WebCore::createContextualFragment(markup, toSVGElement(elemen t), AllowScriptingContentAndDoNotMarkAlreadyStarted, exceptionState);
1164
1118 if (!fragment) 1165 if (!fragment)
1119 return 0; 1166 return 0;
1120 1167
1121 return fragment.release(); 1168 return fragment.release();
1122 } 1169 }
1123 1170
1124 1171
1125 void Range::detach(ExceptionState& exceptionState) 1172 void Range::detach(ExceptionState& exceptionState)
1126 { 1173 {
1127 // Check first to see if we've already detached: 1174 // Check first to see if we've already detached:
(...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after
1915 1962
1916 void showTree(const WebCore::Range* range) 1963 void showTree(const WebCore::Range* range)
1917 { 1964 {
1918 if (range && range->boundaryPointsValid()) { 1965 if (range && range->boundaryPointsValid()) {
1919 range->startContainer()->showTreeAndMark(range->startContainer(), "S", r ange->endContainer(), "E"); 1966 range->startContainer()->showTreeAndMark(range->startContainer(), "S", r ange->endContainer(), "E");
1920 fprintf(stderr, "start offset: %d, end offset: %d\n", range->startOffset (), range->endOffset()); 1967 fprintf(stderr, "start offset: %d, end offset: %d\n", range->startOffset (), range->endOffset());
1921 } 1968 }
1922 } 1969 }
1923 1970
1924 #endif 1971 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698