1 /*
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "JSHTMLDocument.h"
28
29 #include "Frame.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>
46
47 using namespace JSC;
48
49 namespace WebCore {
50
51 using namespace HTMLNames;
52
canGetItemsForName(ExecState *,HTMLDocument * document,const Identifier & propertyName)53 bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, const Identifier& propertyName)
54 {
55 AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
56 return atomicPropertyName && (document->hasNamedItem(atomicPropertyName) || document->hasExtraNamedItem(atomicPropertyName));
57 }
58
nameGetter(ExecState * exec,JSValue slotBase,const Identifier & propertyName)59 JSValue JSHTMLDocument::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
60 {
61 JSHTMLDocument* thisObj = static_cast<JSHTMLDocument*>(asObject(slotBase));
62 HTMLDocument* document = static_cast<HTMLDocument*>(thisObj->impl());
63
64 String name = identifierToString(propertyName);
65 RefPtr<HTMLCollection> collection = document->documentNamedItems(name);
66
67 unsigned length = collection->length();
68 if (!length)
69 return jsUndefined();
70
71 if (length == 1) {
72 Node* node = collection->firstItem();
73
74 Frame* frame;
75 if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
76 return toJS(exec, frame);
77
78 return toJS(exec, node);
79 }
80
81 return toJS(exec, collection.get());
82 }
83
84 // Custom attributes
85
all(ExecState * exec) const86 JSValue JSHTMLDocument::all(ExecState* exec) const
87 {
88 // If "all" has been overwritten, return the overwritten value
89 JSValue v = getDirect(exec->globalData(), Identifier(exec, "all"));
90 if (v)
91 return v;
92
93 return toJS(exec, static_cast<HTMLDocument*>(impl())->all().get());
94 }
95
setAll(ExecState * exec,JSValue value)96 void JSHTMLDocument::setAll(ExecState* exec, JSValue value)
97 {
98 // Add "all" to the property map.
99 putDirect(exec->globalData(), Identifier(exec, "all"), value);
100 }
101
102 // Custom functions
103
open(ExecState * exec)104 JSValue JSHTMLDocument::open(ExecState* exec)
105 {
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();
109 if (frame) {
110 JSDOMWindowShell* wrapper = toJSDOMWindowShell(frame, currentWorld(exec));
111 if (wrapper) {
112 JSValue function = wrapper->get(exec, Identifier(exec, "open"));
113 CallData callData;
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));
118 }
119 }
120 return jsUndefined();
121 }
122
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();
126
127 // In the case of two parameters or fewer, do a normal document open.
128 static_cast<HTMLDocument*>(impl())->open(activeDocument);
129 return this;
130 }
131
132 enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
133
documentWrite(ExecState * exec,HTMLDocument * document,NewlineRequirement addNewline)134 static inline void documentWrite(ExecState* exec, HTMLDocument* document, NewlineRequirement addNewline)
135 {
136 // DOM only specifies single string argument, but browsers allow multiple or no arguments.
137
138 size_t size = exec->argumentCount();
139
140 UString firstString = exec->argument(0).toString(exec);
141 SegmentedString segmentedString = ustringToString(firstString);
142 if (size != 1) {
143 if (!size)
144 segmentedString.clear();
145 else {
146 for (size_t i = 1; i < size; ++i) {
147 UString subsequentString = exec->argument(i).toString(exec);
148 segmentedString.append(SegmentedString(ustringToString(subsequentString)));
149 }
150 }
151 }
152 if (addNewline)
153 segmentedString.append(SegmentedString(String(&newlineCharacter, 1)));
154
155 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
156 document->write(segmentedString, activeDocument);
157 }
158
write(ExecState * exec)159 JSValue JSHTMLDocument::write(ExecState* exec)
160 {
161 documentWrite(exec, static_cast<HTMLDocument*>(impl()), DoNotAddNewline);
162 return jsUndefined();
163 }
164
writeln(ExecState * exec)165 JSValue JSHTMLDocument::writeln(ExecState* exec)
166 {
167 documentWrite(exec, static_cast<HTMLDocument*>(impl()), DoAddNewline);
168 return jsUndefined();
169 }
170
171 } // namespace WebCore
172