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