mirror of
https://github.com/servo/servo.git
synced 2025-07-23 23:33:43 +01:00
DevTools: Allow modification of attributes (#32888)
* feat: allow modification of attributes Signed-off-by: eri <eri@inventati.org> * fix: tidiness Signed-off-by: eri <eri@inventati.org> * feat: clean walker name generation Co-authored-by: Martin Robinson <mrobinson@igalia.com> Signed-off-by: eri <eri@inventati.org> * fix: missed out parameter Signed-off-by: eri <eri@inventati.org> --------- Signed-off-by: eri <eri@inventati.org> Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
4d49b04668
commit
babc844c93
5 changed files with 121 additions and 12 deletions
|
@ -86,14 +86,27 @@ impl Actor for InspectorActor {
|
||||||
self.script_chan.send(GetRootNode(pipeline, tx)).unwrap();
|
self.script_chan.send(GetRootNode(pipeline, tx)).unwrap();
|
||||||
let root_info = rx.recv().unwrap().ok_or(())?;
|
let root_info = rx.recv().unwrap().ok_or(())?;
|
||||||
|
|
||||||
let root = root_info.encode(registry, false, self.script_chan.clone(), pipeline);
|
let name = self
|
||||||
|
.walker
|
||||||
|
.borrow()
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| registry.new_name("walker"));
|
||||||
|
|
||||||
|
let root = root_info.encode(
|
||||||
|
registry,
|
||||||
|
false,
|
||||||
|
self.script_chan.clone(),
|
||||||
|
pipeline,
|
||||||
|
name.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
if self.walker.borrow().is_none() {
|
if self.walker.borrow().is_none() {
|
||||||
let walker = WalkerActor {
|
let walker = WalkerActor {
|
||||||
name: registry.new_name("walker"),
|
name,
|
||||||
script_chan: self.script_chan.clone(),
|
script_chan: self.script_chan.clone(),
|
||||||
pipeline,
|
pipeline,
|
||||||
root_node: root.clone(),
|
root_node: root.clone(),
|
||||||
|
mutations: RefCell::new(vec![]),
|
||||||
};
|
};
|
||||||
let mut walker_name = self.walker.borrow_mut();
|
let mut walker_name = self.walker.borrow_mut();
|
||||||
*walker_name = Some(walker.name());
|
*walker_name = Some(walker.name());
|
||||||
|
|
|
@ -16,6 +16,7 @@ use serde::Serialize;
|
||||||
use serde_json::{self, Map, Value};
|
use serde_json::{self, Map, Value};
|
||||||
|
|
||||||
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
|
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
|
||||||
|
use crate::actors::inspector::walker::WalkerActor;
|
||||||
use crate::protocol::JsonPacketStream;
|
use crate::protocol::JsonPacketStream;
|
||||||
use crate::{EmptyReplyMsg, StreamId};
|
use crate::{EmptyReplyMsg, StreamId};
|
||||||
|
|
||||||
|
@ -77,6 +78,7 @@ pub struct NodeActor {
|
||||||
name: String,
|
name: String,
|
||||||
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
||||||
pipeline: PipelineId,
|
pipeline: PipelineId,
|
||||||
|
pub walker: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Actor for NodeActor {
|
impl Actor for NodeActor {
|
||||||
|
@ -102,12 +104,16 @@ impl Actor for NodeActor {
|
||||||
"modifyAttributes" => {
|
"modifyAttributes" => {
|
||||||
let target = msg.get("to").ok_or(())?.as_str().ok_or(())?;
|
let target = msg.get("to").ok_or(())?.as_str().ok_or(())?;
|
||||||
let mods = msg.get("modifications").ok_or(())?.as_array().ok_or(())?;
|
let mods = msg.get("modifications").ok_or(())?.as_array().ok_or(())?;
|
||||||
let modifications = mods
|
let modifications: Vec<_> = mods
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|json_mod| {
|
.filter_map(|json_mod| {
|
||||||
serde_json::from_str(&serde_json::to_string(json_mod).ok()?).ok()
|
serde_json::from_str(&serde_json::to_string(json_mod).ok()?).ok()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let walker = registry.find::<WalkerActor>(&self.walker);
|
||||||
|
walker.new_mutations(stream, &self.name, &modifications);
|
||||||
|
|
||||||
self.script_chan
|
self.script_chan
|
||||||
.send(ModifyAttribute(
|
.send(ModifyAttribute(
|
||||||
self.pipeline,
|
self.pipeline,
|
||||||
|
@ -127,8 +133,13 @@ impl Actor for NodeActor {
|
||||||
.send(GetDocumentElement(self.pipeline, tx))
|
.send(GetDocumentElement(self.pipeline, tx))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
|
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
|
||||||
let node =
|
let node = doc_elem_info.encode(
|
||||||
doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
|
registry,
|
||||||
|
true,
|
||||||
|
self.script_chan.clone(),
|
||||||
|
self.pipeline,
|
||||||
|
self.walker.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
let msg = GetUniqueSelectorReply {
|
let msg = GetUniqueSelectorReply {
|
||||||
from: self.name(),
|
from: self.name(),
|
||||||
|
@ -150,6 +161,7 @@ pub trait NodeInfoToProtocol {
|
||||||
display: bool,
|
display: bool,
|
||||||
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
||||||
pipeline: PipelineId,
|
pipeline: PipelineId,
|
||||||
|
walker: String,
|
||||||
) -> NodeActorMsg;
|
) -> NodeActorMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,6 +172,7 @@ impl NodeInfoToProtocol for NodeInfo {
|
||||||
display: bool,
|
display: bool,
|
||||||
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
script_chan: IpcSender<DevtoolScriptControlMsg>,
|
||||||
pipeline: PipelineId,
|
pipeline: PipelineId,
|
||||||
|
walker: String,
|
||||||
) -> NodeActorMsg {
|
) -> NodeActorMsg {
|
||||||
let actor = if !actors.script_actor_registered(self.unique_id.clone()) {
|
let actor = if !actors.script_actor_registered(self.unique_id.clone()) {
|
||||||
let name = actors.new_name("node");
|
let name = actors.new_name("node");
|
||||||
|
@ -167,6 +180,7 @@ impl NodeInfoToProtocol for NodeInfo {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
script_chan: script_chan.clone(),
|
script_chan: script_chan.clone(),
|
||||||
pipeline,
|
pipeline,
|
||||||
|
walker: walker.clone(),
|
||||||
};
|
};
|
||||||
actors.register_script_actor(self.unique_id, name.clone());
|
actors.register_script_actor(self.unique_id, name.clone());
|
||||||
actors.register_later(Box::new(node_actor));
|
actors.register_later(Box::new(node_actor));
|
||||||
|
@ -192,7 +206,7 @@ impl NodeInfoToProtocol for NodeInfo {
|
||||||
let mut children = rx.recv().ok()??;
|
let mut children = rx.recv().ok()??;
|
||||||
|
|
||||||
let child = children.pop()?;
|
let child = children.pop()?;
|
||||||
let msg = child.encode(actors, true, script_chan.clone(), pipeline);
|
let msg = child.encode(actors, true, script_chan.clone(), pipeline, walker);
|
||||||
|
|
||||||
// If the node child is not a text node, do not represent it inline.
|
// If the node child is not a text node, do not represent it inline.
|
||||||
if msg.node_type != TEXT_NODE {
|
if msg.node_type != TEXT_NODE {
|
||||||
|
|
|
@ -4,11 +4,12 @@
|
||||||
|
|
||||||
//! The walker actor is responsible for traversing the DOM tree in various ways to create new nodes
|
//! The walker actor is responsible for traversing the DOM tree in various ways to create new nodes
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
|
||||||
use base::id::PipelineId;
|
use base::id::PipelineId;
|
||||||
use devtools_traits::DevtoolScriptControlMsg;
|
|
||||||
use devtools_traits::DevtoolScriptControlMsg::{GetChildren, GetDocumentElement};
|
use devtools_traits::DevtoolScriptControlMsg::{GetChildren, GetDocumentElement};
|
||||||
|
use devtools_traits::{DevtoolScriptControlMsg, Modification};
|
||||||
use ipc_channel::ipc::{self, IpcSender};
|
use ipc_channel::ipc::{self, IpcSender};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::{self, Map, Value};
|
use serde_json::{self, Map, Value};
|
||||||
|
@ -30,6 +31,7 @@ pub struct WalkerActor {
|
||||||
pub script_chan: IpcSender<DevtoolScriptControlMsg>,
|
pub script_chan: IpcSender<DevtoolScriptControlMsg>,
|
||||||
pub pipeline: PipelineId,
|
pub pipeline: PipelineId,
|
||||||
pub root_node: NodeActorMsg,
|
pub root_node: NodeActorMsg,
|
||||||
|
pub mutations: RefCell<Vec<(Modification, String)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
@ -69,12 +71,35 @@ struct WatchRootNodeReply {
|
||||||
node: NodeActorMsg,
|
node: NodeActorMsg,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct MutationMsg {
|
||||||
|
attribute_name: String,
|
||||||
|
new_value: Option<String>,
|
||||||
|
target: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
type_: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct GetMutationsReply {
|
||||||
|
from: String,
|
||||||
|
mutations: Vec<MutationMsg>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct GetOffsetParentReply {
|
struct GetOffsetParentReply {
|
||||||
from: String,
|
from: String,
|
||||||
node: Option<()>,
|
node: Option<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct NewMutationsReply {
|
||||||
|
from: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
type_: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl Actor for WalkerActor {
|
impl Actor for WalkerActor {
|
||||||
fn name(&self) -> String {
|
fn name(&self) -> String {
|
||||||
self.name.clone()
|
self.name.clone()
|
||||||
|
@ -90,6 +115,8 @@ impl Actor for WalkerActor {
|
||||||
///
|
///
|
||||||
/// - `getLayoutInspector`: Returns the Layout inspector actor, placeholder
|
/// - `getLayoutInspector`: Returns the Layout inspector actor, placeholder
|
||||||
///
|
///
|
||||||
|
/// - `getMutations`: Returns the list of attribute changes since it was last called
|
||||||
|
///
|
||||||
/// - `getOffsetParent`: Placeholder
|
/// - `getOffsetParent`: Placeholder
|
||||||
///
|
///
|
||||||
/// - `querySelector`: Recursively looks for the specified selector in the tree, reutrning the
|
/// - `querySelector`: Recursively looks for the specified selector in the tree, reutrning the
|
||||||
|
@ -121,7 +148,13 @@ impl Actor for WalkerActor {
|
||||||
nodes: children
|
nodes: children
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|child| {
|
.map(|child| {
|
||||||
child.encode(registry, true, self.script_chan.clone(), self.pipeline)
|
child.encode(
|
||||||
|
registry,
|
||||||
|
true,
|
||||||
|
self.script_chan.clone(),
|
||||||
|
self.pipeline,
|
||||||
|
self.name(),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
from: self.name(),
|
from: self.name(),
|
||||||
|
@ -140,8 +173,13 @@ impl Actor for WalkerActor {
|
||||||
.send(GetDocumentElement(self.pipeline, tx))
|
.send(GetDocumentElement(self.pipeline, tx))
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
|
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
|
||||||
let node =
|
let node = doc_elem_info.encode(
|
||||||
doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
|
registry,
|
||||||
|
true,
|
||||||
|
self.script_chan.clone(),
|
||||||
|
self.pipeline,
|
||||||
|
self.name(),
|
||||||
|
);
|
||||||
|
|
||||||
let msg = DocumentElementReply {
|
let msg = DocumentElementReply {
|
||||||
from: self.name(),
|
from: self.name(),
|
||||||
|
@ -163,6 +201,24 @@ impl Actor for WalkerActor {
|
||||||
let _ = stream.write_json_packet(&msg);
|
let _ = stream.write_json_packet(&msg);
|
||||||
ActorMessageStatus::Processed
|
ActorMessageStatus::Processed
|
||||||
},
|
},
|
||||||
|
"getMutations" => {
|
||||||
|
let msg = GetMutationsReply {
|
||||||
|
from: self.name(),
|
||||||
|
mutations: self
|
||||||
|
.mutations
|
||||||
|
.borrow_mut()
|
||||||
|
.drain(..)
|
||||||
|
.map(|(mutation, target)| MutationMsg {
|
||||||
|
attribute_name: mutation.attribute_name,
|
||||||
|
new_value: mutation.new_value,
|
||||||
|
target,
|
||||||
|
type_: "attributes".into(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let _ = stream.write_json_packet(&msg);
|
||||||
|
ActorMessageStatus::Processed
|
||||||
|
},
|
||||||
"getOffsetParent" => {
|
"getOffsetParent" => {
|
||||||
let msg = GetOffsetParentReply {
|
let msg = GetOffsetParentReply {
|
||||||
from: self.name(),
|
from: self.name(),
|
||||||
|
@ -177,6 +233,7 @@ impl Actor for WalkerActor {
|
||||||
let mut hierarchy = find_child(
|
let mut hierarchy = find_child(
|
||||||
&self.script_chan,
|
&self.script_chan,
|
||||||
self.pipeline,
|
self.pipeline,
|
||||||
|
&self.name,
|
||||||
registry,
|
registry,
|
||||||
selector,
|
selector,
|
||||||
node,
|
node,
|
||||||
|
@ -211,11 +268,30 @@ impl Actor for WalkerActor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WalkerActor {
|
||||||
|
pub(crate) fn new_mutations(
|
||||||
|
&self,
|
||||||
|
stream: &mut TcpStream,
|
||||||
|
target: &str,
|
||||||
|
modifications: &[Modification],
|
||||||
|
) {
|
||||||
|
{
|
||||||
|
let mut mutations = self.mutations.borrow_mut();
|
||||||
|
mutations.extend(modifications.iter().cloned().map(|m| (m, target.into())));
|
||||||
|
}
|
||||||
|
let _ = stream.write_json_packet(&NewMutationsReply {
|
||||||
|
from: self.name(),
|
||||||
|
type_: "newMutations".into(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Recursively searches for a child with the specified selector
|
/// Recursively searches for a child with the specified selector
|
||||||
/// If it is found, returns a list with the child and all of its ancestors.
|
/// If it is found, returns a list with the child and all of its ancestors.
|
||||||
fn find_child(
|
fn find_child(
|
||||||
script_chan: &IpcSender<DevtoolScriptControlMsg>,
|
script_chan: &IpcSender<DevtoolScriptControlMsg>,
|
||||||
pipeline: PipelineId,
|
pipeline: PipelineId,
|
||||||
|
name: &str,
|
||||||
registry: &ActorRegistry,
|
registry: &ActorRegistry,
|
||||||
selector: &str,
|
selector: &str,
|
||||||
node: &str,
|
node: &str,
|
||||||
|
@ -232,7 +308,7 @@ fn find_child(
|
||||||
let children = rx.recv().unwrap().ok_or(vec![])?;
|
let children = rx.recv().unwrap().ok_or(vec![])?;
|
||||||
|
|
||||||
for child in children {
|
for child in children {
|
||||||
let msg = child.encode(registry, true, script_chan.clone(), pipeline);
|
let msg = child.encode(registry, true, script_chan.clone(), pipeline, name.into());
|
||||||
if msg.display_name == selector {
|
if msg.display_name == selector {
|
||||||
hierarchy.push(msg);
|
hierarchy.push(msg);
|
||||||
return Ok(hierarchy);
|
return Ok(hierarchy);
|
||||||
|
@ -245,6 +321,7 @@ fn find_child(
|
||||||
match find_child(
|
match find_child(
|
||||||
script_chan,
|
script_chan,
|
||||||
pipeline,
|
pipeline,
|
||||||
|
name,
|
||||||
registry,
|
registry,
|
||||||
selector,
|
selector,
|
||||||
&msg.actor,
|
&msg.actor,
|
||||||
|
|
|
@ -235,6 +235,11 @@ pub fn handle_modify_attribute(
|
||||||
node_id: String,
|
node_id: String,
|
||||||
modifications: Vec<Modification>,
|
modifications: Vec<Modification>,
|
||||||
) {
|
) {
|
||||||
|
let Some(document) = documents.find_document(pipeline) else {
|
||||||
|
return warn!("document for pipeline id {} is not found", &pipeline);
|
||||||
|
};
|
||||||
|
let _realm = enter_realm(document.window());
|
||||||
|
|
||||||
let node = match find_node_by_unique_id(documents, pipeline, &node_id) {
|
let node = match find_node_by_unique_id(documents, pipeline, &node_id) {
|
||||||
None => {
|
None => {
|
||||||
return warn!(
|
return warn!(
|
||||||
|
|
|
@ -222,7 +222,7 @@ pub enum DevtoolScriptControlMsg {
|
||||||
Reload(PipelineId),
|
Reload(PipelineId),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Modification {
|
pub struct Modification {
|
||||||
pub attribute_name: String,
|
pub attribute_name: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue