Skip to content

Commit 52176b9

Browse files
eeriimrobinson
authored andcommitted
DevTools: Allow modification of attributes (servo#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>
1 parent 60e72fe commit 52176b9

File tree

5 files changed

+121
-12
lines changed

5 files changed

+121
-12
lines changed

components/devtools/actors/inspector.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,27 @@ impl Actor for InspectorActor {
8686
self.script_chan.send(GetRootNode(pipeline, tx)).unwrap();
8787
let root_info = rx.recv().unwrap().ok_or(())?;
8888

89-
let root = root_info.encode(registry, false, self.script_chan.clone(), pipeline);
89+
let name = self
90+
.walker
91+
.borrow()
92+
.clone()
93+
.unwrap_or_else(|| registry.new_name("walker"));
94+
95+
let root = root_info.encode(
96+
registry,
97+
false,
98+
self.script_chan.clone(),
99+
pipeline,
100+
name.clone(),
101+
);
90102

91103
if self.walker.borrow().is_none() {
92104
let walker = WalkerActor {
93-
name: registry.new_name("walker"),
105+
name,
94106
script_chan: self.script_chan.clone(),
95107
pipeline,
96108
root_node: root.clone(),
109+
mutations: RefCell::new(vec![]),
97110
};
98111
let mut walker_name = self.walker.borrow_mut();
99112
*walker_name = Some(walker.name());

components/devtools/actors/inspector/node.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use serde::Serialize;
1616
use serde_json::{self, Map, Value};
1717

1818
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
19+
use crate::actors::inspector::walker::WalkerActor;
1920
use crate::protocol::JsonPacketStream;
2021
use crate::{EmptyReplyMsg, StreamId};
2122

@@ -77,6 +78,7 @@ pub struct NodeActor {
7778
name: String,
7879
script_chan: IpcSender<DevtoolScriptControlMsg>,
7980
pipeline: PipelineId,
81+
pub walker: String,
8082
}
8183

8284
impl Actor for NodeActor {
@@ -102,12 +104,16 @@ impl Actor for NodeActor {
102104
"modifyAttributes" => {
103105
let target = msg.get("to").ok_or(())?.as_str().ok_or(())?;
104106
let mods = msg.get("modifications").ok_or(())?.as_array().ok_or(())?;
105-
let modifications = mods
107+
let modifications: Vec<_> = mods
106108
.iter()
107109
.filter_map(|json_mod| {
108110
serde_json::from_str(&serde_json::to_string(json_mod).ok()?).ok()
109111
})
110112
.collect();
113+
114+
let walker = registry.find::<WalkerActor>(&self.walker);
115+
walker.new_mutations(stream, &self.name, &modifications);
116+
111117
self.script_chan
112118
.send(ModifyAttribute(
113119
self.pipeline,
@@ -127,8 +133,13 @@ impl Actor for NodeActor {
127133
.send(GetDocumentElement(self.pipeline, tx))
128134
.unwrap();
129135
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
130-
let node =
131-
doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
136+
let node = doc_elem_info.encode(
137+
registry,
138+
true,
139+
self.script_chan.clone(),
140+
self.pipeline,
141+
self.walker.clone(),
142+
);
132143

133144
let msg = GetUniqueSelectorReply {
134145
from: self.name(),
@@ -150,6 +161,7 @@ pub trait NodeInfoToProtocol {
150161
display: bool,
151162
script_chan: IpcSender<DevtoolScriptControlMsg>,
152163
pipeline: PipelineId,
164+
walker: String,
153165
) -> NodeActorMsg;
154166
}
155167

@@ -160,13 +172,15 @@ impl NodeInfoToProtocol for NodeInfo {
160172
display: bool,
161173
script_chan: IpcSender<DevtoolScriptControlMsg>,
162174
pipeline: PipelineId,
175+
walker: String,
163176
) -> NodeActorMsg {
164177
let actor = if !actors.script_actor_registered(self.unique_id.clone()) {
165178
let name = actors.new_name("node");
166179
let node_actor = NodeActor {
167180
name: name.clone(),
168181
script_chan: script_chan.clone(),
169182
pipeline,
183+
walker: walker.clone(),
170184
};
171185
actors.register_script_actor(self.unique_id, name.clone());
172186
actors.register_later(Box::new(node_actor));
@@ -192,7 +206,7 @@ impl NodeInfoToProtocol for NodeInfo {
192206
let mut children = rx.recv().ok()??;
193207

194208
let child = children.pop()?;
195-
let msg = child.encode(actors, true, script_chan.clone(), pipeline);
209+
let msg = child.encode(actors, true, script_chan.clone(), pipeline, walker);
196210

197211
// If the node child is not a text node, do not represent it inline.
198212
if msg.node_type != TEXT_NODE {

components/devtools/actors/inspector/walker.rs

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
//! The walker actor is responsible for traversing the DOM tree in various ways to create new nodes
66
7+
use std::cell::RefCell;
78
use std::net::TcpStream;
89

910
use base::id::PipelineId;
10-
use devtools_traits::DevtoolScriptControlMsg;
1111
use devtools_traits::DevtoolScriptControlMsg::{GetChildren, GetDocumentElement};
12+
use devtools_traits::{DevtoolScriptControlMsg, Modification};
1213
use ipc_channel::ipc::{self, IpcSender};
1314
use serde::Serialize;
1415
use serde_json::{self, Map, Value};
@@ -30,6 +31,7 @@ pub struct WalkerActor {
3031
pub script_chan: IpcSender<DevtoolScriptControlMsg>,
3132
pub pipeline: PipelineId,
3233
pub root_node: NodeActorMsg,
34+
pub mutations: RefCell<Vec<(Modification, String)>>,
3335
}
3436

3537
#[derive(Serialize)]
@@ -69,12 +71,35 @@ struct WatchRootNodeReply {
6971
node: NodeActorMsg,
7072
}
7173

74+
#[derive(Serialize)]
75+
#[serde(rename_all = "camelCase")]
76+
struct MutationMsg {
77+
attribute_name: String,
78+
new_value: Option<String>,
79+
target: String,
80+
#[serde(rename = "type")]
81+
type_: String,
82+
}
83+
84+
#[derive(Serialize)]
85+
struct GetMutationsReply {
86+
from: String,
87+
mutations: Vec<MutationMsg>,
88+
}
89+
7290
#[derive(Serialize)]
7391
struct GetOffsetParentReply {
7492
from: String,
7593
node: Option<()>,
7694
}
7795

96+
#[derive(Serialize)]
97+
struct NewMutationsReply {
98+
from: String,
99+
#[serde(rename = "type")]
100+
type_: String,
101+
}
102+
78103
impl Actor for WalkerActor {
79104
fn name(&self) -> String {
80105
self.name.clone()
@@ -90,6 +115,8 @@ impl Actor for WalkerActor {
90115
///
91116
/// - `getLayoutInspector`: Returns the Layout inspector actor, placeholder
92117
///
118+
/// - `getMutations`: Returns the list of attribute changes since it was last called
119+
///
93120
/// - `getOffsetParent`: Placeholder
94121
///
95122
/// - `querySelector`: Recursively looks for the specified selector in the tree, reutrning the
@@ -121,7 +148,13 @@ impl Actor for WalkerActor {
121148
nodes: children
122149
.into_iter()
123150
.map(|child| {
124-
child.encode(registry, true, self.script_chan.clone(), self.pipeline)
151+
child.encode(
152+
registry,
153+
true,
154+
self.script_chan.clone(),
155+
self.pipeline,
156+
self.name(),
157+
)
125158
})
126159
.collect(),
127160
from: self.name(),
@@ -140,8 +173,13 @@ impl Actor for WalkerActor {
140173
.send(GetDocumentElement(self.pipeline, tx))
141174
.map_err(|_| ())?;
142175
let doc_elem_info = rx.recv().map_err(|_| ())?.ok_or(())?;
143-
let node =
144-
doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
176+
let node = doc_elem_info.encode(
177+
registry,
178+
true,
179+
self.script_chan.clone(),
180+
self.pipeline,
181+
self.name(),
182+
);
145183

146184
let msg = DocumentElementReply {
147185
from: self.name(),
@@ -163,6 +201,24 @@ impl Actor for WalkerActor {
163201
let _ = stream.write_json_packet(&msg);
164202
ActorMessageStatus::Processed
165203
},
204+
"getMutations" => {
205+
let msg = GetMutationsReply {
206+
from: self.name(),
207+
mutations: self
208+
.mutations
209+
.borrow_mut()
210+
.drain(..)
211+
.map(|(mutation, target)| MutationMsg {
212+
attribute_name: mutation.attribute_name,
213+
new_value: mutation.new_value,
214+
target,
215+
type_: "attributes".into(),
216+
})
217+
.collect(),
218+
};
219+
let _ = stream.write_json_packet(&msg);
220+
ActorMessageStatus::Processed
221+
},
166222
"getOffsetParent" => {
167223
let msg = GetOffsetParentReply {
168224
from: self.name(),
@@ -177,6 +233,7 @@ impl Actor for WalkerActor {
177233
let mut hierarchy = find_child(
178234
&self.script_chan,
179235
self.pipeline,
236+
&self.name,
180237
registry,
181238
selector,
182239
node,
@@ -211,11 +268,30 @@ impl Actor for WalkerActor {
211268
}
212269
}
213270

271+
impl WalkerActor {
272+
pub(crate) fn new_mutations(
273+
&self,
274+
stream: &mut TcpStream,
275+
target: &str,
276+
modifications: &[Modification],
277+
) {
278+
{
279+
let mut mutations = self.mutations.borrow_mut();
280+
mutations.extend(modifications.iter().cloned().map(|m| (m, target.into())));
281+
}
282+
let _ = stream.write_json_packet(&NewMutationsReply {
283+
from: self.name(),
284+
type_: "newMutations".into(),
285+
});
286+
}
287+
}
288+
214289
/// Recursively searches for a child with the specified selector
215290
/// If it is found, returns a list with the child and all of its ancestors.
216291
fn find_child(
217292
script_chan: &IpcSender<DevtoolScriptControlMsg>,
218293
pipeline: PipelineId,
294+
name: &str,
219295
registry: &ActorRegistry,
220296
selector: &str,
221297
node: &str,
@@ -232,7 +308,7 @@ fn find_child(
232308
let children = rx.recv().unwrap().ok_or(vec![])?;
233309

234310
for child in children {
235-
let msg = child.encode(registry, true, script_chan.clone(), pipeline);
311+
let msg = child.encode(registry, true, script_chan.clone(), pipeline, name.into());
236312
if msg.display_name == selector {
237313
hierarchy.push(msg);
238314
return Ok(hierarchy);
@@ -245,6 +321,7 @@ fn find_child(
245321
match find_child(
246322
script_chan,
247323
pipeline,
324+
name,
248325
registry,
249326
selector,
250327
&msg.actor,

components/script/devtools.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ pub fn handle_modify_attribute(
235235
node_id: String,
236236
modifications: Vec<Modification>,
237237
) {
238+
let Some(document) = documents.find_document(pipeline) else {
239+
return warn!("document for pipeline id {} is not found", &pipeline);
240+
};
241+
let _realm = enter_realm(document.window());
242+
238243
let node = match find_node_by_unique_id(documents, pipeline, &node_id) {
239244
None => {
240245
return warn!(

components/shared/devtools/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub enum DevtoolScriptControlMsg {
222222
Reload(PipelineId),
223223
}
224224

225-
#[derive(Debug, Deserialize, Serialize)]
225+
#[derive(Clone, Debug, Deserialize, Serialize)]
226226
#[serde(rename_all = "camelCase")]
227227
pub struct Modification {
228228
pub attribute_name: String,

0 commit comments

Comments
 (0)