• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008, 2009 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "HTMLDocument.h"
33 
34 #include "Frame.h"
35 #include "HTMLCollection.h"
36 #include "HTMLIFrameElement.h"
37 #include "HTMLNames.h"
38 
39 #include "V8Binding.h"
40 #include "V8CustomBinding.h"
41 #include "V8Proxy.h"
42 
43 #include <wtf/RefPtr.h>
44 #include <wtf/StdLibExtras.h>
45 
46 namespace WebCore {
47 
NAMED_PROPERTY_DELETER(HTMLDocument)48 NAMED_PROPERTY_DELETER(HTMLDocument)
49 {
50     // Only handle document.all.  Insert the marker object into the
51     // shadow internal field to signal that document.all is no longer
52     // shadowed.
53     AtomicString key = v8StringToAtomicWebCoreString(name);
54     DEFINE_STATIC_LOCAL(const AtomicString, all, ("all"));
55     if (key != all)
56         return deletionNotHandledByInterceptor();
57 
58     ASSERT(info.Holder()->InternalFieldCount() == kHTMLDocumentInternalFieldCount);
59     v8::Local<v8::Value> marker = info.Holder()->GetInternalField(kHTMLDocumentMarkerIndex);
60     info.Holder()->SetInternalField(kHTMLDocumentShadowIndex, marker);
61     return v8::True();
62 }
63 
NAMED_PROPERTY_GETTER(HTMLDocument)64 NAMED_PROPERTY_GETTER(HTMLDocument)
65 {
66     INC_STATS("DOM.HTMLDocument.NamedPropertyGetter");
67     AtomicString key = v8StringToAtomicWebCoreString(name);
68 
69     // Special case for document.all.  If the value in the shadow
70     // internal field is not the marker object, then document.all has
71     // been temporarily shadowed and we return the value.
72     DEFINE_STATIC_LOCAL(const AtomicString, all, ("all"));
73     if (key == all) {
74         ASSERT(info.Holder()->InternalFieldCount() == kHTMLDocumentInternalFieldCount);
75         v8::Local<v8::Value> marker = info.Holder()->GetInternalField(kHTMLDocumentMarkerIndex);
76         v8::Local<v8::Value> value = info.Holder()->GetInternalField(kHTMLDocumentShadowIndex);
77         if (marker != value)
78             return value;
79     }
80 
81     HTMLDocument* htmlDocument = V8DOMWrapper::convertDOMWrapperToNode<HTMLDocument>(info.Holder());
82 
83     // Fast case for named elements that are not there.
84     if (!htmlDocument->hasNamedItem(key.impl()) && !htmlDocument->hasExtraNamedItem(key.impl()))
85         return v8::Handle<v8::Value>();
86 
87     RefPtr<HTMLCollection> items = htmlDocument->documentNamedItems(key);
88     if (!items->length())
89         return notHandledByInterceptor();
90 
91     if (items->length() == 1) {
92         Node* node = items->firstItem();
93         Frame* frame = 0;
94         if (node->hasTagName(HTMLNames::iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
95             return V8DOMWrapper::convertToV8Object(V8ClassIndex::DOMWINDOW, frame->domWindow());
96 
97         return V8DOMWrapper::convertNodeToV8Object(node);
98     }
99 
100     return V8DOMWrapper::convertToV8Object(V8ClassIndex::HTMLCOLLECTION, items.release());
101 }
102 
103 // HTMLDocument ----------------------------------------------------------------
104 
105 // Concatenates "args" to a string. If args is empty, returns empty string.
106 // Firefox/Safari/IE support non-standard arguments to document.write, ex:
107 //   document.write("a", "b", "c") --> document.write("abc")
108 //   document.write() --> document.write("")
writeHelperGetString(const v8::Arguments & args)109 static String writeHelperGetString(const v8::Arguments& args)
110 {
111     String str = "";
112     for (int i = 0; i < args.Length(); ++i)
113         str += toWebCoreString(args[i]);
114     return str;
115 }
116 
CALLBACK_FUNC_DECL(HTMLDocumentWrite)117 CALLBACK_FUNC_DECL(HTMLDocumentWrite)
118 {
119     INC_STATS("DOM.HTMLDocument.write()");
120     HTMLDocument* htmlDocument = V8DOMWrapper::convertDOMWrapperToNode<HTMLDocument>(args.Holder());
121     Frame* frame = V8Proxy::retrieveFrameForCallingContext();
122     ASSERT(frame);
123     htmlDocument->write(writeHelperGetString(args), frame->document());
124     return v8::Undefined();
125 }
126 
CALLBACK_FUNC_DECL(HTMLDocumentWriteln)127 CALLBACK_FUNC_DECL(HTMLDocumentWriteln)
128 {
129     INC_STATS("DOM.HTMLDocument.writeln()");
130     HTMLDocument* htmlDocument = V8DOMWrapper::convertDOMWrapperToNode<HTMLDocument>(args.Holder());
131     Frame* frame = V8Proxy::retrieveFrameForCallingContext();
132     ASSERT(frame);
133     htmlDocument->writeln(writeHelperGetString(args), frame->document());
134     return v8::Undefined();
135 }
136 
CALLBACK_FUNC_DECL(HTMLDocumentOpen)137 CALLBACK_FUNC_DECL(HTMLDocumentOpen)
138 {
139     INC_STATS("DOM.HTMLDocument.open()");
140     HTMLDocument* htmlDocument = V8DOMWrapper::convertDOMWrapperToNode<HTMLDocument>(args.Holder());
141 
142     if (args.Length() > 2) {
143         if (Frame* frame = htmlDocument->frame()) {
144             // Fetch the global object for the frame.
145             v8::Local<v8::Context> context = V8Proxy::context(frame);
146             // Bail out if we cannot get the context.
147             if (context.IsEmpty())
148                 return v8::Undefined();
149             v8::Local<v8::Object> global = context->Global();
150             // Get the open property of the global object.
151             v8::Local<v8::Value> function = global->Get(v8::String::New("open"));
152             // If the open property is not a function throw a type error.
153             if (!function->IsFunction()) {
154                 throwError("open is not a function");
155                 return v8::Undefined();
156             }
157             // Wrap up the arguments and call the function.
158             v8::Local<v8::Value>* params = new v8::Local<v8::Value>[args.Length()];
159             for (int i = 0; i < args.Length(); i++)
160                 params[i] = args[i];
161 
162             V8Proxy* proxy = V8Proxy::retrieve(frame);
163             ASSERT(proxy);
164 
165             v8::Local<v8::Value> result = proxy->callFunction(v8::Local<v8::Function>::Cast(function), global, args.Length(), params);
166             delete[] params;
167             return result;
168         }
169     }
170 
171     Frame* frame = V8Proxy::retrieveFrameForCallingContext();
172     htmlDocument->open(frame->document());
173     // Return the document.
174     return args.Holder();
175 }
176 
ACCESSOR_GETTER(HTMLDocumentAll)177 ACCESSOR_GETTER(HTMLDocumentAll)
178 {
179     INC_STATS("DOM.HTMLDocument.all._get");
180     v8::HandleScope scope;
181     v8::Handle<v8::Object> holder = info.Holder();
182     HTMLDocument* htmlDocument = V8DOMWrapper::convertDOMWrapperToNode<HTMLDocument>(holder);
183     return V8DOMWrapper::convertToV8Object(V8ClassIndex::HTMLCOLLECTION, htmlDocument->all());
184 }
185 
ACCESSOR_SETTER(HTMLDocumentAll)186 ACCESSOR_SETTER(HTMLDocumentAll)
187 {
188     INC_STATS("DOM.HTMLDocument.all._set");
189     v8::Handle<v8::Object> holder = info.Holder();
190     ASSERT(info.Holder()->InternalFieldCount() == kHTMLDocumentInternalFieldCount);
191     info.Holder()->SetInternalField(kHTMLDocumentShadowIndex, value);
192 }
193 
194 } // namespace WebCore
195