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 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 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->hasDocumentNamedItem(*atomicPropertyName); 58} 59 60EncodedJSValue JSHTMLDocument::nameGetter(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName propertyName) 61{ 62 JSHTMLDocument* thisObj = jsCast<JSHTMLDocument*>(slotBase); 63 HTMLDocument& document = thisObj->impl(); 64 65 AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName); 66 if (!atomicPropertyName || !document.hasDocumentNamedItem(*atomicPropertyName)) 67 return JSValue::encode(jsUndefined()); 68 69 if (UNLIKELY(document.documentNamedItemContainsMultipleElements(*atomicPropertyName))) { 70 RefPtr<HTMLCollection> collection = document.documentNamedItems(atomicPropertyName); 71 ASSERT(collection->length() > 1); 72 return JSValue::encode(toJS(exec, thisObj->globalObject(), WTF::getPtr(collection))); 73 } 74 75 Element* element = document.documentNamedItem(*atomicPropertyName); 76 if (UNLIKELY(element->hasTagName(iframeTag))) { 77 if (Frame* frame = toHTMLIFrameElement(element)->contentFrame()) 78 return JSValue::encode(toJS(exec, frame)); 79 } 80 81 return JSValue::encode(toJS(exec, thisObj->globalObject(), element)); 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(), 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 if (Frame* frame = impl().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 = ::getCallData(function, callData); 114 if (callType == CallTypeNone) 115 return throwTypeError(exec); 116 return JSC::call(exec, function, callType, callData, wrapper, ArgList(exec)); 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 impl().open(activeDocument); 128 return this; 129} 130 131enum NewlineRequirement { DoNotAddNewline, DoAddNewline }; 132 133static inline void documentWrite(ExecState* exec, HTMLDocument* document, NewlineRequirement addNewline) 134{ 135 // DOM only specifies single string argument, but browsers allow multiple or no arguments. 136 137 size_t size = exec->argumentCount(); 138 139 String firstString = exec->argument(0).toString(exec)->value(exec); 140 SegmentedString segmentedString = firstString; 141 if (size != 1) { 142 if (!size) 143 segmentedString.clear(); 144 else { 145 for (size_t i = 1; i < size; ++i) { 146 String subsequentString = exec->uncheckedArgument(i).toString(exec)->value(exec); 147 segmentedString.append(SegmentedString(subsequentString)); 148 } 149 } 150 } 151 if (addNewline) 152 segmentedString.append(SegmentedString(String(&newlineCharacter, 1))); 153 154 Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl().document(); 155 document->write(segmentedString, activeDocument); 156} 157 158JSValue JSHTMLDocument::write(ExecState* exec) 159{ 160 documentWrite(exec, &impl(), DoNotAddNewline); 161 return jsUndefined(); 162} 163 164JSValue JSHTMLDocument::writeln(ExecState* exec) 165{ 166 documentWrite(exec, &impl(), DoAddNewline); 167 return jsUndefined(); 168} 169 170} // namespace WebCore 171