JSHTMLDocumentCustom.cpp [plain text]
#include "config.h"
#include "JSHTMLDocument.h"
#include "Frame.h"
#include "HTMLAllCollection.h"
#include "HTMLBodyElement.h"
#include "HTMLCollection.h"
#include "HTMLDocument.h"
#include "HTMLElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
#include "JSDOMWindow.h"
#include "JSDOMWindowCustom.h"
#include "JSDOMWindowShell.h"
#include "JSHTMLCollection.h"
#include "JSMainThreadExecState.h"
#include "SegmentedString.h"
#include "DocumentParser.h"
#include <runtime/Error.h>
#include <runtime/JSCell.h>
#include <wtf/unicode/CharacterNames.h>
using namespace JSC;
namespace WebCore {
using namespace HTMLNames;
bool JSHTMLDocument::canGetItemsForName(ExecState*, HTMLDocument* document, PropertyName propertyName)
{
AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
return atomicPropertyName && document->hasDocumentNamedItem(*atomicPropertyName);
}
EncodedJSValue JSHTMLDocument::nameGetter(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName propertyName)
{
JSHTMLDocument* thisObj = jsCast<JSHTMLDocument*>(slotBase);
HTMLDocument& document = thisObj->impl();
AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
if (!atomicPropertyName || !document.hasDocumentNamedItem(*atomicPropertyName))
return JSValue::encode(jsUndefined());
if (UNLIKELY(document.documentNamedItemContainsMultipleElements(*atomicPropertyName))) {
RefPtr<HTMLCollection> collection = document.documentNamedItems(atomicPropertyName);
ASSERT(collection->length() > 1);
return JSValue::encode(toJS(exec, thisObj->globalObject(), WTF::getPtr(collection)));
}
Element* element = document.documentNamedItem(*atomicPropertyName);
if (UNLIKELY(element->hasTagName(iframeTag))) {
if (Frame* frame = toHTMLIFrameElement(element)->contentFrame())
return JSValue::encode(toJS(exec, frame));
}
return JSValue::encode(toJS(exec, thisObj->globalObject(), element));
}
JSValue JSHTMLDocument::all(ExecState* exec) const
{
JSValue v = getDirect(exec->vm(), Identifier(exec, "all"));
if (v)
return v;
return toJS(exec, globalObject(), impl().all());
}
void JSHTMLDocument::setAll(ExecState* exec, JSValue value)
{
putDirect(exec->vm(), Identifier(exec, "all"), value);
}
JSValue JSHTMLDocument::open(ExecState* exec)
{
if (exec->argumentCount() > 2) {
if (Frame* frame = impl().frame()) {
JSDOMWindowShell* wrapper = toJSDOMWindowShell(frame, currentWorld(exec));
if (wrapper) {
JSValue function = wrapper->get(exec, Identifier(exec, "open"));
CallData callData;
CallType callType = ::getCallData(function, callData);
if (callType == CallTypeNone)
return throwTypeError(exec);
return JSC::call(exec, function, callType, callData, wrapper, ArgList(exec));
}
}
return jsUndefined();
}
Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl().document();
impl().open(activeDocument);
return this;
}
enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
static inline void documentWrite(ExecState* exec, HTMLDocument* document, NewlineRequirement addNewline)
{
size_t size = exec->argumentCount();
String firstString = exec->argument(0).toString(exec)->value(exec);
SegmentedString segmentedString = firstString;
if (size != 1) {
if (!size)
segmentedString.clear();
else {
for (size_t i = 1; i < size; ++i) {
String subsequentString = exec->uncheckedArgument(i).toString(exec)->value(exec);
segmentedString.append(SegmentedString(subsequentString));
}
}
}
if (addNewline)
segmentedString.append(SegmentedString(String(&newlineCharacter, 1)));
Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl().document();
document->write(segmentedString, activeDocument);
}
JSValue JSHTMLDocument::write(ExecState* exec)
{
documentWrite(exec, &impl(), DoNotAddNewline);
return jsUndefined();
}
JSValue JSHTMLDocument::writeln(ExecState* exec)
{
documentWrite(exec, &impl(), DoAddNewline);
return jsUndefined();
}
}