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"
29 #include "CharacterNames.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 "Tokenizer.h"
43 #include <runtime/Error.h>
49 using namespace HTMLNames;
51 bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, const Identifier& propertyName)
53 AtomicStringImpl* atomicPropertyName = AtomicString::find(propertyName);
54 return atomicPropertyName && (document->hasNamedItem(atomicPropertyName) || document->hasExtraNamedItem(atomicPropertyName));
57 JSValuePtr JSHTMLDocument::nameGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
59 JSHTMLDocument* thisObj = static_cast<JSHTMLDocument*>(asObject(slot.slotBase()));
60 HTMLDocument* document = static_cast<HTMLDocument*>(thisObj->impl());
62 String name = propertyName;
63 RefPtr<HTMLCollection> collection = document->documentNamedItems(name);
65 unsigned length = collection->length();
70 Node* node = collection->firstItem();
73 if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
74 return toJS(exec, frame);
76 return toJS(exec, node);
79 return toJS(exec, collection.get());
84 JSValuePtr JSHTMLDocument::all(ExecState* exec) const
86 // If "all" has been overwritten, return the overwritten value
87 JSValuePtr v = getDirect(Identifier(exec, "all"));
91 return toJS(exec, static_cast<HTMLDocument*>(impl())->all().get());
94 void JSHTMLDocument::setAll(ExecState* exec, JSValuePtr value)
96 // Add "all" to the property map.
97 putDirect(Identifier(exec, "all"), value);
102 JSValuePtr JSHTMLDocument::open(ExecState* exec, const ArgList& args)
104 // For compatibility with other browsers, pass open calls with more than 2 parameters to the window.
105 if (args.size() > 2) {
106 Frame* frame = static_cast<HTMLDocument*>(impl())->frame();
108 JSDOMWindowShell* wrapper = toJSDOMWindowShell(frame);
110 JSValuePtr function = wrapper->get(exec, Identifier(exec, "open"));
112 CallType callType = function.getCallData(callData);
113 if (callType == CallTypeNone)
114 return throwError(exec, TypeError);
115 return call(exec, function, callType, callData, wrapper, args);
118 return jsUndefined();
121 // document.open clobbers the security context of the document and
122 // aliases it with the active security context.
123 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
125 // In the case of two parameters or fewer, do a normal document open.
126 static_cast<HTMLDocument*>(impl())->open(activeDocument);
130 enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
132 static inline void documentWrite(ExecState* exec, const ArgList& args, HTMLDocument* document, NewlineRequirement addNewline)
134 // DOM only specifies single string argument, but browsers allow multiple or no arguments.
136 size_t size = args.size();
138 UString firstString = args.at(exec, 0).toString(exec);
139 SegmentedString segmentedString(firstString.data(), firstString.size());
140 Vector<UString> subsequentStrings; // Keeps strings alive until Tokenizer::write is called on them.
143 segmentedString.clear();
145 subsequentStrings.reserveInitialCapacity(size - 1);
146 for (size_t i = 1; i < size; ++i) {
147 UString subsequentString = args.at(exec, i).toString(exec);
148 segmentedString.append(SegmentedString(subsequentString.data(), subsequentString.size()));
149 subsequentStrings.append(subsequentString);
154 segmentedString.append(SegmentedString(&newlineCharacter, 1));
156 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
157 document->write(segmentedString, activeDocument);
160 JSValuePtr JSHTMLDocument::write(ExecState* exec, const ArgList& args)
162 documentWrite(exec, args, static_cast<HTMLDocument*>(impl()), DoNotAddNewline);
163 return jsUndefined();
166 JSValuePtr JSHTMLDocument::writeln(ExecState* exec, const ArgList& args)
168 documentWrite(exec, args, static_cast<HTMLDocument*>(impl()), DoAddNewline);
169 return jsUndefined();
172 } // namespace WebCore