Skip to content

Commit f9c4309

Browse files
Mingundralley
authored andcommitted
Improve documentation for Attributes slightly
1 parent 9c7acf6 commit f9c4309

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/events/attributes.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,54 @@ impl<'a> Attributes<'a> {
256256
}
257257
}
258258

259-
/// Creates a new attribute iterator from a buffer.
259+
/// Creates a new attribute iterator from a buffer, which recognizes only XML-style
260+
/// attributes, i. e. those which in the form `name = "value"` or `name = 'value'`.
261+
/// HTML style attributes (i. e. without quotes or only name) will return a error.
262+
///
263+
/// # Parameters
264+
/// - `buf`: a buffer with a tag name and attributes, usually this is the whole
265+
/// string between `<` and `>` (or `/>`) of a tag;
266+
/// - `pos`: a position in the `buf` where tag name is finished and attributes
267+
/// is started. It is not necessary to point exactly to the end of a tag name,
268+
/// although that is usually that. If it will be more than the `buf` length,
269+
/// then the iterator will return `None`` immediately.
270+
///
271+
/// # Example
272+
/// ```
273+
/// # use quick_xml::events::attributes::{Attribute, Attributes};
274+
/// # use pretty_assertions::assert_eq;
275+
/// #
276+
/// let mut iter = Attributes::new("tag-name attr1 = 'value1' attr2='value2' ", 9);
277+
/// // ^0 ^9
278+
/// assert_eq!(iter.next(), Some(Ok(Attribute::from(("attr1", "value1")))));
279+
/// assert_eq!(iter.next(), Some(Ok(Attribute::from(("attr2", "value2")))));
280+
/// assert_eq!(iter.next(), None);
281+
/// ```
260282
pub const fn new(buf: &'a str, pos: usize) -> Self {
261283
Self::wrap(buf.as_bytes(), pos, false, Decoder::utf8())
262284
}
263285

264286
/// Creates a new attribute iterator from a buffer, allowing HTML attribute syntax.
287+
///
288+
/// # Parameters
289+
/// - `buf`: a buffer with a tag name and attributes, usually this is the whole
290+
/// string between `<` and `>` (or `/>`) of a tag;
291+
/// - `pos`: a position in the `buf` where tag name is finished and attributes
292+
/// is started. It is not necessary to point exactly to the end of a tag name,
293+
/// although that is usually that. If it will be more than the `buf` length,
294+
/// then the iterator will return `None`` immediately.
295+
///
296+
/// # Example
297+
/// ```
298+
/// # use quick_xml::events::attributes::{Attribute, Attributes};
299+
/// # use pretty_assertions::assert_eq;
300+
/// #
301+
/// let mut iter = Attributes::html("tag-name attr1 = value1 attr2 ", 9);
302+
/// // ^0 ^9
303+
/// assert_eq!(iter.next(), Some(Ok(Attribute::from(("attr1", "value1")))));
304+
/// assert_eq!(iter.next(), Some(Ok(Attribute::from(("attr2", "")))));
305+
/// assert_eq!(iter.next(), None);
306+
/// ```
265307
pub const fn html(buf: &'a str, pos: usize) -> Self {
266308
Self::wrap(buf.as_bytes(), pos, true, Decoder::utf8())
267309
}

0 commit comments

Comments
 (0)