File indexing completed on 2024-04-28 15:23:17

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public
0016  *  License along with this library; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "ecma/kjs_views.h"
0021 #include "ecma/kjs_css.h"
0022 #include "ecma/kjs_window.h"
0023 #include "kjs_views.lut.h"
0024 
0025 using namespace KJS;
0026 
0027 // -------------------------------------------------------------------------
0028 
0029 const ClassInfo DOMAbstractView::info = { "AbstractView", nullptr, &DOMAbstractViewTable, nullptr };
0030 
0031 /*
0032 @begin DOMAbstractViewTable 2
0033   document      DOMAbstractView::Document       DontDelete|ReadOnly
0034 @end
0035 @begin DOMAbstractViewProtoTable 1
0036   getComputedStyle  DOMAbstractView::GetComputedStyle   DontDelete|Function 2
0037 @end
0038 */
0039 
0040 KJS_DEFINE_PROTOTYPE(DOMAbstractViewProto)
0041 KJS_IMPLEMENT_PROTOFUNC(DOMAbstractViewProtoFunc)
0042 KJS_IMPLEMENT_PROTOTYPE("DOMAbstractView", DOMAbstractViewProto, DOMAbstractViewProtoFunc, ObjectPrototype)
0043 
0044 DOMAbstractView::DOMAbstractView(ExecState *exec, DOM::AbstractViewImpl *av)
0045     : m_impl(av)
0046 {
0047     setPrototype(exec->lexicalInterpreter()->builtinObjectPrototype());
0048 }
0049 
0050 DOMAbstractView::~DOMAbstractView()
0051 {
0052     ScriptInterpreter::forgetDOMObject(m_impl.get());
0053 }
0054 
0055 JSValue *DOMAbstractView::getValueProperty(ExecState *exec, int token)
0056 {
0057     assert(token == Document);
0058     Q_UNUSED(token);
0059     return getDOMNode(exec, impl()->document());
0060 }
0061 
0062 bool DOMAbstractView::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
0063 {
0064     return getStaticValueSlot<DOMAbstractView, DOMObject>(exec, &DOMAbstractViewTable, this, propertyName, slot);
0065 }
0066 
0067 JSValue *DOMAbstractViewProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
0068 {
0069     KJS_CHECK_THIS(KJS::DOMAbstractView, thisObj);
0070     DOM::AbstractViewImpl &abstractView = *static_cast<DOMAbstractView *>(thisObj)->impl();
0071     switch (id) {
0072     case DOMAbstractView::GetComputedStyle: {
0073         DOM::ElementImpl *arg0 = toElement(args[0]);
0074         if (!arg0) {
0075             return jsUndefined();    // throw exception?
0076         } else {
0077             return getDOMCSSStyleDeclaration(exec, abstractView.getComputedStyle(arg0, args[1]->toString(exec).domString().implementation()));
0078         }
0079     }
0080     }
0081     return jsUndefined();
0082 }
0083 
0084 JSValue *KJS::getDOMAbstractView(ExecState *exec, DOM::AbstractViewImpl *av)
0085 {
0086     return cacheDOMObject<DOM::AbstractViewImpl, DOMAbstractView>(exec, av);
0087 }
0088 
0089 DOM::AbstractViewImpl *KJS::toAbstractView(JSValue *val)
0090 {
0091     JSObject *obj = val->getObject();
0092     if (!obj) {
0093         return nullptr;
0094     }
0095 
0096     // the Window object is considered for all practical purposes as a descendant of AbstractView
0097     if (obj->inherits(&Window::info)) {
0098         return static_cast<const Window *>(obj)->toAbstractView();
0099     }
0100 
0101     if (obj->inherits(&DOMAbstractView::info)) {
0102         return static_cast<const DOMAbstractView *>(obj)->impl();
0103     }
0104 
0105     return nullptr;
0106 }