on master help sign in
Make `Attribute`'s fields private and instead expose an `attr` builder utility
commit07dca7eba925e49f3d9531dc421d566bd23541a9 authorNick Fitzgerald <fitzgen@gmail.com> dateWed 13 Feb 2019, 15:27 (8 years ago) parent3d69bdd
3 files changed+36−16
src/builder.rs+18−8 src/node.rs+14−4 tests/web/main.rs+4−4
src/builder.rs+18−8
@@ -87,14 +87,8 @@ where
8787 /// // Create a `<div>` with a fixed-size array of two attributes.
8888 /// let my_div = div(&b)
8989 /// .attributes([
90− /// Attribute {
91− /// name: "id",
92− /// value: "my-div",
93− /// },
94− /// Attribute {
95− /// name: "class",
96− /// value: "notification",
97− /// },
90+ /// attr("id", "my-div"),
91+ /// attr("class", "notification"),
9892 /// ])
9993 /// .finish();
10094 /// ```
@@ -846,3 +840,19 @@ builder_constructors! {
846840pub fn text<'a>(contents: &'a str) -> Node<'a> {
847841 Node::text(contents)
848842}
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> {
5252/// `href="https://example.com"`.
5353#[derive(Clone, Debug)]
5454pub struct Attribute<'a> {
55− /// The attribute name, such as `id`.
56− pub name: &'a str,
57− /// The attribute value, such as `"my-thing"`.
58− pub value: &'a str,
55+ pub(crate) name: &'a str,
56+ pub(crate) value: &'a str,
5957}
6058
6159impl fmt::Debug for Listener<'_> {
@@ -71,6 +69,18 @@ impl fmt::Debug for Listener<'_> {
7169}
7270
7371impl<'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+
7484 /// Certain attributes are considered "volatile" and can change via user
7585 /// input that we can't see when diffing against the old virtual DOM. For
7686 /// 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) {
103103 );
104104 for attr in expected {
105105 let actual_attr = actual
106− .get_named_item(attr.name)
107− .expect(&format!("should have attribute \"{}\"", attr.name));
106+ .get_named_item(attr.name())
107+ .expect(&format!("should have attribute \"{}\"", attr.name()));
108108 assert_eq!(
109109 actual_attr.value(),
110− attr.value,
110+ attr.value(),
111111 "actual attr value == expected attr value for attr \"{}\"",
112− attr.name
112+ attr.name()
113113 );
114114 }
115115 }