webapi

The webapi package groups browser-flavoured APIs that sit alongside the DOM surface.

console

from domonic.webabi.console import console
console.log("Hello World")

encoding

from domonic.webabi.encoding import TextEncoder, TextDecoder
encoder = TextEncoder()

fetch

from domonic.webabi.fetch import fetch

URL

url is a wrapper around the urlparse and urlencode functions in python.

from domonic.webabi.url import URL

myurl = URL("http://www.google.com/search?q=domonic")
print(myurl.host)
print(myurl.query)
print(myurl.query.q)
print(myurl.query.q.value)

For more information see the MDN URL API docs: https://developer.mozilla.org/en-US/docs/Web/API/URL

XPATH

Here’s a quick example of using xpath…

from domonic.webapi.xpath import XPathEvaluator, XPathResult

somehtml = '''
<div>XPath example</div>
<div>Number of &lt;div&gt;s: <output></output></div>
'''
page = domonic.parseString(somehtml)  # NOTE - probably requries html5lib install
evaluator = XPathEvaluator()
expression = evaluator.createExpression("//div")
result = expression.evaluate(page, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE)
assert result.snapshotLength == 2

for more information see mdn docs… # TODO - link

domonic.webapi.console

https://developer.mozilla.org/en-US/docs/Web/API/Console_API

domonic.webapi.encoding

https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API

class domonic.webapi.encoding.TextDecoderStream(encoding='utf-8')[source]
class domonic.webapi.encoding.TextEncoderStream(encoding='utf-8')[source]

domonic.webapi.fetch

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

domonic.webapi.url

https://developer.mozilla.org/en-US/docs/Web/API/URL

# TODO - move the unit tests for this class from javascript to webapi # TODO - untested

class domonic.webapi.url.URL(url: str = '', *args: Any, **kwargs: Any)[source]

a-tag extends from URL

property hash: str

“ hash Sets or returns the anchor part (#) of a URL

class domonic.webapi.url.URLSearchParams(paramString: str | dict[str, Any] | Iterable[tuple[str, Any]])[source]

[utility methods to work with the query string of a URL]

append(key: str, value: str) None[source]

Appends a specified key/value pair as a new search parameter

delete(key: str) None[source]

Deletes the given search parameter, and its associated value, from the list of all search parameters.

entries() Iterable[tuple[str, list[str]]][source]

Returns an iterator allowing iteration through all key/value pairs contained in this object.

forEach(func: Callable[[str, list[str]], Any]) None[source]

Allows iteration through all values contained in this object via a callback function.

get(key: str) str | None[source]

Returns the first value associated with the given search parameter.

getAll(key: str) list[str] | None[source]

Returns all the values associated with a given search parameter.

has(key: str) bool[source]

Returns a Boolean indicating if such a given parameter exists.

keys() Iterable[str][source]

Returns an iterator allowing iteration through all keys of the key/value pairs contained in this object.

set(key: str, value: str) None[source]

Sets the value associated with a given search parameter to the given value. If there are several values, the others are deleted.

sort() None[source]

Sorts all key/value pairs, if any, by their keys.

toString() str[source]

Returns a string containing a query string suitable for use in a URL.

values() Iterable[list[str]][source]

Returns an iterator allowing iteration through all values of the key/value pairs contained in this object.

domonic.webapi.XMLHttpRequest

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

domonic.webapi.xpath

https://developer.mozilla.org/en-US/docs/Glossary/XPath

uses elementpath lib.

TODO - content strings must be TextNodes for it to work.

so will have to iterate and update them. i.e. Treewalker