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 "CharacterNames.h"
30 #include "Frame.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>
44
45 using namespace JSC;
46
47 namespace WebCore {
48
49 using namespace HTMLNames;
50
canGetItemsForName(ExecState *,HTMLDocument * document,const Identifier & propertyName)51 bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, const Identifier& propertyName)
52 {
53 AtomicStringImpl* atomicPropertyName = AtomicString::find(propertyName);
54 return atomicPropertyName && (document->hasNamedItem(atomicPropertyName) || document->hasExtraNamedItem(atomicPropertyName));
55 }
56
nameGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot & slot)57 JSValue JSHTMLDocument::nameGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
58 {
59 JSHTMLDocument* thisObj = static_cast<JSHTMLDocument*>(asObject(slot.slotBase()));
60 HTMLDocument* document = static_cast<HTMLDocument*>(thisObj->impl());
61
62 String name = propertyName;
63 RefPtr<HTMLCollection> collection = document->documentNamedItems(name);
64
65 unsigned length = collection->length();
66 if (!length)
67 return jsUndefined();
68
69 if (length == 1) {
70 Node* node = collection->firstItem();
71
72 Frame* frame;
73 if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
74 return toJS(exec, frame);
75
76 return toJS(exec, node);
77 }
78
79 return toJS(exec, collection.get());
80 }
81
82 // Custom attributes
83
all(ExecState * exec) const84 JSValue JSHTMLDocument::all(ExecState* exec) const
85 {
86 // If "all" has been overwritten, return the overwritten value
87 JSValue v = getDirect(Identifier(exec, "all"));
88 if (v)
89 return v;
90
91 return toJS(exec, static_cast<HTMLDocument*>(impl())->all().get());
92 }
93
setAll(ExecState * exec,JSValue value)94 void JSHTMLDocument::setAll(ExecState* exec, JSValue value)
95 {
96 // Add "all" to the property map.
97 putDirect(Identifier(exec, "all"), value);
98 }
99
100 // Custom functions
101
open(ExecState * exec,const ArgList & args)102 JSValue JSHTMLDocument::open(ExecState* exec, const ArgList& args)
103 {
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();
107 if (frame) {
108 JSDOMWindowShell* wrapper = toJSDOMWindowShell(frame);
109 if (wrapper) {
110 JSValue function = wrapper->get(exec, Identifier(exec, "open"));
111 CallData callData;
112 CallType callType = function.getCallData(callData);
113 if (callType == CallTypeNone)
114 return throwError(exec, TypeError);
115 return call(exec, function, callType, callData, wrapper, args);
116 }
117 }
118 return jsUndefined();
119 }
120
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();
124
125 // In the case of two parameters or fewer, do a normal document open.
126 static_cast<HTMLDocument*>(impl())->open(activeDocument);
127 return this;
128 }
129
130 enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
131
documentWrite(ExecState * exec,const ArgList & args,HTMLDocument * document,NewlineRequirement addNewline)132 static inline void documentWrite(ExecState* exec, const ArgList& args, HTMLDocument* document, NewlineRequirement addNewline)
133 {
134 // DOM only specifies single string argument, but browsers allow multiple or no arguments.
135
136 size_t size = args.size();
137
138 UString firstString = args.at(0).toString(exec);
139 SegmentedString segmentedString = String(firstString);
140 if (size != 1) {
141 if (!size)
142 segmentedString.clear();
143 else {
144 for (size_t i = 1; i < size; ++i) {
145 UString subsequentString = args.at(i).toString(exec);
146 segmentedString.append(SegmentedString(String(subsequentString)));
147 }
148 }
149 }
150 if (addNewline)
151 segmentedString.append(SegmentedString(&newlineCharacter, 1));
152
153 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
154 document->write(segmentedString, activeDocument);
155 }
156
write(ExecState * exec,const ArgList & args)157 JSValue JSHTMLDocument::write(ExecState* exec, const ArgList& args)
158 {
159 documentWrite(exec, args, static_cast<HTMLDocument*>(impl()), DoNotAddNewline);
160 return jsUndefined();
161 }
162
writeln(ExecState * exec,const ArgList & args)163 JSValue JSHTMLDocument::writeln(ExecState* exec, const ArgList& args)
164 {
165 documentWrite(exec, args, static_cast<HTMLDocument*>(impl()), DoAddNewline);
166 return jsUndefined();
167 }
168
169 } // namespace WebCore
170