FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
ActiveXCore/AXDOM/Document.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Sep 21, 2010
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2010 Facebook, Inc and the Firebreath development team
13 \**********************************************************/
14 
15 #include "IDispatchAPI.h"
16 #include "DOM/Window.h"
17 #include "DOM/Element.h"
18 
19 #include "precompiled_headers.h" // On windows, everything above this line in PCH
20 #include "Document.h"
21 
22 using namespace FB::ActiveX::AXDOM;
23 
24 Document::Document(const FB::JSObjectPtr& obj, IWebBrowser2 *web)
25  : FB::ActiveX::AXDOM::Element(obj, web), FB::ActiveX::AXDOM::Node(obj, web), FB::DOM::Node(obj), FB::DOM::Element(obj),
26  m_htmlDoc(FB::ptr_cast<IDispatchAPI>(obj)->getIDispatch()), m_webBrowser(web), FB::DOM::Document(obj)
27 {
28 }
29 
30 Document::~Document()
31 {
32 }
33 
35 {
36  CComQIPtr<IHTMLWindow2> htmlWin;
37  m_htmlDoc->get_parentWindow(&htmlWin);
38  CComQIPtr<IDispatch> windowDisp(htmlWin);
39  FB::JSObjectPtr api(IDispatchAPI::create(htmlWin, FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())));
40 
41  return FB::DOM::Window::create(api);
42 }
43 
44 std::vector<FB::DOM::ElementPtr> Document::getElementsByTagName(const std::string& tagName) const
45 {
46  CComQIPtr<IHTMLDocument3> doc(m_htmlDoc);
47  CComPtr<IHTMLElementCollection> list;
48  std::vector<FB::DOM::ElementPtr> tagList;
49  CComBSTR tName(FB::utf8_to_wstring(tagName).c_str());
50  if (doc) {
51  doc->getElementsByTagName(tName, &list);
52  } else {
53  throw std::runtime_error("Could not get element by tag name");
54  }
55  long length(0);
56  if (SUCCEEDED(list->get_length(&length))) {
57  for (long i = 0; i < length; i++) {
58  CComPtr<IDispatch> dispObj;
59  CComVariant idx(i);
60  list->item(idx, idx, &dispObj);
61  FB::JSObjectPtr obj(IDispatchAPI::create(dispObj, FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())));
62  tagList.push_back(FB::DOM::Element::create(obj));
63  }
64  }
65  return tagList;
66 }
67 
68 
69 FB::DOM::ElementPtr Document::getElementById(const std::string& elem_id) const
70 {
71  CComQIPtr<IHTMLDocument3> doc3(m_htmlDoc);
72  if (!doc3) {
73  throw std::exception("Document does not support getElementById");
74  }
75  CComPtr<IHTMLElement> el(NULL);
76  doc3->getElementById(CComBSTR(FB::utf8_to_wstring(elem_id).c_str()), &el);
77  CComQIPtr<IDispatch> disp(el);
78  FB::JSObjectPtr ptr(IDispatchAPI::create(disp, FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())));
79  return FB::DOM::Element::create(ptr);
80 }
81 
82 FB::DOM::ElementPtr Document::createElement(const std::string &name) const
83 {
84  CComPtr<IHTMLElement> el(NULL);
85  HRESULT hr = m_htmlDoc->createElement(CComBSTR(FB::utf8_to_wstring(name).c_str()), &el);
86  if (FAILED(hr)) {
87  throw std::runtime_error("Failed to create element!");
88  }
89  CComQIPtr<IDispatch> disp(el);
90  FB::JSObjectPtr ptr(IDispatchAPI::create(disp, FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())));
91  return FB::DOM::Element::create(ptr);
92 }
ActiveX specific implementation of DOM::Document.
boost::shared_ptr< FB::JSObject > JSObjectPtr
Defines an alias representing a JSObject shared_ptr (you should never use a JSObject* directly) ...
Definition: APITypes.h:109
virtual std::vector< ElementPtr > getElementsByTagName(const std::string &tagName) const
Gets a list of all elements in the document with the specified tag name.
virtual ElementPtr getElementById(const std::string &elem_id) const
Gets an element from the DOM with the specified id.
boost::shared_ptr< T > ptr_cast(boost::shared_ptr< U > const &r)
Convenience function for doing a dynamic cast of one boost::shared_ptr to another.
Definition: APITypes.h:339
boost::shared_ptr< Window > WindowPtr
shared_ptr for a FB::DOM::Window
virtual WindowPtr getWindow() const
Gets a DOM::WindowPtr for the window the document is contained in.
Provides an ActiveX specific implementation of DOM::Node.
static WindowPtr create(const FB::JSObjectPtr &api)
Creates a FB::DOM::Window object from a JSObjectPtr representing a DOM object. This will probably thr...
virtual FB::JSObjectPtr getJSObject() const
Returns the FB::JSObjectPtr that this object is wrapping.
static ElementPtr create(const FB::JSObjectPtr &api)
Creates a FB::DOM::Element object from a JSObjectPtr representing a DOM object. This will probably th...
boost::shared_ptr< Element > ElementPtr
shared_ptr for a FB::DOM::Element
ActiveX specific implementation of DOM::Element.
virtual ElementPtr createElement(const std::string &name) const
roughly equivalent to calling document.createElement(name) from javascript
std::wstring utf8_to_wstring(const std::string &src)
Accepts a UTF8-encoded std::string and returns a std::wstring.
Definition: utf8_tools.cpp:50