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, const Identifier& propertyName)
{
AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
return atomicPropertyName && (document->hasNamedItem(atomicPropertyName) || document->hasExtraNamedItem(atomicPropertyName));
}
JSValue JSHTMLDocument::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
{
JSHTMLDocument* thisObj = jsCast<JSHTMLDocument*>(asObject(slotBase));
HTMLDocument* document = static_cast<HTMLDocument*>(thisObj->impl());
HTMLCollection* collection = document->documentNamedItems(identifierToAtomicString(propertyName));
unsigned length = collection->length();
if (!length)
return jsUndefined();
if (length == 1) {
Node* node = collection->firstItem();
Frame* frame;
if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
return toJS(exec, frame);
return toJS(exec, thisObj->globalObject(), node);
}
return toJS(exec, thisObj->globalObject(), collection);
}
JSValue JSHTMLDocument::all(ExecState* exec) const
{
JSValue v = getDirect(exec->globalData(), Identifier(exec, "all"));
if (v)
return v;
return toJS(exec, globalObject(), static_cast<HTMLDocument*>(impl())->all());
}
void JSHTMLDocument::setAll(ExecState* exec, JSValue value)
{
putDirect(exec->globalData(), Identifier(exec, "all"), value);
}
JSValue JSHTMLDocument::open(ExecState* exec)
{
if (exec->argumentCount() > 2) {
Frame* frame = static_cast<HTMLDocument*>(impl())->frame();
if (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 JSMainThreadExecState::call(exec, function, callType, callData, wrapper, ArgList(exec));
}
}
return jsUndefined();
}
Document* activeDocument = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->document();
static_cast<HTMLDocument*>(impl())->open(activeDocument);
return this;
}
enum NewlineRequirement { DoNotAddNewline, DoAddNewline };
static inline void documentWrite(ExecState* exec, HTMLDocument* document, NewlineRequirement addNewline)
{
size_t size = exec->argumentCount();
UString firstString = exec->argument(0).toString(exec)->value(exec);
SegmentedString segmentedString = ustringToString(firstString);
if (size != 1) {
if (!size)
segmentedString.clear();
else {
for (size_t i = 1; i < size; ++i) {
UString subsequentString = exec->argument(i).toString(exec)->value(exec);
segmentedString.append(SegmentedString(ustringToString(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, static_cast<HTMLDocument*>(impl()), DoNotAddNewline);
return jsUndefined();
}
JSValue JSHTMLDocument::writeln(ExecState* exec)
{
documentWrite(exec, static_cast<HTMLDocument*>(impl()), DoAddNewline);
return jsUndefined();
}
}