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

NameTypeAccessDescription
default_view``ReadPer-Document Window object (lazy, cached).
location``ReadReturn the associated Window’s Location object, or None.
implementation"DOMImplementation"ReadPer-Document DOMImplementation object (lazy, cached).
dom_configDOMConfigurationReadPer-Document DOMConfiguration object (lazy, cached).
urlstrReadDocument URL (WHATWG Document.URL), default "about:blank".
document_uristrReadThe document’s URL as a string (WHATWG DOM §4.5 documentURI).
base_uristrReadResolved base URL for this document (WHATWG DOM Node.baseURI).
referrerstrReadReferring document URL (WHATWG HTML Standard §document).
domainstrReadHostname component of the document URL (WHATWG HTML Standard §document).
last_modifiedstrReadLast-modified timestamp string (WHATWG HTML Standard §document).
cookiestrRead/WriteRead-only cookie string stub — always returns "" (WHATWG HTML §8.11.1).
hiddenboolReadWhether the document is hidden — always False in headless mode.
visibility_statestrReadDocument visibility state — always "visible" in headless mode.
design_modestrRead/WriteWhether the document is in design mode (WHATWG HTML §7.6.4).
dirstrRead/WriteDocument text direction (WHATWG HTML §3.3.2).
current_scriptNoneReadThe currently executing script element (WHATWG HTML §8.1.3.4).
character_setstrReadCharacter encoding used during parsing (WHATWG DOM §3.1).
charsetstrReadAlias for :attr:character_set (HTML Standard name).
input_encodingstrReadAlias for :attr:character_set (WHATWG DOM compatibility name).
content_typestrReadDocument MIME type; always "text/html" for HTML documents.
style_sheets"StyleSheetList"ReadLive document stylesheet list.
adopted_style_sheetslist["CSSStyleSheet"]Read/WriteDocument-level constructable stylesheet adoption list.
node_namestrRead'#document'
document_elementElement | NoneReadThe root Element child, or None if not yet set.
document_typeDocumentType | NoneReadThe DocumentType child, or None.
compat_modestrRead/Write'BackCompat' (quirks) or 'CSS1Compat' (no-quirks).
ready_statestrReadDocument loading state.
parse_errorslist[ParseError]ReadParse errors from the last parse operation.
headElement | NoneReadThe <head> element child of document_element, or None.
bodyElement | NoneReadThe <body> element child of document_element, or None.
forms"HTMLCollection"ReadLive collection of all <form> elements in the document.
images"HTMLCollection"ReadLive collection of all <img> elements in the document.
links"HTMLCollection"ReadLive collection of <a href> and <area href> elements.
scripts"HTMLCollection"ReadLive collection of all <script> elements in the document.
embeds"HTMLCollection"ReadLive collection of all <embed> descendants.
applets"HTMLCollection"ReadLive collection of all <applet> descendants.
anchors"HTMLCollection"ReadLive collection of <a name=...> elements (legacy named anchors).
titlestrRead/WriteThe text content of the first <title> element in <head>.
compatible_modestrReadQuirks-mode indicator (WHATWG HTML §3.2.1).
strict_error_checkingboolRead/WriteAlways True in headless mode (DOM3 Core §1.1.1).
first_element_child"Element | None"ReadFirst direct child that is an Element, or None.
last_element_child"Element | None"ReadLast direct child that is an Element, or None.
child_element_countintReadNumber of direct children that are Element nodes.
active_element"Element | None"ReadThe currently focused element, always None in headless mode.
full_screen_enabledboolReadAlways False — no display in headless mode (Fullscreen API §7).
fullscreen_elementNoneReadAlways None — no fullscreen element in headless mode (Fullscreen API §7).
pointer_lock_elementNoneReadAlways None — no pointer lock in headless mode (Pointer Lock API §4).
timeline"_DocumentTimeline"ReadDocument timeline stub; current_time is always 0.0 (Web Animations API §6.2).
all"_AllCollection"ReadAll elements in tree order (WHATWG HTML §legacy document layout interface).
plugins"HTMLCollection"ReadAlias for :attr:embeds per WHATWG HTML §4.1 legacy interface.
children"HTMLCollection"ReadDirect element children of the document (WHATWG DOM §4.2.6 ParentNode).
originstrReadOpaque origin string — always 'null' in headless mode (WHATWG DOM §4.5).
fg_colorstrRead/WriteReflects obsolete fgColor document-level attribute via <body> text attribute.
bg_colorstrRead/WriteReflects obsolete bgColor document-level attribute via <body> bgcolor attribute.
link_colorstrRead/WriteReflects obsolete linkColor document-level attribute via <body> link attribute.
v_link_colorstrRead/WriteReflects obsolete vlinkColor document-level attribute via <body> vlink attribute.
a_link_colorstrRead/WriteReflects obsolete alinkColor document-level attribute via <body> alink attribute.
fonts``ReadAlways None in headless mode.
picture_in_picture_enabledboolReadAlways False in headless mode.
picture_in_picture_element``ReadAlways None in headless mode.
prerenderingboolReadAlways False in headless mode.
node_typeintReadInteger node type constant from NodeType.
node_valuestr | NoneRead/WriteCharacter data for CharacterData nodes; None otherwise.
text_contentstr | NoneRead/WriteThe text content of this node, or None for Document and DocumentType.
parent_nodeNode | NoneReadThe parent node, or None if this node has no parent.
parent_elementElement | NoneReadThe parent node if it is an Element; None otherwise.
is_connectedboolReadWhether this node is part of a document’s node tree (WHATWG DOM §4.4).
owner_documentDocument | NoneReadThe Document this node belongs to, or None for Document itself.
child_nodesNodeListReadA live NodeList of all child nodes.
first_childNode | NoneReadThe first child node, or None.
last_childNode | NoneReadThe last child node, or None.
previous_siblingNode | NoneReadThe preceding sibling node, or None.
next_siblingNode | NoneReadThe following sibling node, or None.

Methods

SignatureDescription
__init__()Calls init on this Document instance.
normalize_document()Normalize this document using existing Node.normalize semantics.
has_focus()boolReturn 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)ElementCreate an Element in a specific namespace, owned by this document.
create_text_node(data: str)TextCreate a Text node with the given character data.
create_comment(data: str)CommentCreate 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()DocumentFragmentCreate 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)NodeAdopt node into this document, moving it from its current tree.
import_node(node: Node, deep: bool)NodeReturn a clone of node owned by this document.
get_element_by_id(id: str)Element | NoneReturn the first element with attribute id matching id.
get_elements_by_tag_name(name: str)HTMLCollectionReturn all descendant elements with the given tag name.
get_elements_by_class_name(names: str)HTMLCollectionReturn all descendant elements with all of the given class names.
filter_fn(el: Element)boolCalls filter_fn(el) on this Document instance.
get_elements_by_name(name: str)NodeListReturn all descendant elements whose name attribute equals name.
get_elements_by_tag_name_ns(namespace: "str | None", local_name: str)listReturn 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 | NoneReturn the first element matching the CSS selector, or None.
query_selector_all(selector: str)NodeListReturn 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)listAlways [] — headless has no layout engine for hit testing (CSSOM View §11.6).
exec_command(command_id: str, show_ui: bool, value: str)boolAlways False — legacy editing command; no pipeline in headless (WHATWG HTML §8.6.1).
query_command_enabled(command_id: str)boolAlways False — no editing pipeline in headless (WHATWG HTML §8.6.1).
has_storage_access()boolAlways 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()boolReturn whether this node has at least one direct child node.
lookup_namespace_uri(prefix: str | None)str | NoneReturn the effective namespace URI for prefix in this node context.
lookup_prefix(namespace_uri: str | None)str | NoneReturn the effective prefix for namespace_uri in this node context.
is_default_namespace(namespace_uri: str | None)boolReturn whether namespace_uri equals this node context default namespace.
is_supported(feature: str, version: str | None)boolReturn legacy feature-probe compatibility status.
get_feature(feature: str, version: str | None)object | NoneReturn the legacy feature object for a probe, or None.
set_user_data(key: str, data: object | None, handler: object | None)object | NoneStore or clear node-local user data and return the previous value.
get_user_data(key: str)object | NoneReturn node-local user data for key or None when absent.
append_child(node: Node)NodeAppend node as the last child of this node.
insert_before(node: Node, reference: Node | None)NodeInsert node before reference in the child list.
remove_child(node: Node)NodeRemove node from the child list and return it.
replace_child(new_child: Node, old_child: Node)NodeReplace old_child with new_child.
clone_node(deep: bool)NodeReturn a copy of this node.
normalize()Put this node and its entire subtree into normalized form.
contains(other: Node | None)boolReturn True if other is an inclusive descendant of this node.
compare_document_position(other: "Node")intReturn a bitmask describing the position of other relative to this node.
is_equal_node(other: Node | None)boolReturn True if other has the same structure as this node.
is_same_node(other_node: Node | None)boolReturn 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")boolDispatch event through the ancestor tree.

See Also