2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "JSHTMLDocument.h"
30 #include "HTMLAllCollection.h"
31 #include "HTMLBodyElement.h"
32 #include "HTMLCollection.h"
33 #include "HTMLDocument.h"
34 #include "HTMLElement.h"
35 #include "HTMLIFrameElement.h"
36 #include "HTMLNames.h"
37 #include "JSDOMWindow.h"
38 #include "JSDOMWindowCustom.h"
39 #include "JSDOMWindowShell.h"
40 #include "JSHTMLCollection.h"
41 #include "SegmentedString.h"
42 #include "DocumentParser.h"
43 #include <runtime/Error.h>
44 #include <runtime/JSCell.h>
45 #include <wtf/unicode/CharacterNames.h>
51 using namespace HTMLNames;
53 bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, const Identifier& propertyName)
55 AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
56 return atomicPropertyName && (document->hasNamedItem(atomicPropertyName) || document->hasExtraNamedItem(atomicPropertyName));
59 JSValue JSHTMLDocument::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
61 JSHTMLDocument* thisObj = static_cast<JSHTMLDocument*>(asObject(slotBase));
62 HTMLDocument* document = static_cast<HTMLDocument*>(thisObj->impl());
64 String name = identifierToString(propertyName);
65 RefPtr<HTMLCollection> collection = document->documentNamedItems(name);
67 unsigned length = collection->length();
72 Node* node = collection->firstItem();
75 if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
76 return toJS(exec, frame);
78 return toJS(exec, thisObj->globalObject(), node);
81 return toJS(exec, thisObj->globalObject(), collection.get());
86 JSValue JSHTMLDocument::all(ExecState* exec) const
88 // If "all" has been overwritten, return the overwritten value
89 JSValue v = getDirect(exec->globalData(), Identifier(exec, "all"));
93 return toJS(exec, globalObject(), static_cast<HTMLDocument*>(impl())->all().get());
96 void JSHTMLDocument::setAll(ExecState* exec, JSValue value)
98 // Add "all" to the property map.
99 putDirect(exec->globalData(), Identifier(exec, "all"), value);
104 JSValue JSHTMLDocument::open(ExecState* exec)
106 // For compatibility with other browsers, pass open calls with more than 2 parameters to the window.
107 if (exec->argumentCount() > 2) {
108 Frame* frame = static_cast<HTMLDocument*>(impl())->frame();
110 JSDOMWindowShell* wrapper = toJSDOMWindowShell(frame, currentWorld(exec));
112 JSValue function = wrapper->get(exec, Identifier(exec, "open"));
114 CallType callType = ::getCallData(function, callData);
115 if (callType == CallTypeNone)
116 return throwTypeError(exec);
117 return JSC::call(exec, function, callType, callData, wrapper, ArgList(exec));
120 return jsUndefined();
123 // document.open clobbers the security context of the document and
124 // aliases it with the active security context.
125 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
127 // In the case of two parameters or fewer, do a normal document open.
128 static_cast<HTMLDocument*>(impl())->open(activeDocument);
132 enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
134 static inline void documentWrite(ExecState* exec, HTMLDocument* document, NewlineRequirement addNewline)
136 // DOM only specifies single string argument, but browsers allow multiple or no arguments.
138 size_t size = exec->argumentCount();
140 UString firstString = exec->argument(0).toString(exec);
141 SegmentedString segmentedString = ustringToString(firstString);
144 segmentedString.clear();
146 for (size_t i = 1; i < size; ++i) {
147 UString subsequentString = exec->argument(i).toString(exec);
148 segmentedString.append(SegmentedString(ustringToString(subsequentString)));
153 segmentedString.append(SegmentedString(String(&newlineCharacter, 1)));
155 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
156 document->write(segmentedString, activeDocument);
159 JSValue JSHTMLDocument::write(ExecState* exec)
161 documentWrite(exec, static_cast<HTMLDocument*>(impl()), DoNotAddNewline);
162 return jsUndefined();
165 JSValue JSHTMLDocument::writeln(ExecState* exec)
167 documentWrite(exec, static_cast<HTMLDocument*>(impl()), DoAddNewline);
168 return jsUndefined();
171 } // namespace WebCore