mogwai 0.7.4

The minimal, obvious, graphical, widget application interface.
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! # Server-Side Rendering (SSR)
//!
//! This module provides the implementation for server-side rendering of views.
//! It provides types and trait impls necessary to create and manipulate HTML
//! on the server side, allowing for the generation of string content that can
//! be sent to clients.
//!
//! ## Key Components
//!
//! - **[`SsrText`]**: Represents text nodes in the SSR context, allowing for text
//!   content manipulation and event handling.
//!
//! - **[`SsrElement`]**: Represents HTML elements in the SSR context, providing
//!   methods for managing attributes, styles, and child nodes.
//!
//! - **[`SsrNode`]**: An enumeration of possible node types (elements and text)
//!   used in SSR.
//!
//! - **[`SsrEventListener`]**: Handles event listening in the SSR context, enabling
//!   asynchronous event handling.
//!
//! ## Usage
//!
//! This module is intended for use in environments where server-side rendering
//! is required, providing a way to generate HTML content dynamically based on
//! application state and logic.
use std::{borrow::Cow, sync::atomic::AtomicUsize};

use crate::{
    Str,
    sync::{Global, Shared},
    view::*,
};

pub mod prelude {
    pub use super::{Ssr, SsrElement, SsrEventListener, SsrEventTarget, SsrText};
    pub use crate::prelude::*;
}

static NEXT_ID: Global<AtomicUsize> = Global::new(|| AtomicUsize::new(0));

fn next_id() -> usize {
    NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}

#[derive(Clone)]
pub struct SsrText {
    pub id: usize,
    pub text: Shared<Str>,
    pub events: Shared<Vec<SsrEventListener>>,
}

impl PartialEq for SsrText {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl ViewText for SsrText {
    fn new(text: impl AsRef<str>) -> Self {
        SsrText {
            id: next_id(),
            text: Shared::from_string(text),
            events: Default::default(),
        }
    }

    fn set_text(&self, text: impl AsRef<str>) {
        let cow = Cow::from(text.as_ref().to_owned());
        self.text.set(cow);
    }

    fn get_text(&self) -> Str {
        self.text.get().clone()
    }
}

impl ViewChild<Ssr> for SsrText {
    fn as_append_arg(&self) -> AppendArg<Ssr, impl Iterator<Item = Cow<'_, SsrNode>>> {
        AppendArg::new(std::iter::once(Cow::Owned(SsrNode::Text(self.clone()))))
    }
}

impl ViewEventTarget<Ssr> for SsrText {
    fn listen(&self, event_name: impl Into<Str>) -> <Ssr as View>::EventListener {
        let listener = SsrEventListener::new(SsrEventTarget::Node(self.clone().into()), event_name);
        self.events.get_mut().push(listener.clone());
        listener
    }
}

#[derive(Clone)]
pub struct SsrElement {
    pub id: usize,
    pub name: Str,
    pub attributes: Shared<Vec<(Str, Option<Str>)>>,
    pub styles: Shared<Vec<(Str, Str)>>,
    pub events: Shared<Vec<SsrEventListener>>,
    pub children: Shared<Vec<SsrNode>>,
}

impl PartialEq for SsrElement {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl ViewParent<Ssr> for SsrElement {
    fn append_node(&self, node: Cow<'_, <Ssr as View>::Node>) {
        self.children.get_mut().push(node.into_owned());
    }

    fn remove_node(&self, node: Cow<'_, <Ssr as View>::Node>) {
        self.children
            .get_mut()
            .retain(|child| child != node.as_ref());
    }

    fn replace_node(
        &self,
        new_node: Cow<'_, <Ssr as View>::Node>,
        old_node: Cow<'_, <Ssr as View>::Node>,
    ) {
        let mut children = self.children.get_mut();
        let found = children
            .iter_mut()
            .find(|child| *child == old_node.as_ref());
        if let Some(node) = found {
            *node = new_node.into_owned();
        }
    }

    fn insert_node_before(
        &self,
        new_node: Cow<'_, <Ssr as View>::Node>,
        before_node: Option<Cow<'_, <Ssr as View>::Node>>,
    ) {
        if let Some(before_node) = before_node {
            let mut children = self.children.get_mut();
            let found = children.iter().enumerate().find_map(|(i, child)| {
                if child == before_node.as_ref() {
                    Some(i)
                } else {
                    None
                }
            });
            if let Some(index) = found {
                children.insert(index, new_node.into_owned());
            }
        } else {
            self.append_node(new_node);
        }
    }
}

impl ViewChild<Ssr> for SsrElement {
    fn as_append_arg(&self) -> AppendArg<Ssr, impl Iterator<Item = Cow<'_, SsrNode>>> {
        AppendArg::new(std::iter::once(Cow::Owned(SsrNode::Element(self.clone()))))
    }
}

impl ViewProperties for SsrElement {
    fn set_property(&self, key: impl AsRef<str>, value: impl AsRef<str>) {
        let mut attributes = self.attributes.get_mut();
        let (k, v) = (
            key.as_ref().to_owned().into(),
            value.as_ref().to_owned().into(),
        );
        for (k_prev, v_prev) in attributes.iter_mut() {
            if k_prev == &k {
                *v_prev = Some(v);
                return;
            }
        }
        attributes.push((k, Some(v)));
    }

    fn has_property(&self, key: impl AsRef<str>) -> bool {
        for (pkey, _pval) in self.attributes.get().iter() {
            if pkey == key.as_ref() {
                return true;
            }
        }
        false
    }

    fn get_property(&self, key: impl AsRef<str>) -> Option<Str> {
        for (pkey, pval) in self.attributes.get().iter() {
            if pkey == key.as_ref() {
                return pval.clone();
            }
        }
        None
    }

    fn remove_property(&self, key: impl AsRef<str>) {
        self.attributes
            .get_mut()
            .retain_mut(|p| p.0 != key.as_ref());
    }

    /// Add a style property.
    fn set_style(&self, key: impl AsRef<str>, value: impl AsRef<str>) {
        let mut styles = self.styles.get_mut();
        let key = key.as_ref().to_owned().into();
        let value = value.as_ref().to_owned().into();
        for (pkey, pval) in styles.iter_mut() {
            if pkey == &key {
                *pval = value;
                return;
            }
        }
        styles.push((key, value));
    }

    /// Remove a style property.
    ///
    /// Returns the previous style value, if any.
    fn remove_style(&self, key: impl AsRef<str>) {
        self.styles.get_mut().retain_mut(|p| p.0 != key.as_ref());
    }
}

impl ViewEventTarget<Ssr> for SsrElement {
    fn listen(&self, event_name: impl Into<Str>) -> SsrEventListener {
        let event_listener = SsrEventListener::new(
            SsrEventTarget::Node(SsrNode::Element(self.clone())),
            event_name,
        );
        self.events.get_mut().push(event_listener.clone());
        event_listener
    }
}

impl SsrElement {
    pub fn html_string(&self) -> String {
        // Only certain nodes can be "void" - which means written as <tag /> when
        // the node contains no children. Writing non-void nodes in void notation
        // does some spooky things to the DOM at parse-time.
        //
        // From https://riptutorial.com/html/example/4736/void-elements
        // HTML 4.01/XHTML 1.0 Strict includes the following void elements:
        //
        //     rea - clickable, defined area in an image
        //     base - specifies a base URL from which all links base
        //     br - line break
        //     col - column in a table [deprecated]
        //     hr - horizontal rule (line)
        //     img - image
        //     input - field where users enter data
        //     link - links an external resource to the document
        //     meta - provides information about the document
        //     param - defines parameters for plugins
        //
        //     HTML 5 standards include all non-deprecated tags from the previous list
        // and
        //
        //     command - represents a command users can invoke [obsolete]
        //     keygen - facilitates public key generation for web certificates
        // [deprecated]     source - specifies media sources for picture, audio, and
        // video elements
        fn tag_is_voidable(tag: &str) -> bool {
            tag == "area"
                || tag == "base"
                || tag == "br"
                || tag == "col"
                || tag == "hr"
                || tag == "img"
                || tag == "input"
                || tag == "link"
                || tag == "meta"
                || tag == "param"
                || tag == "command"
                || tag == "keygen"
                || tag == "source"
        }
        let name = &self.name;
        let styles = self.styles.get();
        let attributes = self.attributes.get_mut();
        let children = self.children.get();

        let mut attributes = attributes.clone();
        if !styles.is_empty() {
            let styles = styles
                .iter()
                .map(|(k, v)| format!("{}: {};", k, v))
                .collect::<Vec<_>>()
                .join(" ");

            let mut style_added = false;
            for (key, value) in attributes.iter_mut() {
                if key == "style" {
                    if let Some(prev_style) = value.take() {
                        let spaced = (prev_style + " " + styles.as_str()).into_owned();
                        *value = Some(spaced.into());
                        style_added = true;
                        break;
                    }
                }
            }
            if !style_added {
                attributes.push(("style".into(), Some(styles.into())));
            }
        }

        let atts = attributes
            .iter()
            .map(|(key, may_val)| {
                if let Some(val) = may_val {
                    format!(r#"{}="{}""#, key, val)
                } else {
                    format!("{}", key)
                }
            })
            .collect::<Vec<_>>()
            .join(" ");

        if children.is_empty() {
            if attributes.is_empty() {
                if tag_is_voidable(name) {
                    format!("<{} />", name)
                } else {
                    format!("<{}></{}>", name, name)
                }
            } else if tag_is_voidable(name) {
                format!("<{} {} />", name, atts)
            } else {
                format!("<{} {}></{}>", name, atts, name)
            }
        } else {
            let mut kids = vec![];
            for kid in children.iter() {
                let node = match kid {
                    SsrNode::Element(element_builder) => element_builder.html_string(),
                    SsrNode::Text(text_builder) => text_builder.text.get().to_string(),
                };
                kids.push(node);
            }
            let kids: String = kids.concat();
            if attributes.is_empty() {
                format!("<{}>{}</{}>", name, kids, name)
            } else {
                format!("<{} {}>{}</{}>", name, atts, kids, name)
            }
        }
    }
}

#[derive(Clone, PartialEq)]
pub enum SsrNode {
    Element(SsrElement),
    Text(SsrText),
}

impl From<SsrText> for SsrNode {
    fn from(value: SsrText) -> Self {
        SsrNode::Text(value)
    }
}

impl From<SsrElement> for SsrNode {
    fn from(value: SsrElement) -> Self {
        SsrNode::Element(value)
    }
}

impl SsrNode {
    pub fn id(&self) -> usize {
        match self {
            SsrNode::Element(ssr_element) => ssr_element.id,
            SsrNode::Text(ssr_text) => ssr_text.id,
        }
    }

    pub fn name(&self) -> String {
        match self {
            SsrNode::Element(ssr_element) => ssr_element.name.to_string(),
            SsrNode::Text(ssr_text) => ssr_text.text.get().to_string(),
        }
    }
}

#[derive(Clone, PartialEq)]
pub enum SsrEventTarget {
    Node(SsrNode),
    Window,
    Document,
}

#[derive(Clone)]
pub struct SsrEventListener {
    pub name: Str,
    pub target: SsrEventTarget,
    pub channel: Shared<Option<(async_channel::Sender<()>, async_channel::Receiver<()>)>>,
}

impl PartialEq for SsrEventListener {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.target == other.target
    }
}

impl ViewEventListener<Ssr> for SsrEventListener {
    fn next(&self) -> impl Future<Output = ()> {
        self.ensure_channel();
        let channel = self.channel.get();
        let (_, rx) = channel.as_ref().unwrap();
        let rx = rx.clone();
        async move { rx.recv().await.unwrap() }
    }

    fn on_window(event_name: impl Into<Cow<'static, str>>) -> <Ssr as View>::EventListener {
        SsrEventListener::new(SsrEventTarget::Window, event_name)
    }

    fn on_document(event_name: impl Into<Cow<'static, str>>) -> <Ssr as View>::EventListener {
        SsrEventListener::new(SsrEventTarget::Document, event_name)
    }
}

impl SsrEventListener {
    pub fn new(target: SsrEventTarget, name: impl Into<Str>) -> Self {
        SsrEventListener {
            name: name.into(),
            channel: Default::default(),
            target,
        }
    }

    fn ensure_channel(&self) {
        if self.channel.get().is_none() {
            *self.channel.get_mut() = Some(async_channel::bounded(1));
        }
    }

    /// Fire an event occurence to any waiting listeners.
    pub async fn fire(&self) {
        self.ensure_channel();
        let channel = self.channel.get();
        let (tx, _) = channel.as_ref().unwrap();
        tx.send(()).await.unwrap();
    }
}

#[derive(Clone)]
pub struct Ssr;

impl View for Ssr {
    type Element = SsrElement;
    type Text = SsrText;
    type Node = SsrNode;
    type EventListener = SsrEventListener;
    type Event = ();
}

impl ViewElement for SsrElement {
    type View = Ssr;

    fn new(name: impl AsRef<str>) -> Self {
        Self {
            id: next_id(),
            name: name.as_ref().to_owned().into(),
            attributes: Default::default(),
            styles: Default::default(),
            events: Default::default(),
            children: Default::default(),
        }
    }
}

impl ViewEvent for () {
    type View = Ssr;
}

#[cfg(test)]
mod test {
    use crate as mogwai;
    use mogwai::ssr::prelude::*;

    #[test]
    fn proxy_update_text_node() {
        #[derive(Debug, PartialEq)]
        struct Status {
            color: String,
            message: String,
        }

        struct Widget<V: View> {
            root: V::Element,
            state: Proxy<Status>,
        }

        fn new_widget<V: View>() -> Widget<V> {
            let mut state = Proxy::new(Status {
                color: "black".to_string(),
                message: "Hello".to_string(),
            });

            // We start out with a `div` element bound to `root`, containing a nested `p` tag
            // with the message "Hello" in black.
            rsx! {
                let root = div() {
                    p(
                        id = "message_wrapper",
                        // proxy use in attribute position
                        style:color = state(s => &s.color)
                    ) {
                        // proxy use in node position
                        {state(s => {
                            println!("updating state to: {s:#?}");
                            &s.message
                        })}
                    }
                }
            }

            Widget { root, state }
        }

        println!("creating");
        // Verify at creation that the view shows "Hello" in black.
        let mut w = new_widget::<mogwai::ssr::Ssr>();
        assert_eq!(
            r#"<div><p id="message_wrapper" style="color: black;">Hello</p></div>"#,
            w.root.html_string()
        );

        // Then later we change the message to show "Goodbye" in red.
        w.state.set(Status {
            color: "red".to_string(),
            message: "Goodbye".to_string(),
        });
        assert_eq!(
            r#"<div><p id="message_wrapper" style="color: red;">Goodbye</p></div>"#,
            w.root.html_string()
        );
    }

    #[test]
    fn no_whitespace() {
        fn view<V: View>() -> V::Element {
            rsx! {
                let view = div(){
                    div(){ "test1" }
                    span(){ "test2" }
                    span(){ "test3" }
                }
            }
            view
        }

        let view = view::<Ssr>();
        let expected = "<div><div>test1</div><span>test2</span><span>test3</span></div>";
        assert_eq!(expected, view.html_string());
    }
}