on master help sign in
Make `Attribute`'s fields private and instead expose an `attr` builder utility
07dca7eNick Fitzgerald
src/builder.rs+18−8
@@ -87,14 +87,8 @@ where
87 /// // Create a `<div>` with a fixed-size array of two attributes.
87 /// // Create a `<div>` with a fixed-size array of two attributes.
88 /// let my_div = div(&b)
88 /// let my_div = div(&b)
89 /// .attributes([
89 /// .attributes([
90− /// Attribute {
91− /// name: "id",
92− /// value: "my-div",
90+ /// attr("id", "my-div"),
93− /// },
94− /// Attribute {
95− /// name: "class",
96− /// value: "notification",
91+ /// attr("class", "notification"),
97− /// },
98 /// ])
92 /// ])
99 /// .finish();
93 /// .finish();
100 /// ```
94 /// ```
@@ -846,3 +840,19 @@ builder_constructors! {
846pub fn text<'a>(contents: &'a str) -> Node<'a> {
840pub fn text<'a>(contents: &'a str) -> Node<'a> {
847 Node::text(contents)
841 Node::text(contents)
848}
842}
843+
844+/// Construct an attribute for an element.
845+///
846+/// # Example
847+///
848+/// This example creates the `id="my-id"` for some element like `<div
849+/// id="my-id"/>`.
850+///
851+/// ```no_run
852+/// use dodrio::builder::*;
853+///
854+/// let my_id_attr = attr("id", "my-id");
855+/// ```
856+pub fn attr<'a>(name: &'a str, value: &'a str) -> Attribute<'a> {
857+ Attribute { name, value }
858+}
src/node.rs+14−4
@@ -52,10 +52,8 @@ pub struct Listener<'a> {
52/// `href="https://example.com"`.
52/// `href="https://example.com"`.
53#[derive(Clone, Debug)]
53#[derive(Clone, Debug)]
54pub struct Attribute<'a> {
54pub struct Attribute<'a> {
55− /// The attribute name, such as `id`.
56− pub name: &'a str,
55+ pub(crate) name: &'a str,
57− /// The attribute value, such as `"my-thing"`.
58− pub value: &'a str,
56+ pub(crate) value: &'a str,
59}
57}
60
58
61impl fmt::Debug for Listener<'_> {
59impl fmt::Debug for Listener<'_> {
@@ -71,6 +69,18 @@ impl fmt::Debug for Listener<'_> {
71}
69}
72
70
73impl<'a> Attribute<'a> {
71impl<'a> Attribute<'a> {
72+ /// Get this attribute's name, such as `"id"` in `<div id="my-thing" />`.
73+ #[inline]
74+ pub fn name(&self) -> &'a str {
75+ self.name
76+ }
77+
78+ /// The attribute value, such as `"my-thing"` in `<div id="my-thing" />`.
79+ #[inline]
80+ pub fn value(&self) -> &'a str {
81+ self.value
82+ }
83+
74 /// Certain attributes are considered "volatile" and can change via user
84 /// Certain attributes are considered "volatile" and can change via user
75 /// input that we can't see when diffing against the old virtual DOM. For
85 /// input that we can't see when diffing against the old virtual DOM. For
76 /// these attributes, we want to always re-set the attribute on the physical
86 /// these attributes, we want to always re-set the attribute on the physical
tests/web/main.rs+4−4
@@ -103,13 +103,13 @@ pub fn assert_rendered<R: Render>(container: &web_sys::Element, r: &R) {
103 );
103 );
104 for attr in expected {
104 for attr in expected {
105 let actual_attr = actual
105 let actual_attr = actual
106− .get_named_item(attr.name)
106+ .get_named_item(attr.name())
107− .expect(&format!("should have attribute \"{}\"", attr.name));
107+ .expect(&format!("should have attribute \"{}\"", attr.name()));
108 assert_eq!(
108 assert_eq!(
109 actual_attr.value(),
109 actual_attr.value(),
110− attr.value,
110+ attr.value(),
111 "actual attr value == expected attr value for attr \"{}\"",
111 "actual attr value == expected attr value for attr \"{}\"",
112− attr.name
112+ attr.name()
113 );
113 );
114 }
114 }
115 }
115 }