8000 Browser refactor by arihant2math · Pull Request #5754 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Browser refactor #5754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add HTMLElement
  • Loading branch information
arihant2math committed Apr 29, 2025
commit 026eaf8f9d5e73c0080366b806eab35978c7ad4a
5 changes: 3 additions & 2 deletions wasm/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ web-sys = { version = "0.3", features = [
"console",
"Document",
"Element",
"Window",
"Headers",
"HtmlElement",
"Request",
"RequestInit",
"Response"
"Response",
"Window",
] }

[package.metadata.wasm-pack.profile.release]
Expand Down
60 changes: 30 additions & 30 deletions wasm/lib/src/browser_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use rustpython_vm::VirtualMachine;

pub(crate) use _browser::make_module;
use crate::wasm_builtins::window;
use rustpython_vm::PyRef;
use rustpython_vm::builtins::PyModule;
use rustpython_vm::PyPayload;

#[pymodule]
mod _browser {
Expand Down Expand Up @@ -169,42 +172,21 @@ mod _browser {
let body = self
.doc
.body()
.map(|elem| Element { elem })
.map(|elem| HTMLElement { elem })
.to_pyobject(vm);
Ok(body)
}

#[pygetset]
fn cookie(&self, vm: &VirtualMachine) -> PyResult {
let cookie = self
.doc
.cookie()
.map_err(|err| convert::js_py_typeerror(vm, err))?;
Ok(vm.ctx.new_str(cookie).into())
}

#[pymethod]
fn get_element_by_id(&self, id: PyStrRef, vm: &VirtualMachine) -> PyResult {
let elem = self
.doc
.get_element_by_id(id.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))?
.map(|elem| Element { elem })
.to_pyobject(vm);
Ok(elem)
}

#[pygetset]
fn head(&self, vm: &VirtualMachine) -> PyResult {
let head = self
.doc
.head()
.map_err(|err| convert::js_py_typeerror(vm, err))?
.map(|elem| Element { elem })
.to_pyobject(vm);
Ok(head)
}

#[pymethod]
fn query(&self, query: PyStrRef, vm: &VirtualMachine) -> PyResult {
let elem = self
Expand All @@ -220,16 +202,13 @@ mod _browser {
fn title(&self, vm: &VirtualMachine) -> PyResult {
let title = self
.doc
.title()
.map_err(|err| convert::js_py_typeerror(vm, err))?;
.title();
Ok(vm.ctx.new_str(title).into())
}

#[pygetset(setter)]
fn set_title(&self, title: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
self.doc
.set_title(title.as_str())
.map_err(|err| convert::js_py_typeerror(vm, err))
fn set_title(&self, title: PyStrRef) {
self.doc.set_title(title.as_str());
}
}

Expand Down Expand Up @@ -274,6 +253,27 @@ mod _browser {
}
}

#[pyattr]
#[pyclass(module = "browser", name)]
#[derive(Debug, PyPayload)]
struct HTMLElement {
elem: web_sys::HtmlElement,
}

#[pyclass]
impl HTMLElement {
#[pygetset]
fn title(&self, vm: &VirtualMachine) -> PyResult {
let title = self.elem.title();
Ok(vm.ctx.new_str(title).into())
}

#[pygetset(setter)]
fn set_title(&self, title: PyStrRef) {
self.elem.set_title(title.as_str());
}
}

#[pyfunction]
fn load_module(module: PyStrRef, path: PyStrRef, vm: &VirtualMachine) -> PyResult {
let weak_vm = weak_vm(vm);
Expand Down Expand Up @@ -310,11 +310,11 @@ mod _browser {
}
}

fn init_browser_module(vm: &VirtualMachine) {
fn init_browser_module(vm: &VirtualMachine) -> PyRef<PyModule> {
let module = make_module(vm);

extend_module!(vm, &module, {
"window" => js_module::PyJsValue::new(wasm_builtins::window()).into_ref(&vm.ctx),
"window" => crate::js_module::PyJsValue::new(crate::wasm_builtins::window()).into_ref(&vm.ctx),
});

module
Expand Down
0