Skip to content

Commit c07371f

Browse files
committed
Final pyscript.web API updates.
1 parent c931655 commit c07371f

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

docs/api.md

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ store = await storage("my-data-store", storage_class=MyStorage)
413413

414414
### `pyscript.web`
415415

416+
TODO: Use `display(element)` not `element.display()`.
417+
416418
The classes and references in this namespace provide a Pythonic way to interact
417419
with the DOM. An explanation for how to idiomatically use this API can be found
418420
[in the user guide](../user-guide/dom/#pyscriptweb)
@@ -424,14 +426,19 @@ This object has two attributes and a single method:
424426
* `head` - a reference to a Python object representing the [document's head](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head).
425427
* `body` - a reference to a Python object representing the [document's body](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body).
426428
* `find` - a method that takes a single [selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors)
427-
argument and returns a collection of matching elements.
429+
argument and returns a collection of Python objects representing the matching
430+
elements.
431+
432+
These are provided as a convenience so you have several simple and obvious
433+
options for accessing the content of the page (DOM).
428434

429-
You have several options for accessing the
435+
All the Python objects returned by these attributes and method are instances of
436+
classes defined in the `pyscript.web.elements` namespace.
430437

431438
#### `pyscript.web.elements.*`
432439

433440
There are many classes in this namespace. Each is a one-to-one mapping of any
434-
HTML element name for a Python class representing the HTML element of that
441+
HTML element name to a Python class representing the HTML element of that
435442
name. Each Python class ensures only valid properties and attributes can be
436443
assigned, according to web standards.
437444

@@ -472,6 +479,9 @@ Usage of these classes is
472479
(`_`) because they are also keywords in Python, and the `grid` is a custom
473480
class for a `div` with a `grid` style `display` property.
474481

482+
All these classes ultimately derive from the
483+
`pyscript.web.elements.Element` base class.
484+
475485
In addition to properties defined by the HTML standard for each type of HTML
476486
element (e.g. `title`, `src` or `href`), all elements have the following
477487
properties and methods (in alphabetical order):
@@ -482,15 +492,34 @@ properties and methods (in alphabetical order):
482492
* `classes` - a set of CSS classes associated with the element.
483493
* `clone(clone_id=None)` - Make a clone of the element (and the underlying DOM
484494
object), and assign it the optional `clone_id`.
485-
* `content` - get or set the
486-
[innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML)
487-
of the element.
488495
* `find(selector)` - use a CSS selector to find matching child elements.
489496
* `parent` - the element's parent element (that contains it).
490497
* `show_me` - scroll the element into view.
491498
* `style` - a dictionary of CSS style properties associated with the element.
492499
* `update(classes=None, style=None, **kwargs)` - update the element with the
493500
specified classes (set), style (dict) and DOM properties (kwargs).
501+
* `_dom_element` - a reference to the proxy object that represents the
502+
underlying native HTML element.
503+
504+
!!! info
505+
506+
All elements, by virtue of inheriting from the base `Element` class, may
507+
have the following properties:
508+
509+
```
510+
accesskey, autofocus, autocapitalize,
511+
className, contenteditable,
512+
draggable,
513+
enterkeyhint,
514+
hidden,
515+
innerHTML, id,
516+
lang,
517+
nonce,
518+
part, popover,
519+
slot, spellcheck,
520+
tabindex, text, title, translate,
521+
virtualkeyboardpolicy
522+
```
494523

495524
The `classes` set-like object has the following convenience functions:
496525

@@ -501,8 +530,26 @@ The `classes` set-like object has the following convenience functions:
501530
* `replace(old_class, new_class)` - replace the `old_class` with `new_class`.
502531
* `toggle(class_name)` - add a class if it is absent, or remove a class if it
503532
is present.
504-
* `_dom_element` - a reference to the proxy object that represents the
505-
underlying HTML element.
533+
534+
Elements that require options (such as the `datalist`, `optgroup` and `select`
535+
elements), can have options passed in when they are created:
536+
537+
```python
538+
my_select = select_(option("apple", value=1), option("pear"))
539+
```
540+
541+
Notice how options can be a tuple of two values (the name and associated value)
542+
or just the single name (whose associated value will default to the given
543+
name).
544+
545+
It's possible to access and manipulate the `options` of the resulting elements:
546+
547+
```python
548+
selected_option = my_select.options.selected
549+
my_select.options.remove(0) # Remove the first option (in position 0).
550+
my_select.clear()
551+
my_select.options.add(html="Orange")
552+
```
506553

507554
Finally, the collection of elements returned by `find` and `children` is
508555
iterable, indexable and sliceable:
@@ -512,11 +559,12 @@ for child in my_element.children[10:]:
512559
print(child.html)
513560
```
514561

515-
Furthermore, three attributes related to all elements contained in the
562+
Furthermore, four attributes related to all elements contained in the
516563
collection can be read (as a list) or set (applied to all contained elements):
517564

565+
* `classes` - the list of classes associated with the elements.
566+
* `innerHTML` - the innerHTML of each element.
518567
* `style` - a dictionary like object for interacting with CSS style rules.
519-
* `html` - the `innerHTML` of each element.
520568
* `value` - the `value` attribute associated with each element.
521569

522570
### `pyscript.when`

docs/user-guide/dom.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ for p in paragraphs[-2:]:
155155
It also makes available the following read and writable attributes related to
156156
all contained elements:
157157

158+
* `classes` - the list of classes associated with the elements.
159+
* `innerHTML` - the innerHTML of each element.
158160
* `style` - a dictionary like object for interacting with CSS style rules.
159-
* `html` - the `innerHTML` of each element.
160161
* `value` - the `value` attribute associated with each element.
161162

162-
For example, to continue the example above, `paragraphs.html` will return a
163-
list of all the values of the `html` attribute on each contained element.
164-
Alternatively, set an attribute for all elements contained in the
163+
For example, to continue the example above, `paragraphs.innerHTML` will return
164+
a list of all the values of the `innerHTML` attribute on each contained
165+
element. Alternatively, set an attribute for all elements contained in the
165166
collection like this: `paragraphs.style["background-color"] = "blue"`.
166167

167168
It's possible to create new elements to add to the DOM:
@@ -252,6 +253,9 @@ Should you wish direct access to the proxy object representing the underlying
252253
HTML element, each Python element has a `_dom_element` property for this
253254
purpose.
254255

256+
Once again, the technical details of these classes are described in the
257+
[built-in API documentation](../../api/#pyscriptweb).
258+
255259
## Working with JavaScript
256260

257261
There are three ways in which JavaScript can get into a web page.

0 commit comments

Comments
 (0)