cef/libcef/renderer/dom_node_impl.cc

455 lines
10 KiB
C++
Raw Normal View History

// 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/blink_glue.h"
#include "libcef/renderer/browser_impl.h"
#include "libcef/renderer/dom_document_impl.h"
#include "libcef/renderer/thread_util.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_dom_event.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_form_control_element.h"
#include "third_party/blink/public/web/web_input_element.h"
#include "third_party/blink/public/web/web_node.h"
#include "third_party/blink/public/web/web_select_element.h"
using blink::WebDocument;
using blink::WebDOMEvent;
using blink::WebElement;
using blink::WebFormControlElement;
using blink::WebInputElement;
using blink::WebNode;
using blink::WebSelectElement;
using blink::WebString;
CefDOMNodeImpl::CefDOMNodeImpl(CefRefPtr<CefDOMDocumentImpl> document,
const blink::WebNode& node)
: document_(document), node_(node) {}
CefDOMNodeImpl::~CefDOMNodeImpl() {
CEF_REQUIRE_RT();
if (document_.get() && !node_.IsNull()) {
// Remove the node from the document.
document_->RemoveNode(node_);
}
}
CefDOMNodeImpl::Type CefDOMNodeImpl::GetType() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return DOM_NODE_TYPE_UNSUPPORTED;
2023-01-02 23:59:03 +01:00
}
return blink_glue::GetNodeType(node_);
}
bool CefDOMNodeImpl::IsText() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
return node_.IsTextNode();
}
bool CefDOMNodeImpl::IsElement() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
return node_.IsElementNode();
}
// Logic copied from RenderViewImpl::IsEditableNode.
bool CefDOMNodeImpl::IsEditable() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (node_.IsContentEditable()) {
return true;
2023-01-02 23:59:03 +01:00
}
if (node_.IsElementNode()) {
const WebElement& element = node_.To<WebElement>();
2023-01-02 23:59:03 +01:00
if (blink_glue::IsTextControlElement(element)) {
return true;
2023-01-02 23:59:03 +01:00
}
// Also return true if it has an ARIA role of 'textbox'.
for (unsigned i = 0; i < element.AttributeCount(); ++i) {
if (base::EqualsCaseInsensitiveASCII(element.AttributeLocalName(i).Utf8(),
"role")) {
if (base::EqualsCaseInsensitiveASCII(element.AttributeValue(i).Utf8(),
"textbox")) {
return true;
}
break;
}
}
}
return false;
}
bool CefDOMNodeImpl::IsFormControlElement() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
if (node_.IsElementNode()) {
const WebElement& element = node_.To<WebElement>();
return element.IsFormControlElement();
}
return false;
}
CefString CefDOMNodeImpl::GetFormControlElementType() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
if (node_.IsElementNode()) {
const WebElement& element = node_.To<WebElement>();
if (element.IsFormControlElement()) {
// Retrieve the type from the form control element.
const WebFormControlElement& formElement =
node_.To<WebFormControlElement>();
const std::u16string& form_control_type =
formElement.FormControlType().Utf16();
str = form_control_type;
}
}
return str;
}
bool CefDOMNodeImpl::IsSame(CefRefPtr<CefDOMNode> that) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
CefDOMNodeImpl* impl = static_cast<CefDOMNodeImpl*>(that.get());
2023-01-02 23:59:03 +01:00
if (!impl || !impl->VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
return node_.Equals(impl->node_);
}
CefString CefDOMNodeImpl::GetName() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebString& name = blink_glue::GetNodeName(node_);
2023-01-02 23:59:03 +01:00
if (!name.IsNull()) {
str = name.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefString CefDOMNodeImpl::GetValue() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
if (node_.IsElementNode()) {
const WebElement& element = node_.To<WebElement>();
if (element.IsFormControlElement()) {
// Retrieve the value from the form control element.
const WebFormControlElement& formElement =
node_.To<WebFormControlElement>();
std::u16string value;
const std::u16string& form_control_type =
formElement.FormControlType().Utf16();
if (form_control_type == u"text") {
const WebInputElement& input_element =
formElement.To<WebInputElement>();
value = input_element.Value().Utf16();
} else if (form_control_type == u"select-one") {
const WebSelectElement& select_element =
formElement.To<WebSelectElement>();
value = select_element.Value().Utf16();
}
base::TrimWhitespace(value, base::TRIM_LEADING, &value);
str = value;
}
}
if (str.empty()) {
const WebString& value = node_.NodeValue();
2023-01-02 23:59:03 +01:00
if (!value.IsNull()) {
str = value.Utf16();
2023-01-02 23:59:03 +01:00
}
}
return str;
}
bool CefDOMNodeImpl::SetValue(const CefString& value) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (node_.IsElementNode()) {
return false;
2023-01-02 23:59:03 +01:00
}
return blink_glue::SetNodeValue(node_,
WebString::FromUTF16(value.ToString16()));
}
CefString CefDOMNodeImpl::GetAsMarkup() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebString& markup = blink_glue::CreateNodeMarkup(node_);
2023-01-02 23:59:03 +01:00
if (!markup.IsNull()) {
str = markup.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefRefPtr<CefDOMDocument> CefDOMNodeImpl::GetDocument() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_.get();
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetParent() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_->GetOrCreateNode(node_.ParentNode());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetPreviousSibling() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_->GetOrCreateNode(node_.PreviousSibling());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetNextSibling() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_->GetOrCreateNode(node_.NextSibling());
}
bool CefDOMNodeImpl::HasChildren() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
return !node_.FirstChild().IsNull();
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetFirstChild() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_->GetOrCreateNode(node_.FirstChild());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetLastChild() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return document_->GetOrCreateNode(node_.LastChild());
}
CefString CefDOMNodeImpl::GetElementTagName() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return str;
}
const WebElement& element = node_.To<blink::WebElement>();
const WebString& tagname = element.TagName();
2023-01-02 23:59:03 +01:00
if (!tagname.IsNull()) {
str = tagname.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
bool CefDOMNodeImpl::HasElementAttributes() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return false;
}
const WebElement& element = node_.To<blink::WebElement>();
return (element.AttributeCount() > 0);
}
bool CefDOMNodeImpl::HasElementAttribute(const CefString& attrName) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return false;
}
const WebElement& element = node_.To<blink::WebElement>();
return element.HasAttribute(WebString::FromUTF16(attrName.ToString16()));
}
CefString CefDOMNodeImpl::GetElementAttribute(const CefString& attrName) {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return str;
}
const WebElement& element = node_.To<blink::WebElement>();
const WebString& attr =
element.GetAttribute(WebString::FromUTF16(attrName.ToString16()));
2023-01-02 23:59:03 +01:00
if (!attr.IsNull()) {
str = attr.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
void CefDOMNodeImpl::GetElementAttributes(AttributeMap& attrMap) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return;
}
const WebElement& element = node_.To<blink::WebElement>();
unsigned int len = element.AttributeCount();
2023-01-02 23:59:03 +01:00
if (len == 0) {
return;
2023-01-02 23:59:03 +01:00
}
for (unsigned int i = 0; i < len; ++i) {
std::u16string name = element.AttributeLocalName(i).Utf16();
std::u16string value = element.AttributeValue(i).Utf16();
attrMap.insert(std::make_pair(name, value));
}
}
bool CefDOMNodeImpl::SetElementAttribute(const CefString& attrName,
const CefString& value) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return false;
}
WebElement element = node_.To<blink::WebElement>();
element.SetAttribute(WebString::FromUTF16(attrName.ToString16()),
WebString::FromUTF16(value.ToString16()));
return true;
}
CefString CefDOMNodeImpl::GetElementInnerText() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return str;
}
WebElement element = node_.To<blink::WebElement>();
const WebString& text = element.TextContent();
2023-01-02 23:59:03 +01:00
if (!text.IsNull()) {
str = text.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefRect CefDOMNodeImpl::GetElementBounds() {
CefRect rect;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return rect;
2023-01-02 23:59:03 +01:00
}
if (!node_.IsElementNode()) {
DCHECK(false);
return rect;
}
WebElement element = node_.To<blink::WebElement>();
const auto& rc = element.BoundsInWidget();
rect.Set(rc.x(), rc.y(), rc.width(), rc.height());
return rect;
}
void CefDOMNodeImpl::Detach() {
document_ = nullptr;
node_.Assign(WebNode());
}
bool CefDOMNodeImpl::VerifyContext() {
if (!document_.get()) {
DCHECK(false);
return false;
}
2023-01-02 23:59:03 +01:00
if (!document_->VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
if (node_.IsNull()) {
DCHECK(false);
return false;
}
return true;
}