FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
ActiveXCore/AXDOM/Element.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Sep 21, 2009
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 "precompiled_headers.h" // On windows, everything above this line in PCH
16 #include "Element.h"
17 
18 using namespace FB::ActiveX::AXDOM;
19 
20 Element::Element(const FB::JSObjectPtr& element, IWebBrowser *web)
21  : FB::ActiveX::AXDOM::Node(element, web), FB::DOM::Node(element), FB::DOM::Element(element),
22  m_axDisp(FB::ptr_cast<IDispatchAPI>(element)->getIDispatch()), m_webBrowser(web)
23 {
24  if (!m_axDisp)
25  throw std::bad_cast();
26 }
27 
28 Element::~Element()
29 {
30 }
31 
32 std::vector<FB::DOM::ElementPtr> Element::getElementsByTagName(const std::string& tagName) const
33 {
34  CComQIPtr<IHTMLElement2> elem(m_axDisp);
35  CComQIPtr<IHTMLDocument3> doc(m_axDisp);
36  CComPtr<IHTMLElementCollection> list;
37  std::vector<FB::DOM::ElementPtr> tagList;
38  CComBSTR tName(FB::utf8_to_wstring(tagName).c_str());
39  if (elem) {
40  elem->getElementsByTagName(tName, &list);
41  } else if (doc) {
42  doc->getElementsByTagName(tName, &list);
43  } else {
44  throw std::runtime_error("Could not get element by tag name");
45  }
46  long length(0);
47  if (SUCCEEDED(list->get_length(&length))) {
48  for (long i = 0; i < length; i++) {
49  CComPtr<IDispatch> dispObj;
50  CComVariant idx(i);
51  list->item(idx, idx, &dispObj);
52  FB::JSObjectPtr obj(IDispatchAPI::create(dispObj, FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())));
53  tagList.push_back(FB::DOM::Element::create(obj));
54  }
55  }
56  return tagList;
57 }
58 
59 std::string FB::ActiveX::AXDOM::Element::getStringAttribute( const std::string& attr ) const
60 {
61  CComQIPtr<IHTMLElement> elem(m_axDisp);
62  CComQIPtr<IHTMLDocument5> doc(m_axDisp);
63  CComVariant var;
64  HRESULT hr = S_OK;
65  if (elem) {
66  hr = elem->getAttribute(CComBSTR(FB::utf8_to_wstring(attr).c_str()), 0, &var);
67  return FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())->getVariant(&var).convert_cast<std::string>();
68  } else {
69  return getProperty<std::string>(attr);
70  }
71 }
72 
74 {
75  CComBSTR htmlStr;
76  CComQIPtr<IHTMLElement> elem(m_axDisp);
77  HRESULT hr = elem->get_innerHTML(&htmlStr);
78  if (SUCCEEDED(hr)) {
79  CComVariant var(htmlStr);
80  return FB::ptr_cast<ActiveXBrowserHost>(getJSObject()->getHost())->getVariant(&var).convert_cast<std::string>();
81  } else {
82  throw FB::script_error("Could not get innerhtml");
83  }
84 }
85 
86 void FB::ActiveX::AXDOM::Element::setInnerHTML( const std::string& html ) const
87 {
88  CComBSTR newHtml(html.c_str());
89  CComQIPtr<IHTMLElement> elem(m_axDisp);
90  HRESULT hr = elem->put_innerHTML(newHtml);
91  if (!SUCCEEDED(hr)) {
92  throw FB::script_error("Could not set innerHtml");
93  }
94 }
95 
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.
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
virtual void setInnerHTML(const std::string &html) const
Sets an inner html for the element.
Exception type; when thrown in a JSAPI method, a javascript exception will be thrown.
Definition: JSExceptions.h:28
Provides an ActiveX specific implementation of DOM::Node.
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...
virtual std::string getInnerHTML() const
Gets the inner html for the element.
ActiveX specific implementation of DOM::Element.
Provides a BrowserHost implementation for ActiveX.
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