2 * Copyright (C) 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "JSDocument.h"
23 #include "CustomElementDefinitions.h"
24 #include "ExceptionCode.h"
26 #include "FrameLoader.h"
27 #include "HTMLDocument.h"
28 #include "JSCanvasRenderingContext2D.h"
29 #include "JSDOMWindowCustom.h"
30 #include "JSHTMLDocument.h"
31 #include "JSLocation.h"
32 #include "JSNodeOrString.h"
33 #include "JSSVGDocument.h"
35 #include "NodeTraversal.h"
36 #include "SVGDocument.h"
37 #include "ScriptController.h"
38 #include "TouchList.h"
39 #include <wtf/GetPtr.h>
42 #include "JSWebGLRenderingContextBase.h"
45 #if ENABLE(TOUCH_EVENTS)
47 #include "JSTouchList.h"
54 static inline JSValue createNewDocumentWrapper(ExecState& state, JSDOMGlobalObject& globalObject, Document& document)
57 if (document.isHTMLDocument())
58 wrapper = CREATE_DOM_WRAPPER(&globalObject, HTMLDocument, &document);
59 else if (document.isSVGDocument())
60 wrapper = CREATE_DOM_WRAPPER(&globalObject, SVGDocument, &document);
62 wrapper = CREATE_DOM_WRAPPER(&globalObject, Document, &document);
64 // Make sure the document is kept around by the window object, and works right with the
65 // back/forward cache.
66 if (!document.frame()) {
68 for (Node* n = &document; n; n = NodeTraversal::next(*n))
71 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
72 // https://bugs.webkit.org/show_bug.cgi?id=142595
73 state.heap()->deprecatedReportExtraMemory(nodeCount * sizeof(Node));
79 JSValue toJS(ExecState* state, JSDOMGlobalObject* globalObject, Document* document)
84 JSObject* wrapper = getCachedWrapper(globalObject->world(), document);
88 if (DOMWindow* domWindow = document->domWindow()) {
89 globalObject = toJSDOMWindow(toJS(state, domWindow));
90 // Creating a wrapper for domWindow might have created a wrapper for document as well.
91 wrapper = getCachedWrapper(globalObject->world(), document);
96 return createNewDocumentWrapper(*state, *globalObject, *document);
99 JSValue toJSNewlyCreated(ExecState* state, JSDOMGlobalObject* globalObject, Document* document)
101 return document ? createNewDocumentWrapper(*state, *globalObject, *document) : jsNull();
104 JSValue JSDocument::prepend(ExecState& state)
106 ExceptionCode ec = 0;
107 wrapped().prepend(toNodeOrStringVector(state), ec);
108 setDOMException(&state, ec);
110 return jsUndefined();
113 JSValue JSDocument::append(ExecState& state)
115 ExceptionCode ec = 0;
116 wrapped().append(toNodeOrStringVector(state), ec);
117 setDOMException(&state, ec);
119 return jsUndefined();
122 #if ENABLE(TOUCH_EVENTS)
123 JSValue JSDocument::createTouchList(ExecState& state)
125 RefPtr<TouchList> touchList = TouchList::create();
127 for (size_t i = 0; i < state.argumentCount(); i++)
128 touchList->append(JSTouch::toWrapped(state.argument(i)));
130 return toJS(&state, globalObject(), touchList.release());
134 #if ENABLE(CUSTOM_ELEMENTS)
135 JSValue JSDocument::defineCustomElement(ExecState& state)
137 AtomicString tagName(state.argument(0).toString(&state)->toAtomicString(&state));
138 if (UNLIKELY(state.hadException()))
139 return jsUndefined();
141 JSObject* object = state.argument(1).getObject();
142 ConstructData callData;
143 if (!object || object->methodTable()->getConstructData(object, callData) == ConstructTypeNone)
144 return throwTypeError(&state, "The second argument must be a constructor");
146 Document& document = wrapped();
147 switch (CustomElementDefinitions::checkName(tagName)) {
148 case CustomElementDefinitions::NameStatus::Valid:
150 case CustomElementDefinitions::NameStatus::ConflictsWithBuiltinNames:
151 return throwSyntaxError(&state, "Custom element name cannot be same as one of the builtin elements");
152 case CustomElementDefinitions::NameStatus::NoHyphen:
153 return throwSyntaxError(&state, "Custom element name must contain a hyphen");
154 case CustomElementDefinitions::NameStatus::ContainsUpperCase:
155 return throwSyntaxError(&state, "Custom element name cannot contain an upper case letter");
158 QualifiedName name(nullAtom, tagName, HTMLNames::xhtmlNamespaceURI);
159 auto& definitions = document.ensureCustomElementDefinitions();
160 if (definitions.findInterface(tagName)) {
161 ExceptionCodeWithMessage ec;
162 ec.code = NOT_SUPPORTED_ERR;
163 ec.message = "Cannot define multiple custom elements with the same tag name";
164 setDOMException(&state, ec);
165 return jsUndefined();
167 definitions.defineElement(name, JSCustomElementInterface::create(object, globalObject()));
168 PrivateName uniquePrivateName;
169 globalObject()->putDirect(globalObject()->vm(), uniquePrivateName, object);
171 return jsUndefined();
175 } // namespace WebCore