Document
Overview
Document is a class in Aspose.Html FOSS for Python.
Inherits from: Node.
The root of a DOM tree.
This class provides 66 methods for working with Document objects in Python programs.
Available methods include: __init__, add_event_listener, adopt_node, append, append_child, attach_style_sheet, clone_node, close, compare_document_position, contains, create_attribute, create_attribute_ns, and 54 additional methods.
All public members are accessible to any Python application after installing the Aspose.Html FOSS for Python package.
Properties: a_link_color, active_element, adopted_style_sheets, all, anchors, applets, and 67 more.
Properties
| Name | Type | Access | Description |
|---|---|---|---|
default_view | `` | Read | Per-Document Window object (lazy, cached). |
location | `` | Read | Return the associated Window’s Location object, or None. |
implementation | "DOMImplementation" | Read | Per-Document DOMImplementation object (lazy, cached). |
dom_config | DOMConfiguration | Read | Per-Document DOMConfiguration object (lazy, cached). |
url | str | Read | Document URL (WHATWG Document.URL), default "about:blank". |
document_uri | str | Read | The document’s URL as a string (WHATWG DOM §4.5 documentURI). |
base_uri | str | Read | Resolved base URL for this document (WHATWG DOM Node.baseURI). |
referrer | str | Read | Referring document URL (WHATWG HTML Standard §document). |
domain | str | Read | Hostname component of the document URL (WHATWG HTML Standard §document). |
last_modified | str | Read | Last-modified timestamp string (WHATWG HTML Standard §document). |
cookie | str | Read/Write | Read-only cookie string stub — always returns "" (WHATWG HTML §8.11.1). |
hidden | bool | Read | Whether the document is hidden — always False in headless mode. |
visibility_state | str | Read | Document visibility state — always "visible" in headless mode. |
design_mode | str | Read/Write | Whether the document is in design mode (WHATWG HTML §7.6.4). |
dir | str | Read/Write | Document text direction (WHATWG HTML §3.3.2). |
current_script | None | Read | The currently executing script element (WHATWG HTML §8.1.3.4). |
character_set | str | Read | Character encoding used during parsing (WHATWG DOM §3.1). |
charset | str | Read | Alias for :attr:character_set (HTML Standard name). |
input_encoding | str | Read | Alias for :attr:character_set (WHATWG DOM compatibility name). |
content_type | str | Read | Document MIME type; always "text/html" for HTML documents. |
style_sheets | "StyleSheetList" | Read | Live document stylesheet list. |
adopted_style_sheets | list["CSSStyleSheet"] | Read/Write | Document-level constructable stylesheet adoption list. |
node_name | str | Read | '#document' |
document_element | Element | None | Read | The root Element child, or None if not yet set. |
document_type | DocumentType | None | Read | The DocumentType child, or None. |
compat_mode | str | Read/Write | 'BackCompat' (quirks) or 'CSS1Compat' (no-quirks). |
ready_state | str | Read | Document loading state. |
parse_errors | list[ParseError] | Read | Parse errors from the last parse operation. |
head | Element | None | Read | The <head> element child of document_element, or None. |
body | Element | None | Read | The <body> element child of document_element, or None. |
forms | "HTMLCollection" | Read | Live collection of all <form> elements in the document. |
images | "HTMLCollection" | Read | Live collection of all <img> elements in the document. |
links | "HTMLCollection" | Read | Live collection of <a href> and <area href> elements. |
scripts | "HTMLCollection" | Read | Live collection of all <script> elements in the document. |
embeds | "HTMLCollection" | Read | Live collection of all <embed> descendants. |
applets | "HTMLCollection" | Read | Live collection of all <applet> descendants. |
anchors | "HTMLCollection" | Read | Live collection of <a name=...> elements (legacy named anchors). |
title | str | Read/Write | The text content of the first <title> element in <head>. |
compatible_mode | str | Read | Quirks-mode indicator (WHATWG HTML §3.2.1). |
strict_error_checking | bool | Read/Write | Always True in headless mode (DOM3 Core §1.1.1). |
first_element_child | "Element | None" | Read | First direct child that is an Element, or None. |
last_element_child | "Element | None" | Read | Last direct child that is an Element, or None. |
child_element_count | int | Read | Number of direct children that are Element nodes. |
active_element | "Element | None" | Read | The currently focused element, always None in headless mode. |
full_screen_enabled | bool | Read | Always False — no display in headless mode (Fullscreen API §7). |
fullscreen_element | None | Read | Always None — no fullscreen element in headless mode (Fullscreen API §7). |
pointer_lock_element | None | Read | Always None — no pointer lock in headless mode (Pointer Lock API §4). |
timeline | "_DocumentTimeline" | Read | Document timeline stub; current_time is always 0.0 (Web Animations API §6.2). |
all | "_AllCollection" | Read | All elements in tree order (WHATWG HTML §legacy document layout interface). |
plugins | "HTMLCollection" | Read | Alias for :attr:embeds per WHATWG HTML §4.1 legacy interface. |
children | "HTMLCollection" | Read | Direct element children of the document (WHATWG DOM §4.2.6 ParentNode). |
origin | str | Read | Opaque origin string — always 'null' in headless mode (WHATWG DOM §4.5). |
fg_color | str | Read/Write | Reflects obsolete fgColor document-level attribute via <body> text attribute. |
bg_color | str | Read/Write | Reflects obsolete bgColor document-level attribute via <body> bgcolor attribute. |
link_color | str | Read/Write | Reflects obsolete linkColor document-level attribute via <body> link attribute. |
v_link_color | str | Read/Write | Reflects obsolete vlinkColor document-level attribute via <body> vlink attribute. |
a_link_color | str | Read/Write | Reflects obsolete alinkColor document-level attribute via <body> alink attribute. |
fonts | `` | Read | Always None in headless mode. |
picture_in_picture_enabled | bool | Read | Always False in headless mode. |
picture_in_picture_element | `` | Read | Always None in headless mode. |
prerendering | bool | Read | Always False in headless mode. |
node_type | int | Read | Integer node type constant from NodeType. |
node_value | str | None | Read/Write | Character data for CharacterData nodes; None otherwise. |
text_content | str | None | Read/Write | The text content of this node, or None for Document and DocumentType. |
parent_node | Node | None | Read | The parent node, or None if this node has no parent. |
parent_element | Element | None | Read | The parent node if it is an Element; None otherwise. |
is_connected | bool | Read | Whether this node is part of a document’s node tree (WHATWG DOM §4.4). |
owner_document | Document | None | Read | The Document this node belongs to, or None for Document itself. |
child_nodes | NodeList | Read | A live NodeList of all child nodes. |
first_child | Node | None | Read | The first child node, or None. |
last_child | Node | None | Read | The last child node, or None. |
previous_sibling | Node | None | Read | The preceding sibling node, or None. |
next_sibling | Node | None | Read | The following sibling node, or None. |
Methods
| Signature | Description |
|---|---|
__init__() | Calls init on this Document instance. |
normalize_document() | Normalize this document using existing Node.normalize semantics. |
has_focus() → bool | Return whether the document has focus — always False in headless mode. |
open(type: str, replace: str) → "Document" | Open the document for writing (WHATWG HTML §7.7). |
close() | Close the document after writing (WHATWG HTML §7.7). |
write() | Write a string to the document stream (WHATWG HTML §7.7). |
writeln() | Write a string followed by a newline (WHATWG HTML §7.7). |
attach_style_sheet(sheet: "CSSStyleSheet") | Attach a stylesheet to this document. |
detach_style_sheet(sheet: "CSSStyleSheet") | Detach a previously attached stylesheet. |
save(path: "str | pathlib.Path", encoding: str) | Serialise the document and write it to a file. |
create_element(tag_name: str) → "HTMLElement" | Create an HTMLElement owned by this document. |
create_element_ns(namespace_uri: str, qualified_name: str) → Element | Create an Element in a specific namespace, owned by this document. |
create_text_node(data: str) → Text | Create a Text node with the given character data. |
create_comment(data: str) → Comment | Create a Comment node with the given character data. |
create_cdata_section(data: str) → "CDATASection" | Create a CDATASection node owned by this document. |
create_document_fragment() → DocumentFragment | Create an empty DocumentFragment owned by this document. |
create_event(interface: str) → "Event" | Create a legacy event object for interface. |
create_range() → "Range" | Create a new Range collapsed at this document. |
get_selection() → "Selection" | Return this document’s singleton selection object. |
create_processing_instruction(target: str, data: str) → "ProcessingInstruction" | Create a ProcessingInstruction node owned by this document. |
create_attribute(local_name: str) → "Attr" | Create a non-namespaced Attr owned by this document. |
create_attribute_ns(namespace_uri: str | None, qualified_name: str) → "Attr" | Create a new namespaced Attr owned by this document. |
create_tree_walker(root: "Node", what_to_show: int, node_filter: "Callable[[Node], int] | None") → "TreeWalker" | Create a TreeWalker for filtered cursor-style DOM traversal. |
create_node_iterator(root: "Node", what_to_show: int, node_filter: "Callable[[Node], int] | None") → "NodeIterator" | Create a NodeIterator for flat filtered DOM traversal. |
adopt_node(node: Node) → Node | Adopt node into this document, moving it from its current tree. |
import_node(node: Node, deep: bool) → Node | Return a clone of node owned by this document. |
get_element_by_id(id: str) → Element | None | Return the first element with attribute id matching id. |
get_elements_by_tag_name(name: str) → HTMLCollection | Return all descendant elements with the given tag name. |
get_elements_by_class_name(names: str) → HTMLCollection | Return all descendant elements with all of the given class names. |
filter_fn(el: Element) → bool | Calls filter_fn(el) on this Document instance. |
get_elements_by_name(name: str) → NodeList | Return all descendant elements whose name attribute equals name. |
get_elements_by_tag_name_ns(namespace: "str | None", local_name: str) → list | Return all descendant elements matching namespace and local name. |
prepend() | Insert nodes before the first child of this document. |
append() | Append nodes as the last children of this document. |
replace_children() | Remove all children of this document, then append nodes. |
query_selector(selector: str) → Element | None | Return the first element matching the CSS selector, or None. |
query_selector_all(selector: str) → NodeList | Return a static NodeList of all elements matching the CSS selector. |
get_animations() → list[object] | Return document-level animations (headless stub: always []). |
element_from_point(x: float, y: float) | Always None — headless has no layout engine for hit testing (CSSOM View §11.6). |
elements_from_point(x: float, y: float) → list | Always [] — headless has no layout engine for hit testing (CSSOM View §11.6). |
exec_command(command_id: str, show_ui: bool, value: str) → bool | Always False — legacy editing command; no pipeline in headless (WHATWG HTML §8.6.1). |
query_command_enabled(command_id: str) → bool | Always False — no editing pipeline in headless (WHATWG HTML §8.6.1). |
has_storage_access() → bool | Always True — headless origin is treated as same-origin (Storage Access API). |
request_storage_access() | No-op stub — headless always has storage access; no async grant needed (Storage Access API). |
get_root_node(composed: bool) → "Node" | Return the root of the node’s tree. |
has_child_nodes() → bool | Return whether this node has at least one direct child node. |
lookup_namespace_uri(prefix: str | None) → str | None | Return the effective namespace URI for prefix in this node context. |
lookup_prefix(namespace_uri: str | None) → str | None | Return the effective prefix for namespace_uri in this node context. |
is_default_namespace(namespace_uri: str | None) → bool | Return whether namespace_uri equals this node context default namespace. |
is_supported(feature: str, version: str | None) → bool | Return legacy feature-probe compatibility status. |
get_feature(feature: str, version: str | None) → object | None | Return the legacy feature object for a probe, or None. |
set_user_data(key: str, data: object | None, handler: object | None) → object | None | Store or clear node-local user data and return the previous value. |
get_user_data(key: str) → object | None | Return node-local user data for key or None when absent. |
append_child(node: Node) → Node | Append node as the last child of this node. |
insert_before(node: Node, reference: Node | None) → Node | Insert node before reference in the child list. |
remove_child(node: Node) → Node | Remove node from the child list and return it. |
replace_child(new_child: Node, old_child: Node) → Node | Replace old_child with new_child. |
clone_node(deep: bool) → Node | Return a copy of this node. |
normalize() | Put this node and its entire subtree into normalized form. |
contains(other: Node | None) → bool | Return True if other is an inclusive descendant of this node. |
compare_document_position(other: "Node") → int | Return a bitmask describing the position of other relative to this node. |
is_equal_node(other: Node | None) → bool | Return True if other has the same structure as this node. |
is_same_node(other_node: Node | None) → bool | Return True when other_node is this node instance. |
add_event_listener(type: str, listener: Callable, capture: bool, once: bool) | Register listener for events of type. |
remove_event_listener(type: str, listener: Callable, capture: bool) | Deregister a previously-registered listener. |
dispatch_event(event: "Event") → bool | Dispatch event through the ancestor tree. |