2012-04-27 23:19:06 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/renderer/dom_node_impl.h"
|
|
|
|
#include "libcef/common/tracker.h"
|
|
|
|
#include "libcef/renderer/browser_impl.h"
|
|
|
|
#include "libcef/renderer/dom_document_impl.h"
|
|
|
|
#include "libcef/renderer/thread_util.h"
|
2013-10-29 18:53:18 +01:00
|
|
|
#include "libcef/renderer/webkit_glue.h"
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebString.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDOMEvent.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebFrame.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebFormControlElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebInputElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebNode.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebSelectElement.h"
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebDocument;
|
|
|
|
using blink::WebDOMEvent;
|
|
|
|
using blink::WebElement;
|
|
|
|
using blink::WebFrame;
|
|
|
|
using blink::WebFormControlElement;
|
|
|
|
using blink::WebInputElement;
|
|
|
|
using blink::WebNode;
|
|
|
|
using blink::WebSelectElement;
|
|
|
|
using blink::WebString;
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
CefDOMNodeImpl::CefDOMNodeImpl(CefRefPtr<CefDOMDocumentImpl> document,
|
2013-11-08 22:28:56 +01:00
|
|
|
const blink::WebNode& node)
|
2012-04-27 23:19:06 +02:00
|
|
|
: document_(document),
|
|
|
|
node_(node) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefDOMNodeImpl::~CefDOMNodeImpl() {
|
|
|
|
CEF_REQUIRE_RT();
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (document_.get() && !node_.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
// Remove the node from the document.
|
|
|
|
document_->RemoveNode(node_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefDOMNodeImpl::Type CefDOMNodeImpl::GetType() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return DOM_NODE_TYPE_UNSUPPORTED;
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
return webkit_glue::GetNodeType(node_);
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::IsText() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return node_.IsTextNode();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::IsElement() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return node_.IsElementNode();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 17:52:49 +02:00
|
|
|
// Logic copied from RenderViewImpl::IsEditableNode.
|
|
|
|
bool CefDOMNodeImpl::IsEditable() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsContentEditable())
|
2012-06-11 17:52:49 +02:00
|
|
|
return true;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsElementNode()) {
|
|
|
|
const WebElement& element = node_.ToConst<WebElement>();
|
2016-11-23 21:54:29 +01:00
|
|
|
if (webkit_glue::IsTextControlElement(element))
|
2012-06-11 17:52:49 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Also return true if it has an ARIA role of 'textbox'.
|
2017-04-20 21:28:17 +02:00
|
|
|
for (unsigned i = 0; i < element.AttributeCount(); ++i) {
|
|
|
|
if (base::LowerCaseEqualsASCII(element.AttributeLocalName(i).Utf8(),
|
2015-08-14 16:41:08 +02:00
|
|
|
"role")) {
|
2017-04-20 21:28:17 +02:00
|
|
|
if (base::LowerCaseEqualsASCII(element.AttributeValue(i).Utf8(),
|
2015-08-14 16:41:08 +02:00
|
|
|
"textbox")) {
|
2012-06-11 17:52:49 +02:00
|
|
|
return true;
|
2015-08-14 16:41:08 +02:00
|
|
|
}
|
2012-06-11 17:52:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-27 23:19:06 +02:00
|
|
|
bool CefDOMNodeImpl::IsFormControlElement() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsElementNode()) {
|
|
|
|
const WebElement& element = node_.ToConst<WebElement>();
|
|
|
|
return element.IsFormControlElement();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetFormControlElementType() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsElementNode()) {
|
|
|
|
const WebElement& element = node_.ToConst<WebElement>();
|
|
|
|
if (element.IsFormControlElement()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
// Retrieve the type from the form control element.
|
|
|
|
const WebFormControlElement& formElement =
|
2017-04-20 21:28:17 +02:00
|
|
|
node_.ToConst<WebFormControlElement>();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-03-03 23:37:23 +01:00
|
|
|
const base::string16& form_control_type =
|
2017-04-20 21:28:17 +02:00
|
|
|
formElement.FormControlType().Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
str = form_control_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::IsSame(CefRefPtr<CefDOMNode> that) {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CefDOMNodeImpl* impl = static_cast<CefDOMNodeImpl*>(that.get());
|
|
|
|
if (!impl || !impl->VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return node_.Equals(impl->node_);
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetName() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
const WebString& name = webkit_glue::GetNodeName(node_);
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!name.IsNull())
|
|
|
|
str = name.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetValue() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsElementNode()) {
|
|
|
|
const WebElement& element = node_.ToConst<WebElement>();
|
|
|
|
if (element.IsFormControlElement()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
// Retrieve the value from the form control element.
|
|
|
|
const WebFormControlElement& formElement =
|
2017-04-20 21:28:17 +02:00
|
|
|
node_.ToConst<WebFormControlElement>();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16 value;
|
2017-03-03 23:37:23 +01:00
|
|
|
const base::string16& form_control_type =
|
2017-04-20 21:28:17 +02:00
|
|
|
formElement.FormControlType().Utf16();
|
2014-01-02 23:41:11 +01:00
|
|
|
if (form_control_type == base::ASCIIToUTF16("text")) {
|
2012-04-27 23:19:06 +02:00
|
|
|
const WebInputElement& input_element =
|
2017-04-20 21:28:17 +02:00
|
|
|
formElement.ToConst<WebInputElement>();
|
|
|
|
value = input_element.Value().Utf16();
|
2014-01-02 23:41:11 +01:00
|
|
|
} else if (form_control_type == base::ASCIIToUTF16("select-one")) {
|
2012-04-27 23:19:06 +02:00
|
|
|
const WebSelectElement& select_element =
|
2017-04-20 21:28:17 +02:00
|
|
|
formElement.ToConst<WebSelectElement>();
|
|
|
|
value = select_element.Value().Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-04 18:50:38 +02:00
|
|
|
base::TrimWhitespace(value, base::TRIM_LEADING, &value);
|
2012-04-27 23:19:06 +02:00
|
|
|
str = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str.empty()) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebString& value = node_.NodeValue();
|
|
|
|
if (!value.IsNull())
|
|
|
|
str = value.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::SetValue(const CefString& value) {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsElementNode())
|
2012-04-27 23:19:06 +02:00
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return webkit_glue::SetNodeValue(node_, WebString::FromUTF16(value));
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetAsMarkup() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
const WebString& markup = webkit_glue::CreateNodeMarkup(node_);
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!markup.IsNull())
|
|
|
|
str = markup.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMDocument> CefDOMNodeImpl::GetDocument() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return document_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetParent() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return document_->GetOrCreateNode(node_.ParentNode());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetPreviousSibling() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return document_->GetOrCreateNode(node_.PreviousSibling());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetNextSibling() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return document_->GetOrCreateNode(node_.NextSibling());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::HasChildren() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return !node_.FirstChild().IsNull();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetFirstChild() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return document_->GetOrCreateNode(node_.FirstChild());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetLastChild() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return document_->GetOrCreateNode(node_.LastChild());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetElementTagName() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebElement& element = node_.ToConst<blink::WebElement>();
|
|
|
|
const WebString& tagname = element.TagName();
|
|
|
|
if (!tagname.IsNull())
|
|
|
|
str = tagname.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::HasElementAttributes() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebElement& element = node_.ToConst<blink::WebElement>();
|
|
|
|
return (element.AttributeCount() > 0);
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::HasElementAttribute(const CefString& attrName) {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebElement& element = node_.ToConst<blink::WebElement>();
|
|
|
|
return element.HasAttribute(WebString::FromUTF16(attrName));
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetElementAttribute(const CefString& attrName) {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebElement& element = node_.ToConst<blink::WebElement>();
|
|
|
|
const WebString& attr = element.GetAttribute(WebString::FromUTF16(attrName));
|
|
|
|
if (!attr.IsNull())
|
|
|
|
str = attr.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDOMNodeImpl::GetElementAttributes(AttributeMap& attrMap) {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebElement& element = node_.ToConst<blink::WebElement>();
|
|
|
|
unsigned int len = element.AttributeCount();
|
2012-04-27 23:19:06 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < len; ++i) {
|
2017-04-20 21:28:17 +02:00
|
|
|
base::string16 name = element.AttributeLocalName(i).Utf16();
|
|
|
|
base::string16 value = element.AttributeValue(i).Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
attrMap.insert(std::make_pair(name, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::SetElementAttribute(const CefString& attrName,
|
|
|
|
const CefString& value) {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
WebElement element = node_.To<blink::WebElement>();
|
|
|
|
element.SetAttribute(WebString::FromUTF16(attrName),
|
|
|
|
WebString::FromUTF16(value));
|
2016-05-25 01:35:43 +02:00
|
|
|
return true;
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMNodeImpl::GetElementInnerText() {
|
|
|
|
CefString str;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return str;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
WebElement element = node_.To<blink::WebElement>();
|
|
|
|
const WebString& text = element.TextContent();
|
|
|
|
if (!text.IsNull())
|
|
|
|
str = text.Utf16();
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-09-09 20:02:57 +02:00
|
|
|
CefRect CefDOMNodeImpl::GetElementBounds() {
|
|
|
|
CefRect rect;
|
|
|
|
if (!VerifyContext())
|
|
|
|
return rect;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!node_.IsElementNode()) {
|
2016-09-09 20:02:57 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
WebElement element = node_.To<blink::WebElement>();
|
|
|
|
blink::WebRect rc = element.BoundsInViewport();
|
2016-09-09 20:02:57 +02:00
|
|
|
rect.Set(rc.x, rc.y, rc.width, rc.height);
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2012-04-27 23:19:06 +02:00
|
|
|
void CefDOMNodeImpl::Detach() {
|
|
|
|
document_ = NULL;
|
2017-04-20 21:28:17 +02:00
|
|
|
node_.Assign(WebNode());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMNodeImpl::VerifyContext() {
|
|
|
|
if (!document_.get()) {
|
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!document_->VerifyContext())
|
|
|
|
return false;
|
2017-04-20 21:28:17 +02:00
|
|
|
if (node_.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|