Skip to content

CefDOMNode

Header: cef_dom.h
Interfaces: CefDOMVisitor, CefDOMDocument, CefDOMNode
Category: Core APIs

Overview

Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.

CefDOMVisitor

Source File: include/cef_dom.h

Process / Thread Context: Render Process / Main Thread (per class comment: "The methods of this class will be called on the render process main thread.")

Purpose: USER-IMPLEMENTED visitor interface. The user implements Visit() and passes the visitor to CefFrame::VisitDOM() (per standard CEF integration). The framework invokes the visitor with a CefDOMDocument snapshot. DOM objects are only valid for the scope of Visit().

Methods

void Visit(CefRefPtr<CefDOMDocument> document)

Parameters:

  • document: A snapshot of the DOM at the time Visit() is executed. Must not be referenced outside the scope of this method.

Return Value: None (void). Side-effect-driven callback.

Usage Instruction: Implement this method on a subclass of CefDOMVisitor, perform any DOM traversal or inspection using document inside the body, then return. Do NOT keep references to DOM objects past method scope.

Threading Constraint: Called on the render process main thread.

Usage Example

cpp
class MyDOMVisitor : public CefDOMVisitor {
 public:
  void Visit(CefRefPtr<CefDOMDocument> document) override {
    if (document->HasSelection()) {
      CefString text = document->GetSelectionAsText();
      // ... use text within scope only ...
    }
  }
};

// Trigger via CefFrame (render process side):
CefRefPtr<CefDOMVisitor> visitor = new MyDOMVisitor();
frame->VisitDOM(visitor);  // CefFrame::VisitDOM invokes visitor->Visit(...)

CefDOMDocument

Source File: include/cef_dom.h

Process / Thread Context: Render Process / Main Thread (per class comment: "The methods of this class should only be called on the render process main thread thread.")

Purpose: USER-CALLED library interface that represents a DOM document snapshot, exposing its root/body/head nodes, title, current selection, and URL helpers. Obtained only via CefDOMVisitor::Visit().

Methods

CefRefPtr<CefDOMNode> GetDocument()

Parameters:

  • None.

Return Value: The root document node (the #document node itself, not html).

Usage Instruction: Entry point for full-tree traversal.

Threading Constraint: Render process main thread.

CefRefPtr<CefDOMNode> GetBody()

Parameters:

  • None.

Return Value: The BODY node of an HTML document (may be NULL for non-HTML).

Usage Instruction: Access document body content.

Threading Constraint: Render process main thread.

CefRefPtr<CefDOMNode> GetHead()

Parameters:

  • None.

Return Value: The HEAD node of an HTML document.

Usage Instruction: Read/inspect metadata nodes in <head>.

Threading Constraint: Render process main thread.

CefString GetTitle()

Parameters:

  • None.

Return Value: Document title (contents of <title>).

Usage Instruction: Display or log the current page title.

Threading Constraint: Render process main thread.

CefRefPtr<CefDOMNode> GetElementById(const CefString& id)

Parameters:

  • id: The element id attribute to look up.

Return Value: Matching element node, or NULL if not found.

Usage Instruction: Equivalent of document.getElementById() — direct node lookup by id.

Threading Constraint: Render process main thread.

CefRefPtr<CefDOMNode> GetFocusedNode()

Parameters:

  • None.

Return Value: The node currently holding keyboard focus, or NULL.

Usage Instruction: Read form input state, accessibility inspection.

Threading Constraint: Render process main thread.

bool HasSelection()

Parameters:

  • None.

Return Value: True if a portion of the document is currently selected.

Usage Instruction: Gate selection retrieval calls.

Threading Constraint: Render process main thread.

int GetSelectionStartOffset()** / **int GetSelectionEndOffset()

Parameters:

  • None.

Return Value: Character offsets within the start/end selection nodes.

Usage Instruction: Determine exact selection bounds.

Threading Constraint: Render process main thread.

CefString GetSelectionAsMarkup()

Parameters:

  • None.

Return Value: Selection rendered as HTML markup.

Usage Instruction: Capture rich-text selection.

Threading Constraint: Render process main thread.

CefString GetSelectionAsText()

Parameters:

  • None.

Return Value: Selection rendered as plain text.

Usage Instruction: Capture plain-text selection.

Threading Constraint: Render process main thread.

CefString GetBaseURL()

Parameters:

  • None.

Return Value: Document base URL (from <base> or document URL).

Usage Instruction: Use to resolve relative URLs from the document.

Threading Constraint: Render process main thread.

CefString GetCompleteURL(const CefString& partialURL)

Parameters:

  • partialURL: A possibly-relative URL.

Return Value: Fully-qualified URL resolved against the document base URL.

Usage Instruction: Use when resolving attribute URLs (e.g., href/src values).

Threading Constraint: Render process main thread.

Usage Example

cpp
class TitleReader : public CefDOMVisitor {
 public:
  void Visit(CefRefPtr<CefDOMDocument> document) override {
    // Inspect document.
    title_ = document->GetTitle();
    CefRefPtr<CefDOMNode> body = document->GetBody();
    if (body && body->HasChildren()) {
      CefRefPtr<CefDOMNode> child = body->GetFirstChild();
      // ... walk subtree ...
    }
    // Resolve a relative URL:
    CefString abs = document->GetCompleteURL("images/logo.png");
  }
  CefString title_;
};

CefDOMNode

Source File: include/cef_dom.h

Process / Thread Context: Render Process / Main Thread (per class comment: "The methods of this class should only be called on the render process main thread.")

Purpose: USER-CALLED library interface representing a single DOM node. Provides node type/identity queries, tree navigation (parent, siblings, children), name/value accessors, and element-specific attribute/bounds accessors.

Methods

Type GetType()

Return Value: Returns the node type. Defaults to DOM_NODE_TYPE_UNSUPPORTED.

Threading Constraint: Threading constraint not specified in source.

bool IsText()

Return Value: True if text node.

Threading Constraint: Threading constraint not specified in source.

bool IsElement()

Return Value: True if element node.

Threading Constraint: Threading constraint not specified in source.

bool IsEditable()

Return Value: True if editable.

Threading Constraint: Threading constraint not specified in source.

bool IsFormControlElement()

Return Value: True if a form control element node. Default false.

Threading Constraint: Threading constraint not specified in source.

FormControlType GetFormControlElementType()

Return Value: Returns form control element type. Defaults to DOM_FORM_CONTROL_TYPE_UNSUPPORTED.

Threading Constraint: Threading constraint not specified in source.

bool IsSame(CefRefPtr<CefDOMNode> that)

Return Value: True if both objects reference the same underlying DOM handle.

Threading Constraint: Threading constraint not specified in source.

CefString GetName()

Return Value: Node name (tag name for elements, #text for text nodes, etc.).

Threading Constraint: Threading constraint not specified in source.

CefString GetValue()

Return Value: Node value (text content for text/comment nodes; varies).

Threading Constraint: Threading constraint not specified in source.

bool SetValue(const CefString& value)

Return Value: Sets node value. Returns true on success.

Threading Constraint: Threading constraint not specified in source.

CefString GetAsMarkup()

Return Value: Returns node contents serialized as markup.

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefDOMDocument> GetDocument()

Return Value: Returns owning document.

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefDOMNode> GetParent()

Return Value: Parent node (NULL at root).

Threading Constraint: Threading constraint not specified in source.

bool HasChildren()

Return Value: True if the node has child nodes.

Threading Constraint: Threading constraint not specified in source.

CefString GetElementTagName()

Return Value: Tag name (e.g., "div", "img").

Threading Constraint: Threading constraint not specified in source.

bool HasElementAttributes()

Return Value: True if element has any attributes.

Threading Constraint: Threading constraint not specified in source.

bool HasElementAttribute(const CefString& attrName)

Return Value: True if the named attribute exists.

Threading Constraint: Threading constraint not specified in source.

CefString GetElementAttribute(const CefString& attrName)

Return Value: Attribute value, or empty if absent.

Threading Constraint: Threading constraint not specified in source.

void GetElementAttributes(AttributeMap& attrMap)

Return Value: Populates attrMap with all attributes.

Threading Constraint: Threading constraint not specified in source.

bool SetElementAttribute(const CefString& attrName, const CefString& value)

Return Value: Sets/updates an attribute. Returns true on success.

Threading Constraint: Threading constraint not specified in source.

CefString GetElementInnerText()

Return Value: Inner text content of element.

Threading Constraint: Threading constraint not specified in source.

CefRect GetElementBounds()

Return Value: Element bounds in device pixels. Use window.devicePixelRatio to convert to/from CSS pixels.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
void WalkTree(CefRefPtr<CefDOMNode> node, int depth = 0) {
  if (!node) return;
  if (node->IsElement()) {
    CefString tag = node->GetElementTagName();
    if (node->HasElementAttribute("href")) {
      CefString href = node->GetElementAttribute("href");
      // ...
    }
    CefRect bounds = node->GetElementBounds();
  }
  if (node->HasChildren()) {
    CefRefPtr<CefDOMNode> child = node->GetFirstChild();
    while (child) {
      WalkTree(child, depth + 1);
      child = child->GetNextSibling();
    }
  }
}

// Inside a CefDOMVisitor::Visit():
CefRefPtr<CefDOMNode> root = document->GetDocument();
WalkTree(root);

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.