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 "JSMainThreadExecState.h" 42#include "SegmentedString.h" 43#include "DocumentParser.h" 44#include <runtime/Error.h> 45#include <runtime/JSCell.h> 46#include <wtf/unicode/CharacterNames.h> 47 48using namespace JSC; 49 50namespace WebCore { 51 52using namespace HTMLNames; 53 54bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, PropertyName propertyName) 55{ 56 AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName); 57 return atomicPropertyName && document->documentNamedItemMap().contains(atomicPropertyName); 58} 59 60JSValue JSHTMLDocument::nameGetter(ExecState* exec, JSValue slotBase, PropertyName propertyName) 61{ 62 JSHTMLDocument* thisObj = jsCast<JSHTMLDocument*>(asObject(slotBase)); 63 HTMLDocument* document = toHTMLDocument(thisObj->impl()); 64 65 AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName); 66 if (!atomicPropertyName || !document->documentNamedItemMap().contains(atomicPropertyName)) 67 return jsUndefined(); 68 69 if (UNLIKELY(!document->documentNamedItemMap().containsSingle(atomicPropertyName))) { 70 RefPtr<HTMLCollection> collection = document->documentNamedItems(atomicPropertyName); 71 ASSERT(!collection->isEmpty()); 72 ASSERT(!collection->hasExactlyOneItem()); 73 return toJS(exec, thisObj->globalObject(), WTF::getPtr(collection)); 74 } 75 76 Node* node = document->documentNamedItemMap().getElementByDocumentNamedItem(atomicPropertyName, document); 77 Frame* frame; 78 if (node->hasTagName(iframeTag) && (frame = toHTMLIFrameElement(node)->contentFrame())) 79 return toJS(exec, frame); 80 81 return toJS(exec, thisObj->globalObject(), node); 82} 83 84// Custom attributes 85 86JSValue JSHTMLDocument::all(ExecState* exec) const 87{ 88 // If "all" has been overwritten, return the overwritten value 89 JSValue v = getDirect(exec->vm(), Identifier(exec, "all")); 90 if (v) 91 return v; 92 93 return toJS(exec, globalObject(), toHTMLDocument(impl())->all()); 94} 95 96void JSHTMLDocument::setAll(ExecState* exec, JSValue value) 97{ 98 // Add "all" to the property map. 99 putDirect(exec->vm(), Identifier(exec, "all"), value); 100} 101 102// Custom functions 103 104JSValue 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 = toHTMLDocument(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 JSMainThreadExecState::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 toHTMLDocument(impl())->open(activeDocument); 129 return this; 130} 131 132enum NewlineRequirement { DoNotAddNewline, DoAddNewline }; 133 134static 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 String firstString = exec->argument(0).toString(exec)->value(exec); 141 SegmentedString segmentedString = firstString; 142 if (size != 1) { 143 if (!size) 144 segmentedString.clear(); 145 else { 146 for (size_t i = 1; i < size; ++i) { 147 String subsequentString = exec->argument(i).toString(exec)->value(exec); 148 segmentedString.append(SegmentedString(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 159JSValue JSHTMLDocument::write(ExecState* exec) 160{ 161 documentWrite(exec, toHTMLDocument(impl()), DoNotAddNewline); 162 return jsUndefined(); 163} 164 165JSValue JSHTMLDocument::writeln(ExecState* exec) 166{ 167 documentWrite(exec, toHTMLDocument(impl()), DoAddNewline); 168 return jsUndefined(); 169} 170 171} // namespace WebCore 172