servo-devtools 0.3.0

A component of the servo web-engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! This actor represents one DOM node. It is created by the Walker actor when it is traversing the
//! document tree.

use std::collections::HashMap;

use atomic_refcell::AtomicRefCell;
use devtools_traits::{
    AttrModification, DevtoolScriptControlMsg, EventListenerInfo, MatchedRule, NodeInfo,
    ShadowRootMode,
};
use malloc_size_of_derive::MallocSizeOf;
use serde::Serialize;
use serde_json::{self, Map, Value};
use servo_base::generic_channel::{self, GenericSender};
use servo_base::id::PipelineId;

use crate::actor::{Actor, ActorError, ActorRegistry};
use crate::protocol::ClientRequest;
use crate::{EmptyReplyMsg, StreamId};

/// Text node type constant. This is defined again to avoid depending on `script`, where it is defined originally.
/// See `script::dom::bindings::codegen::Bindings::NodeBinding::NodeConstants`.
const TEXT_NODE: u16 = 3;

/// The maximum length of a text node for it to appear as an inline child in the inspector.
const MAX_INLINE_LENGTH: usize = 50;

#[derive(Serialize)]
struct GetEventListenerInfoReply {
    from: String,
    events: Vec<DevtoolsEventListenerInfo>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct DevtoolsEventListenerInfo {
    r#type: String,
    handler: String,
    origin: String,
    tags: String,
    capturing: bool,
    // This will always be an empty object, we just need a value that serializes to "{}".
    hide: Value,
    native: bool,
    source_actor: String,
    enabled: bool,
    is_user_defined: bool,
    event_listener_info_id: String,
}

#[derive(Serialize)]
struct GetUniqueSelectorReply {
    from: String,
    value: String,
}

#[derive(Serialize)]
struct GetXPathReply {
    from: String,
    value: String,
}

#[derive(Clone, Serialize)]
struct AttrMsg {
    name: String,
    value: String,
}

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct NodeActorMsg {
    pub actor: String,

    /// The ID of the shadow host of this node, if it is
    /// a shadow root
    host: Option<String>,
    #[serde(rename = "baseURI")]
    base_uri: String,
    causes_overflow: bool,
    container_type: Option<()>,
    pub display_name: String,
    display_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    inline_text_child: Option<Box<NodeActorMsg>>,
    is_after_pseudo_element: bool,
    is_anonymous: bool,
    is_before_pseudo_element: bool,
    is_direct_shadow_host_child: Option<bool>,
    /// Whether or not this node is displayed.
    ///
    /// Setting this value to `false` will cause the devtools to render the node name in gray.
    is_displayed: bool,
    #[serde(rename = "isInHTMLDocument")]
    is_in_html_document: Option<bool>,
    is_marker_pseudo_element: bool,
    is_native_anonymous: bool,
    is_scrollable: bool,
    is_shadow_host: bool,
    is_shadow_root: bool,
    is_top_level_document: bool,
    node_name: String,
    node_type: u16,
    node_value: Option<String>,
    pub num_children: usize,
    #[serde(skip_serializing_if = "String::is_empty")]
    parent: String,
    shadow_root_mode: Option<String>,
    traits: HashMap<String, ()>,
    attrs: Vec<AttrMsg>,

    /// The `DOCTYPE` name if this is a `DocumentType` node, `None` otherwise
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,

    /// The `DOCTYPE` public identifier if this is a `DocumentType` node, `None` otherwise
    #[serde(skip_serializing_if = "Option::is_none")]
    public_id: Option<String>,

    /// The `DOCTYPE` system identifier if this is a `DocumentType` node, `None` otherwise
    #[serde(skip_serializing_if = "Option::is_none")]
    system_id: Option<String>,

    has_event_listeners: bool,
}

#[derive(MallocSizeOf)]
pub(crate) struct NodeActor {
    name: String,
    pub script_chan: GenericSender<DevtoolScriptControlMsg>,
    pub pipeline: PipelineId,
    pub walker_name: String,
    pub style_rules: AtomicRefCell<HashMap<MatchedRule, String>>,
}

impl Actor for NodeActor {
    fn name(&self) -> String {
        self.name.clone()
    }

    /// The node actor can handle the following messages:
    ///
    /// - `modifyAttributes`: Asks the script to change a value in the attribute of the
    ///   corresponding node
    ///
    /// - `getUniqueSelector`: Returns the display name of this node
    fn handle_message(
        &self,
        request: ClientRequest,
        registry: &ActorRegistry,
        msg_type: &str,
        msg: &Map<String, Value>,
        _id: StreamId,
    ) -> Result<(), ActorError> {
        match msg_type {
            "modifyAttributes" => {
                let mods = msg
                    .get("modifications")
                    .ok_or(ActorError::MissingParameter)?
                    .as_array()
                    .ok_or(ActorError::BadParameterType)?;
                let modifications: Vec<AttrModification> = mods
                    .iter()
                    .filter_map(|json_mod| {
                        serde_json::from_str(&serde_json::to_string(json_mod).ok()?).ok()
                    })
                    .collect();

                self.script_chan
                    .send(DevtoolScriptControlMsg::ModifyAttribute(
                        self.pipeline,
                        registry.actor_to_script(self.name()),
                        modifications,
                    ))
                    .map_err(|_| ActorError::Internal)?;

                let reply = EmptyReplyMsg { from: self.name() };
                request.reply_final(&reply)?
            },
            "getEventListenerInfo" => {
                let target = msg
                    .get("to")
                    .ok_or(ActorError::MissingParameter)?
                    .as_str()
                    .ok_or(ActorError::BadParameterType)?;

                let (tx, rx) = generic_channel::channel().ok_or(ActorError::Internal)?;
                self.script_chan
                    .send(DevtoolScriptControlMsg::GetEventListenerInfo(
                        self.pipeline,
                        registry.actor_to_script(target.to_owned()),
                        tx,
                    ))
                    .unwrap();
                let event_listeners = rx.recv().map_err(|_| ActorError::Internal)?;

                let msg = GetEventListenerInfoReply {
                    from: self.name(),
                    events: event_listeners.into_iter().map(From::from).collect(),
                };
                request.reply_final(&msg)?
            },
            "getUniqueSelector" => {
                let (tx, rx) = generic_channel::channel().unwrap();
                self.script_chan
                    .send(DevtoolScriptControlMsg::GetDocumentElement(
                        self.pipeline,
                        tx,
                    ))
                    .unwrap();
                let doc_elem_info = rx
                    .recv()
                    .map_err(|_| ActorError::Internal)?
                    .ok_or(ActorError::Internal)?;
                let node = doc_elem_info.encode(
                    registry,
                    self.script_chan.clone(),
                    self.pipeline,
                    self.walker_name.clone(),
                );

                let msg = GetUniqueSelectorReply {
                    from: self.name(),
                    value: node.display_name,
                };
                request.reply_final(&msg)?
            },
            "getXPath" => {
                let target = msg
                    .get("to")
                    .ok_or(ActorError::MissingParameter)?
                    .as_str()
                    .ok_or(ActorError::BadParameterType)?;

                let (tx, rx) = generic_channel::channel().unwrap();
                self.script_chan
                    .send(DevtoolScriptControlMsg::GetXPath(
                        self.pipeline,
                        registry.actor_to_script(target.to_owned()),
                        tx,
                    ))
                    .unwrap();

                let xpath_selector = rx.recv().map_err(|_| ActorError::Internal)?;
                let msg = GetXPathReply {
                    from: self.name(),
                    value: xpath_selector,
                };
                request.reply_final(&msg)?
            },

            _ => return Err(ActorError::UnrecognizedPacketType),
        };
        Ok(())
    }
}

impl NodeActor {
    pub fn register(
        registry: &ActorRegistry,
        script_id: String,
        script_chan: GenericSender<DevtoolScriptControlMsg>,
        pipeline: PipelineId,
        walker_name: String,
    ) -> String {
        let name = registry.new_name::<Self>();

        registry.register_script_actor(script_id, name.clone());

        let actor = Self {
            name: name.clone(),
            script_chan,
            pipeline,
            walker_name,
            style_rules: AtomicRefCell::new(HashMap::new()),
        };

        registry.register(actor);
        name
    }
}
pub trait NodeInfoToProtocol {
    fn encode(
        self,
        registry: &ActorRegistry,
        script_chan: GenericSender<DevtoolScriptControlMsg>,
        pipeline: PipelineId,
        walker_name: String,
    ) -> NodeActorMsg;
}

impl NodeInfoToProtocol for NodeInfo {
    fn encode(
        self,
        registry: &ActorRegistry,
        script_chan: GenericSender<DevtoolScriptControlMsg>,
        pipeline: PipelineId,
        walker_name: String,
    ) -> NodeActorMsg {
        let get_or_register_node_actor = |id: &str| {
            if !registry.script_actor_registered(id.to_string()) {
                NodeActor::register(
                    registry,
                    id.to_string(),
                    script_chan.clone(),
                    pipeline,
                    walker_name.clone(),
                )
            } else {
                registry.script_to_actor(id.to_string())
            }
        };

        let actor = get_or_register_node_actor(&self.unique_id);
        let host = self
            .host
            .as_ref()
            .map(|host_id| get_or_register_node_actor(host_id));

        let name = registry.actor_to_script(actor.clone());

        // If a node only has a single text node as a child whith a small enough text,
        // return it with this node as an `inlineTextChild`.
        let inline_text_child = (|| {
            // TODO: Also return if this node is a flex element.
            if self.num_children != 1 || self.node_name == "SLOT" {
                return None;
            }

            let (tx, rx) = generic_channel::channel()?;
            script_chan
                .send(DevtoolScriptControlMsg::GetChildren(
                    pipeline,
                    name.clone(),
                    tx,
                ))
                .unwrap();
            let mut children = rx.recv().ok()??;

            let child = children.pop()?;
            let msg = child.encode(registry, script_chan.clone(), pipeline, walker_name);

            // If the node child is not a text node, do not represent it inline.
            if msg.node_type != TEXT_NODE {
                return None;
            }

            // If the text node child is too big, do not represent it inline.
            if msg.node_value.clone().unwrap_or_default().len() > MAX_INLINE_LENGTH {
                return None;
            }

            Some(Box::new(msg))
        })();

        NodeActorMsg {
            actor,
            host,
            base_uri: self.base_uri,
            causes_overflow: false,
            container_type: None,
            display_name: self.node_name.clone().to_lowercase(),
            display_type: self.display,
            inline_text_child,
            is_after_pseudo_element: false,
            is_anonymous: false,
            is_before_pseudo_element: false,
            is_direct_shadow_host_child: None,
            is_displayed: self.is_displayed,
            is_in_html_document: Some(true),
            is_marker_pseudo_element: false,
            is_native_anonymous: false,
            is_scrollable: false,
            is_shadow_host: self.is_shadow_host,
            is_shadow_root: self.shadow_root_mode.is_some(),
            is_top_level_document: self.is_top_level_document,
            node_name: self.node_name,
            node_type: self.node_type,
            node_value: self.node_value,
            num_children: self.num_children,
            parent: registry.script_to_actor(self.parent.clone()),
            shadow_root_mode: self
                .shadow_root_mode
                .as_ref()
                .map(ShadowRootMode::to_string),
            traits: HashMap::new(),
            attrs: self
                .attrs
                .into_iter()
                .map(|attr| AttrMsg {
                    name: attr.name,
                    value: attr.value,
                })
                .collect(),
            name: self.doctype_name,
            public_id: self.doctype_public_identifier,
            system_id: self.doctype_system_identifier,
            has_event_listeners: self.has_event_listeners,
        }
    }
}

impl From<EventListenerInfo> for DevtoolsEventListenerInfo {
    fn from(event_listener_info: EventListenerInfo) -> Self {
        Self {
            r#type: event_listener_info.event_type,
            handler: "todo".to_owned(),
            capturing: event_listener_info.capturing,
            origin: "todo".to_owned(),
            tags: "".to_owned(),
            hide: Value::Object(Default::default()),
            native: false,
            source_actor: "todo".to_owned(),
            enabled: true,
            is_user_defined: false,
            event_listener_info_id: "todo".to_owned(),
        }
    }
}