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->documentNamedItemMap().contains(atomicPropertyName);
}
JSValue JSHTMLDocument::nameGetter(ExecState* exec, JSValue slotBase, PropertyName propertyName)
{
JSHTMLDocument* thisObj = jsCast<JSHTMLDocument*>(asObject(slotBase));
HTMLDocument* document = toHTMLDocument(thisObj->impl());
AtomicStringImpl* atomicPropertyName = findAtomicString(propertyName);
if (!atomicPropertyName || !document->documentNamedItemMap().contains(atomicPropertyName))
return jsUndefined();
if (UNLIKELY(!document->documentNamedItemMap().containsSingle(atomicPropertyName))) {
RefPtr<HTMLCollection> collection = document->documentNamedItems(atomicPropertyName);
ASSERT(!collection->isEmpty());
ASSERT(!collection->hasExactlyOneItem());
return toJS(exec, thisObj->globalObject(), WTF::getPtr(collection));
}
Node* node = document->documentNamedItemMap().getElementByDocumentNamedItem(atomicPropertyName, document);
Frame* frame;
if (node->hasTagName(iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
return toJS(exec, frame);
return toJS(exec, thisObj->globalObject(), node);
}
JSValue JSHTMLDocument::all(ExecState* exec) const
{
JSValue v = getDirect(exec->vm(), Identifier(exec, "all"));
if (v)
return v;
return toJS(exec, globalObject(), toHTMLDocument(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) {
Frame* frame = toHTMLDocument(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();
toHTMLDocument(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->argument(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, toHTMLDocument(impl()), DoNotAddNewline);
return jsUndefined();
}
JSValue JSHTMLDocument::writeln(ExecState* exec)
{
documentWrite(exec, toHTMLDocument(impl()), DoAddNewline);
return jsUndefined();
}
}