From 28045361f3f970ee5c6145e975dc600055c9b2f1 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 20:46:44 +0100 Subject: [PATCH 001/111] initial commit --- Cargo.toml | 7 ++ src/error.rs | 32 ++++++ src/lib.rs | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 320 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/error.rs create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..765a12ddfcb7d4715cf4b7a8c2fd3155b78ff631 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "minidom" +version = "0.1.0" +authors = ["lumi "] + +[dependencies] +xml-rs = "*" diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000000000000000000000000000000000000..db184064d6a49210e4817f384f8b0afec85152ad --- /dev/null +++ b/src/error.rs @@ -0,0 +1,32 @@ +use std::io; + +use std::convert::From; + +use xml::writer::Error as WriterError; +use xml::reader::Error as ReaderError; + +#[derive(Debug)] +pub enum Error { + IoError(io::Error), + XmlWriterError(WriterError), + XmlReaderError(ReaderError), + EndOfDocument, +} + +impl From for Error { + fn from(err: io::Error) -> Error { + Error::IoError(err) + } +} + +impl From for Error { + fn from(err: WriterError) -> Error { + Error::XmlWriterError(err) + } +} + +impl From for Error { + fn from(err: ReaderError) -> Error { + Error::XmlReaderError(err) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..7509f40c273b2742776dc7512b37238ecf5eb12b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,281 @@ +extern crate xml; + +pub mod error; + +use std::io::prelude::*; + +use std::convert::From; + +use std::fmt; + +use xml::name::{OwnedName, Name}; +use xml::reader::{XmlEvent as ReaderEvent, EventReader}; +use xml::writer::{XmlEvent as WriterEvent, EventWriter}; +use xml::attribute::OwnedAttribute; + +use error::Error; + +#[derive(Clone, PartialEq, Eq)] +pub struct Element { + name: OwnedName, + attributes: Vec, + children: Vec, +} + +impl fmt::Debug for Element { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(fmt, "<{}", self.name)?; + for attr in &self.attributes { + write!(fmt, " {}", attr)?; + } + write!(fmt, ">")?; + for child in &self.children { + match *child { + Fork::Element(ref e) => { + write!(fmt, "{:?}", e)?; + }, + Fork::Text(ref s) => { + write!(fmt, "{}", s)?; + }, + } + } + write!(fmt, "", self.name)?; + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Fork { + Element(Element), + Text(String), +} + +impl Element { + pub fn new(name: OwnedName, attributes: Vec) -> Element { + Element { + name: name, + attributes: attributes, + children: Vec::new(), + } + } + + pub fn builder>(name: S) -> ElementBuilder { + ElementBuilder { + name: OwnedName::local(name), + attributes: Vec::new(), + } + } + + pub fn tag(&self) -> &str { + &self.name.local_name + } + + pub fn ns(&self) -> Option<&str> { + self.name.namespace.as_ref() + .map(String::as_ref) + } + + pub fn attr(&self, key: &str) -> Option<&str> { + for attr in &self.attributes { + if attr.name.local_name == key { + return Some(&attr.value); + } + } + None + } + + pub fn from_reader(reader: &mut EventReader) -> Result { + loop { + let e = reader.next()?; + match e { + ReaderEvent::StartElement { name, attributes, .. } => { + let mut root = Element::new(name, attributes); + root.from_reader_inner(reader); + return Ok(root); + }, + ReaderEvent::EndDocument => { + return Err(Error::EndOfDocument); + }, + _ => () // TODO: may need more errors + } + } + } + + fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { + loop { + let e = reader.next()?; + match e { + ReaderEvent::StartElement { name, attributes, .. } => { + let elem = Element::new(name, attributes); + let elem_ref = self.append_child(elem); + elem_ref.from_reader_inner(reader); + }, + ReaderEvent::EndElement { .. } => { + // TODO: may want to check whether we're closing the correct element + return Ok(()); + }, + ReaderEvent::Characters(s) => { + self.append_text_node(s); + }, + ReaderEvent::CData(s) => { + self.append_text_node(s); + }, + ReaderEvent::EndDocument => { + return Err(Error::EndOfDocument); + }, + _ => (), // TODO: may need to implement more + } + } + } + + pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { + let mut start = WriterEvent::start_element(self.name.borrow()); + if let Some(ref ns) = self.name.namespace { + start = start.default_ns(ns.as_ref()); + } + for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently + start = start.attr(attr.name.borrow(), &attr.value); + } + writer.write(start)?; + for child in &self.children { + match *child { + Fork::Element(ref e) => { + e.write_to(writer)?; + }, + Fork::Text(ref s) => { + writer.write(WriterEvent::characters(s))?; + }, + } + } + writer.write(WriterEvent::end_element())?; + Ok(()) + } + + pub fn children<'a>(&'a self) -> Children<'a> { + unimplemented!(); + } + + pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { + unimplemented!(); + } + + pub fn append_child(&mut self, child: Element) -> &mut Element { + self.children.push(Fork::Element(child)); + if let Fork::Element(ref mut cld) = *self.children.last_mut().unwrap() { + cld + } + else { + unreachable!() + } + } + + pub fn append_text_node>(&mut self, child: S) { + self.children.push(Fork::Text(child.into())); + } + + pub fn text(&self) -> &str { + unimplemented!() + } + + pub fn get_child<'a, N: Into>>(&self, name: N) -> Option<&Element> { + unimplemented!() + } + + pub fn get_child_mut<'a, N: Into>>(&mut self, name: N) -> Option<&mut Element> { + unimplemented!() + } + + pub fn into_child<'a, N: Into>>(self, name: N) -> Option { + unimplemented!() + } +} + +pub struct Children<'a> { + elem: &'a Element, +} + +pub struct ChildrenMut<'a> { + elem: &'a mut Element, +} + +pub struct ElementBuilder { + name: OwnedName, + attributes: Vec, +} + +impl ElementBuilder { + pub fn ns>(mut self, namespace: S) -> ElementBuilder { + self.name.namespace = Some(namespace.into()); + self + } + + pub fn attr, V: Into>(mut self, name: S, value: V) -> ElementBuilder { + self.attributes.push(OwnedAttribute::new(OwnedName::local(name), value)); + self + } + + pub fn attr_ns, N: Into, V: Into>(mut self, name: S, namespace: N, value: V) -> ElementBuilder { + self.attributes.push(OwnedAttribute::new(OwnedName::qualified::<_, _, &'static str>(name, namespace, None), value)); + self + } + + pub fn build(self) -> Element { + Element::new(self.name, self.attributes) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_STRING: &'static str = r#"meownya"#; + + fn build_test_tree() -> Element { + let mut root = Element::builder("root") + .ns("root_ns") + .attr("a", "b") + .build(); + root.append_text_node("meow"); + let child = Element::builder("child") + .attr("c", "d") + .build(); + root.append_child(child); + let other_child = Element::builder("child") + .ns("child_ns") + .attr("d", "e") + .build(); + root.append_child(other_child); + root.append_text_node("nya"); + root + } + + #[test] + fn reader_works() { + use std::io::Cursor; + let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + // TODO: fix a bunch of namespace stuff so this test passes + assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree()); + } + + #[test] + fn writer_works() { + let root = build_test_tree(); + let mut out = Vec::new(); + { + let mut writer = EventWriter::new(&mut out); + root.write_to(&mut writer); + } + assert_eq!(String::from_utf8(out).unwrap(), TEST_STRING); + } + + #[test] + fn builder_works() { + let elem = Element::builder("a") + .ns("b") + .attr("c", "d") + .build(); + assert_eq!(elem.tag(), "a"); + assert_eq!(elem.ns(), Some("b")); + assert_eq!(elem.attr("c"), Some("d")); + } +} From f0ebf7583aa9b4d70f99dd3c29593a2a44335712 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 20:57:59 +0100 Subject: [PATCH 002/111] add .gitignore, add README.md, add license --- .gitignore | 2 + COPYING | 675 +++++++++++++++++++++++++++++++++++++++++++++++++ COPYING.LESSER | 166 ++++++++++++ README.md | 12 + 4 files changed, 855 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 COPYING.LESSER create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..a9d37c560c6ab8d4afbf47eda643e8c42e857716 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/COPYING b/COPYING new file mode 100644 index 0000000000000000000000000000000000000000..a737dcfed5db21fb99a8fcb812e995937d38401b --- /dev/null +++ b/COPYING @@ -0,0 +1,675 @@ + + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/COPYING.LESSER b/COPYING.LESSER new file mode 100644 index 0000000000000000000000000000000000000000..5f5ff16a4a0f6104fadb6a5beef527573e46b425 --- /dev/null +++ b/COPYING.LESSER @@ -0,0 +1,166 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e477240b6b4307f5575cda9a12f037ed087f1d52 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +minidom-rs +========== + +What's this? +------------ + +A minimal DOM library on top of minidom-rs. + +What license is it under? +------------------------- + +LGPLv3. From 977dace5368452cf5562ed703dd53037de9d129d Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 20:59:34 +0100 Subject: [PATCH 003/111] resolve a cyclic dependency in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e477240b6b4307f5575cda9a12f037ed087f1d52..d253eb0f7d6ce4cf0a96fe37cf6b2e3ddd890342 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ minidom-rs What's this? ------------ -A minimal DOM library on top of minidom-rs. +A minimal DOM library on top of xml-rs. What license is it under? ------------------------- From 75b494e02ceaed92f296abf6fc016228bb3549d8 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 20:02:28 +0000 Subject: [PATCH 004/111] Add .gitlab-ci.yml --- .gitlab-ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..cb292343052f44cf4efaa6f5bd686defc527ae8a --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,6 @@ +image: "scorpil/rust:stable" + +test:cargo: + script: + - rustc --version && cargo --version + - cargo test --verbose --jobs 1 --release \ No newline at end of file From fda38ff242620ef45561eef797de0cc591d9f2ba Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 21:45:51 +0100 Subject: [PATCH 005/111] add child iterators and fix up some of the tests, which still do not pass (yet) --- src/lib.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7509f40c273b2742776dc7512b37238ecf5eb12b..7268c97079ea8fdb0d88f6fa00fd0fb7dc0bb746 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,10 @@ use std::io::prelude::*; use std::convert::From; +use std::iter::Iterator; + +use std::slice; + use std::fmt; use xml::name::{OwnedName, Name}; @@ -152,11 +156,15 @@ impl Element { } pub fn children<'a>(&'a self) -> Children<'a> { - unimplemented!(); + Children { + iter: self.children.iter(), + } } pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { - unimplemented!(); + ChildrenMut { + iter: self.children.iter_mut(), + } } pub fn append_child(&mut self, child: Element) -> &mut Element { @@ -191,11 +199,37 @@ impl Element { } pub struct Children<'a> { - elem: &'a Element, + iter: slice::Iter<'a, Fork>, +} + +impl<'a> Iterator for Children<'a> { + type Item = &'a Element; + + fn next(&mut self) -> Option<&'a Element> { + while let Some(item) = self.iter.next() { + if let Fork::Element(ref child) = *item { + return Some(child); + } + } + None + } } pub struct ChildrenMut<'a> { - elem: &'a mut Element, + iter: slice::IterMut<'a, Fork>, +} + +impl<'a> Iterator for ChildrenMut<'a> { + type Item = &'a mut Element; + + fn next(&mut self) -> Option<&'a mut Element> { + while let Some(item) = self.iter.next() { + if let Fork::Element(ref mut child) = *item { + return Some(child); + } + } + None + } } pub struct ElementBuilder { @@ -228,6 +262,9 @@ impl ElementBuilder { mod tests { use super::*; + use xml::reader::EventReader; + use xml::writer::EventWriter; + const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { @@ -263,7 +300,7 @@ mod tests { let mut out = Vec::new(); { let mut writer = EventWriter::new(&mut out); - root.write_to(&mut writer); + root.write_to(&mut writer).unwrap(); } assert_eq!(String::from_utf8(out).unwrap(), TEST_STRING); } From 8663a140403248b0c8358c0415d271d37c399887 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 23:15:43 +0100 Subject: [PATCH 006/111] lots of fixes and simplifications of the API --- src/lib.rs | 125 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 90 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7268c97079ea8fdb0d88f6fa00fd0fb7dc0bb746..f52477229a6ff6bddad1de23ccb70991d401912e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ pub mod error; use std::io::prelude::*; -use std::convert::From; +use std::convert::{From, AsRef}; use std::iter::Iterator; @@ -12,23 +12,51 @@ use std::slice; use std::fmt; -use xml::name::{OwnedName, Name}; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; -use xml::attribute::OwnedAttribute; +use xml::name::Name; +use xml::escape::escape_str_attribute; +use xml::namespace::NS_NO_PREFIX; use error::Error; +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Attribute { + pub name: String, + pub value: String, +} + +impl fmt::Display for Attribute { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) + } +} + +impl Attribute { + pub fn new, V: Into>(name: N, value: V) -> Attribute { + Attribute { + name: name.into(), + value: value.into(), + } + } +} + #[derive(Clone, PartialEq, Eq)] pub struct Element { - name: OwnedName, - attributes: Vec, + name: String, + namespace: Option, + attributes: Vec, children: Vec, } impl fmt::Debug for Element { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(fmt, "<{}", self.name)?; + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + if let Some(ref ns) = self.namespace { + write!(fmt, "<{{{}}}{}", ns, self.name)?; + } + else { + write!(fmt, "<{}", self.name)?; + } for attr in &self.attributes { write!(fmt, " {}", attr)?; } @@ -55,9 +83,10 @@ pub enum Fork { } impl Element { - pub fn new(name: OwnedName, attributes: Vec) -> Element { + pub fn new(name: String, namespace: Option, attributes: Vec) -> Element { Element { name: name, + namespace: namespace, attributes: attributes, children: Vec::new(), } @@ -65,35 +94,44 @@ impl Element { pub fn builder>(name: S) -> ElementBuilder { ElementBuilder { - name: OwnedName::local(name), + name: name.into(), + namespace: None, attributes: Vec::new(), } } - pub fn tag(&self) -> &str { - &self.name.local_name + pub fn name(&self) -> &str { + &self.name } pub fn ns(&self) -> Option<&str> { - self.name.namespace.as_ref() - .map(String::as_ref) + self.namespace.as_ref() + .map(String::as_ref) } - pub fn attr(&self, key: &str) -> Option<&str> { + pub fn attr(&self, name: &str) -> Option<&str> { for attr in &self.attributes { - if attr.name.local_name == key { + if attr.name == name { return Some(&attr.value); } } None } + pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { + let ns = self.namespace.as_ref().map(String::as_ref); + self.name == name.as_ref() && ns == Some(namespace.as_ref()) + } + pub fn from_reader(reader: &mut EventReader) -> Result { loop { let e = reader.next()?; match e { - ReaderEvent::StartElement { name, attributes, .. } => { - let mut root = Element::new(name, attributes); + ReaderEvent::StartElement { name, attributes, namespace } => { + let attributes = attributes.into_iter() + .map(|o| Attribute::new(o.name.local_name, o.value)) + .collect(); + let mut root = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); root.from_reader_inner(reader); return Ok(root); }, @@ -109,8 +147,11 @@ impl Element { loop { let e = reader.next()?; match e { - ReaderEvent::StartElement { name, attributes, .. } => { - let elem = Element::new(name, attributes); + ReaderEvent::StartElement { name, attributes, namespace } => { + let attributes = attributes.into_iter() + .map(|o| Attribute::new(o.name.local_name, o.value)) + .collect(); + let elem = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); let elem_ref = self.append_child(elem); elem_ref.from_reader_inner(reader); }, @@ -133,12 +174,18 @@ impl Element { } pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { - let mut start = WriterEvent::start_element(self.name.borrow()); - if let Some(ref ns) = self.name.namespace { + let name = if let Some(ref ns) = self.namespace { + Name::qualified(&self.name, &ns, None) + } + else { + Name::local(&self.name) + }; + let mut start = WriterEvent::start_element(name); + if let Some(ref ns) = self.namespace { start = start.default_ns(ns.as_ref()); } for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently - start = start.attr(attr.name.borrow(), &attr.value); + start = start.attr(Name::local(&attr.name), &attr.value); } writer.write(start)?; for child in &self.children { @@ -167,7 +214,10 @@ impl Element { } } - pub fn append_child(&mut self, child: Element) -> &mut Element { + pub fn append_child(&mut self, mut child: Element) -> &mut Element { + if child.namespace.is_none() { + child.namespace = self.namespace.clone(); + } self.children.push(Fork::Element(child)); if let Fork::Element(ref mut cld) = *self.children.last_mut().unwrap() { cld @@ -233,28 +283,24 @@ impl<'a> Iterator for ChildrenMut<'a> { } pub struct ElementBuilder { - name: OwnedName, - attributes: Vec, + name: String, + namespace: Option, + attributes: Vec, } impl ElementBuilder { pub fn ns>(mut self, namespace: S) -> ElementBuilder { - self.name.namespace = Some(namespace.into()); + self.namespace = Some(namespace.into()); self } pub fn attr, V: Into>(mut self, name: S, value: V) -> ElementBuilder { - self.attributes.push(OwnedAttribute::new(OwnedName::local(name), value)); - self - } - - pub fn attr_ns, N: Into, V: Into>(mut self, name: S, namespace: N, value: V) -> ElementBuilder { - self.attributes.push(OwnedAttribute::new(OwnedName::qualified::<_, _, &'static str>(name, namespace, None), value)); + self.attributes.push(Attribute::new(name, value)); self } pub fn build(self) -> Element { - Element::new(self.name, self.attributes) + Element::new(self.name, self.namespace, self.attributes) } } @@ -290,7 +336,6 @@ mod tests { fn reader_works() { use std::io::Cursor; let mut reader = EventReader::new(Cursor::new(TEST_STRING)); - // TODO: fix a bunch of namespace stuff so this test passes assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree()); } @@ -311,8 +356,18 @@ mod tests { .ns("b") .attr("c", "d") .build(); - assert_eq!(elem.tag(), "a"); + assert_eq!(elem.name(), "a"); assert_eq!(elem.ns(), Some("b")); assert_eq!(elem.attr("c"), Some("d")); + assert_eq!(elem.is("a", "b"), true); + } + + #[test] + fn children_iter_works() { + let root = build_test_tree(); + let mut iter = root.children(); + assert!(iter.next().unwrap().is("child", "root_ns")); + assert!(iter.next().unwrap().is("child", "child_ns")); + assert_eq!(iter.next(), None); } } From 2d97e2d5d9afae6ea4dd71f9ad96cb447b38be8c Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 23:29:19 +0100 Subject: [PATCH 007/111] put Attribute into its own module, take into account prefixes when determining namespaces --- src/attribute.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 39 ++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/attribute.rs diff --git a/src/attribute.rs b/src/attribute.rs new file mode 100644 index 0000000000000000000000000000000000000000..ac41f1288a583a23f5b4ecc4d138c296194cbfe2 --- /dev/null +++ b/src/attribute.rs @@ -0,0 +1,24 @@ +use xml::escape::escape_str_attribute; + +use std::fmt; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Attribute { + pub name: String, + pub value: String, +} + +impl fmt::Display for Attribute { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) + } +} + +impl Attribute { + pub fn new, V: Into>(name: N, value: V) -> Attribute { + Attribute { + name: name.into(), + value: value.into(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index f52477229a6ff6bddad1de23ccb70991d401912e..51a83732a65007f4eac5153f97baafa20c0d7c61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate xml; pub mod error; +pub mod attribute; use std::io::prelude::*; @@ -15,31 +16,11 @@ use std::fmt; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; use xml::name::Name; -use xml::escape::escape_str_attribute; use xml::namespace::NS_NO_PREFIX; use error::Error; -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Attribute { - pub name: String, - pub value: String, -} - -impl fmt::Display for Attribute { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) - } -} - -impl Attribute { - pub fn new, V: Into>(name: N, value: V) -> Attribute { - Attribute { - name: name.into(), - value: value.into(), - } - } -} +use attribute::Attribute; #[derive(Clone, PartialEq, Eq)] pub struct Element { @@ -131,7 +112,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let mut root = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes); root.from_reader_inner(reader); return Ok(root); }, @@ -151,7 +138,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let elem = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let elem = Element::new(name.local_name, ns, attributes); let elem_ref = self.append_child(elem); elem_ref.from_reader_inner(reader); }, From 663eba6754036d8b92272af83499e12f7360c43f Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 23:42:31 +0100 Subject: [PATCH 008/111] implemented text, get_child and get_child_mut; added and updated some tests --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 51a83732a65007f4eac5153f97baafa20c0d7c61..063d0b94ada63c19e58fbd7f4f83f3e392e29be7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -224,21 +224,38 @@ impl Element { self.children.push(Fork::Text(child.into())); } - pub fn text(&self) -> &str { - unimplemented!() + pub fn text(&self) -> String { + let mut ret = String::new(); + for fork in &self.children { + if let Fork::Text(ref s) = *fork { + ret += s; + } + } + ret } - pub fn get_child<'a, N: Into>>(&self, name: N) -> Option<&Element> { - unimplemented!() + pub fn get_child, NS: AsRef>(&self, name: N, namespace: NS) -> Option<&Element> { + for fork in &self.children { + if let Fork::Element(ref e) = *fork { + if e.is(name.as_ref(), namespace.as_ref()) { + return Some(e); + } + } + } + None } - pub fn get_child_mut<'a, N: Into>>(&mut self, name: N) -> Option<&mut Element> { - unimplemented!() + pub fn get_child_mut, NS: AsRef>(&mut self, name: N, namespace: NS) -> Option<&mut Element> { + for fork in &mut self.children { + if let Fork::Element(ref mut e) = *fork { + if e.is(name.as_ref(), namespace.as_ref()) { + return Some(e); + } + } + } + None } - pub fn into_child<'a, N: Into>>(self, name: N) -> Option { - unimplemented!() - } } pub struct Children<'a> { @@ -352,7 +369,8 @@ mod tests { assert_eq!(elem.name(), "a"); assert_eq!(elem.ns(), Some("b")); assert_eq!(elem.attr("c"), Some("d")); - assert_eq!(elem.is("a", "b"), true); + assert_eq!(elem.attr("x"), None); + assert!(elem.is("a", "b")); } #[test] @@ -363,4 +381,15 @@ mod tests { assert!(iter.next().unwrap().is("child", "child_ns")); assert_eq!(iter.next(), None); } + + #[test] + fn get_child_works() { + let root = build_test_tree(); + assert_eq!(root.get_child("child", "inexistent_ns"), None); + assert_eq!(root.get_child("not_a_child", "root_ns"), None); + assert!(root.get_child("child", "root_ns").unwrap().is("child", "root_ns")); + assert!(root.get_child("child", "child_ns").unwrap().is("child", "child_ns")); + assert_eq!(root.get_child("child", "root_ns").unwrap().attr("c"), Some("d")); + assert_eq!(root.get_child("child", "child_ns").unwrap().attr("d"), Some("e")); + } } From 41e1cf2654d34e78722a52f73ae6c42cad69f46b Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 20 Feb 2017 02:04:45 +0100 Subject: [PATCH 009/111] added has_child, fixed some warnings --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 063d0b94ada63c19e58fbd7f4f83f3e392e29be7..0292bbe99bdc88830dded31a426b0089cd331b41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod attribute; use std::io::prelude::*; -use std::convert::{From, AsRef}; +use std::convert::AsRef; use std::iter::Iterator; @@ -119,7 +119,7 @@ impl Element { namespace.get(NS_NO_PREFIX) }.map(|s| s.to_owned()); let mut root = Element::new(name.local_name, ns, attributes); - root.from_reader_inner(reader); + root.from_reader_inner(reader)?; return Ok(root); }, ReaderEvent::EndDocument => { @@ -146,7 +146,7 @@ impl Element { }.map(|s| s.to_owned()); let elem = Element::new(name.local_name, ns, attributes); let elem_ref = self.append_child(elem); - elem_ref.from_reader_inner(reader); + elem_ref.from_reader_inner(reader)?; }, ReaderEvent::EndElement { .. } => { // TODO: may want to check whether we're closing the correct element @@ -256,6 +256,9 @@ impl Element { None } + pub fn has_child, NS: AsRef>(&self, name: N, namespace: NS) -> bool { + self.get_child(name, namespace).is_some() + } } pub struct Children<'a> { From e0b35477cd2ae6ecb93d4b6f1e90f84aa034940c Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 20 Feb 2017 23:58:44 +0100 Subject: [PATCH 010/111] add ElementBuilder::text --- src/lib.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0292bbe99bdc88830dded31a426b0089cd331b41..ce8a26d8498db8df477ffbc724c0afe9f076fb5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,7 @@ impl Element { pub fn builder>(name: S) -> ElementBuilder { ElementBuilder { name: name.into(), + text: None, namespace: None, attributes: Vec::new(), } @@ -297,6 +298,7 @@ impl<'a> Iterator for ChildrenMut<'a> { pub struct ElementBuilder { name: String, + text: Option, namespace: Option, attributes: Vec, } @@ -312,8 +314,17 @@ impl ElementBuilder { self } + pub fn text>(mut self, text: S) -> ElementBuilder { + self.text = Some(text.into()); + self + } + pub fn build(self) -> Element { - Element::new(self.name, self.namespace, self.attributes) + let mut elem = Element::new(self.name, self.namespace, self.attributes); + if let Some(text) = self.text { + elem.append_text_node(text); + } + elem } } @@ -368,11 +379,13 @@ mod tests { let elem = Element::builder("a") .ns("b") .attr("c", "d") + .text("e") .build(); assert_eq!(elem.name(), "a"); assert_eq!(elem.ns(), Some("b")); assert_eq!(elem.attr("c"), Some("d")); assert_eq!(elem.attr("x"), None); + assert_eq!(elem.text(), "e"); assert!(elem.is("a", "b")); } From 0a45a6993e8c119fffd6e6a2ce8f55b34ba070af Mon Sep 17 00:00:00 2001 From: lumi Date: Tue, 21 Feb 2017 15:46:06 +0100 Subject: [PATCH 011/111] document all the things! --- examples/articles.rs | 45 +++++++ src/attribute.rs | 19 +++ src/error.rs | 1 + src/lib.rs | 274 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 334 insertions(+), 5 deletions(-) create mode 100644 examples/articles.rs diff --git a/examples/articles.rs b/examples/articles.rs new file mode 100644 index 0000000000000000000000000000000000000000..6db61d86d078032baf8ed6057770c2db0bab89ae --- /dev/null +++ b/examples/articles.rs @@ -0,0 +1,45 @@ +extern crate minidom; + +use minidom::Element; + +const DATA: &'static str = r#" +
+ 10 Terrible Bugs You Would NEVER Believe Happened + + Rust fixed them all. <3 + +
+
+ BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen + + Just kidding! + +
+
"#; + +const ARTICLE_NS: &'static str = "article"; + +#[derive(Debug)] +pub struct Article { + title: String, + body: String, +} + +fn main() { + let root: Element = DATA.parse().unwrap(); + + let mut articles: Vec
= Vec::new(); + + for child in root.children() { + if child.is("article", ARTICLE_NS) { + let title = child.get_child("title", ARTICLE_NS).unwrap().text(); + let body = child.get_child("body", ARTICLE_NS).unwrap().text(); + articles.push(Article { + title: title, + body: body.trim().to_owned(), + }); + } + } + + println!("{:?}", articles); +} diff --git a/src/attribute.rs b/src/attribute.rs index ac41f1288a583a23f5b4ecc4d138c296194cbfe2..5d45f40fad0f5ec77be5705cc370a2d6630ea051 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -2,9 +2,16 @@ use xml::escape::escape_str_attribute; use std::fmt; +/// An attribute of a DOM element. +/// +/// This is of the form: `name`="`value`" +/// +/// This does not support prefixed/namespaced attributes yet. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Attribute { + /// The name of the attribute. pub name: String, + /// The value of the attribute. pub value: String, } @@ -15,6 +22,18 @@ impl fmt::Display for Attribute { } impl Attribute { + /// Construct a new attribute from the given `name` and `value`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Attribute; + /// + /// let attr = Attribute::new("name", "value"); + /// + /// assert_eq!(attr.name, "name"); + /// assert_eq!(attr.value, "value"); + /// ``` pub fn new, V: Into>(name: N, value: V) -> Attribute { Attribute { name: name.into(), diff --git a/src/error.rs b/src/error.rs index db184064d6a49210e4817f384f8b0afec85152ad..38f4069051b2de05338ac2ff08b8ff81394e54a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,7 @@ use std::convert::From; use xml::writer::Error as WriterError; use xml::reader::Error as ReaderError; +/// An enum representing the possible errors. #[derive(Debug)] pub enum Error { IoError(io::Error), diff --git a/src/lib.rs b/src/lib.rs index ce8a26d8498db8df477ffbc724c0afe9f076fb5d..5420df83efc3982d2038ad99c467d951b37ef889 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,76 @@ +//! A minimal DOM crate built on top of xml-rs. +//! +//! This library exports an `Element` struct which represents a DOM tree. +//! +//! # Example +//! +//! Run with `cargo run --example articles`. Located in `examples/articles.rs`. +//! +//! ```rust,ignore +//! extern crate minidom; +//! +//! use minidom::Element; +//! +//! const DATA: &'static str = r#" +//!
+//! 10 Terrible Bugs You Would NEVER Believe Happened +//! +//! Rust fixed them all. <3 +//! +//!
+//!
+//! BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen +//! +//! Just kidding! +//! +//!
+//!
"#; +//! +//! const ARTICLE_NS: &'static str = "article"; +//! +//! #[derive(Debug)] +//! pub struct Article { +//! title: String, +//! body: String, +//! } +//! +//! fn main() { +//! let root: Element = DATA.parse().unwrap(); +//! +//! let mut articles: Vec
= Vec::new(); +//! +//! for child in root.children() { +//! if child.is("article", ARTICLE_NS) { +//! let title = child.get_child("title", ARTICLE_NS).unwrap().text(); +//! let body = child.get_child("body", ARTICLE_NS).unwrap().text(); +//! articles.push(Article { +//! title: title, +//! body: body.trim().to_owned(), +//! }); +//! } +//! } +//! +//! println!("{:?}", articles); +//! } +//! ``` +//! +//! # Usage +//! +//! To use `minidom`, add this to your `Cargo.toml`: +//! +//! ```toml,ignore +//! [dependencies.minidom] +//! git = "https://gitlab.com/lumi/minidom-rs.git" +//! ``` + extern crate xml; -pub mod error; -pub mod attribute; +mod error; + +mod attribute; use std::io::prelude::*; +use std::io::Cursor; use std::convert::AsRef; @@ -13,16 +80,19 @@ use std::slice; use std::fmt; +use std::str::FromStr; + use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; use xml::name::Name; use xml::namespace::NS_NO_PREFIX; -use error::Error; +pub use error::Error; -use attribute::Attribute; +pub use attribute::Attribute; #[derive(Clone, PartialEq, Eq)] +/// A struct representing a DOM Element. pub struct Element { name: String, namespace: Option, @@ -57,13 +127,40 @@ impl fmt::Debug for Element { } } +impl FromStr for Element { + type Err = Error; + + fn from_str(s: &str) -> Result { + let mut reader = EventReader::new(Cursor::new(s)); + Element::from_reader(&mut reader) + } +} + #[derive(Clone, Debug, PartialEq, Eq)] -pub enum Fork { +enum Fork { Element(Element), Text(String), } impl Element { + /// Constructs a new `Element` with the given `name`, `namespace` and `attributes`. + /// + /// You probably should be using `Element::builder` instead of this. + /// + /// # Examples + /// + /// ``` + /// use minidom::{Element, Attribute}; + /// + /// let elem = Element::new( "name".to_owned() + /// , Some("namespace".to_owned()) + /// , vec![ Attribute::new("name", "value") ] ); + /// + /// assert_eq!(elem.name(), "name"); + /// assert_eq!(elem.ns(), Some("namespace")); + /// assert_eq!(elem.attr("name"), Some("value")); + /// assert_eq!(elem.attr("inexistent"), None); + /// ``` pub fn new(name: String, namespace: Option, attributes: Vec) -> Element { Element { name: name, @@ -73,6 +170,25 @@ impl Element { } } + /// Return a builder for an `Element` with the given `name`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem = Element::builder("name") + /// .ns("namespace") + /// .attr("name", "value") + /// .text("inner") + /// .build(); + /// + /// assert_eq!(elem.name(), "name"); + /// assert_eq!(elem.ns(), Some("namespace")); + /// assert_eq!(elem.attr("name"), Some("value")); + /// assert_eq!(elem.attr("inexistent"), None); + /// assert_eq!(elem.text(), "inner"); + /// ``` pub fn builder>(name: S) -> ElementBuilder { ElementBuilder { name: name.into(), @@ -82,15 +198,41 @@ impl Element { } } + /// Returns a bare minimum `Element` with this name. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let bare = Element::bare("name"); + /// + /// assert_eq!(bare.name(), "name"); + /// assert_eq!(bare.ns(), None); + /// assert_eq!(bare.attr("name"), None); + /// assert_eq!(bare.text(), ""); + /// ``` + pub fn bare>(name: S) -> Element { + Element { + name: name.into(), + namespace: None, + attributes: Vec::new(), + children: Vec::new(), + } + } + + /// Returns a reference to the name of this element. pub fn name(&self) -> &str { &self.name } + /// Returns a reference to the namespace of this element, if it has one, else `None`. pub fn ns(&self) -> Option<&str> { self.namespace.as_ref() .map(String::as_ref) } + /// Returns a reference to the value of the given attribute, if it exists, else `None`. pub fn attr(&self, name: &str) -> Option<&str> { for attr in &self.attributes { if attr.name == name { @@ -100,6 +242,20 @@ impl Element { None } + /// Returns whether the element has the given name and namespace. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem = Element::builder("name").ns("namespace").build(); + /// + /// assert_eq!(elem.is("name", "namespace"), true); + /// assert_eq!(elem.is("name", "wrong"), false); + /// assert_eq!(elem.is("wrong", "namespace"), false); + /// assert_eq!(elem.is("wrong", "wrong"), false); + /// ``` pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { let ns = self.namespace.as_ref().map(String::as_ref); self.name == name.as_ref() && ns == Some(namespace.as_ref()) @@ -196,18 +352,57 @@ impl Element { Ok(()) } + /// Returns an iterator over references to the children of this element. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = "".parse().unwrap(); + /// + /// let mut iter = elem.children(); + /// assert_eq!(iter.next().unwrap().name(), "child1"); + /// assert_eq!(iter.next().unwrap().name(), "child2"); + /// assert_eq!(iter.next().unwrap().name(), "child3"); + /// assert_eq!(iter.next(), None); + /// ``` pub fn children<'a>(&'a self) -> Children<'a> { Children { iter: self.children.iter(), } } + /// Returns an iterator over mutable references to the children of this element. pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { ChildrenMut { iter: self.children.iter_mut(), } } + /// Appends a child node to the `Element`, returning the appended node. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let mut elem = Element::bare("root"); + /// + /// assert_eq!(elem.children().count(), 0); + /// + /// elem.append_child(Element::bare("child")); + /// + /// { + /// let mut iter = elem.children(); + /// assert_eq!(iter.next().unwrap().name(), "child"); + /// assert_eq!(iter.next(), None); + /// } + /// + /// let child = elem.append_child(Element::bare("new")); + /// + /// assert_eq!(child.name(), "new"); + /// ``` pub fn append_child(&mut self, mut child: Element) -> &mut Element { if child.namespace.is_none() { child.namespace = self.namespace.clone(); @@ -221,10 +416,36 @@ impl Element { } } + /// Appends a text node to an `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let mut elem = Element::bare("node"); + /// + /// assert_eq!(elem.text(), ""); + /// + /// elem.append_text_node("text"); + /// + /// assert_eq!(elem.text(), "text"); + /// ``` pub fn append_text_node>(&mut self, child: S) { self.children.push(Fork::Text(child.into())); } + /// Returns the concatenation of all text nodes in the `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = "hello, world!".parse().unwrap(); + /// + /// assert_eq!(elem.text(), "hello, world!"); + /// ``` pub fn text(&self) -> String { let mut ret = String::new(); for fork in &self.children { @@ -235,6 +456,23 @@ impl Element { ret } + /// Returns a reference to the first child element with the specific name and namespace, if it + /// exists in the direct descendants of this `Element`, else returns `None`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = r#""#.parse().unwrap(); + /// + /// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns")); + /// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns")); + /// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns")); + /// assert_eq!(elem.get_child("c", "ns"), None); + /// assert_eq!(elem.get_child("b", "other_ns"), None); + /// assert_eq!(elem.get_child("a", "inexistent_ns"), None); + /// ``` pub fn get_child, NS: AsRef>(&self, name: N, namespace: NS) -> Option<&Element> { for fork in &self.children { if let Fork::Element(ref e) = *fork { @@ -246,6 +484,8 @@ impl Element { None } + /// Returns a mutable reference to the first child element with the specific name and namespace, + /// if it exists in the direct descendants of this `Element`, else returns `None`. pub fn get_child_mut, NS: AsRef>(&mut self, name: N, namespace: NS) -> Option<&mut Element> { for fork in &mut self.children { if let Fork::Element(ref mut e) = *fork { @@ -257,11 +497,29 @@ impl Element { None } + /// Returns whether a specific child with this name and namespace exists in the direct + /// descendants of the `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = r#""#.parse().unwrap(); + /// + /// assert_eq!(elem.has_child("a", "other_ns"), true); + /// assert_eq!(elem.has_child("a", "ns"), true); + /// assert_eq!(elem.has_child("a", "inexistent_ns"), false); + /// assert_eq!(elem.has_child("b", "ns"), true); + /// assert_eq!(elem.has_child("b", "other_ns"), false); + /// assert_eq!(elem.has_child("b", "inexistent_ns"), false); + /// ``` pub fn has_child, NS: AsRef>(&self, name: N, namespace: NS) -> bool { self.get_child(name, namespace).is_some() } } +/// An iterator over references to children of an `Element`. pub struct Children<'a> { iter: slice::Iter<'a, Fork>, } @@ -279,6 +537,7 @@ impl<'a> Iterator for Children<'a> { } } +/// An iterator over mutable references to children of an `Element`. pub struct ChildrenMut<'a> { iter: slice::IterMut<'a, Fork>, } @@ -296,6 +555,7 @@ impl<'a> Iterator for ChildrenMut<'a> { } } +/// A builder for `Element`s. pub struct ElementBuilder { name: String, text: Option, @@ -304,21 +564,25 @@ pub struct ElementBuilder { } impl ElementBuilder { + /// Sets the namespace. pub fn ns>(mut self, namespace: S) -> ElementBuilder { self.namespace = Some(namespace.into()); self } + /// Sets an attribute. pub fn attr, V: Into>(mut self, name: S, value: V) -> ElementBuilder { self.attributes.push(Attribute::new(name, value)); self } + /// Sets the inner text. pub fn text>(mut self, text: S) -> ElementBuilder { self.text = Some(text.into()); self } + /// Builds the `Element`. pub fn build(self) -> Element { let mut elem = Element::new(self.name, self.namespace, self.attributes); if let Some(text) = self.text { From 9a00c998ae153268509b0b3f5553d0b4d407e300 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 25 Feb 2017 00:10:18 +0100 Subject: [PATCH 012/111] add a test for an issue with namespace propagation --- src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5420df83efc3982d2038ad99c467d951b37ef889..c5f8f9abbaf2ff246a3e67f389a451d01df02d06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -672,4 +672,15 @@ mod tests { assert_eq!(root.get_child("child", "root_ns").unwrap().attr("c"), Some("d")); assert_eq!(root.get_child("child", "child_ns").unwrap().attr("d"), Some("e")); } + + #[test] + fn namespace_propagation_works() { + let mut root = Element::builder("root").ns("root_ns").build(); + let mut child = Element::bare("child"); + let grandchild = Element::bare("grandchild"); + child.append_child(grandchild); + root.append_child(child); + assert_eq!(root.get_child("child", "root_ns").unwrap().ns(), root.ns()); + assert_eq!(root.get_child("grandchild", "root_ns").unwrap().ns(), root.ns()); + } } From a047fdd8708614c31af781696cf2e60db9c3dd86 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 25 Feb 2017 15:31:58 +0100 Subject: [PATCH 013/111] add data to Cargo.toml --- Cargo.toml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 765a12ddfcb7d4715cf4b7a8c2fd3155b78ff631..7b9131611d368c24e54becdc80db46c17ee42076 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,15 @@ name = "minidom" version = "0.1.0" authors = ["lumi "] +description = "A small, simple DOM implementation on top of xml-rs." +homepage = "https://gitlab.com/lumi/minidom-rs" +repository = "https://gitlab.com/lumi/minidom-rs" +readme = "README.md" +keywords = ["xml"] +license = "LGPL-3.0+" + +[badges] +gitlab = { repository = "lumi/minidom-rs" } [dependencies] -xml-rs = "*" +xml-rs = "0.3.6" From 112b3fa0c8b0363ec1536758ec401cbbbcd7a050 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 25 Feb 2017 15:43:56 +0100 Subject: [PATCH 014/111] added documentation url and fixed a dumb mistake --- Cargo.toml | 3 ++- src/lib.rs | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7b9131611d368c24e54becdc80db46c17ee42076..a28773a998f9f0e29dc2467bd5a8c4267e7188b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "minidom" -version = "0.1.0" +version = "0.1.1" authors = ["lumi "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" +documentation = "https://docs.rs/minidom" readme = "README.md" keywords = ["xml"] license = "LGPL-3.0+" diff --git a/src/lib.rs b/src/lib.rs index c5f8f9abbaf2ff246a3e67f389a451d01df02d06..9dfed4489b2e9282bc47e1648424e21b0394dd35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,11 +56,10 @@ //! //! # Usage //! -//! To use `minidom`, add this to your `Cargo.toml`: +//! To use `minidom`, add this to your `Cargo.toml` under `dependencies`: //! //! ```toml,ignore -//! [dependencies.minidom] -//! git = "https://gitlab.com/lumi/minidom-rs.git" +//! minidom = "*" //! ``` extern crate xml; From 5da36ecdf44e8fc998426b371bc36ab16755450d Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 27 Feb 2017 16:59:47 +0100 Subject: [PATCH 015/111] license updates --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d253eb0f7d6ce4cf0a96fe37cf6b2e3ddd890342..ca2fae7a68b0167107a3f2e3e8e556e16dfc9c58 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,22 @@ A minimal DOM library on top of xml-rs. What license is it under? ------------------------- -LGPLv3. +LGPLv3 or later. See `COPYING` and `COPYING.LESSER`. + +License yadda yadda. +-------------------- + + Copyright 2017, minidom-rs contributors. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . From 7c04aff416135910a1226b17318cc9c2e5d5003b Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 8 Mar 2017 20:34:17 +0100 Subject: [PATCH 016/111] overhauling the library, made tests pass --- .gitlab-ci.yml | 4 +- src/attribute.rs | 2 + src/convert.rs | 101 ++++++++ src/element.rs | 558 ++++++++++++++++++++++++++++++++++++++++++ src/error.rs | 6 + src/lib.rs | 624 +---------------------------------------------- src/tests.rs | 96 ++++++++ 7 files changed, 774 insertions(+), 617 deletions(-) create mode 100644 src/convert.rs create mode 100644 src/element.rs create mode 100644 src/tests.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cb292343052f44cf4efaa6f5bd686defc527ae8a..bd8cf4313765d2f38bea2ff4edc4a22ba61b9866 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,6 @@ -image: "scorpil/rust:stable" +image: "scorpil/rust:nightly" test:cargo: script: - rustc --version && cargo --version - - cargo test --verbose --jobs 1 --release \ No newline at end of file + - cargo test --verbose --jobs 1 --release diff --git a/src/attribute.rs b/src/attribute.rs index 5d45f40fad0f5ec77be5705cc370a2d6630ea051..57373a06dc7ee6b6a62b9b226edc0911dc1e22f4 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -1,3 +1,5 @@ +//! Provides an `Attribute` type which represents an attribute in an XML document. + use xml::escape::escape_str_attribute; use std::fmt; diff --git a/src/convert.rs b/src/convert.rs new file mode 100644 index 0000000000000000000000000000000000000000..83c187713fd98177a8a998a563e7ad928464a8ec --- /dev/null +++ b/src/convert.rs @@ -0,0 +1,101 @@ +//! A module which exports a few traits for converting types to elements and attributes. + +use element::{Element, ElementBuilder}; + +/// A struct which is used for emitting `Element`s and text nodes into an `Element` without +/// exposing more functionality than necessary. +pub struct ElementEmitter<'a>(&'a mut Element); + +impl<'a> ElementEmitter<'a> { + /// Creates a new `ElementEmitter`. + pub fn new(root: &'a mut Element) -> ElementEmitter<'a> { + ElementEmitter(root) + } + + /// Appends an `Element` to the target. + pub fn append_child(&mut self, element: Element) { + self.0.append_child(element); + } + + /// Appends a text node to the target. + pub fn append_text_node(&mut self, text: String) { + self.0.append_text_node(text); + } +} + +/// A trait for types which can be converted to one or multiple `Element`s. +pub trait IntoElements { + /// Emits this as a sequence of text nodes and `Element`s. + fn into_elements(self, emitter: &mut ElementEmitter); +} + +impl IntoElements for Vec { + fn into_elements(self, emitter: &mut ElementEmitter) { + for elem in self { + elem.into_elements(emitter); + } + } +} + +impl<'a, T: IntoElements + Clone> IntoElements for &'a [T] { + fn into_elements(self, emitter: &mut ElementEmitter) { + self.to_vec().into_elements(emitter); + } +} + +impl IntoElements for Option { + fn into_elements(self, emitter: &mut ElementEmitter) { + match self { + Some(e) => e.into_elements(emitter), + None => (), + } + } +} + +impl IntoElements for Element { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_child(self); + } +} + +impl IntoElements for ElementBuilder { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_child(self.build()); + } +} + +impl IntoElements for String { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_text_node(self); + } +} + +impl<'a> IntoElements for &'a str { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_text_node(self.to_owned()); + } +} + +/// A trait for types which can be converted to an attribute value. +pub trait IntoAttributeValue { + /// Turns this into an attribute string, or None if it shouldn't be added. + fn into_attribute_value(self) -> Option; +} + +impl IntoAttributeValue for String { + fn into_attribute_value(self) -> Option { + Some(self.clone()) + } +} + +impl<'a> IntoAttributeValue for &'a str { + fn into_attribute_value(self) -> Option { + Some((*self).to_owned()) + } +} + +impl IntoAttributeValue for Option { + fn into_attribute_value(self) -> Option { + self.and_then(|t| t.into_attribute_value()) + } +} diff --git a/src/element.rs b/src/element.rs new file mode 100644 index 0000000000000000000000000000000000000000..cedb90e5d9ede927ef116369fb2c8aca0f4d7bd3 --- /dev/null +++ b/src/element.rs @@ -0,0 +1,558 @@ +//! Provides an `Element` type, which represents DOM nodes, and a builder to create them with. + +use std::io::prelude::*; +use std::io::Cursor; + +use std::fmt; + +use error::Error; + +use attribute::Attribute; + +use xml::reader::{XmlEvent as ReaderEvent, EventReader}; +use xml::writer::{XmlEvent as WriterEvent, EventWriter}; +use xml::name::Name; +use xml::namespace::NS_NO_PREFIX; + +use std::str::FromStr; + +use std::slice; + +use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; + +#[derive(Clone, PartialEq, Eq)] +/// A struct representing a DOM Element. +pub struct Element { + name: String, + namespace: Option, + attributes: Vec, + children: Vec, +} + +impl fmt::Debug for Element { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + if let Some(ref ns) = self.namespace { + write!(fmt, "<{{{}}}{}", ns, self.name)?; + } + else { + write!(fmt, "<{}", self.name)?; + } + for attr in &self.attributes { + write!(fmt, " {}", attr)?; + } + write!(fmt, ">")?; + for child in &self.children { + match *child { + Node::Element(ref e) => { + write!(fmt, "{:?}", e)?; + }, + Node::Text(ref s) => { + write!(fmt, "{}", s)?; + }, + } + } + write!(fmt, "", self.name)?; + Ok(()) + } +} + +impl FromStr for Element { + type Err = Error; + + fn from_str(s: &str) -> Result { + let mut reader = EventReader::new(Cursor::new(s)); + Element::from_reader(&mut reader) + } +} + +/// A node in an element tree. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Node { + /// An `Element`. + Element(Element), + /// A text node. + Text(String), +} + +impl Element { + fn new(name: String, namespace: Option, attributes: Vec, children: Vec) -> Element { + Element { + name: name, + namespace: namespace, + attributes: attributes, + children: children, + } + } + + /// Return a builder for an `Element` with the given `name`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem = Element::builder("name") + /// .ns("namespace") + /// .attr("name", "value") + /// .append("inner") + /// .build(); + /// + /// assert_eq!(elem.name(), "name"); + /// assert_eq!(elem.ns(), Some("namespace")); + /// assert_eq!(elem.attr("name"), Some("value")); + /// assert_eq!(elem.attr("inexistent"), None); + /// assert_eq!(elem.text(), "inner"); + /// ``` + pub fn builder>(name: S) -> ElementBuilder { + ElementBuilder { + root: Element::new(name.into(), None, Vec::new(), Vec::new()), + } + } + + /// Returns a bare minimum `Element` with this name. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let bare = Element::bare("name"); + /// + /// assert_eq!(bare.name(), "name"); + /// assert_eq!(bare.ns(), None); + /// assert_eq!(bare.attr("name"), None); + /// assert_eq!(bare.text(), ""); + /// ``` + pub fn bare>(name: S) -> Element { + Element { + name: name.into(), + namespace: None, + attributes: Vec::new(), + children: Vec::new(), + } + } + + /// Returns a reference to the name of this element. + pub fn name(&self) -> &str { + &self.name + } + + /// Returns a reference to the namespace of this element, if it has one, else `None`. + pub fn ns(&self) -> Option<&str> { + self.namespace.as_ref() + .map(String::as_ref) + } + + /// Returns a reference to the value of the given attribute, if it exists, else `None`. + pub fn attr(&self, name: &str) -> Option<&str> { + for attr in &self.attributes { + if attr.name == name { + return Some(&attr.value); + } + } + None + } + + /// Modifies the value of an attribute. + pub fn set_attr, V: IntoAttributeValue>(&mut self, name: S, val: V) { + let name = name.into(); + let val = val.into_attribute_value(); + for attr in &mut self.attributes { + if attr.name == name { + attr.value = val.expect("removing existing value via set_attr, this is not yet supported (TODO)"); // TODO + return; + } + } + if let Some(val) = val { + self.attributes.push(Attribute::new(name, val)); + } + } + + /// Returns whether the element has the given name and namespace. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem = Element::builder("name").ns("namespace").build(); + /// + /// assert_eq!(elem.is("name", "namespace"), true); + /// assert_eq!(elem.is("name", "wrong"), false); + /// assert_eq!(elem.is("wrong", "namespace"), false); + /// assert_eq!(elem.is("wrong", "wrong"), false); + /// ``` + pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { + let ns = self.namespace.as_ref().map(String::as_ref); + self.name == name.as_ref() && ns == Some(namespace.as_ref()) + } + + /// Parse a document from an `EventReader`. + pub fn from_reader(reader: &mut EventReader) -> Result { + loop { + let e = reader.next()?; + match e { + ReaderEvent::StartElement { name, attributes, namespace } => { + let attributes = attributes.into_iter() + .map(|o| Attribute::new(o.name.local_name, o.value)) + .collect(); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes, Vec::new()); + root.from_reader_inner(reader)?; + return Ok(root); + }, + ReaderEvent::EndDocument => { + return Err(Error::EndOfDocument); + }, + _ => () // TODO: may need more errors + } + } + } + + fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { + loop { + let e = reader.next()?; + match e { + ReaderEvent::StartElement { name, attributes, namespace } => { + let attributes = attributes.into_iter() + .map(|o| Attribute::new(o.name.local_name, o.value)) + .collect(); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let elem = Element::new(name.local_name, ns, attributes, Vec::with_capacity(1)); + let elem_ref = self.append_child(elem); + elem_ref.from_reader_inner(reader)?; + }, + ReaderEvent::EndElement { .. } => { + // TODO: may want to check whether we're closing the correct element + return Ok(()); + }, + ReaderEvent::Characters(s) => { + self.append_text_node(s); + }, + ReaderEvent::CData(s) => { + self.append_text_node(s); + }, + ReaderEvent::EndDocument => { + return Err(Error::EndOfDocument); + }, + _ => (), // TODO: may need to implement more + } + } + } + + /// Output a document to an `EventWriter`. + pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { + let name = if let Some(ref ns) = self.namespace { + Name::qualified(&self.name, &ns, None) + } + else { + Name::local(&self.name) + }; + let mut start = WriterEvent::start_element(name); + if let Some(ref ns) = self.namespace { + start = start.default_ns(ns.as_ref()); + } + for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently + start = start.attr(Name::local(&attr.name), &attr.value); + } + writer.write(start)?; + for child in &self.children { + match *child { + Node::Element(ref e) => { + e.write_to(writer)?; + }, + Node::Text(ref s) => { + writer.write(WriterEvent::characters(s))?; + }, + } + } + writer.write(WriterEvent::end_element())?; + Ok(()) + } + + /// Returns an iterator over references to the children of this element. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = "".parse().unwrap(); + /// + /// let mut iter = elem.children(); + /// assert_eq!(iter.next().unwrap().name(), "child1"); + /// assert_eq!(iter.next().unwrap().name(), "child2"); + /// assert_eq!(iter.next().unwrap().name(), "child3"); + /// assert_eq!(iter.next(), None); + /// ``` + pub fn children<'a>(&'a self) -> Children<'a> { + Children { + iter: self.children.iter(), + } + } + + /// Returns an iterator over mutable references to the children of this element. + pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { + ChildrenMut { + iter: self.children.iter_mut(), + } + } + + fn propagate_namespaces(&mut self) { + let ns = self.namespace.clone(); + for child in self.children_mut() { + if child.namespace.is_none() { + child.namespace = ns.clone(); + child.propagate_namespaces(); + } + } + } + + /// Appends a child node to the `Element`, returning the appended node. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let mut elem = Element::bare("root"); + /// + /// assert_eq!(elem.children().count(), 0); + /// + /// elem.append_child(Element::bare("child")); + /// + /// { + /// let mut iter = elem.children(); + /// assert_eq!(iter.next().unwrap().name(), "child"); + /// assert_eq!(iter.next(), None); + /// } + /// + /// let child = elem.append_child(Element::bare("new")); + /// + /// assert_eq!(child.name(), "new"); + /// ``` + pub fn append_child(&mut self, mut child: Element) -> &mut Element { + if child.namespace.is_none() && self.namespace.is_some() { + child.namespace = self.namespace.clone(); + child.propagate_namespaces(); + } + self.children.push(Node::Element(child)); + if let Node::Element(ref mut cld) = *self.children.last_mut().unwrap() { + cld + } + else { + unreachable!() + } + } + + /// Appends a text node to an `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let mut elem = Element::bare("node"); + /// + /// assert_eq!(elem.text(), ""); + /// + /// elem.append_text_node("text"); + /// + /// assert_eq!(elem.text(), "text"); + /// ``` + pub fn append_text_node>(&mut self, child: S) { + self.children.push(Node::Text(child.into())); + } + + /// Appends a node to an `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::{Element, Node}; + /// + /// let mut elem = Element::bare("node"); + /// + /// elem.append_node(Node::Text("hello".to_owned())); + /// + /// assert_eq!(elem.text(), "hello"); + /// ``` + pub fn append_node(&mut self, node: Node) { + self.children.push(node); + } + + /// Returns the concatenation of all text nodes in the `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = "hello, world!".parse().unwrap(); + /// + /// assert_eq!(elem.text(), "hello, world!"); + /// ``` + pub fn text(&self) -> String { + let mut ret = String::new(); + for fork in &self.children { + if let Node::Text(ref s) = *fork { + ret += s; + } + } + ret + } + + /// Returns a reference to the first child element with the specific name and namespace, if it + /// exists in the direct descendants of this `Element`, else returns `None`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = r#""#.parse().unwrap(); + /// + /// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns")); + /// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns")); + /// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns")); + /// assert_eq!(elem.get_child("c", "ns"), None); + /// assert_eq!(elem.get_child("b", "other_ns"), None); + /// assert_eq!(elem.get_child("a", "inexistent_ns"), None); + /// ``` + pub fn get_child, NS: AsRef>(&self, name: N, namespace: NS) -> Option<&Element> { + for fork in &self.children { + if let Node::Element(ref e) = *fork { + if e.is(name.as_ref(), namespace.as_ref()) { + return Some(e); + } + } + } + None + } + + /// Returns a mutable reference to the first child element with the specific name and namespace, + /// if it exists in the direct descendants of this `Element`, else returns `None`. + pub fn get_child_mut, NS: AsRef>(&mut self, name: N, namespace: NS) -> Option<&mut Element> { + for fork in &mut self.children { + if let Node::Element(ref mut e) = *fork { + if e.is(name.as_ref(), namespace.as_ref()) { + return Some(e); + } + } + } + None + } + + /// Returns whether a specific child with this name and namespace exists in the direct + /// descendants of the `Element`. + /// + /// # Examples + /// + /// ``` + /// use minidom::Element; + /// + /// let elem: Element = r#""#.parse().unwrap(); + /// + /// assert_eq!(elem.has_child("a", "other_ns"), true); + /// assert_eq!(elem.has_child("a", "ns"), true); + /// assert_eq!(elem.has_child("a", "inexistent_ns"), false); + /// assert_eq!(elem.has_child("b", "ns"), true); + /// assert_eq!(elem.has_child("b", "other_ns"), false); + /// assert_eq!(elem.has_child("b", "inexistent_ns"), false); + /// ``` + pub fn has_child, NS: AsRef>(&self, name: N, namespace: NS) -> bool { + self.get_child(name, namespace).is_some() + } +} + +/// An iterator over references to children of an `Element`. +pub struct Children<'a> { + iter: slice::Iter<'a, Node>, +} + +impl<'a> Iterator for Children<'a> { + type Item = &'a Element; + + fn next(&mut self) -> Option<&'a Element> { + while let Some(item) = self.iter.next() { + if let Node::Element(ref child) = *item { + return Some(child); + } + } + None + } +} + +/// An iterator over mutable references to children of an `Element`. +pub struct ChildrenMut<'a> { + iter: slice::IterMut<'a, Node>, +} + +impl<'a> Iterator for ChildrenMut<'a> { + type Item = &'a mut Element; + + fn next(&mut self) -> Option<&'a mut Element> { + while let Some(item) = self.iter.next() { + if let Node::Element(ref mut child) = *item { + return Some(child); + } + } + None + } +} + +/// A builder for `Element`s. +pub struct ElementBuilder { + root: Element, +} + +impl ElementBuilder { + /// Sets the namespace. + pub fn ns>(mut self, namespace: S) -> ElementBuilder { + self.root.namespace = Some(namespace.into()); + self + } + + /// Sets an attribute. + pub fn attr, V: IntoAttributeValue>(mut self, name: S, value: V) -> ElementBuilder { + self.root.set_attr(name, value); + self + } + + /// Appends anything implementing `IntoElements` into the tree. + pub fn append(mut self, into: T) -> ElementBuilder { + { + let mut emitter = ElementEmitter::new(&mut self.root); + into.into_elements(&mut emitter); + } + self + } + + /// Builds the `Element`. + pub fn build(self) -> Element { + self.root + } +} + +#[test] +fn test_element_new() { + let elem = Element::new( "name".to_owned() + , Some("namespace".to_owned()) + , vec![ Attribute::new("name", "value") ] + , Vec::new() ); + + assert_eq!(elem.name(), "name"); + assert_eq!(elem.ns(), Some("namespace")); + assert_eq!(elem.attr("name"), Some("value")); + assert_eq!(elem.attr("inexistent"), None); +} diff --git a/src/error.rs b/src/error.rs index 38f4069051b2de05338ac2ff08b8ff81394e54a2..dc818e2f651e8452d548182f5957b57663147aa1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,5 @@ +//! Provides an error type for this crate. + use std::io; use std::convert::From; @@ -8,9 +10,13 @@ use xml::reader::Error as ReaderError; /// An enum representing the possible errors. #[derive(Debug)] pub enum Error { + /// An io::Error. IoError(io::Error), + /// An error in the xml-rs `EventWriter`. XmlWriterError(WriterError), + /// An error in the xml-rs `EventReader`. XmlReaderError(ReaderError), + /// The end of the document has been reached unexpectedly. EndOfDocument, } diff --git a/src/lib.rs b/src/lib.rs index 9dfed4489b2e9282bc47e1648424e21b0394dd35..4728608db797f5ee40e62273acd5bffa58e38f8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![deny(missing_docs)] + //! A minimal DOM crate built on top of xml-rs. //! //! This library exports an `Element` struct which represents a DOM tree. @@ -64,622 +66,14 @@ extern crate xml; -mod error; - -mod attribute; - -use std::io::prelude::*; -use std::io::Cursor; - -use std::convert::AsRef; - -use std::iter::Iterator; - -use std::slice; - -use std::fmt; +pub mod error; +pub mod attribute; +pub mod element; +pub mod convert; -use std::str::FromStr; - -use xml::reader::{XmlEvent as ReaderEvent, EventReader}; -use xml::writer::{XmlEvent as WriterEvent, EventWriter}; -use xml::name::Name; -use xml::namespace::NS_NO_PREFIX; +#[cfg(test)] mod tests; pub use error::Error; - pub use attribute::Attribute; - -#[derive(Clone, PartialEq, Eq)] -/// A struct representing a DOM Element. -pub struct Element { - name: String, - namespace: Option, - attributes: Vec, - children: Vec, -} - -impl fmt::Debug for Element { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - if let Some(ref ns) = self.namespace { - write!(fmt, "<{{{}}}{}", ns, self.name)?; - } - else { - write!(fmt, "<{}", self.name)?; - } - for attr in &self.attributes { - write!(fmt, " {}", attr)?; - } - write!(fmt, ">")?; - for child in &self.children { - match *child { - Fork::Element(ref e) => { - write!(fmt, "{:?}", e)?; - }, - Fork::Text(ref s) => { - write!(fmt, "{}", s)?; - }, - } - } - write!(fmt, "", self.name)?; - Ok(()) - } -} - -impl FromStr for Element { - type Err = Error; - - fn from_str(s: &str) -> Result { - let mut reader = EventReader::new(Cursor::new(s)); - Element::from_reader(&mut reader) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -enum Fork { - Element(Element), - Text(String), -} - -impl Element { - /// Constructs a new `Element` with the given `name`, `namespace` and `attributes`. - /// - /// You probably should be using `Element::builder` instead of this. - /// - /// # Examples - /// - /// ``` - /// use minidom::{Element, Attribute}; - /// - /// let elem = Element::new( "name".to_owned() - /// , Some("namespace".to_owned()) - /// , vec![ Attribute::new("name", "value") ] ); - /// - /// assert_eq!(elem.name(), "name"); - /// assert_eq!(elem.ns(), Some("namespace")); - /// assert_eq!(elem.attr("name"), Some("value")); - /// assert_eq!(elem.attr("inexistent"), None); - /// ``` - pub fn new(name: String, namespace: Option, attributes: Vec) -> Element { - Element { - name: name, - namespace: namespace, - attributes: attributes, - children: Vec::new(), - } - } - - /// Return a builder for an `Element` with the given `name`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem = Element::builder("name") - /// .ns("namespace") - /// .attr("name", "value") - /// .text("inner") - /// .build(); - /// - /// assert_eq!(elem.name(), "name"); - /// assert_eq!(elem.ns(), Some("namespace")); - /// assert_eq!(elem.attr("name"), Some("value")); - /// assert_eq!(elem.attr("inexistent"), None); - /// assert_eq!(elem.text(), "inner"); - /// ``` - pub fn builder>(name: S) -> ElementBuilder { - ElementBuilder { - name: name.into(), - text: None, - namespace: None, - attributes: Vec::new(), - } - } - - /// Returns a bare minimum `Element` with this name. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let bare = Element::bare("name"); - /// - /// assert_eq!(bare.name(), "name"); - /// assert_eq!(bare.ns(), None); - /// assert_eq!(bare.attr("name"), None); - /// assert_eq!(bare.text(), ""); - /// ``` - pub fn bare>(name: S) -> Element { - Element { - name: name.into(), - namespace: None, - attributes: Vec::new(), - children: Vec::new(), - } - } - - /// Returns a reference to the name of this element. - pub fn name(&self) -> &str { - &self.name - } - - /// Returns a reference to the namespace of this element, if it has one, else `None`. - pub fn ns(&self) -> Option<&str> { - self.namespace.as_ref() - .map(String::as_ref) - } - - /// Returns a reference to the value of the given attribute, if it exists, else `None`. - pub fn attr(&self, name: &str) -> Option<&str> { - for attr in &self.attributes { - if attr.name == name { - return Some(&attr.value); - } - } - None - } - - /// Returns whether the element has the given name and namespace. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem = Element::builder("name").ns("namespace").build(); - /// - /// assert_eq!(elem.is("name", "namespace"), true); - /// assert_eq!(elem.is("name", "wrong"), false); - /// assert_eq!(elem.is("wrong", "namespace"), false); - /// assert_eq!(elem.is("wrong", "wrong"), false); - /// ``` - pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { - let ns = self.namespace.as_ref().map(String::as_ref); - self.name == name.as_ref() && ns == Some(namespace.as_ref()) - } - - pub fn from_reader(reader: &mut EventReader) -> Result { - loop { - let e = reader.next()?; - match e { - ReaderEvent::StartElement { name, attributes, namespace } => { - let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) - .collect(); - let ns = if let Some(ref prefix) = name.prefix { - namespace.get(prefix) - } - else { - namespace.get(NS_NO_PREFIX) - }.map(|s| s.to_owned()); - let mut root = Element::new(name.local_name, ns, attributes); - root.from_reader_inner(reader)?; - return Ok(root); - }, - ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); - }, - _ => () // TODO: may need more errors - } - } - } - - fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { - loop { - let e = reader.next()?; - match e { - ReaderEvent::StartElement { name, attributes, namespace } => { - let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) - .collect(); - let ns = if let Some(ref prefix) = name.prefix { - namespace.get(prefix) - } - else { - namespace.get(NS_NO_PREFIX) - }.map(|s| s.to_owned()); - let elem = Element::new(name.local_name, ns, attributes); - let elem_ref = self.append_child(elem); - elem_ref.from_reader_inner(reader)?; - }, - ReaderEvent::EndElement { .. } => { - // TODO: may want to check whether we're closing the correct element - return Ok(()); - }, - ReaderEvent::Characters(s) => { - self.append_text_node(s); - }, - ReaderEvent::CData(s) => { - self.append_text_node(s); - }, - ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); - }, - _ => (), // TODO: may need to implement more - } - } - } - - pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { - let name = if let Some(ref ns) = self.namespace { - Name::qualified(&self.name, &ns, None) - } - else { - Name::local(&self.name) - }; - let mut start = WriterEvent::start_element(name); - if let Some(ref ns) = self.namespace { - start = start.default_ns(ns.as_ref()); - } - for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently - start = start.attr(Name::local(&attr.name), &attr.value); - } - writer.write(start)?; - for child in &self.children { - match *child { - Fork::Element(ref e) => { - e.write_to(writer)?; - }, - Fork::Text(ref s) => { - writer.write(WriterEvent::characters(s))?; - }, - } - } - writer.write(WriterEvent::end_element())?; - Ok(()) - } - - /// Returns an iterator over references to the children of this element. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem: Element = "".parse().unwrap(); - /// - /// let mut iter = elem.children(); - /// assert_eq!(iter.next().unwrap().name(), "child1"); - /// assert_eq!(iter.next().unwrap().name(), "child2"); - /// assert_eq!(iter.next().unwrap().name(), "child3"); - /// assert_eq!(iter.next(), None); - /// ``` - pub fn children<'a>(&'a self) -> Children<'a> { - Children { - iter: self.children.iter(), - } - } - - /// Returns an iterator over mutable references to the children of this element. - pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { - ChildrenMut { - iter: self.children.iter_mut(), - } - } - - /// Appends a child node to the `Element`, returning the appended node. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let mut elem = Element::bare("root"); - /// - /// assert_eq!(elem.children().count(), 0); - /// - /// elem.append_child(Element::bare("child")); - /// - /// { - /// let mut iter = elem.children(); - /// assert_eq!(iter.next().unwrap().name(), "child"); - /// assert_eq!(iter.next(), None); - /// } - /// - /// let child = elem.append_child(Element::bare("new")); - /// - /// assert_eq!(child.name(), "new"); - /// ``` - pub fn append_child(&mut self, mut child: Element) -> &mut Element { - if child.namespace.is_none() { - child.namespace = self.namespace.clone(); - } - self.children.push(Fork::Element(child)); - if let Fork::Element(ref mut cld) = *self.children.last_mut().unwrap() { - cld - } - else { - unreachable!() - } - } - - /// Appends a text node to an `Element`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let mut elem = Element::bare("node"); - /// - /// assert_eq!(elem.text(), ""); - /// - /// elem.append_text_node("text"); - /// - /// assert_eq!(elem.text(), "text"); - /// ``` - pub fn append_text_node>(&mut self, child: S) { - self.children.push(Fork::Text(child.into())); - } - - /// Returns the concatenation of all text nodes in the `Element`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem: Element = "hello, world!".parse().unwrap(); - /// - /// assert_eq!(elem.text(), "hello, world!"); - /// ``` - pub fn text(&self) -> String { - let mut ret = String::new(); - for fork in &self.children { - if let Fork::Text(ref s) = *fork { - ret += s; - } - } - ret - } - - /// Returns a reference to the first child element with the specific name and namespace, if it - /// exists in the direct descendants of this `Element`, else returns `None`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem: Element = r#""#.parse().unwrap(); - /// - /// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns")); - /// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns")); - /// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns")); - /// assert_eq!(elem.get_child("c", "ns"), None); - /// assert_eq!(elem.get_child("b", "other_ns"), None); - /// assert_eq!(elem.get_child("a", "inexistent_ns"), None); - /// ``` - pub fn get_child, NS: AsRef>(&self, name: N, namespace: NS) -> Option<&Element> { - for fork in &self.children { - if let Fork::Element(ref e) = *fork { - if e.is(name.as_ref(), namespace.as_ref()) { - return Some(e); - } - } - } - None - } - - /// Returns a mutable reference to the first child element with the specific name and namespace, - /// if it exists in the direct descendants of this `Element`, else returns `None`. - pub fn get_child_mut, NS: AsRef>(&mut self, name: N, namespace: NS) -> Option<&mut Element> { - for fork in &mut self.children { - if let Fork::Element(ref mut e) = *fork { - if e.is(name.as_ref(), namespace.as_ref()) { - return Some(e); - } - } - } - None - } - - /// Returns whether a specific child with this name and namespace exists in the direct - /// descendants of the `Element`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Element; - /// - /// let elem: Element = r#""#.parse().unwrap(); - /// - /// assert_eq!(elem.has_child("a", "other_ns"), true); - /// assert_eq!(elem.has_child("a", "ns"), true); - /// assert_eq!(elem.has_child("a", "inexistent_ns"), false); - /// assert_eq!(elem.has_child("b", "ns"), true); - /// assert_eq!(elem.has_child("b", "other_ns"), false); - /// assert_eq!(elem.has_child("b", "inexistent_ns"), false); - /// ``` - pub fn has_child, NS: AsRef>(&self, name: N, namespace: NS) -> bool { - self.get_child(name, namespace).is_some() - } -} - -/// An iterator over references to children of an `Element`. -pub struct Children<'a> { - iter: slice::Iter<'a, Fork>, -} - -impl<'a> Iterator for Children<'a> { - type Item = &'a Element; - - fn next(&mut self) -> Option<&'a Element> { - while let Some(item) = self.iter.next() { - if let Fork::Element(ref child) = *item { - return Some(child); - } - } - None - } -} - -/// An iterator over mutable references to children of an `Element`. -pub struct ChildrenMut<'a> { - iter: slice::IterMut<'a, Fork>, -} - -impl<'a> Iterator for ChildrenMut<'a> { - type Item = &'a mut Element; - - fn next(&mut self) -> Option<&'a mut Element> { - while let Some(item) = self.iter.next() { - if let Fork::Element(ref mut child) = *item { - return Some(child); - } - } - None - } -} - -/// A builder for `Element`s. -pub struct ElementBuilder { - name: String, - text: Option, - namespace: Option, - attributes: Vec, -} - -impl ElementBuilder { - /// Sets the namespace. - pub fn ns>(mut self, namespace: S) -> ElementBuilder { - self.namespace = Some(namespace.into()); - self - } - - /// Sets an attribute. - pub fn attr, V: Into>(mut self, name: S, value: V) -> ElementBuilder { - self.attributes.push(Attribute::new(name, value)); - self - } - - /// Sets the inner text. - pub fn text>(mut self, text: S) -> ElementBuilder { - self.text = Some(text.into()); - self - } - - /// Builds the `Element`. - pub fn build(self) -> Element { - let mut elem = Element::new(self.name, self.namespace, self.attributes); - if let Some(text) = self.text { - elem.append_text_node(text); - } - elem - } -} - -#[cfg(test)] -mod tests { - use super::*; - - use xml::reader::EventReader; - use xml::writer::EventWriter; - - const TEST_STRING: &'static str = r#"meownya"#; - - fn build_test_tree() -> Element { - let mut root = Element::builder("root") - .ns("root_ns") - .attr("a", "b") - .build(); - root.append_text_node("meow"); - let child = Element::builder("child") - .attr("c", "d") - .build(); - root.append_child(child); - let other_child = Element::builder("child") - .ns("child_ns") - .attr("d", "e") - .build(); - root.append_child(other_child); - root.append_text_node("nya"); - root - } - - #[test] - fn reader_works() { - use std::io::Cursor; - let mut reader = EventReader::new(Cursor::new(TEST_STRING)); - assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree()); - } - - #[test] - fn writer_works() { - let root = build_test_tree(); - let mut out = Vec::new(); - { - let mut writer = EventWriter::new(&mut out); - root.write_to(&mut writer).unwrap(); - } - assert_eq!(String::from_utf8(out).unwrap(), TEST_STRING); - } - - #[test] - fn builder_works() { - let elem = Element::builder("a") - .ns("b") - .attr("c", "d") - .text("e") - .build(); - assert_eq!(elem.name(), "a"); - assert_eq!(elem.ns(), Some("b")); - assert_eq!(elem.attr("c"), Some("d")); - assert_eq!(elem.attr("x"), None); - assert_eq!(elem.text(), "e"); - assert!(elem.is("a", "b")); - } - - #[test] - fn children_iter_works() { - let root = build_test_tree(); - let mut iter = root.children(); - assert!(iter.next().unwrap().is("child", "root_ns")); - assert!(iter.next().unwrap().is("child", "child_ns")); - assert_eq!(iter.next(), None); - } - - #[test] - fn get_child_works() { - let root = build_test_tree(); - assert_eq!(root.get_child("child", "inexistent_ns"), None); - assert_eq!(root.get_child("not_a_child", "root_ns"), None); - assert!(root.get_child("child", "root_ns").unwrap().is("child", "root_ns")); - assert!(root.get_child("child", "child_ns").unwrap().is("child", "child_ns")); - assert_eq!(root.get_child("child", "root_ns").unwrap().attr("c"), Some("d")); - assert_eq!(root.get_child("child", "child_ns").unwrap().attr("d"), Some("e")); - } - - #[test] - fn namespace_propagation_works() { - let mut root = Element::builder("root").ns("root_ns").build(); - let mut child = Element::bare("child"); - let grandchild = Element::bare("grandchild"); - child.append_child(grandchild); - root.append_child(child); - assert_eq!(root.get_child("child", "root_ns").unwrap().ns(), root.ns()); - assert_eq!(root.get_child("grandchild", "root_ns").unwrap().ns(), root.ns()); - } -} +pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; +pub use convert::{IntoElements, IntoAttributeValue}; diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000000000000000000000000000000000000..b9263446cd110826889fbd63e7d9d560d4188e6a --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,96 @@ +use std::io::Cursor; + +use std::iter::Iterator; + +use xml::reader::EventReader; +use xml::writer::EventWriter; + +use element::Element; + +const TEST_STRING: &'static str = r#"meownya"#; + +fn build_test_tree() -> Element { + let mut root = Element::builder("root") + .ns("root_ns") + .attr("a", "b") + .build(); + root.append_text_node("meow"); + let child = Element::builder("child") + .attr("c", "d") + .build(); + root.append_child(child); + let other_child = Element::builder("child") + .ns("child_ns") + .attr("d", "e") + .build(); + root.append_child(other_child); + root.append_text_node("nya"); + root +} + +#[test] +fn reader_works() { + let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree()); +} + +#[test] +fn writer_works() { + let root = build_test_tree(); + let mut out = Vec::new(); + { + let mut writer = EventWriter::new(&mut out); + root.write_to(&mut writer).unwrap(); + } + assert_eq!(String::from_utf8(out).unwrap(), TEST_STRING); +} + +#[test] +fn builder_works() { + let elem = Element::builder("a") + .ns("b") + .attr("c", "d") + .append(Element::builder("child")) + .append("e") + .build(); + assert_eq!(elem.name(), "a"); + assert_eq!(elem.ns(), Some("b")); + assert_eq!(elem.attr("c"), Some("d")); + assert_eq!(elem.attr("x"), None); + assert_eq!(elem.text(), "e"); + assert!(elem.has_child("child", "b")); + assert!(elem.is("a", "b")); +} + +#[test] +fn children_iter_works() { + let root = build_test_tree(); + let mut iter = root.children(); + assert!(iter.next().unwrap().is("child", "root_ns")); + assert!(iter.next().unwrap().is("child", "child_ns")); + assert_eq!(iter.next(), None); +} + +#[test] +fn get_child_works() { + let root = build_test_tree(); + assert_eq!(root.get_child("child", "inexistent_ns"), None); + assert_eq!(root.get_child("not_a_child", "root_ns"), None); + assert!(root.get_child("child", "root_ns").unwrap().is("child", "root_ns")); + assert!(root.get_child("child", "child_ns").unwrap().is("child", "child_ns")); + assert_eq!(root.get_child("child", "root_ns").unwrap().attr("c"), Some("d")); + assert_eq!(root.get_child("child", "child_ns").unwrap().attr("d"), Some("e")); +} + +#[test] +fn namespace_propagation_works() { + let mut root = Element::builder("root").ns("root_ns").build(); + let mut child = Element::bare("child"); + let grandchild = Element::bare("grandchild"); + child.append_child(grandchild); + root.append_child(child); + assert_eq!(root.get_child("child", "root_ns").unwrap().ns(), root.ns()); + assert_eq!(root.get_child("child", "root_ns").unwrap() + .get_child("grandchild", "root_ns").unwrap() + .ns(), root.ns()); +} From f301f2cb106f121a8d732f185f4f0274d2b78417 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 31 Mar 2017 16:59:20 +0100 Subject: [PATCH 017/111] make Debug on Element return valid XML --- src/element.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index cedb90e5d9ede927ef116369fb2c8aca0f4d7bd3..50b63cd01359354040447926e0c4634245a1ba5b 100644 --- a/src/element.rs +++ b/src/element.rs @@ -31,11 +31,9 @@ pub struct Element { impl fmt::Debug for Element { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "<{}", self.name)?; if let Some(ref ns) = self.namespace { - write!(fmt, "<{{{}}}{}", ns, self.name)?; - } - else { - write!(fmt, "<{}", self.name)?; + write!(fmt, " xmlns=\"{}\"", ns)?; } for attr in &self.attributes { write!(fmt, " {}", attr)?; From 9af7d252d7041646a64685ce847b91a60cb3421e Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 31 Mar 2017 17:01:48 +0100 Subject: [PATCH 018/111] auto-close elements with no child --- src/element.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/element.rs b/src/element.rs index 50b63cd01359354040447926e0c4634245a1ba5b..13c8baf1c4e02fd86d0d86767964a424626de476 100644 --- a/src/element.rs +++ b/src/element.rs @@ -38,18 +38,23 @@ impl fmt::Debug for Element { for attr in &self.attributes { write!(fmt, " {}", attr)?; } - write!(fmt, ">")?; - for child in &self.children { - match *child { - Node::Element(ref e) => { - write!(fmt, "{:?}", e)?; - }, - Node::Text(ref s) => { - write!(fmt, "{}", s)?; - }, + if self.children.is_empty() { + write!(fmt, "/>")?; + } + else { + write!(fmt, ">")?; + for child in &self.children { + match *child { + Node::Element(ref e) => { + write!(fmt, "{:?}", e)?; + }, + Node::Text(ref s) => { + write!(fmt, "{}", s)?; + }, + } } + write!(fmt, "", self.name)?; } - write!(fmt, "", self.name)?; Ok(()) } } From 6f0e88b25beab91b921090d1bbd4879105b1140b Mon Sep 17 00:00:00 2001 From: Eijebong Date: Mon, 24 Apr 2017 16:56:29 +0200 Subject: [PATCH 019/111] Correctly add namespaced attributes to elements Instead of adding the local_name of an attribute, if a prefix exists, add prefix:local_name to allow users to retrieve it via the namespaced key name. For example, with this XML: ``` ``` `root.attr("xml:lang").unwrap()` will now correctly return "en". `root.attr("lang")` will not retrieve "xml:lang" value anymore. This is a breaking change. Fixes #2 --- src/element.rs | 21 +++++++++++++++++++-- src/tests.rs | 12 +++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/element.rs b/src/element.rs index 13c8baf1c4e02fd86d0d86767964a424626de476..0db7ba81fd9aa816fcc650a8670e200eb345b825 100644 --- a/src/element.rs +++ b/src/element.rs @@ -197,7 +197,15 @@ impl Element { match e { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) + .map(|o| { + Attribute::new( + match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value + ) + }) .collect(); let ns = if let Some(ref prefix) = name.prefix { namespace.get(prefix) @@ -205,6 +213,7 @@ impl Element { else { namespace.get(NS_NO_PREFIX) }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes, Vec::new()); root.from_reader_inner(reader)?; return Ok(root); @@ -223,7 +232,15 @@ impl Element { match e { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) + .map(|o| { + Attribute::new( + match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value + ) + }) .collect(); let ns = if let Some(ref prefix) = name.prefix { namespace.get(prefix) diff --git a/src/tests.rs b/src/tests.rs index b9263446cd110826889fbd63e7d9d560d4188e6a..03d3ab9ab60b81542f832d66867879fffe92c2a8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -7,11 +7,12 @@ use xml::writer::EventWriter; use element::Element; -const TEST_STRING: &'static str = r#"meownya"#; +const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { let mut root = Element::builder("root") .ns("root_ns") + .attr("xml:lang", "en") .attr("a", "b") .build(); root.append_text_node("meow"); @@ -22,6 +23,7 @@ fn build_test_tree() -> Element { let other_child = Element::builder("child") .ns("child_ns") .attr("d", "e") + .attr("xml:lang", "fr") .build(); root.append_child(other_child); root.append_text_node("nya"); @@ -94,3 +96,11 @@ fn namespace_propagation_works() { .get_child("grandchild", "root_ns").unwrap() .ns(), root.ns()); } + +#[test] +fn namespace_attributes_works() { + let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + let root = Element::from_reader(&mut reader).unwrap(); + assert_eq!("en", root.attr("xml:lang").unwrap()); + assert_eq!("fr", root.get_child("child", "child_ns").unwrap().attr("xml:lang").unwrap()); +} From 0d0c4b76eb674f02aa4cdccd3f3a9a8d466926d0 Mon Sep 17 00:00:00 2001 From: Eijebong Date: Mon, 24 Apr 2017 20:06:21 +0200 Subject: [PATCH 020/111] Fix the PartialEq implementation for Element The order of attributes in an `Element` doesn't matter anymore. `` and `` are now correctly considered equal. For that I had to derive `PartialOrd` and `Ord` for `Attribute`. This allows us to sort cloned vectors of `Attribute` in the `PartialEq` implementation and compare them instead of the struct `attributes`. Fixes #3 --- src/attribute.rs | 2 +- src/element.rs | 16 +++++++++++++++- src/tests.rs | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/attribute.rs b/src/attribute.rs index 57373a06dc7ee6b6a62b9b226edc0911dc1e22f4..10e0e3d2a466196e406485e17d9b466a1b8e360e 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -9,7 +9,7 @@ use std::fmt; /// This is of the form: `name`="`value`" /// /// This does not support prefixed/namespaced attributes yet. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] pub struct Attribute { /// The name of the attribute. pub name: String, diff --git a/src/element.rs b/src/element.rs index 13c8baf1c4e02fd86d0d86767964a424626de476..197d9ed0ebb0153353a2b8bbe52a4e085ea0c4d1 100644 --- a/src/element.rs +++ b/src/element.rs @@ -20,7 +20,7 @@ use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, Eq)] /// A struct representing a DOM Element. pub struct Element { name: String, @@ -29,6 +29,20 @@ pub struct Element { children: Vec, } +impl PartialEq for Element { + fn eq(&self, other: &Element) -> bool { + let mut my_attr = self.attributes.clone(); + my_attr.sort(); + let mut other_attr = other.attributes.clone(); + other_attr.sort(); + + self.name == other.name && + self.namespace == other.namespace && + my_attr == other_attr && + self.children == other.children + } +} + impl fmt::Debug for Element { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "<{}", self.name)?; diff --git a/src/tests.rs b/src/tests.rs index b9263446cd110826889fbd63e7d9d560d4188e6a..44fef37b89381a075a1c7c975989b51ad28b1468 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -94,3 +94,14 @@ fn namespace_propagation_works() { .get_child("grandchild", "root_ns").unwrap() .ns(), root.ns()); } + +#[test] +fn two_elements_with_same_arguments_different_order_are_equal() { + let elem1: Element = "".parse().unwrap(); + let elem2: Element = "".parse().unwrap(); + assert_eq!(elem1, elem2); + + let elem1: Element = "".parse().unwrap(); + let elem2: Element = "".parse().unwrap(); + assert_ne!(elem1, elem2); +} From d54b002243c5a8490016a5fddd8195a48ce81e3d Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 26 Apr 2017 01:14:16 +0200 Subject: [PATCH 021/111] bump to 2.0.0, switch license to MIT --- COPYING | 675 ------------------------------------------------- COPYING.LESSER | 166 ------------ Cargo.toml | 6 +- LICENSE | 7 + README.md | 17 +- 5 files changed, 15 insertions(+), 856 deletions(-) delete mode 100644 COPYING delete mode 100644 COPYING.LESSER create mode 100644 LICENSE diff --git a/COPYING b/COPYING deleted file mode 100644 index a737dcfed5db21fb99a8fcb812e995937d38401b..0000000000000000000000000000000000000000 --- a/COPYING +++ /dev/null @@ -1,675 +0,0 @@ - - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/COPYING.LESSER b/COPYING.LESSER deleted file mode 100644 index 5f5ff16a4a0f6104fadb6a5beef527573e46b425..0000000000000000000000000000000000000000 --- a/COPYING.LESSER +++ /dev/null @@ -1,166 +0,0 @@ - - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. - - 0. Additional Definitions. - - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. - - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. - - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. diff --git a/Cargo.toml b/Cargo.toml index a28773a998f9f0e29dc2467bd5a8c4267e7188b4..94dac84b005af32ffbdbc81bca8438f4b84774bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "minidom" -version = "0.1.1" -authors = ["lumi "] +version = "0.2.0" +authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" documentation = "https://docs.rs/minidom" readme = "README.md" keywords = ["xml"] -license = "LGPL-3.0+" +license = "MIT" [badges] gitlab = { repository = "lumi/minidom-rs" } diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..c13d199536618beae551d74c02dcb94caf466b15 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2017 minidom-rs contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index ca2fae7a68b0167107a3f2e3e8e556e16dfc9c58..c9a9d31f72f391e5d6502c09db5435270092fb80 100644 --- a/README.md +++ b/README.md @@ -9,22 +9,15 @@ A minimal DOM library on top of xml-rs. What license is it under? ------------------------- -LGPLv3 or later. See `COPYING` and `COPYING.LESSER`. +MIT or later. See `LICENSE`. License yadda yadda. -------------------- - Copyright 2017, minidom-rs contributors. +Copyright 2017 minidom-rs contributors - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From 8116c6e657235182fdea86a24b2d66f291e807bd Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 26 Apr 2017 01:15:56 +0200 Subject: [PATCH 022/111] Well, that's embarrassing. Hope nobody will ever look at this commit. (if you see this, please don't look) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c9a9d31f72f391e5d6502c09db5435270092fb80..4cac5469b3f1226c13b2848b4fd70a202cfb8a81 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A minimal DOM library on top of xml-rs. What license is it under? ------------------------- -MIT or later. See `LICENSE`. +MIT. See `LICENSE`. License yadda yadda. -------------------- From 2aef4aacb851cd40170201de575a753a46979a4e Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 26 Apr 2017 01:26:29 +0200 Subject: [PATCH 023/111] linewrap license --- LICENSE | 16 ++++++++++++---- README.md | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/LICENSE b/LICENSE index c13d199536618beae551d74c02dcb94caf466b15..e3f453dd6acacbda82e62c5535f8055520dcad5e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,15 @@ -Copyright 2017 minidom-rs contributors -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT +OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 4cac5469b3f1226c13b2848b4fd70a202cfb8a81..c0426ba8e5a48d5f3e957b9769672152d8bc9587 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,17 @@ License yadda yadda. Copyright 2017 minidom-rs contributors -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT +OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From f87e2442d4350d187ce5403bb87919437cdfab21 Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Wed, 26 Apr 2017 23:44:58 +0200 Subject: [PATCH 024/111] Use a BTreeMap instead of a Vec to store attributes This way we don't need to reimplement PartialEq for Element. It's also way easier to get an attribute by name as we don't need to iterate over every attribute to see if it exists. The only side effect is that now, in the Debug output, attributes are automatically sorted by names instead of being sorted by insertion order. Fixes #4 --- src/attribute.rs | 45 ----------------------------- src/element.rs | 75 ++++++++++++++++++------------------------------ src/lib.rs | 2 -- src/tests.rs | 2 +- 4 files changed, 29 insertions(+), 95 deletions(-) delete mode 100644 src/attribute.rs diff --git a/src/attribute.rs b/src/attribute.rs deleted file mode 100644 index 10e0e3d2a466196e406485e17d9b466a1b8e360e..0000000000000000000000000000000000000000 --- a/src/attribute.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Provides an `Attribute` type which represents an attribute in an XML document. - -use xml::escape::escape_str_attribute; - -use std::fmt; - -/// An attribute of a DOM element. -/// -/// This is of the form: `name`="`value`" -/// -/// This does not support prefixed/namespaced attributes yet. -#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] -pub struct Attribute { - /// The name of the attribute. - pub name: String, - /// The value of the attribute. - pub value: String, -} - -impl fmt::Display for Attribute { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) - } -} - -impl Attribute { - /// Construct a new attribute from the given `name` and `value`. - /// - /// # Examples - /// - /// ``` - /// use minidom::Attribute; - /// - /// let attr = Attribute::new("name", "value"); - /// - /// assert_eq!(attr.name, "name"); - /// assert_eq!(attr.value, "value"); - /// ``` - pub fn new, V: Into>(name: N, value: V) -> Attribute { - Attribute { - name: name.into(), - value: value.into(), - } - } -} diff --git a/src/element.rs b/src/element.rs index 58acc309198fc9a2313740b71dbd0fe411f090e6..4d3d59e1f10a1c9e949e6885eff166c78366c39b 100644 --- a/src/element.rs +++ b/src/element.rs @@ -2,13 +2,13 @@ use std::io::prelude::*; use std::io::Cursor; +use std::collections::BTreeMap; +use std::iter::FromIterator; use std::fmt; use error::Error; -use attribute::Attribute; - use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; use xml::name::Name; @@ -20,28 +20,15 @@ use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; -#[derive(Clone, Eq)] +#[derive(Clone, PartialEq, Eq)] /// A struct representing a DOM Element. pub struct Element { name: String, namespace: Option, - attributes: Vec, + attributes: BTreeMap, children: Vec, } -impl PartialEq for Element { - fn eq(&self, other: &Element) -> bool { - let mut my_attr = self.attributes.clone(); - my_attr.sort(); - let mut other_attr = other.attributes.clone(); - other_attr.sort(); - - self.name == other.name && - self.namespace == other.namespace && - my_attr == other_attr && - self.children == other.children - } -} impl fmt::Debug for Element { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { @@ -50,7 +37,7 @@ impl fmt::Debug for Element { write!(fmt, " xmlns=\"{}\"", ns)?; } for attr in &self.attributes { - write!(fmt, " {}", attr)?; + write!(fmt, " {}=\"{}\"", attr.0, attr.1)?; } if self.children.is_empty() { write!(fmt, "/>")?; @@ -92,7 +79,7 @@ pub enum Node { } impl Element { - fn new(name: String, namespace: Option, attributes: Vec, children: Vec) -> Element { + fn new(name: String, namespace: Option, attributes: BTreeMap, children: Vec) -> Element { Element { name: name, namespace: namespace, @@ -122,7 +109,7 @@ impl Element { /// ``` pub fn builder>(name: S) -> ElementBuilder { ElementBuilder { - root: Element::new(name.into(), None, Vec::new(), Vec::new()), + root: Element::new(name.into(), None, BTreeMap::new(), Vec::new()), } } @@ -144,7 +131,7 @@ impl Element { Element { name: name.into(), namespace: None, - attributes: Vec::new(), + attributes: BTreeMap::new(), children: Vec::new(), } } @@ -162,10 +149,8 @@ impl Element { /// Returns a reference to the value of the given attribute, if it exists, else `None`. pub fn attr(&self, name: &str) -> Option<&str> { - for attr in &self.attributes { - if attr.name == name { - return Some(&attr.value); - } + if let Some(value) = self.attributes.get(name) { + return Some(&value) } None } @@ -174,14 +159,14 @@ impl Element { pub fn set_attr, V: IntoAttributeValue>(&mut self, name: S, val: V) { let name = name.into(); let val = val.into_attribute_value(); - for attr in &mut self.attributes { - if attr.name == name { - attr.value = val.expect("removing existing value via set_attr, this is not yet supported (TODO)"); // TODO - return; - } + + if let Some(value) = self.attributes.get_mut(&name) { + *value = val.expect("removing existing value via set_attr, this is not yet supported (TODO)"); // TODO + return; } + if let Some(val) = val { - self.attributes.push(Attribute::new(name, val)); + self.attributes.insert(name, val); } } @@ -212,13 +197,11 @@ impl Element { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() .map(|o| { - Attribute::new( - match o.name.prefix { - Some(prefix) => format!("{}:{}", prefix, o.name.local_name), - None => o.name.local_name - }, - o.value - ) + (match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value) }) .collect(); let ns = if let Some(ref prefix) = name.prefix { @@ -247,13 +230,11 @@ impl Element { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() .map(|o| { - Attribute::new( - match o.name.prefix { - Some(prefix) => format!("{}:{}", prefix, o.name.local_name), - None => o.name.local_name - }, - o.value - ) + (match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value) }) .collect(); let ns = if let Some(ref prefix) = name.prefix { @@ -297,7 +278,7 @@ impl Element { start = start.default_ns(ns.as_ref()); } for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently - start = start.attr(Name::local(&attr.name), &attr.value); + start = start.attr(Name::local(&attr.0), &attr.1); } writer.write(start)?; for child in &self.children { @@ -582,7 +563,7 @@ impl ElementBuilder { fn test_element_new() { let elem = Element::new( "name".to_owned() , Some("namespace".to_owned()) - , vec![ Attribute::new("name", "value") ] + , BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() ) , Vec::new() ); assert_eq!(elem.name(), "name"); diff --git a/src/lib.rs b/src/lib.rs index 4728608db797f5ee40e62273acd5bffa58e38f8e..23e234c93f4205249f305a26e331a7dc9b65bd9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,13 +67,11 @@ extern crate xml; pub mod error; -pub mod attribute; pub mod element; pub mod convert; #[cfg(test)] mod tests; pub use error::Error; -pub use attribute::Attribute; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; pub use convert::{IntoElements, IntoAttributeValue}; diff --git a/src/tests.rs b/src/tests.rs index 64a32f920d49db61214b3522f7976d2b1eaf4546..f58bb6ebb1804f095bb928ece8fb459da3dc6029 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -7,7 +7,7 @@ use xml::writer::EventWriter; use element::Element; -const TEST_STRING: &'static str = r#"meownya"#; +const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { let mut root = Element::builder("root") From 154afda327d8383c1ba3dcbbd7fbf46a50b3b321 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 27 Apr 2017 17:42:00 +0100 Subject: [PATCH 025/111] Bump xml-rs dependency to 0.4.1. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 94dac84b005af32ffbdbc81bca8438f4b84774bf..00705bec73f645b9ed3ccf7af645aee0b1aee9fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,4 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -xml-rs = "0.3.6" +xml-rs = "0.4.1" From 875b95bdc91141cd8368f1ad0c40e84488c5d666 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 30 Apr 2017 23:46:29 +0100 Subject: [PATCH 026/111] Reexport ElementEmitter from the lib. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 23e234c93f4205249f305a26e331a7dc9b65bd9e..9846e75666de2696fc8506a7e02111e0328d86a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,4 +74,4 @@ pub mod convert; pub use error::Error; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; -pub use convert::{IntoElements, IntoAttributeValue}; +pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; From e9d33e5e7e1bdeb06f0d49fc088e9e8990b53233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 2 May 2017 16:30:31 +0100 Subject: [PATCH 027/111] Implement From for String and use it for fmt::Debug --- src/element.rs | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/element.rs b/src/element.rs index 4d3d59e1f10a1c9e949e6885eff166c78366c39b..ff31b87de02de1df932530007b87668d13b9e684 100644 --- a/src/element.rs +++ b/src/element.rs @@ -10,7 +10,7 @@ use std::fmt; use error::Error; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; -use xml::writer::{XmlEvent as WriterEvent, EventWriter}; +use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig}; use xml::name::Name; use xml::namespace::NS_NO_PREFIX; @@ -30,32 +30,19 @@ pub struct Element { } +impl<'a> From<&'a Element> for String { + fn from(elem: &'a Element) -> String { + let mut out = Vec::new(); + let config = EmitterConfig::new() + .write_document_declaration(false); + elem.write_to(&mut EventWriter::new_with_config(&mut out, config)).unwrap(); + String::from_utf8(out).unwrap() + } +} + impl fmt::Debug for Element { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "<{}", self.name)?; - if let Some(ref ns) = self.namespace { - write!(fmt, " xmlns=\"{}\"", ns)?; - } - for attr in &self.attributes { - write!(fmt, " {}=\"{}\"", attr.0, attr.1)?; - } - if self.children.is_empty() { - write!(fmt, "/>")?; - } - else { - write!(fmt, ">")?; - for child in &self.children { - match *child { - Node::Element(ref e) => { - write!(fmt, "{:?}", e)?; - }, - Node::Text(ref s) => { - write!(fmt, "{}", s)?; - }, - } - } - write!(fmt, "", self.name)?; - } + write!(fmt, "{}", String::from(self))?; Ok(()) } } @@ -275,7 +262,7 @@ impl Element { }; let mut start = WriterEvent::start_element(name); if let Some(ref ns) = self.namespace { - start = start.default_ns(ns.as_ref()); + start = start.default_ns(ns.clone()); } for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently start = start.attr(Name::local(&attr.0), &attr.1); From 1bc1437da8a585bf828291470268ab8a9e222114 Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 3 May 2017 18:37:03 +0200 Subject: [PATCH 028/111] bump version to 0.3.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 00705bec73f645b9ed3ccf7af645aee0b1aee9fb..582eb5ae57effe9344463c069da1ec8a04656a1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.2.0" +version = "0.3.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" From 33f9dee31fd58dd912a2c8ccc7dfb1ee806988b4 Mon Sep 17 00:00:00 2001 From: lumi Date: Wed, 3 May 2017 18:39:40 +0200 Subject: [PATCH 029/111] move use std::iter::FromIterator to tests --- src/element.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index ff31b87de02de1df932530007b87668d13b9e684..5ac2a1445101083780ec5ebcbd7df04f21585a9f 100644 --- a/src/element.rs +++ b/src/element.rs @@ -3,7 +3,6 @@ use std::io::prelude::*; use std::io::Cursor; use std::collections::BTreeMap; -use std::iter::FromIterator; use std::fmt; @@ -548,6 +547,8 @@ impl ElementBuilder { #[test] fn test_element_new() { + use std::iter::FromIterator; + let elem = Element::new( "name".to_owned() , Some("namespace".to_owned()) , BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() ) From 83d80dd2353f0144da76aea95153103c33f7c8f0 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 14 May 2017 16:38:56 +0200 Subject: [PATCH 030/111] Add iterators over attributes, nodes and text nodes. Clean up lots of code. Add as_element and as_text on Node. Add some inline annotations. --- src/element.rs | 268 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 227 insertions(+), 41 deletions(-) diff --git a/src/element.rs b/src/element.rs index 5ac2a1445101083780ec5ebcbd7df04f21585a9f..71bd391129144580ccfe57a642879c4305b45a7e 100644 --- a/src/element.rs +++ b/src/element.rs @@ -3,6 +3,7 @@ use std::io::prelude::*; use std::io::Cursor; use std::collections::BTreeMap; +use std::collections::btree_map; use std::fmt; @@ -19,6 +20,57 @@ use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; +/// A node in an element tree. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Node { + /// An `Element`. + Element(Element), + /// A text node. + Text(String), +} + +impl Node { + /// Turns this into an `Element` if possible, else returns None. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_element().unwrap().name(), "meow"); + /// assert_eq!(txt.as_element(), None); + /// ``` + pub fn as_element<'a>(&'a self) -> Option<&'a Element> { + match *self { + Node::Element(ref e) => Some(e), + Node::Text(_) => None, + } + } + + /// Turns this into a `String` if possible, else returns None. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_text(), None); + /// assert_eq!(txt.as_text().unwrap(), "meow"); + /// ``` + pub fn as_text<'a>(&'a self) -> Option<&'a str> { + match *self { + Node::Element(_) => None, + Node::Text(ref s) => Some(s), + } + } +} + #[derive(Clone, PartialEq, Eq)] /// A struct representing a DOM Element. pub struct Element { @@ -28,7 +80,6 @@ pub struct Element { children: Vec, } - impl<'a> From<&'a Element> for String { fn from(elem: &'a Element) -> String { let mut out = Vec::new(); @@ -55,15 +106,6 @@ impl FromStr for Element { } } -/// A node in an element tree. -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Node { - /// An `Element`. - Element(Element), - /// A text node. - Text(String), -} - impl Element { fn new(name: String, namespace: Option, attributes: BTreeMap, children: Vec) -> Element { Element { @@ -78,7 +120,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let elem = Element::builder("name") @@ -103,7 +145,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let bare = Element::bare("name"); @@ -141,6 +183,34 @@ impl Element { None } + /// Returns an iterator over the attributes of this element. + /// + /// # Example + /// + /// ```rust + /// use minidom::Element; + /// + /// let elm: Element = "".parse().unwrap(); + /// + /// let mut iter = elm.attrs(); + /// + /// assert_eq!(iter.next().unwrap(), ("a", "b")); + /// assert_eq!(iter.next(), None); + /// ``` + pub fn attrs<'a>(&'a self) -> Attrs<'a> { + Attrs { + iter: self.attributes.iter(), + } + } + + /// Returns an iterator over the attributes of this element, with the value being a mutable + /// reference. + pub fn attrs_mut<'a>(&'a mut self) -> AttrsMut<'a> { + AttrsMut { + iter: self.attributes.iter_mut(), + } + } + /// Modifies the value of an attribute. pub fn set_attr, V: IntoAttributeValue>(&mut self, name: S, val: V) { let name = name.into(); @@ -160,7 +230,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let elem = Element::builder("name").ns("namespace").build(); @@ -281,14 +351,41 @@ impl Element { Ok(()) } - /// Returns an iterator over references to the children of this element. + /// Returns an iterator over references to every child node of this element. /// /// # Examples /// + /// ```rust + /// use minidom::{Element, Node}; + /// + /// let elem: Element = "abc".parse().unwrap(); + /// + /// let mut iter = elem.nodes(); + /// + /// assert_eq!(iter.next().unwrap().as_text().unwrap(), "a"); + /// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c1"); + /// assert_eq!(iter.next().unwrap().as_text().unwrap(), "b"); + /// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c2"); + /// assert_eq!(iter.next().unwrap().as_text().unwrap(), "c"); + /// assert_eq!(iter.next(), None); /// ``` + #[inline] pub fn nodes<'a>(&'a self) -> Nodes<'a> { + self.children.iter() + } + + /// Returns an iterator over mutable references to every child node of this element. + #[inline] pub fn nodes_mut<'a>(&'a mut self) -> NodesMut<'a> { + self.children.iter_mut() + } + + /// Returns an iterator over references to every child element of this element. + /// + /// # Examples + /// + /// ```rust /// use minidom::Element; /// - /// let elem: Element = "".parse().unwrap(); + /// let elem: Element = "hellothisisignored".parse().unwrap(); /// /// let mut iter = elem.children(); /// assert_eq!(iter.next().unwrap().name(), "child1"); @@ -296,26 +393,43 @@ impl Element { /// assert_eq!(iter.next().unwrap().name(), "child3"); /// assert_eq!(iter.next(), None); /// ``` - pub fn children<'a>(&'a self) -> Children<'a> { + #[inline] pub fn children<'a>(&'a self) -> Children<'a> { Children { iter: self.children.iter(), } } - /// Returns an iterator over mutable references to the children of this element. - pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { + /// Returns an iterator over mutable references to every child element of this element. + #[inline] pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { ChildrenMut { iter: self.children.iter_mut(), } } - fn propagate_namespaces(&mut self) { - let ns = self.namespace.clone(); - for child in self.children_mut() { - if child.namespace.is_none() { - child.namespace = ns.clone(); - child.propagate_namespaces(); - } + /// Returns an iterator over references to every text node of this element. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Element; + /// + /// let elem: Element = "hello world!".parse().unwrap(); + /// + /// let mut iter = elem.texts(); + /// assert_eq!(iter.next().unwrap(), "hello"); + /// assert_eq!(iter.next().unwrap(), " world!"); + /// assert_eq!(iter.next(), None); + /// ``` + #[inline] pub fn texts<'a>(&'a self) -> Texts<'a> { + Texts { + iter: self.children.iter(), + } + } + + /// Returns an iterator over mutable references to every text node of this element. + #[inline] pub fn texts_mut<'a>(&'a mut self) -> TextsMut<'a> { + TextsMut { + iter: self.children.iter_mut(), } } @@ -323,7 +437,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let mut elem = Element::bare("root"); @@ -356,11 +470,21 @@ impl Element { } } + fn propagate_namespaces(&mut self) { + let ns = self.namespace.clone(); + for child in self.children_mut() { + if child.namespace.is_none() { + child.namespace = ns.clone(); + child.propagate_namespaces(); + } + } + } + /// Appends a text node to an `Element`. /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let mut elem = Element::bare("node"); @@ -379,7 +503,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::{Element, Node}; /// /// let mut elem = Element::bare("node"); @@ -396,21 +520,15 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// - /// let elem: Element = "hello, world!".parse().unwrap(); + /// let elem: Element = "hello, world!".parse().unwrap(); /// /// assert_eq!(elem.text(), "hello, world!"); /// ``` pub fn text(&self) -> String { - let mut ret = String::new(); - for fork in &self.children { - if let Node::Text(ref s) = *fork { - ret += s; - } - } - ret + self.texts().fold(String::new(), |ret, new| ret + new) } /// Returns a reference to the first child element with the specific name and namespace, if it @@ -418,7 +536,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let elem: Element = r#""#.parse().unwrap(); @@ -459,7 +577,7 @@ impl Element { /// /// # Examples /// - /// ``` + /// ```rust /// use minidom::Element; /// /// let elem: Element = r#""#.parse().unwrap(); @@ -476,7 +594,7 @@ impl Element { } } -/// An iterator over references to children of an `Element`. +/// An iterator over references to child elements of an `Element`. pub struct Children<'a> { iter: slice::Iter<'a, Node>, } @@ -494,7 +612,7 @@ impl<'a> Iterator for Children<'a> { } } -/// An iterator over mutable references to children of an `Element`. +/// An iterator over mutable references to child elements of an `Element`. pub struct ChildrenMut<'a> { iter: slice::IterMut<'a, Node>, } @@ -512,6 +630,74 @@ impl<'a> Iterator for ChildrenMut<'a> { } } +/// An iterator over references to child text nodes of an `Element`. +pub struct Texts<'a> { + iter: slice::Iter<'a, Node>, +} + +impl<'a> Iterator for Texts<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option<&'a str> { + while let Some(item) = self.iter.next() { + if let Node::Text(ref child) = *item { + return Some(child); + } + } + None + } +} + +/// An iterator over mutable references to child text nodes of an `Element`. +pub struct TextsMut<'a> { + iter: slice::IterMut<'a, Node>, +} + +impl<'a> Iterator for TextsMut<'a> { + type Item = &'a mut String; + + fn next(&mut self) -> Option<&'a mut String> { + while let Some(item) = self.iter.next() { + if let Node::Text(ref mut child) = *item { + return Some(child); + } + } + None + } +} + +/// An iterator over references to all child nodes of an `Element`. +pub type Nodes<'a> = slice::Iter<'a, Node>; + +/// An iterator over mutable references to all child nodes of an `Element`. +pub type NodesMut<'a> = slice::IterMut<'a, Node>; + +/// An iterator over the attributes of an `Element`. +pub struct Attrs<'a> { + iter: btree_map::Iter<'a, String, String>, +} + +impl<'a> Iterator for Attrs<'a> { + type Item = (&'a str, &'a str); + + fn next(&mut self) -> Option { + self.iter.next().map(|(x, y)| (x.as_ref(), y.as_ref())) + } +} + +/// An iterator over the attributes of an `Element`, with the values mutable. +pub struct AttrsMut<'a> { + iter: btree_map::IterMut<'a, String, String>, +} + +impl<'a> Iterator for AttrsMut<'a> { + type Item = (&'a str, &'a mut String); + + fn next(&mut self) -> Option { + self.iter.next().map(|(x, y)| (x.as_ref(), y)) + } +} + /// A builder for `Element`s. pub struct ElementBuilder { root: Element, From cb17ca24c111ec7aa8ade07f41a005b97a455472 Mon Sep 17 00:00:00 2001 From: lumi Date: Fri, 19 May 2017 13:12:46 +0200 Subject: [PATCH 031/111] bump version to 0.3.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 582eb5ae57effe9344463c069da1ec8a04656a1b..2ad76a9cec0c030b2eba80c5a0b308e7086d8b98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.3.0" +version = "0.3.1" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" From f4e5f5380ec6dc8e3c54bfc6ed131691ebe7528d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 21 May 2017 21:07:37 +0100 Subject: [PATCH 032/111] implement IntoElements and IntoAttributeValue for &String --- src/convert.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/convert.rs b/src/convert.rs index 83c187713fd98177a8a998a563e7ad928464a8ec..f3f2ae0975088e9ae7aecb2a3f5e03aca0f0be08 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -70,6 +70,12 @@ impl IntoElements for String { } } +impl<'a> IntoElements for &'a String { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_text_node(self.to_owned()); + } +} + impl<'a> IntoElements for &'a str { fn into_elements(self, emitter: &mut ElementEmitter) { emitter.append_text_node(self.to_owned()); @@ -88,6 +94,12 @@ impl IntoAttributeValue for String { } } +impl<'a> IntoAttributeValue for &'a String { + fn into_attribute_value(self) -> Option { + Some(self.to_owned()) + } +} + impl<'a> IntoAttributeValue for &'a str { fn into_attribute_value(self) -> Option { Some((*self).to_owned()) From f6ad64c910bf8364d5008e44bdf8ff04c0253371 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 16:09:04 +0200 Subject: [PATCH 033/111] now at version 0.3.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2ad76a9cec0c030b2eba80c5a0b308e7086d8b98..66d9055ce7ac588e9c0eb2228a9a1d6464238bf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.3.1" +version = "0.3.2" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" From 49d25968cbf45b5e936508e6b38863e77c24f71a Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 16:15:04 +0200 Subject: [PATCH 034/111] no need to clone that --- src/convert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.rs b/src/convert.rs index f3f2ae0975088e9ae7aecb2a3f5e03aca0f0be08..fb6fbfb03159c8964dc559a9844619fb986fc6ac 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -90,7 +90,7 @@ pub trait IntoAttributeValue { impl IntoAttributeValue for String { fn into_attribute_value(self) -> Option { - Some(self.clone()) + Some(self) } } From 3223fc11fe048d3a47edb6f4e6c809b7900e2556 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 16:15:26 +0200 Subject: [PATCH 035/111] this will already get auto-dereferenced --- src/convert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.rs b/src/convert.rs index fb6fbfb03159c8964dc559a9844619fb986fc6ac..ebca8e2a0629070ceb0ca1c239ca21d861637014 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -102,7 +102,7 @@ impl<'a> IntoAttributeValue for &'a String { impl<'a> IntoAttributeValue for &'a str { fn into_attribute_value(self) -> Option { - Some((*self).to_owned()) + Some(self.to_owned()) } } From dbcbe7cd9b0a30816b8e9c37a122c708a82b8574 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 18:45:43 +0200 Subject: [PATCH 036/111] make clippy happy --- src/convert.rs | 5 ++--- src/element.rs | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index ebca8e2a0629070ceb0ca1c239ca21d861637014..3e3ed4162c0e3e8a3676fdf26720fa9938bb17f4 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -45,9 +45,8 @@ impl<'a, T: IntoElements + Clone> IntoElements for &'a [T] { impl IntoElements for Option { fn into_elements(self, emitter: &mut ElementEmitter) { - match self { - Some(e) => e.into_elements(emitter), - None => (), + if let Some(e) = self { + e.into_elements(emitter); } } } diff --git a/src/element.rs b/src/element.rs index 71bd391129144580ccfe57a642879c4305b45a7e..31e0a16ff5c05e3f2a664368ed98ca1d3771f040 100644 --- a/src/element.rs +++ b/src/element.rs @@ -43,7 +43,7 @@ impl Node { /// assert_eq!(elm.as_element().unwrap().name(), "meow"); /// assert_eq!(txt.as_element(), None); /// ``` - pub fn as_element<'a>(&'a self) -> Option<&'a Element> { + pub fn as_element(&self) -> Option<&Element> { match *self { Node::Element(ref e) => Some(e), Node::Text(_) => None, @@ -63,7 +63,7 @@ impl Node { /// assert_eq!(elm.as_text(), None); /// assert_eq!(txt.as_text().unwrap(), "meow"); /// ``` - pub fn as_text<'a>(&'a self) -> Option<&'a str> { + pub fn as_text(&self) -> Option<&str> { match *self { Node::Element(_) => None, Node::Text(ref s) => Some(s), @@ -178,7 +178,7 @@ impl Element { /// Returns a reference to the value of the given attribute, if it exists, else `None`. pub fn attr(&self, name: &str) -> Option<&str> { if let Some(value) = self.attributes.get(name) { - return Some(&value) + return Some(value) } None } @@ -197,7 +197,7 @@ impl Element { /// assert_eq!(iter.next().unwrap(), ("a", "b")); /// assert_eq!(iter.next(), None); /// ``` - pub fn attrs<'a>(&'a self) -> Attrs<'a> { + pub fn attrs(&self) -> Attrs { Attrs { iter: self.attributes.iter(), } @@ -205,7 +205,7 @@ impl Element { /// Returns an iterator over the attributes of this element, with the value being a mutable /// reference. - pub fn attrs_mut<'a>(&'a mut self) -> AttrsMut<'a> { + pub fn attrs_mut(&mut self) -> AttrsMut { AttrsMut { iter: self.attributes.iter_mut(), } @@ -279,6 +279,7 @@ impl Element { } } + #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))] fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { loop { let e = reader.next()?; @@ -307,10 +308,7 @@ impl Element { // TODO: may want to check whether we're closing the correct element return Ok(()); }, - ReaderEvent::Characters(s) => { - self.append_text_node(s); - }, - ReaderEvent::CData(s) => { + ReaderEvent::Characters(s) | ReaderEvent::CData(s) => { self.append_text_node(s); }, ReaderEvent::EndDocument => { @@ -324,7 +322,7 @@ impl Element { /// Output a document to an `EventWriter`. pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { let name = if let Some(ref ns) = self.namespace { - Name::qualified(&self.name, &ns, None) + Name::qualified(&self.name, ns, None) } else { Name::local(&self.name) @@ -334,7 +332,7 @@ impl Element { start = start.default_ns(ns.clone()); } for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently - start = start.attr(Name::local(&attr.0), &attr.1); + start = start.attr(Name::local(attr.0), attr.1); } writer.write(start)?; for child in &self.children { @@ -369,12 +367,12 @@ impl Element { /// assert_eq!(iter.next().unwrap().as_text().unwrap(), "c"); /// assert_eq!(iter.next(), None); /// ``` - #[inline] pub fn nodes<'a>(&'a self) -> Nodes<'a> { + #[inline] pub fn nodes(&self) -> Nodes { self.children.iter() } /// Returns an iterator over mutable references to every child node of this element. - #[inline] pub fn nodes_mut<'a>(&'a mut self) -> NodesMut<'a> { + #[inline] pub fn nodes_mut(&mut self) -> NodesMut { self.children.iter_mut() } @@ -393,14 +391,14 @@ impl Element { /// assert_eq!(iter.next().unwrap().name(), "child3"); /// assert_eq!(iter.next(), None); /// ``` - #[inline] pub fn children<'a>(&'a self) -> Children<'a> { + #[inline] pub fn children(&self) -> Children { Children { iter: self.children.iter(), } } /// Returns an iterator over mutable references to every child element of this element. - #[inline] pub fn children_mut<'a>(&'a mut self) -> ChildrenMut<'a> { + #[inline] pub fn children_mut(&mut self) -> ChildrenMut { ChildrenMut { iter: self.children.iter_mut(), } @@ -420,14 +418,14 @@ impl Element { /// assert_eq!(iter.next().unwrap(), " world!"); /// assert_eq!(iter.next(), None); /// ``` - #[inline] pub fn texts<'a>(&'a self) -> Texts<'a> { + #[inline] pub fn texts(&self) -> Texts { Texts { iter: self.children.iter(), } } /// Returns an iterator over mutable references to every text node of this element. - #[inline] pub fn texts_mut<'a>(&'a mut self) -> TextsMut<'a> { + #[inline] pub fn texts_mut(&mut self) -> TextsMut { TextsMut { iter: self.children.iter_mut(), } @@ -602,6 +600,7 @@ pub struct Children<'a> { impl<'a> Iterator for Children<'a> { type Item = &'a Element; + #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a Element> { while let Some(item) = self.iter.next() { if let Node::Element(ref child) = *item { @@ -620,6 +619,7 @@ pub struct ChildrenMut<'a> { impl<'a> Iterator for ChildrenMut<'a> { type Item = &'a mut Element; + #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a mut Element> { while let Some(item) = self.iter.next() { if let Node::Element(ref mut child) = *item { @@ -638,6 +638,7 @@ pub struct Texts<'a> { impl<'a> Iterator for Texts<'a> { type Item = &'a str; + #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a str> { while let Some(item) = self.iter.next() { if let Node::Text(ref child) = *item { @@ -656,6 +657,7 @@ pub struct TextsMut<'a> { impl<'a> Iterator for TextsMut<'a> { type Item = &'a mut String; + #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a mut String> { while let Some(item) = self.iter.next() { if let Node::Text(ref mut child) = *item { From 3341d7e69204233075d7be89bea320297947a780 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 18:49:24 +0200 Subject: [PATCH 037/111] turns out i was wrong and clippy was right, all hail clippy --- src/element.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/element.rs b/src/element.rs index 31e0a16ff5c05e3f2a664368ed98ca1d3771f040..c77ed0967ab08d16138fedb9596dd2734a1ee650 100644 --- a/src/element.rs +++ b/src/element.rs @@ -600,9 +600,8 @@ pub struct Children<'a> { impl<'a> Iterator for Children<'a> { type Item = &'a Element; - #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a Element> { - while let Some(item) = self.iter.next() { + for item in &mut self.iter { if let Node::Element(ref child) = *item { return Some(child); } @@ -619,9 +618,8 @@ pub struct ChildrenMut<'a> { impl<'a> Iterator for ChildrenMut<'a> { type Item = &'a mut Element; - #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a mut Element> { - while let Some(item) = self.iter.next() { + for item in &mut self.iter { if let Node::Element(ref mut child) = *item { return Some(child); } @@ -638,9 +636,8 @@ pub struct Texts<'a> { impl<'a> Iterator for Texts<'a> { type Item = &'a str; - #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a str> { - while let Some(item) = self.iter.next() { + for item in &mut self.iter { if let Node::Text(ref child) = *item { return Some(child); } @@ -657,9 +654,8 @@ pub struct TextsMut<'a> { impl<'a> Iterator for TextsMut<'a> { type Item = &'a mut String; - #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))] fn next(&mut self) -> Option<&'a mut String> { - while let Some(item) = self.iter.next() { + for item in &mut self.iter { if let Node::Text(ref mut child) = *item { return Some(child); } From 6323529cd70ff9a57fb2ee33e7a64ad1e34e4364 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 18:50:43 +0200 Subject: [PATCH 038/111] bump version to 0.3.3 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 66d9055ce7ac588e9c0eb2228a9a1d6464238bf8..b5ee1a1381735c7226e209252280c16fb1779565 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.3.2" +version = "0.3.3" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" From 508c9714160c74863fecce75c4e5d5b882306bbe Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 19:20:01 +0200 Subject: [PATCH 039/111] port error.rs to error_chain, bump version to 0.4.0 --- Cargo.toml | 3 ++- src/element.rs | 14 +++++++------- src/error.rs | 43 +++++++++++++++++-------------------------- src/lib.rs | 1 + 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5ee1a1381735c7226e209252280c16fb1779565..8b6cf5e41440241e064f9cceb2d2f7e4a1a81b3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.3.3" +version = "0.4.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" @@ -15,3 +15,4 @@ gitlab = { repository = "lumi/minidom-rs" } [dependencies] xml-rs = "0.4.1" +error-chain = "0.10.0" diff --git a/src/element.rs b/src/element.rs index c77ed0967ab08d16138fedb9596dd2734a1ee650..3c5c9d522076cf593bedb57345a32fa29b1aad0c 100644 --- a/src/element.rs +++ b/src/element.rs @@ -7,7 +7,7 @@ use std::collections::btree_map; use std::fmt; -use error::Error; +use error::{Error, ErrorKind, Result}; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig}; @@ -100,7 +100,7 @@ impl fmt::Debug for Element { impl FromStr for Element { type Err = Error; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { let mut reader = EventReader::new(Cursor::new(s)); Element::from_reader(&mut reader) } @@ -246,7 +246,7 @@ impl Element { } /// Parse a document from an `EventReader`. - pub fn from_reader(reader: &mut EventReader) -> Result { + pub fn from_reader(reader: &mut EventReader) -> Result { loop { let e = reader.next()?; match e { @@ -272,7 +272,7 @@ impl Element { return Ok(root); }, ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); + bail!(ErrorKind::EndOfDocument); }, _ => () // TODO: may need more errors } @@ -280,7 +280,7 @@ impl Element { } #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))] - fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { + fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<()> { loop { let e = reader.next()?; match e { @@ -312,7 +312,7 @@ impl Element { self.append_text_node(s); }, ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); + bail!(ErrorKind::EndOfDocument); }, _ => (), // TODO: may need to implement more } @@ -320,7 +320,7 @@ impl Element { } /// Output a document to an `EventWriter`. - pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { + pub fn write_to(&self, writer: &mut EventWriter) -> Result<()> { let name = if let Some(ref ns) = self.namespace { Name::qualified(&self.name, ns, None) } diff --git a/src/error.rs b/src/error.rs index dc818e2f651e8452d548182f5957b57663147aa1..4e76c6a9a1be50012fe4f115f05c325e16dcda76 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,33 +7,24 @@ use std::convert::From; use xml::writer::Error as WriterError; use xml::reader::Error as ReaderError; -/// An enum representing the possible errors. -#[derive(Debug)] -pub enum Error { - /// An io::Error. - IoError(io::Error), - /// An error in the xml-rs `EventWriter`. - XmlWriterError(WriterError), - /// An error in the xml-rs `EventReader`. - XmlReaderError(ReaderError), - /// The end of the document has been reached unexpectedly. - EndOfDocument, -} - -impl From for Error { - fn from(err: io::Error) -> Error { - Error::IoError(err) - } -} - -impl From for Error { - fn from(err: WriterError) -> Error { - Error::XmlWriterError(err) +error_chain! { + foreign_links { + XmlWriterError(WriterError) + /// An error with writing an XML event, from xml::writer::EventWriter. + ; + XmlReaderError(ReaderError) + /// An error with reading an XML event, from xml::reader::EventReader. + ; + IoError(io::Error) + /// An I/O error, from std::io. + ; } -} -impl From for Error { - fn from(err: ReaderError) -> Error { - Error::XmlReaderError(err) + errors { + /// En error which is returned when the end of the document was reached prematurely. + EndOfDocument { + description("the end of the document has been reached prematurely") + display("the end of the document has been reached prematurely") + } } } diff --git a/src/lib.rs b/src/lib.rs index 9846e75666de2696fc8506a7e02111e0328d86a5..3539c99b4644b64ad28a0a890f475fe7271361d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ //! ``` extern crate xml; +#[macro_use] extern crate error_chain; pub mod error; pub mod element; From 9d45230c17b2b7b90f99674f294beec5bb054e76 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 22 May 2017 19:30:52 +0200 Subject: [PATCH 040/111] should re-export more things under the error module --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8b6cf5e41440241e064f9cceb2d2f7e4a1a81b3c..bc53cd5a57a77ba225dfa2641dbc48b157e8eae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.4.0" +version = "0.4.1" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" diff --git a/src/lib.rs b/src/lib.rs index 3539c99b4644b64ad28a0a890f475fe7271361d3..d51de436e8250786a1b24288daf74c08d14899e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,6 @@ pub mod convert; #[cfg(test)] mod tests; -pub use error::Error; +pub use error::{Error, ErrorKind, Result, ResultExt}; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; From ec31c2b4a3abbef3934c2d44247643e50d432c02 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 27 May 2017 21:37:37 +0000 Subject: [PATCH 041/111] make CI work, hopefully --- .gitlab-ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bd8cf4313765d2f38bea2ff4edc4a22ba61b9866..0f54ef57dff651c4da0e8d1054489abc4653019e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,9 @@ image: "scorpil/rust:nightly" +before_script: +- apt-get update -yqq +- apt-get install -yqq --no-install-recommends build-essential + test:cargo: script: - rustc --version && cargo --version From d63c7b991d6285ae7f9d4c4498ca6c6b3c8cfdff Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 May 2017 22:33:20 +0100 Subject: [PATCH 042/111] implement IntoAttributeValue for usize --- src/convert.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/convert.rs b/src/convert.rs index 3e3ed4162c0e3e8a3676fdf26720fa9938bb17f4..93631ad8b28cf068d66f783b75e18f61b93c1844 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -87,6 +87,12 @@ pub trait IntoAttributeValue { fn into_attribute_value(self) -> Option; } +impl IntoAttributeValue for usize { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } +} + impl IntoAttributeValue for String { fn into_attribute_value(self) -> Option { Some(self) From 638866fa4c9a7d0bd863cb00690cea68407fbab4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 May 2017 22:56:17 +0100 Subject: [PATCH 043/111] implement IntoAttributeValue for u32, u16 and u8 --- src/convert.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/convert.rs b/src/convert.rs index 93631ad8b28cf068d66f783b75e18f61b93c1844..980f68f72ced8458a42975ae0989c081952d9e19 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -93,6 +93,24 @@ impl IntoAttributeValue for usize { } } +impl IntoAttributeValue for u32 { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } +} + +impl IntoAttributeValue for u16 { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } +} + +impl IntoAttributeValue for u8 { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } +} + impl IntoAttributeValue for String { fn into_attribute_value(self) -> Option { Some(self) From 4166751828ec2b4dd81109ff2903f86e7946769b Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 28 May 2017 00:02:26 +0200 Subject: [PATCH 044/111] bump version to 0.4.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bc53cd5a57a77ba225dfa2641dbc48b157e8eae1..39ba344161f302afe9efac7853b3a9d608e3a251 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.4.1" +version = "0.4.2" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" From d6a9e6e9eae67069342a9e98083aafae87cf391c Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 28 May 2017 00:25:57 +0200 Subject: [PATCH 045/111] implement ToAttributeValue on integral types, bump version --- Cargo.toml | 2 +- src/convert.rs | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39ba344161f302afe9efac7853b3a9d608e3a251..0ddb0252e38126f9e33a170a48d5a52b17f88ca0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.4.2" +version = "0.4.3" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" diff --git a/src/convert.rs b/src/convert.rs index 980f68f72ced8458a42975ae0989c081952d9e19..126688b7a5f997a14b996677fe750b2878368649 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -87,29 +87,23 @@ pub trait IntoAttributeValue { fn into_attribute_value(self) -> Option; } -impl IntoAttributeValue for usize { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) - } -} - -impl IntoAttributeValue for u32 { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) +macro_rules! impl_into_attribute_value { + ($t:ty) => { + impl IntoAttributeValue for $t { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } + } } } -impl IntoAttributeValue for u16 { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) +macro_rules! impl_into_attribute_values { + ($($t:ty),*) => { + $(impl_into_attribute_value!($t);)* } } -impl IntoAttributeValue for u8 { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) - } -} +impl_into_attribute_values!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8); impl IntoAttributeValue for String { fn into_attribute_value(self) -> Option { @@ -134,3 +128,20 @@ impl IntoAttributeValue for Option { self.and_then(|t| t.into_attribute_value()) } } + +#[cfg(test)] +mod tests { + use super::IntoAttributeValue; + + #[test] + fn test_into_attribute_value_on_ints() { + assert_eq!(16u8.into_attribute_value().unwrap() , "16"); + assert_eq!(17u16.into_attribute_value().unwrap() , "17"); + assert_eq!(18u32.into_attribute_value().unwrap() , "18"); + assert_eq!(19u64.into_attribute_value().unwrap() , "19"); + assert_eq!( 16i8.into_attribute_value().unwrap() , "16"); + assert_eq!((-17i16).into_attribute_value().unwrap(), "-17"); + assert_eq!( 18i32.into_attribute_value().unwrap(), "18"); + assert_eq!((-19i64).into_attribute_value().unwrap(), "-19"); + } +} From 9cec9fce9b8d203cd5b4c6edb7b5a0b78b8e129a Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Wed, 7 Jun 2017 22:40:53 +0200 Subject: [PATCH 046/111] Replace xml-rs by quick_xml quick_xml is way faster than xml-rs Here is an example with a quick atom parser: With xml-rs: test parse_factorio_atom ... bench: 3,295,678 ns/iter (+/- 165,851) With quick_xml: test parse_factorio_atom ... bench: 203,215 ns/iter (+/- 13,485) Unfortunately I had to break the API for this change to happen. * Element::from_reader now takes `R: BufRead` instead of `R: Read` * Element::write_to now takes `W: io::Write` instead of `EventWriter` This migration also allow us to have a write_to function which assumes we're already in a given namespace (see `write_to_in_namespace`). --- Cargo.toml | 6 +- src/element.rs | 223 +++++++++++++++++++++++++++---------------------- src/error.rs | 22 ++--- src/lib.rs | 2 +- src/tests.rs | 24 ++++-- 5 files changed, 153 insertions(+), 124 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0ddb0252e38126f9e33a170a48d5a52b17f88ca0..ebb09d1893cad3bff8654b0da2c514d214fde03e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "minidom" -version = "0.4.3" +version = "0.5.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] -description = "A small, simple DOM implementation on top of xml-rs." +description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" documentation = "https://docs.rs/minidom" @@ -14,5 +14,5 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -xml-rs = "0.4.1" +quick-xml = "0.7.3" error-chain = "0.10.0" diff --git a/src/element.rs b/src/element.rs index 3c5c9d522076cf593bedb57345a32fa29b1aad0c..ee2485083823876146d84774dcbde32770a38943 100644 --- a/src/element.rs +++ b/src/element.rs @@ -1,18 +1,16 @@ //! Provides an `Element` type, which represents DOM nodes, and a builder to create them with. -use std::io::prelude::*; -use std::io::Cursor; -use std::collections::BTreeMap; -use std::collections::btree_map; +use std::io:: Write; +use std::collections::{btree_map, BTreeMap}; -use std::fmt; +use std::str; use error::{Error, ErrorKind, Result}; -use xml::reader::{XmlEvent as ReaderEvent, EventReader}; -use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig}; -use xml::name::Name; -use xml::namespace::NS_NO_PREFIX; +use quick_xml::reader::Reader as EventReader; +use quick_xml::events::{Event, BytesStart}; + +use std::io::BufRead; use std::str::FromStr; @@ -69,9 +67,18 @@ impl Node { Node::Text(ref s) => Some(s), } } + + fn write_to_inner(&self, writer: &mut W, last_namespace: &mut Option) -> Result<()>{ + match *self { + Node::Element(ref elmt) => elmt.write_to_inner(writer, last_namespace)?, + Node::Text(ref s) => write!(writer, "{}", s)?, + } + + Ok(()) + } } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Debug)] /// A struct representing a DOM Element. pub struct Element { name: String, @@ -82,26 +89,18 @@ pub struct Element { impl<'a> From<&'a Element> for String { fn from(elem: &'a Element) -> String { - let mut out = Vec::new(); - let config = EmitterConfig::new() - .write_document_declaration(false); - elem.write_to(&mut EventWriter::new_with_config(&mut out, config)).unwrap(); - String::from_utf8(out).unwrap() + let mut writer = Vec::new(); + elem.write_to(&mut writer).unwrap(); + String::from_utf8(writer).unwrap() } } -impl fmt::Debug for Element { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", String::from(self))?; - Ok(()) - } -} impl FromStr for Element { type Err = Error; fn from_str(s: &str) -> Result { - let mut reader = EventReader::new(Cursor::new(s)); + let mut reader = EventReader::from_str(s); Element::from_reader(&mut reader) } } @@ -246,106 +245,105 @@ impl Element { } /// Parse a document from an `EventReader`. - pub fn from_reader(reader: &mut EventReader) -> Result { + pub fn from_reader(reader: &mut EventReader) -> Result { + let mut buf = Vec::new(); + let root: Element; + loop { - let e = reader.next()?; + let e = reader.read_event(&mut buf)?; match e { - ReaderEvent::StartElement { name, attributes, namespace } => { - let attributes = attributes.into_iter() - .map(|o| { - (match o.name.prefix { - Some(prefix) => format!("{}:{}", prefix, o.name.local_name), - None => o.name.local_name - }, - o.value) - }) - .collect(); - let ns = if let Some(ref prefix) = name.prefix { - namespace.get(prefix) - } - else { - namespace.get(NS_NO_PREFIX) - }.map(|s| s.to_owned()); - - let mut root = Element::new(name.local_name, ns, attributes, Vec::new()); - root.from_reader_inner(reader)?; - return Ok(root); + Event::Empty(ref e) | Event::Start(ref e) => { + root = build_element(e)?; // FIXME: could be break build_element(e)? when break value is stable + break; }, - ReaderEvent::EndDocument => { + Event::Eof => { bail!(ErrorKind::EndOfDocument); }, _ => () // TODO: may need more errors } - } - } + }; + + let mut stack = vec![root]; - #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))] - fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<()> { loop { - let e = reader.next()?; - match e { - ReaderEvent::StartElement { name, attributes, namespace } => { - let attributes = attributes.into_iter() - .map(|o| { - (match o.name.prefix { - Some(prefix) => format!("{}:{}", prefix, o.name.local_name), - None => o.name.local_name - }, - o.value) - }) - .collect(); - let ns = if let Some(ref prefix) = name.prefix { - namespace.get(prefix) - } - else { - namespace.get(NS_NO_PREFIX) - }.map(|s| s.to_owned()); - let elem = Element::new(name.local_name, ns, attributes, Vec::with_capacity(1)); - let elem_ref = self.append_child(elem); - elem_ref.from_reader_inner(reader)?; + match reader.read_event(&mut buf)? { + Event::Empty(ref e) => { + let elem = build_element(e)?; + // Since there is no Event::End after, directly append it to the current node + stack.last_mut().unwrap().append_child(elem); }, - ReaderEvent::EndElement { .. } => { - // TODO: may want to check whether we're closing the correct element - return Ok(()); + Event::Start(ref e) => { + let elem = build_element(e)?; + stack.push(elem); }, - ReaderEvent::Characters(s) | ReaderEvent::CData(s) => { - self.append_text_node(s); + Event::End(ref e) => { + if stack.len() <= 1 { + break; + } + let elem = stack.pop().unwrap(); + if let Some(to) = stack.last_mut() { + if elem.name().as_bytes() != e.name() { + bail!(ErrorKind::InvalidElementClosed); + } + to.append_child(elem); + } }, - ReaderEvent::EndDocument => { - bail!(ErrorKind::EndOfDocument); + Event::Text(s) | Event::CData(s) => { + let text = s.unescape_and_decode(reader)?; + if text != "" { + let mut current_elem = stack.last_mut().unwrap(); + current_elem.append_text_node(text); + } + }, + Event::Eof => { + break; }, _ => (), // TODO: may need to implement more } } + Ok(stack.pop().unwrap()) } - /// Output a document to an `EventWriter`. - pub fn write_to(&self, writer: &mut EventWriter) -> Result<()> { - let name = if let Some(ref ns) = self.namespace { - Name::qualified(&self.name, ns, None) - } - else { - Name::local(&self.name) - }; - let mut start = WriterEvent::start_element(name); + /// Output a document to a `Writer`. + pub fn write_to(&self, writer: &mut W) -> Result<()> { + let mut last_namespace = None; + write!(writer, "")?; + self.write_to_inner(writer, &mut last_namespace) + } + + /// Output a document to a `Writer` assuming you're already in the provided namespace + pub fn write_to_in_namespace(&self, writer: &mut W, namespace: &str) -> Result<()> { + write!(writer, "")?; + self.write_to_inner(writer, &mut Some(namespace.to_owned())) + } + + fn write_to_inner(&self, writer: &mut W, last_namespace: &mut Option) -> Result<()> { + write!(writer, "<")?; + write!(writer, "{}", self.name)?; + if let Some(ref ns) = self.namespace { - start = start.default_ns(ns.clone()); + if *last_namespace != self.namespace { + write!(writer, " xmlns=\"{}\"", ns)?; + *last_namespace = Some(ns.clone()); + } + } + + for (key, value) in &self.attributes { + write!(writer, " {}=\"{}\"", key, value)?; } - for attr in &self.attributes { // TODO: I think this could be done a lot more efficiently - start = start.attr(Name::local(attr.0), attr.1); + + if self.children.is_empty() { + write!(writer, " />")?; + return Ok(()) } - writer.write(start)?; + + write!(writer, ">")?; + for child in &self.children { - match *child { - Node::Element(ref e) => { - e.write_to(writer)?; - }, - Node::Text(ref s) => { - writer.write(WriterEvent::characters(s))?; - }, - } + child.write_to_inner(writer, last_namespace)?; } - writer.write(WriterEvent::end_element())?; + + write!(writer, "", self.name)?; Ok(()) } @@ -354,7 +352,7 @@ impl Element { /// # Examples /// /// ```rust - /// use minidom::{Element, Node}; + /// use minidom::Element; /// /// let elem: Element = "abc".parse().unwrap(); /// @@ -592,6 +590,31 @@ impl Element { } } +fn build_element(event: &BytesStart) -> Result { + let mut attributes = event.attributes() + .map(|o| { + let o = o?; + let key = str::from_utf8(o.key)?.to_owned(); + let value = str::from_utf8(o.value)?.to_owned(); + Ok((key, value)) + } + ) + .collect::>>()?; + let mut ns_key = None; + for (key, _) in &attributes { + if key == "xmlns" || key.starts_with("xmlns:") { + ns_key = Some(key.clone()); + } + } + + let ns = match ns_key { + None => None, + Some(key) => attributes.remove(&key), + }; + let name = str::from_utf8(event.name())?.to_owned(); + Ok(Element::new(name, ns, attributes, Vec::new())) +} + /// An iterator over references to child elements of an `Element`. pub struct Children<'a> { iter: slice::Iter<'a, Node>, diff --git a/src/error.rs b/src/error.rs index 4e76c6a9a1be50012fe4f115f05c325e16dcda76..0b8eb23911911c327fb13c14b356685a9373f92e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,30 +1,30 @@ //! Provides an error type for this crate. -use std::io; - use std::convert::From; -use xml::writer::Error as WriterError; -use xml::reader::Error as ReaderError; - error_chain! { foreign_links { - XmlWriterError(WriterError) - /// An error with writing an XML event, from xml::writer::EventWriter. + XmlError(::quick_xml::errors::Error) + /// An error from quick_xml. ; - XmlReaderError(ReaderError) - /// An error with reading an XML event, from xml::reader::EventReader. + Utf8Error(::std::str::Utf8Error) + /// An UTF-8 conversion error. ; - IoError(io::Error) + IoError(::std::io::Error) /// An I/O error, from std::io. ; } errors { - /// En error which is returned when the end of the document was reached prematurely. + /// An error which is returned when the end of the document was reached prematurely. EndOfDocument { description("the end of the document has been reached prematurely") display("the end of the document has been reached prematurely") } + /// An error which is returned when an element is closed when it shouldn't be + InvalidElementClosed { + description("The XML is invalid, an element was wrongly closed") + display("the XML is invalid, an element was wrongly closed") + } } } diff --git a/src/lib.rs b/src/lib.rs index d51de436e8250786a1b24288daf74c08d14899e5..2e00b9fd17be4ddb88e0ec7566d8863d223fe487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ //! minidom = "*" //! ``` -extern crate xml; +extern crate quick_xml; #[macro_use] extern crate error_chain; pub mod error; diff --git a/src/tests.rs b/src/tests.rs index f58bb6ebb1804f095bb928ece8fb459da3dc6029..bc71ca22c41730f403e52c1506c38dec8be14d5a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,9 +1,6 @@ -use std::io::Cursor; - use std::iter::Iterator; -use xml::reader::EventReader; -use xml::writer::EventWriter; +use quick_xml::reader::Reader; use element::Element; @@ -32,19 +29,18 @@ fn build_test_tree() -> Element { #[test] fn reader_works() { - let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + let mut reader = Reader::from_str(TEST_STRING); assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree()); } #[test] fn writer_works() { let root = build_test_tree(); - let mut out = Vec::new(); + let mut writer = Vec::new(); { - let mut writer = EventWriter::new(&mut out); root.write_to(&mut writer).unwrap(); } - assert_eq!(String::from_utf8(out).unwrap(), TEST_STRING); + assert_eq!(String::from_utf8(writer).unwrap(), TEST_STRING); } #[test] @@ -110,8 +106,18 @@ fn two_elements_with_same_arguments_different_order_are_equal() { #[test] fn namespace_attributes_works() { - let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + let mut reader = Reader::from_str(TEST_STRING); let root = Element::from_reader(&mut reader).unwrap(); assert_eq!("en", root.attr("xml:lang").unwrap()); assert_eq!("fr", root.get_child("child", "child_ns").unwrap().attr("xml:lang").unwrap()); } + +#[test] +fn wrongly_closed_elements_error() { + let elem1 = "".parse::(); + assert!(elem1.is_err()); + let elem1 = "".parse::(); + assert!(elem1.is_err()); + let elem1 = "".parse::(); + assert!(elem1.is_ok()); +} From 8b50dadb92a876b7ecb3760dcd12587ed7ced698 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 10 Jun 2017 17:44:28 +0200 Subject: [PATCH 047/111] added changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..5063d8fe24f6667b2020de57e223412c9482cb69 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +Version 0.5.0, released 2017-06-10: + * Big changes + - Eijebong made parsing a lot faster by switching the crate from xml-rs to quick_xml. ( https://gitlab.com/lumi/minidom-rs/merge_requests/11 ) From 444b78c63a481f236728bd996152a7e392174879 Mon Sep 17 00:00:00 2001 From: Johann Tuffe Date: Sat, 8 Jul 2017 12:18:15 +0800 Subject: [PATCH 048/111] unescape_and_decode_value for element attributes --- README.md | 2 +- src/element.rs | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c0426ba8e5a48d5f3e957b9769672152d8bc9587..93582491b6513fc889957fd7c8b2a7d331329e36 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ minidom-rs What's this? ------------ -A minimal DOM library on top of xml-rs. +A minimal DOM library on top of quick-xml. What license is it under? ------------------------- diff --git a/src/element.rs b/src/element.rs index ee2485083823876146d84774dcbde32770a38943..854fcb0b3b35d0e111356664225ffadbcb57d931 100644 --- a/src/element.rs +++ b/src/element.rs @@ -253,7 +253,7 @@ impl Element { let e = reader.read_event(&mut buf)?; match e { Event::Empty(ref e) | Event::Start(ref e) => { - root = build_element(e)?; // FIXME: could be break build_element(e)? when break value is stable + root = build_element(&reader, e)?; // FIXME: could be break build_element(e)? when break value is stable break; }, Event::Eof => { @@ -268,12 +268,12 @@ impl Element { loop { match reader.read_event(&mut buf)? { Event::Empty(ref e) => { - let elem = build_element(e)?; + let elem = build_element(&reader, e)?; // Since there is no Event::End after, directly append it to the current node stack.last_mut().unwrap().append_child(elem); }, Event::Start(ref e) => { - let elem = build_element(e)?; + let elem = build_element(&reader, e)?; stack.push(elem); }, Event::End(ref e) => { @@ -590,29 +590,29 @@ impl Element { } } -fn build_element(event: &BytesStart) -> Result { +fn build_element(reader: &EventReader, event: &BytesStart) -> Result { let mut attributes = event.attributes() .map(|o| { let o = o?; let key = str::from_utf8(o.key)?.to_owned(); - let value = str::from_utf8(o.value)?.to_owned(); + let value = o.unescape_and_decode_value(reader)?; Ok((key, value)) } ) .collect::>>()?; - let mut ns_key = None; - for (key, _) in &attributes { - if key == "xmlns" || key.starts_with("xmlns:") { - ns_key = Some(key.clone()); - } + let mut ns_key = None; + for (key, _) in &attributes { + if key == "xmlns" || key.starts_with("xmlns:") { + ns_key = Some(key.clone()); } + } - let ns = match ns_key { - None => None, - Some(key) => attributes.remove(&key), - }; - let name = str::from_utf8(event.name())?.to_owned(); - Ok(Element::new(name, ns, attributes, Vec::new())) + let ns = match ns_key { + None => None, + Some(key) => attributes.remove(&key), + }; + let name = str::from_utf8(event.name())?.to_owned(); + Ok(Element::new(name, ns, attributes, Vec::new())) } /// An iterator over references to child elements of an `Element`. From 052c46635ac891d1292976853fac5d1fe0ef09c4 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 12 Aug 2017 02:05:18 +0200 Subject: [PATCH 049/111] escape text and attribute values --- src/element.rs | 26 +++++++++++++++++++++++--- src/tests.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/element.rs b/src/element.rs index ee2485083823876146d84774dcbde32770a38943..db41fd55e2d663f4845e63e7f4c7df3f074b0ceb 100644 --- a/src/element.rs +++ b/src/element.rs @@ -18,6 +18,22 @@ use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; +/// Escape XML text +pub fn write_escaped(writer: &mut W, input: &str) -> Result<()> { + for c in input.chars() { + match c { + '&' => write!(writer, "&")?, + '<' => write!(writer, "<")?, + '>' => write!(writer, ">")?, + '\'' => write!(writer, "'")?, + '"' => write!(writer, """)?, + _ => write!(writer, "{}", c)?, + } + } + + Ok(()) +} + /// A node in an element tree. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Node { @@ -71,7 +87,7 @@ impl Node { fn write_to_inner(&self, writer: &mut W, last_namespace: &mut Option) -> Result<()>{ match *self { Node::Element(ref elmt) => elmt.write_to_inner(writer, last_namespace)?, - Node::Text(ref s) => write!(writer, "{}", s)?, + Node::Text(ref s) => write_escaped(writer, s)?, } Ok(()) @@ -323,13 +339,17 @@ impl Element { if let Some(ref ns) = self.namespace { if *last_namespace != self.namespace { - write!(writer, " xmlns=\"{}\"", ns)?; + write!(writer, " xmlns=\"")?; + write_escaped(writer, ns)?; + write!(writer, "\"")?; *last_namespace = Some(ns.clone()); } } for (key, value) in &self.attributes { - write!(writer, " {}=\"{}\"", key, value)?; + write!(writer, " {}=\"", key)?; + write_escaped(writer, value)?; + write!(writer, "\"")?; } if self.children.is_empty() { diff --git a/src/tests.rs b/src/tests.rs index bc71ca22c41730f403e52c1506c38dec8be14d5a..6dd6a2ba0026df27291533fe009994798df3ecf2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -43,6 +43,34 @@ fn writer_works() { assert_eq!(String::from_utf8(writer).unwrap(), TEST_STRING); } +#[test] +fn writer_escapes_attributes() { + let root = Element::builder("root") + .attr("a", "\"Air\" quotes") + .build(); + let mut writer = Vec::new(); + { + root.write_to(&mut writer).unwrap(); + } + assert_eq!(String::from_utf8(writer).unwrap(), + r#""# + ); +} + +#[test] +fn writer_escapes_text() { + let root = Element::builder("root") + .append("<3") + .build(); + let mut writer = Vec::new(); + { + root.write_to(&mut writer).unwrap(); + } + assert_eq!(String::from_utf8(writer).unwrap(), + r#"<3"# + ); +} + #[test] fn builder_works() { let elem = Element::builder("a") From 77bc215cba9a1058f5fd9b1ba4c2bb0f68cc325d Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 19 Jul 2017 01:14:33 +0200 Subject: [PATCH 050/111] tests: add namespace inheritance tests --- src/tests.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 6dd6a2ba0026df27291533fe009994798df3ecf2..40558eca1d9581d0f270a8ca857d72e325ef3792 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -149,3 +149,50 @@ fn wrongly_closed_elements_error() { let elem1 = "".parse::(); assert!(elem1.is_ok()); } + +#[test] +fn namespace_simple() { + let elem: Element = "".parse().unwrap(); + assert_eq!(elem.name(), "message"); + assert_eq!(elem.ns(), Some("jabber:client")); +} + +#[test] +fn namespace_prefixed() { + let elem: Element = "" + .parse().unwrap(); + assert_eq!(elem.name(), "features"); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); +} + +#[test] +fn namespace_inherited_simple() { + let elem: Element = "".parse().unwrap(); + assert_eq!(elem.name(), "stream"); + assert_eq!(elem.ns(), Some("jabber:client")); + let child = elem.children().next().unwrap(); + assert_eq!(child.name(), "message"); + assert_eq!(child.ns(), Some("jabber:client")); +} + +#[test] +fn namespace_inherited_prefixed1() { + let elem: Element = "" + .parse().unwrap(); + assert_eq!(elem.name(), "features"); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); + let child = elem.children().next().unwrap(); + assert_eq!(child.name(), "message"); + assert_eq!(child.ns(), Some("jabber:client")); +} + +#[test] +fn namespace_inherited_prefixed2() { + let elem: Element = "" + .parse().unwrap(); + assert_eq!(elem.name(), "stream"); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); + let child = elem.children().next().unwrap(); + assert_eq!(child.name(), "message"); + assert_eq!(child.ns(), Some("jabber:client")); +} From a01cf553aed8da17cd0b403dff5b1a289841fa68 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 28 Jul 2017 23:58:03 +0200 Subject: [PATCH 051/111] switch Element namespaces to NamespaceSet with parent reference --- src/element.rs | 269 +++++++++++++++++++++++++++++++++++-------------- src/error.rs | 5 + src/tests.rs | 21 ++-- 3 files changed, 210 insertions(+), 85 deletions(-) diff --git a/src/element.rs b/src/element.rs index db41fd55e2d663f4845e63e7f4c7df3f074b0ceb..5aecfa86cfe52be4d73904ee200b9f2bc6c8b2a7 100644 --- a/src/element.rs +++ b/src/element.rs @@ -4,6 +4,8 @@ use std::io:: Write; use std::collections::{btree_map, BTreeMap}; use std::str; +use std::cell::RefCell; +use std::rc::Rc; use error::{Error, ErrorKind, Result}; @@ -84,9 +86,9 @@ impl Node { } } - fn write_to_inner(&self, writer: &mut W, last_namespace: &mut Option) -> Result<()>{ + fn write_to_inner(&self, writer: &mut W) -> Result<()>{ match *self { - Node::Element(ref elmt) => elmt.write_to_inner(writer, last_namespace)?, + Node::Element(ref elmt) => elmt.write_to_inner(writer)?, Node::Text(ref s) => write_escaped(writer, s)?, } @@ -94,11 +96,109 @@ impl Node { } } +#[derive(Clone, Debug, PartialEq, Eq)] +struct NamespaceSet { + parent: RefCell>>, + namespaces: BTreeMap, String>, +} + +impl Default for NamespaceSet { + fn default() -> Self { + NamespaceSet { + parent: RefCell::new(None), + namespaces: BTreeMap::new(), + } + } +} + +impl NamespaceSet { + fn get(&self, prefix: &Option) -> Option { + match self.namespaces.get(prefix) { + Some(ns) => Some(ns.clone()), + None => match *self.parent.borrow() { + None => None, + Some(ref parent) => parent.get(prefix) + }, + } + } + + fn has>(&self, prefix: &Option, wanted_ns: NS) -> bool { + match self.namespaces.get(prefix) { + Some(ns) => + ns == wanted_ns.as_ref(), + None => match *self.parent.borrow() { + None => + false, + Some(ref parent) => + parent.has(prefix, wanted_ns), + }, + } + } + + fn set_parent(&self, parent: &Element) { + let mut parent_ns = self.parent.borrow_mut(); + let new_set = ((&parent.namespaces) as &Rc).clone(); + *parent_ns = Some(new_set); + } + +} + +impl From, String>> for NamespaceSet { + fn from(namespaces: BTreeMap, String>) -> Self { + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From> for NamespaceSet { + fn from(namespace: Option) -> Self { + match namespace { + None => Self::default(), + Some(namespace) => Self::from(namespace), + } + } +} + +impl From for NamespaceSet { + fn from(namespace: String) -> Self { + let mut namespaces = BTreeMap::new(); + namespaces.insert(None, namespace); + + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From<(Option, String)> for NamespaceSet { + fn from(prefix_namespace: (Option, String)) -> Self { + let (prefix, namespace) = prefix_namespace; + let mut namespaces = BTreeMap::new(); + namespaces.insert(prefix, namespace); + + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From<(String, String)> for NamespaceSet { + fn from(prefix_namespace: (String, String)) -> Self { + let (prefix, namespace) = prefix_namespace; + Self::from((Some(prefix), namespace)) + } +} + #[derive(Clone, PartialEq, Eq, Debug)] /// A struct representing a DOM Element. pub struct Element { + prefix: Option, name: String, - namespace: Option, + namespaces: Rc, attributes: BTreeMap, children: Vec, } @@ -122,10 +222,10 @@ impl FromStr for Element { } impl Element { - fn new(name: String, namespace: Option, attributes: BTreeMap, children: Vec) -> Element { + fn new>(name: String, prefix: Option, namespaces: NS, attributes: BTreeMap, children: Vec) -> Element { Element { - name: name, - namespace: namespace, + prefix, name, + namespaces: Rc::new(namespaces.into()), attributes: attributes, children: children, } @@ -145,14 +245,16 @@ impl Element { /// .build(); /// /// assert_eq!(elem.name(), "name"); - /// assert_eq!(elem.ns(), Some("namespace")); + /// assert_eq!(elem.ns(), Some("namespace".to_owned())); /// assert_eq!(elem.attr("name"), Some("value")); /// assert_eq!(elem.attr("inexistent"), None); /// assert_eq!(elem.text(), "inner"); /// ``` - pub fn builder>(name: S) -> ElementBuilder { + pub fn builder>(name: S) -> ElementBuilder { + let (prefix, name) = split_element_name(name).unwrap(); ElementBuilder { - root: Element::new(name.into(), None, BTreeMap::new(), Vec::new()), + root: Element::new(name, prefix, None, BTreeMap::new(), Vec::new()), + namespaces: Default::default(), } } @@ -172,8 +274,9 @@ impl Element { /// ``` pub fn bare>(name: S) -> Element { Element { + prefix: None, name: name.into(), - namespace: None, + namespaces: Rc::new(NamespaceSet::default()), attributes: BTreeMap::new(), children: Vec::new(), } @@ -185,9 +288,8 @@ impl Element { } /// Returns a reference to the namespace of this element, if it has one, else `None`. - pub fn ns(&self) -> Option<&str> { - self.namespace.as_ref() - .map(String::as_ref) + pub fn ns(&self) -> Option { + self.namespaces.get(&self.prefix) } /// Returns a reference to the value of the given attribute, if it exists, else `None`. @@ -256,8 +358,8 @@ impl Element { /// assert_eq!(elem.is("wrong", "wrong"), false); /// ``` pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { - let ns = self.namespace.as_ref().map(String::as_ref); - self.name == name.as_ref() && ns == Some(namespace.as_ref()) + self.name == name.as_ref() && + self.namespaces.has(&self.prefix, namespace) } /// Parse a document from an `EventReader`. @@ -322,27 +424,30 @@ impl Element { /// Output a document to a `Writer`. pub fn write_to(&self, writer: &mut W) -> Result<()> { - let mut last_namespace = None; - write!(writer, "")?; - self.write_to_inner(writer, &mut last_namespace) - } - - /// Output a document to a `Writer` assuming you're already in the provided namespace - pub fn write_to_in_namespace(&self, writer: &mut W, namespace: &str) -> Result<()> { write!(writer, "")?; - self.write_to_inner(writer, &mut Some(namespace.to_owned())) + self.write_to_inner(writer) } - fn write_to_inner(&self, writer: &mut W, last_namespace: &mut Option) -> Result<()> { - write!(writer, "<")?; - write!(writer, "{}", self.name)?; - - if let Some(ref ns) = self.namespace { - if *last_namespace != self.namespace { - write!(writer, " xmlns=\"")?; - write_escaped(writer, ns)?; - write!(writer, "\"")?; - *last_namespace = Some(ns.clone()); + /// Like `write_to()` but without the `` prelude + pub fn write_to_inner(&self, writer: &mut W) -> Result<()> { + let name = match &self.prefix { + &None => self.name.to_owned(), + &Some(ref prefix) => format!("{}:{}", prefix, self.name), + }; + write!(writer, "<{}", name)?; + + for (prefix, ns) in &self.namespaces.namespaces { + match prefix { + &None => { + write!(writer, " xmlns=\"")?; + write_escaped(writer, ns)?; + write!(writer, "\"")?; + }, + &Some(ref prefix) => { + write!(writer, " xmlns:{}=\"", prefix)?; + write_escaped(writer, ns)?; + write!(writer, "\"")?; + }, } } @@ -360,10 +465,10 @@ impl Element { write!(writer, ">")?; for child in &self.children { - child.write_to_inner(writer, last_namespace)?; + child.write_to_inner(writer)?; } - write!(writer, "", self.name)?; + write!(writer, "", name)?; Ok(()) } @@ -472,30 +577,17 @@ impl Element { /// /// assert_eq!(child.name(), "new"); /// ``` - pub fn append_child(&mut self, mut child: Element) -> &mut Element { - if child.namespace.is_none() && self.namespace.is_some() { - child.namespace = self.namespace.clone(); - child.propagate_namespaces(); - } + pub fn append_child(&mut self, child: Element) -> &mut Element { + child.namespaces.set_parent(&self); + self.children.push(Node::Element(child)); if let Node::Element(ref mut cld) = *self.children.last_mut().unwrap() { cld - } - else { + } else { unreachable!() } } - fn propagate_namespaces(&mut self) { - let ns = self.namespace.clone(); - for child in self.children_mut() { - if child.namespace.is_none() { - child.namespace = ns.clone(); - child.propagate_namespaces(); - } - } - } - /// Appends a text node to an `Element`. /// /// # Examples @@ -610,29 +702,42 @@ impl Element { } } +fn split_element_name>(s: S) -> Result<(Option, String)> { + let name_parts = s.as_ref().split(':').collect::>(); + match name_parts.len() { + 2 => Ok((Some(name_parts[0].to_owned()), name_parts[1].to_owned())), + 1 => Ok((None, name_parts[0].to_owned())), + _ => bail!(ErrorKind::InvalidElement), + } +} + fn build_element(event: &BytesStart) -> Result { - let mut attributes = event.attributes() - .map(|o| { - let o = o?; - let key = str::from_utf8(o.key)?.to_owned(); - let value = str::from_utf8(o.value)?.to_owned(); - Ok((key, value)) - } - ) - .collect::>>()?; - let mut ns_key = None; - for (key, _) in &attributes { - if key == "xmlns" || key.starts_with("xmlns:") { - ns_key = Some(key.clone()); + let mut namespaces = BTreeMap::new(); + let attributes = event.attributes() + .map(|o| { + let o = o?; + let key = str::from_utf8(o.key)?.to_owned(); + let value = str::from_utf8(o.value)?.to_owned(); + Ok((key, value)) + }) + .filter(|o| { + match o { + &Ok((ref key, ref value)) if key == "xmlns" => { + namespaces.insert(None, value.to_owned()); + false + }, + &Ok((ref key, ref value)) if key.starts_with("xmlns:") => { + namespaces.insert(Some(key[6..].to_owned()), value.to_owned()); + false + }, + _ => true, } - } + }) + .collect::>>()?; - let ns = match ns_key { - None => None, - Some(key) => attributes.remove(&key), - }; - let name = str::from_utf8(event.name())?.to_owned(); - Ok(Element::new(name, ns, attributes, Vec::new())) + let (prefix, name) = split_element_name(str::from_utf8(event.name())?)?; + let element = Element::new(name, prefix, namespaces, attributes, Vec::new()); + Ok(element) } /// An iterator over references to child elements of an `Element`. @@ -742,12 +847,14 @@ impl<'a> Iterator for AttrsMut<'a> { /// A builder for `Element`s. pub struct ElementBuilder { root: Element, + namespaces: BTreeMap, String>, } impl ElementBuilder { /// Sets the namespace. pub fn ns>(mut self, namespace: S) -> ElementBuilder { - self.root.namespace = Some(namespace.into()); + self.namespaces + .insert(self.root.prefix.clone(), namespace.into()); self } @@ -768,21 +875,33 @@ impl ElementBuilder { /// Builds the `Element`. pub fn build(self) -> Element { - self.root + let mut element = self.root; + // Set namespaces + element.namespaces = Rc::new(NamespaceSet::from(self.namespaces)); + // Propagate namespaces + for node in &element.children { + if let Node::Element(ref e) = *node { + e.namespaces.set_parent(&element); + } + } + + element } } +#[cfg(test)] #[test] fn test_element_new() { use std::iter::FromIterator; let elem = Element::new( "name".to_owned() + , None , Some("namespace".to_owned()) , BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() ) , Vec::new() ); assert_eq!(elem.name(), "name"); - assert_eq!(elem.ns(), Some("namespace")); + assert_eq!(elem.ns(), Some("namespace".to_owned())); assert_eq!(elem.attr("name"), Some("value")); assert_eq!(elem.attr("inexistent"), None); } diff --git a/src/error.rs b/src/error.rs index 0b8eb23911911c327fb13c14b356685a9373f92e..4079365fa4d028181d140a6d3954421ca2764764 100644 --- a/src/error.rs +++ b/src/error.rs @@ -26,5 +26,10 @@ error_chain! { description("The XML is invalid, an element was wrongly closed") display("the XML is invalid, an element was wrongly closed") } + /// An error which is returned when an elemet's name contains more than one colon + InvalidElement { + description("The XML element is invalid") + display("the XML element is invalid") + } } } diff --git a/src/tests.rs b/src/tests.rs index 40558eca1d9581d0f270a8ca857d72e325ef3792..e787715830823ef66285348a0f67ae63ad6a0ad8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -80,7 +80,7 @@ fn builder_works() { .append("e") .build(); assert_eq!(elem.name(), "a"); - assert_eq!(elem.ns(), Some("b")); + assert_eq!(elem.ns(), Some("b".to_owned())); assert_eq!(elem.attr("c"), Some("d")); assert_eq!(elem.attr("x"), None); assert_eq!(elem.text(), "e"); @@ -115,6 +115,7 @@ fn namespace_propagation_works() { let grandchild = Element::bare("grandchild"); child.append_child(grandchild); root.append_child(child); + assert_eq!(root.get_child("child", "root_ns").unwrap().ns(), root.ns()); assert_eq!(root.get_child("child", "root_ns").unwrap() .get_child("grandchild", "root_ns").unwrap() @@ -154,7 +155,7 @@ fn wrongly_closed_elements_error() { fn namespace_simple() { let elem: Element = "".parse().unwrap(); assert_eq!(elem.name(), "message"); - assert_eq!(elem.ns(), Some("jabber:client")); + assert_eq!(elem.ns(), Some("jabber:client".to_owned())); } #[test] @@ -162,28 +163,28 @@ fn namespace_prefixed() { let elem: Element = "" .parse().unwrap(); assert_eq!(elem.name(), "features"); - assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned())); } #[test] fn namespace_inherited_simple() { let elem: Element = "".parse().unwrap(); assert_eq!(elem.name(), "stream"); - assert_eq!(elem.ns(), Some("jabber:client")); + assert_eq!(elem.ns(), Some("jabber:client".to_owned())); let child = elem.children().next().unwrap(); assert_eq!(child.name(), "message"); - assert_eq!(child.ns(), Some("jabber:client")); + assert_eq!(child.ns(), Some("jabber:client".to_owned())); } #[test] fn namespace_inherited_prefixed1() { - let elem: Element = "" + let elem: Element = "" .parse().unwrap(); assert_eq!(elem.name(), "features"); - assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned())); let child = elem.children().next().unwrap(); assert_eq!(child.name(), "message"); - assert_eq!(child.ns(), Some("jabber:client")); + assert_eq!(child.ns(), Some("jabber:client".to_owned())); } #[test] @@ -191,8 +192,8 @@ fn namespace_inherited_prefixed2() { let elem: Element = "" .parse().unwrap(); assert_eq!(elem.name(), "stream"); - assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams")); + assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned())); let child = elem.children().next().unwrap(); assert_eq!(child.name(), "message"); - assert_eq!(child.ns(), Some("jabber:client")); + assert_eq!(child.ns(), Some("jabber:client".to_owned())); } From 26f03c27edbe798f0a77f34d4c352f4e4fd79afe Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 12 Aug 2017 02:13:32 +0200 Subject: [PATCH 052/111] optimize Element::write_to_inner() with CoW --- src/element.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/element.rs b/src/element.rs index 5aecfa86cfe52be4d73904ee200b9f2bc6c8b2a7..2c16b3c2ce9fc15bab75c8bd3a329c838ec84a8b 100644 --- a/src/element.rs +++ b/src/element.rs @@ -6,6 +6,7 @@ use std::collections::{btree_map, BTreeMap}; use std::str; use std::cell::RefCell; use std::rc::Rc; +use std::borrow::Cow; use error::{Error, ErrorKind, Result}; @@ -431,8 +432,8 @@ impl Element { /// Like `write_to()` but without the `` prelude pub fn write_to_inner(&self, writer: &mut W) -> Result<()> { let name = match &self.prefix { - &None => self.name.to_owned(), - &Some(ref prefix) => format!("{}:{}", prefix, self.name), + &None => Cow::Borrowed(&self.name), + &Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)), }; write!(writer, "<{}", name)?; From 98fd71fd3f491a49b042ab63d0a3cc30ffbea34d Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 02:26:45 +0200 Subject: [PATCH 053/111] NamespaceSet: rm unneeded cast --- src/element.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index 2c16b3c2ce9fc15bab75c8bd3a329c838ec84a8b..862313147239cbb157ca26e179f5deb1594750db 100644 --- a/src/element.rs +++ b/src/element.rs @@ -138,7 +138,7 @@ impl NamespaceSet { fn set_parent(&self, parent: &Element) { let mut parent_ns = self.parent.borrow_mut(); - let new_set = ((&parent.namespaces) as &Rc).clone(); + let new_set = parent.namespaces.clone(); *parent_ns = Some(new_set); } From 7d2699e08e36d28997f573ca3e76ee09cabe9669 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 02:28:44 +0200 Subject: [PATCH 054/111] NamespaceSet::set_parent(): don't require a full Element by passing just a reference to a NamespaceSet we could reuse this for xmlns elision in the serialization code. --- src/element.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index 862313147239cbb157ca26e179f5deb1594750db..1a9aeb84869421b6fb4080816d9080e9e3e07456 100644 --- a/src/element.rs +++ b/src/element.rs @@ -136,9 +136,9 @@ impl NamespaceSet { } } - fn set_parent(&self, parent: &Element) { + fn set_parent(&self, parent: Rc) { let mut parent_ns = self.parent.borrow_mut(); - let new_set = parent.namespaces.clone(); + let new_set = parent; *parent_ns = Some(new_set); } @@ -579,7 +579,7 @@ impl Element { /// assert_eq!(child.name(), "new"); /// ``` pub fn append_child(&mut self, child: Element) -> &mut Element { - child.namespaces.set_parent(&self); + child.namespaces.set_parent(self.namespaces.clone()); self.children.push(Node::Element(child)); if let Node::Element(ref mut cld) = *self.children.last_mut().unwrap() { @@ -882,7 +882,7 @@ impl ElementBuilder { // Propagate namespaces for node in &element.children { if let Node::Element(ref e) = *node { - e.namespaces.set_parent(&element); + e.namespaces.set_parent(element.namespaces.clone()); } } From 0148790a0137fb480a4adbe35f3594a95354fd68 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 02:35:24 +0200 Subject: [PATCH 055/111] move NamespaceSet to namespaces mod --- src/element.rs | 101 +------------------------------------------- src/lib.rs | 1 + src/namespaces.rs | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 99 deletions(-) create mode 100644 src/namespaces.rs diff --git a/src/element.rs b/src/element.rs index 1a9aeb84869421b6fb4080816d9080e9e3e07456..47e25d73cadce76f7881836227ac66d0a9be61ba 100644 --- a/src/element.rs +++ b/src/element.rs @@ -4,7 +4,6 @@ use std::io:: Write; use std::collections::{btree_map, BTreeMap}; use std::str; -use std::cell::RefCell; use std::rc::Rc; use std::borrow::Cow; @@ -20,6 +19,7 @@ use std::str::FromStr; use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; +use namespaces::NamespaceSet; /// Escape XML text pub fn write_escaped(writer: &mut W, input: &str) -> Result<()> { @@ -97,103 +97,6 @@ impl Node { } } -#[derive(Clone, Debug, PartialEq, Eq)] -struct NamespaceSet { - parent: RefCell>>, - namespaces: BTreeMap, String>, -} - -impl Default for NamespaceSet { - fn default() -> Self { - NamespaceSet { - parent: RefCell::new(None), - namespaces: BTreeMap::new(), - } - } -} - -impl NamespaceSet { - fn get(&self, prefix: &Option) -> Option { - match self.namespaces.get(prefix) { - Some(ns) => Some(ns.clone()), - None => match *self.parent.borrow() { - None => None, - Some(ref parent) => parent.get(prefix) - }, - } - } - - fn has>(&self, prefix: &Option, wanted_ns: NS) -> bool { - match self.namespaces.get(prefix) { - Some(ns) => - ns == wanted_ns.as_ref(), - None => match *self.parent.borrow() { - None => - false, - Some(ref parent) => - parent.has(prefix, wanted_ns), - }, - } - } - - fn set_parent(&self, parent: Rc) { - let mut parent_ns = self.parent.borrow_mut(); - let new_set = parent; - *parent_ns = Some(new_set); - } - -} - -impl From, String>> for NamespaceSet { - fn from(namespaces: BTreeMap, String>) -> Self { - NamespaceSet { - parent: RefCell::new(None), - namespaces: namespaces, - } - } -} - -impl From> for NamespaceSet { - fn from(namespace: Option) -> Self { - match namespace { - None => Self::default(), - Some(namespace) => Self::from(namespace), - } - } -} - -impl From for NamespaceSet { - fn from(namespace: String) -> Self { - let mut namespaces = BTreeMap::new(); - namespaces.insert(None, namespace); - - NamespaceSet { - parent: RefCell::new(None), - namespaces: namespaces, - } - } -} - -impl From<(Option, String)> for NamespaceSet { - fn from(prefix_namespace: (Option, String)) -> Self { - let (prefix, namespace) = prefix_namespace; - let mut namespaces = BTreeMap::new(); - namespaces.insert(prefix, namespace); - - NamespaceSet { - parent: RefCell::new(None), - namespaces: namespaces, - } - } -} - -impl From<(String, String)> for NamespaceSet { - fn from(prefix_namespace: (String, String)) -> Self { - let (prefix, namespace) = prefix_namespace; - Self::from((Some(prefix), namespace)) - } -} - #[derive(Clone, PartialEq, Eq, Debug)] /// A struct representing a DOM Element. pub struct Element { @@ -437,7 +340,7 @@ impl Element { }; write!(writer, "<{}", name)?; - for (prefix, ns) in &self.namespaces.namespaces { + for (prefix, ns) in self.namespaces.declared_ns() { match prefix { &None => { write!(writer, " xmlns=\"")?; diff --git a/src/lib.rs b/src/lib.rs index 2e00b9fd17be4ddb88e0ec7566d8863d223fe487..eabd0e7c6e9a6595d77e9f7e75fa406e3c37d67b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,6 +70,7 @@ extern crate quick_xml; pub mod error; pub mod element; pub mod convert; +mod namespaces; #[cfg(test)] mod tests; diff --git a/src/namespaces.rs b/src/namespaces.rs new file mode 100644 index 0000000000000000000000000000000000000000..590d9cbb57b41024edfecc46b9734c7a64cb105c --- /dev/null +++ b/src/namespaces.rs @@ -0,0 +1,105 @@ +use std::collections::BTreeMap; +use std::cell::RefCell; +use std::rc::Rc; + + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NamespaceSet { + parent: RefCell>>, + namespaces: BTreeMap, String>, +} + +impl Default for NamespaceSet { + fn default() -> Self { + NamespaceSet { + parent: RefCell::new(None), + namespaces: BTreeMap::new(), + } + } +} + +impl NamespaceSet { + pub fn declared_ns(&self) -> &BTreeMap, String> { + &self.namespaces + } + + pub fn get(&self, prefix: &Option) -> Option { + match self.namespaces.get(prefix) { + Some(ns) => Some(ns.clone()), + None => match *self.parent.borrow() { + None => None, + Some(ref parent) => parent.get(prefix) + }, + } + } + + pub fn has>(&self, prefix: &Option, wanted_ns: NS) -> bool { + match self.namespaces.get(prefix) { + Some(ns) => + ns == wanted_ns.as_ref(), + None => match *self.parent.borrow() { + None => + false, + Some(ref parent) => + parent.has(prefix, wanted_ns), + }, + } + } + + pub fn set_parent(&self, parent: Rc) { + let mut parent_ns = self.parent.borrow_mut(); + let new_set = parent; + *parent_ns = Some(new_set); + } + +} + +impl From, String>> for NamespaceSet { + fn from(namespaces: BTreeMap, String>) -> Self { + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From> for NamespaceSet { + fn from(namespace: Option) -> Self { + match namespace { + None => Self::default(), + Some(namespace) => Self::from(namespace), + } + } +} + +impl From for NamespaceSet { + fn from(namespace: String) -> Self { + let mut namespaces = BTreeMap::new(); + namespaces.insert(None, namespace); + + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From<(Option, String)> for NamespaceSet { + fn from(prefix_namespace: (Option, String)) -> Self { + let (prefix, namespace) = prefix_namespace; + let mut namespaces = BTreeMap::new(); + namespaces.insert(prefix, namespace); + + NamespaceSet { + parent: RefCell::new(None), + namespaces: namespaces, + } + } +} + +impl From<(String, String)> for NamespaceSet { + fn from(prefix_namespace: (String, String)) -> Self { + let (prefix, namespace) = prefix_namespace; + Self::from((Some(prefix), namespace)) + } +} From 7297dbc5fd37ac9c50e4626f82f984a79758710e Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 02:48:28 +0200 Subject: [PATCH 056/111] namespaces: add some tests --- src/namespaces.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/namespaces.rs b/src/namespaces.rs index 590d9cbb57b41024edfecc46b9734c7a64cb105c..767106280929bbccea5d4dbd4ce7c8b1e966b142 100644 --- a/src/namespaces.rs +++ b/src/namespaces.rs @@ -103,3 +103,48 @@ impl From<(String, String)> for NamespaceSet { Self::from((Some(prefix), namespace)) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::rc::Rc; + + #[test] + fn get_has() { + let namespaces = NamespaceSet::from("foo".to_owned()); + assert_eq!(namespaces.get(&None), Some("foo".to_owned())); + assert!(namespaces.has(&None, "foo")); + } + + #[test] + fn get_has_prefixed() { + let namespaces = NamespaceSet::from(("x".to_owned(), "bar".to_owned())); + assert_eq!(namespaces.get(&Some("x".to_owned())), Some("bar".to_owned())); + assert!(namespaces.has(&Some("x".to_owned()), "bar")); + } + + #[test] + fn get_has_recursive() { + let mut parent = NamespaceSet::from("foo".to_owned()); + for _ in 0..1000 { + let namespaces = NamespaceSet::default(); + namespaces.set_parent(Rc::new(parent)); + assert_eq!(namespaces.get(&None), Some("foo".to_owned())); + assert!(namespaces.has(&None, "foo")); + parent = namespaces; + } + } + + #[test] + fn get_has_prefixed_recursive() { + let mut parent = NamespaceSet::from(("x".to_owned(), "bar".to_owned())); + for _ in 0..1000 { + let namespaces = NamespaceSet::default(); + namespaces.set_parent(Rc::new(parent)); + assert_eq!(namespaces.get(&Some("x".to_owned())), Some("bar".to_owned())); + assert!(namespaces.has(&Some("x".to_owned()), "bar")); + parent = namespaces; + } + } + +} From b14155908115014e91a8e7f4968b518740484ea3 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 17:29:38 +0200 Subject: [PATCH 057/111] rename namespaces mod to namespace_set --- src/element.rs | 2 +- src/lib.rs | 2 +- src/{namespaces.rs => namespace_set.rs} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{namespaces.rs => namespace_set.rs} (100%) diff --git a/src/element.rs b/src/element.rs index 47e25d73cadce76f7881836227ac66d0a9be61ba..e0aebf863abad30b8d52cc7efbf043921574e4be 100644 --- a/src/element.rs +++ b/src/element.rs @@ -19,7 +19,7 @@ use std::str::FromStr; use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; -use namespaces::NamespaceSet; +use namespace_set::NamespaceSet; /// Escape XML text pub fn write_escaped(writer: &mut W, input: &str) -> Result<()> { diff --git a/src/lib.rs b/src/lib.rs index eabd0e7c6e9a6595d77e9f7e75fa406e3c37d67b..b11fe0603ddf40c566a5af23675f0a5adab88b50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ extern crate quick_xml; pub mod error; pub mod element; pub mod convert; -mod namespaces; +mod namespace_set; #[cfg(test)] mod tests; diff --git a/src/namespaces.rs b/src/namespace_set.rs similarity index 100% rename from src/namespaces.rs rename to src/namespace_set.rs From 2398e4f6d1938bad3fe3dabe8aef20bc81c87aea Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 13 Aug 2017 17:29:48 +0200 Subject: [PATCH 058/111] Cargo.toml: add Astro to authors --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ebb09d1893cad3bff8654b0da2c514d214fde03e..bb7a765648ae80fffe492f8f2ec23a7b92c28af8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "minidom" version = "0.5.0" -authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] +authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" From 4546db95fcdd2c05453d5defcc05104fe3777516 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 13 Aug 2017 20:14:01 +0200 Subject: [PATCH 059/111] bump version and update CHANGELOG.md --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5063d8fe24f6667b2020de57e223412c9482cb69..5d4e682056809ad69ad5d882c061a1387c4c06ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Version 0.6.0, released 2017-08-13: + * Big changes + - Astro added proper support for namespace prefixes. ( https://gitlab.com/lumi/minidom-rs/merge_requests/14 ) + * Fixes + - Astro fixed a regression that caused the writer not to escape its xml output properly. ( https://gitlab.com/lumi/minidom-rs/merge_requests/15 ) Version 0.5.0, released 2017-06-10: * Big changes - Eijebong made parsing a lot faster by switching the crate from xml-rs to quick_xml. ( https://gitlab.com/lumi/minidom-rs/merge_requests/11 ) diff --git a/Cargo.toml b/Cargo.toml index bb7a765648ae80fffe492f8f2ec23a7b92c28af8..ed74ebbc7910461ae3b6aaa0f2fae5037fddf2c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.5.0" +version = "0.6.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" From 8af3e50311c808fb65c0dcd051ab8a3a1678951a Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 19 Aug 2017 01:17:45 +0200 Subject: [PATCH 060/111] add Element::has_ns(&self, NS) --- src/element.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index e0aebf863abad30b8d52cc7efbf043921574e4be..1cbdd2722a161a93dc1354c55ad832e8606b3acc 100644 --- a/src/element.rs +++ b/src/element.rs @@ -263,7 +263,23 @@ impl Element { /// ``` pub fn is, NS: AsRef>(&self, name: N, namespace: NS) -> bool { self.name == name.as_ref() && - self.namespaces.has(&self.prefix, namespace) + self.has_ns(namespace) + } + + /// Returns whether the element has the given namespace. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Element; + /// + /// let elem = Element::builder("name").ns("namespace").build(); + /// + /// assert_eq!(elem.has_ns("namespace"), true); + /// assert_eq!(elem.has_ns("wrong"), false); + /// ``` + pub fn has_ns>(&self, namespace: NS) -> bool { + self.namespaces.has(&self.prefix, namespace) } /// Parse a document from an `EventReader`. From 89ab8d9aca4eb79f567925f5fccd6a8967dd51e1 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 19 Aug 2017 00:34:44 +0100 Subject: [PATCH 061/111] fix a FIXME, break value is now stable --- src/element.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index e0aebf863abad30b8d52cc7efbf043921574e4be..75faec6af8cb97433abf3f0625634cf4890fc045 100644 --- a/src/element.rs +++ b/src/element.rs @@ -269,14 +269,12 @@ impl Element { /// Parse a document from an `EventReader`. pub fn from_reader(reader: &mut EventReader) -> Result { let mut buf = Vec::new(); - let root: Element; - loop { + let root: Element = loop { let e = reader.read_event(&mut buf)?; match e { Event::Empty(ref e) | Event::Start(ref e) => { - root = build_element(e)?; // FIXME: could be break build_element(e)? when break value is stable - break; + break build_element(e)?; }, Event::Eof => { bail!(ErrorKind::EndOfDocument); From fb4e802999c2f4b84fb71dca35e83a579627b1b0 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 19 Aug 2017 20:26:41 +0100 Subject: [PATCH 062/111] update quick-xml to its latest version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ed74ebbc7910461ae3b6aaa0f2fae5037fddf2c8..6aa5d6cbf6b878993b811cc3475f24e0267c6378 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,5 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.7.3" +quick-xml = "0.9.0" error-chain = "0.10.0" From 501909c32ded6121f4037b47a04222b9e96842d5 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 20 Aug 2017 17:26:45 +0200 Subject: [PATCH 063/111] bump version to 0.6.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6aa5d6cbf6b878993b811cc3475f24e0267c6378..a89aea2cf53fd811d7f22a53b2064cc4312432c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.6.0" +version = "0.6.1" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" From 5d0e4721f65ce7862dbaf63bedd3f1ba9d06ccaa Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 20 Aug 2017 17:35:20 +0200 Subject: [PATCH 064/111] update change log --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d4e682056809ad69ad5d882c061a1387c4c06ad..b749476eacc492075761dc033c1ab3fda355df60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 0.6.1, released 2017-08-20: + * Additions + - Astro added Element::has_ns, which checks whether an element's namespace matches the passed argument. ( https://gitlab.com/lumi/minidom-rs/merge_requests/16 ) + - Link Mauve updated the quick-xml dependency to the latest version. + * Fixes + - Because break value is now stable, Link Mauve rewrote some code marked FIXME to use it. Version 0.6.0, released 2017-08-13: * Big changes - Astro added proper support for namespace prefixes. ( https://gitlab.com/lumi/minidom-rs/merge_requests/14 ) From b49ea5175d73d6d2e1863d326d3469e83aeaf682 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 21 Jul 2017 00:02:11 +0100 Subject: [PATCH 065/111] implement IntoElements on Into instead of Element --- src/convert.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 126688b7a5f997a14b996677fe750b2878368649..1b35414c12b0495e693b4d71d0238afd6f3345de 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -51,9 +51,9 @@ impl IntoElements for Option { } } -impl IntoElements for Element { +impl IntoElements for T where T: Into { fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_child(self); + emitter.append_child(self.into()); } } From 6b4061ad8612ffb3ace9b6a8a6230ba5df16aa87 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 27 Aug 2017 01:44:22 +0100 Subject: [PATCH 066/111] update change log --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b749476eacc492075761dc033c1ab3fda355df60..369ef8ad62d81000184f2cf47b2e713ac4d80326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version 0.6.2, released 2017-08-27: + * Additions + - Link Mauve added an implementation of IntoElements for all Into ( https://gitlab.com/lumi/minidom-rs/merge_requests/19 ) Version 0.6.1, released 2017-08-20: * Additions - Astro added Element::has_ns, which checks whether an element's namespace matches the passed argument. ( https://gitlab.com/lumi/minidom-rs/merge_requests/16 ) From 8fbb27296923f38be0ad7bc7277ef3c1f6862057 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 27 Aug 2017 01:40:30 +0100 Subject: [PATCH 067/111] bump vesion to 0.6.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a89aea2cf53fd811d7f22a53b2064cc4312432c0..b9fa61ad49721e7ad916477d1e11d507def0cab1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.6.1" +version = "0.6.2" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" From df76bc147a6511d3df9bdf5e072d200292e29eb4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 12 Nov 2017 19:58:07 +0000 Subject: [PATCH 068/111] fix clippy warnings --- examples/articles.rs | 4 ++-- src/element.rs | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/articles.rs b/examples/articles.rs index 6db61d86d078032baf8ed6057770c2db0bab89ae..29b1e513464b36a0b094d0babd282c151ab24316 100644 --- a/examples/articles.rs +++ b/examples/articles.rs @@ -2,7 +2,7 @@ extern crate minidom; use minidom::Element; -const DATA: &'static str = r#" +const DATA: &str = r#"
10 Terrible Bugs You Would NEVER Believe Happened @@ -17,7 +17,7 @@ const DATA: &'static str = r#"
"#; -const ARTICLE_NS: &'static str = "article"; +const ARTICLE_NS: &str = "article"; #[derive(Debug)] pub struct Article { diff --git a/src/element.rs b/src/element.rs index 13426449a646d7463f10e129be03c17fa664047a..e398d40f74d035817dcf79b9ffcf15b78fdb08bd 100644 --- a/src/element.rs +++ b/src/element.rs @@ -290,7 +290,7 @@ impl Element { let e = reader.read_event(&mut buf)?; match e { Event::Empty(ref e) | Event::Start(ref e) => { - break build_element(&reader, e)?; + break build_element(reader, e)?; }, Event::Eof => { bail!(ErrorKind::EndOfDocument); @@ -304,12 +304,12 @@ impl Element { loop { match reader.read_event(&mut buf)? { Event::Empty(ref e) => { - let elem = build_element(&reader, e)?; + let elem = build_element(reader, e)?; // Since there is no Event::End after, directly append it to the current node stack.last_mut().unwrap().append_child(elem); }, Event::Start(ref e) => { - let elem = build_element(&reader, e)?; + let elem = build_element(reader, e)?; stack.push(elem); }, Event::End(ref e) => { @@ -348,20 +348,20 @@ impl Element { /// Like `write_to()` but without the `` prelude pub fn write_to_inner(&self, writer: &mut W) -> Result<()> { - let name = match &self.prefix { - &None => Cow::Borrowed(&self.name), - &Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)), + let name = match self.prefix { + None => Cow::Borrowed(&self.name), + Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)), }; write!(writer, "<{}", name)?; for (prefix, ns) in self.namespaces.declared_ns() { - match prefix { - &None => { + match *prefix { + None => { write!(writer, " xmlns=\"")?; write_escaped(writer, ns)?; write!(writer, "\"")?; }, - &Some(ref prefix) => { + Some(ref prefix) => { write!(writer, " xmlns:{}=\"", prefix)?; write_escaped(writer, ns)?; write!(writer, "\"")?; @@ -496,7 +496,7 @@ impl Element { /// assert_eq!(child.name(), "new"); /// ``` pub fn append_child(&mut self, child: Element) -> &mut Element { - child.namespaces.set_parent(self.namespaces.clone()); + child.namespaces.set_parent(Rc::clone(&self.namespaces)); self.children.push(Node::Element(child)); if let Node::Element(ref mut cld) = *self.children.last_mut().unwrap() { @@ -639,12 +639,12 @@ fn build_element(reader: &EventReader, event: &BytesStart) -> Res Ok((key, value)) }) .filter(|o| { - match o { - &Ok((ref key, ref value)) if key == "xmlns" => { + match *o { + Ok((ref key, ref value)) if key == "xmlns" => { namespaces.insert(None, value.to_owned()); false }, - &Ok((ref key, ref value)) if key.starts_with("xmlns:") => { + Ok((ref key, ref value)) if key.starts_with("xmlns:") => { namespaces.insert(Some(key[6..].to_owned()), value.to_owned()); false }, @@ -799,7 +799,7 @@ impl ElementBuilder { // Propagate namespaces for node in &element.children { if let Node::Element(ref e) = *node { - e.namespaces.set_parent(element.namespaces.clone()); + e.namespaces.set_parent(Rc::clone(&element.namespaces)); } } From 8f0549b0ddbb825d12ba54bdd6db675cbd27777d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 27 Dec 2017 16:03:19 +0100 Subject: [PATCH 069/111] update dependencies, and bump version --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9fa61ad49721e7ad916477d1e11d507def0cab1..e04d8b04cb2b8cb05a37ebd40c353480c4bb984e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.6.2" +version = "0.7.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" @@ -14,5 +14,5 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.9.0" -error-chain = "0.10.0" +quick-xml = "0.10.0" +error-chain = "0.11.0" From 1c3a701d2e5847586cdadaebc18f6482c721878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 16:48:07 +0100 Subject: [PATCH 070/111] element: Fix element name comparison in from_reader quick-xml's Events seem to always return prefixed names, and the from_reader function, when comparing name of the pop-ed element, and received event element, was using the unprefixed name to compare. --- src/element.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index e398d40f74d035817dcf79b9ffcf15b78fdb08bd..4494d411782521ae73127c94142c53179dcd1c97 100644 --- a/src/element.rs +++ b/src/element.rs @@ -191,6 +191,11 @@ impl Element { &self.name } + /// Returns a reference to the prefix of this element. + pub fn prefix(&self) -> Option { + self.prefix.clone() + } + /// Returns a reference to the namespace of this element, if it has one, else `None`. pub fn ns(&self) -> Option { self.namespaces.get(&self.prefix) @@ -318,7 +323,11 @@ impl Element { } let elem = stack.pop().unwrap(); if let Some(to) = stack.last_mut() { - if elem.name().as_bytes() != e.name() { + let name = match elem.prefix() { + Some(ref prefix) => format!("{}:", prefix.clone()), + None => String::from(""), + } + elem.name(); + if name.as_bytes() != e.name() { bail!(ErrorKind::InvalidElementClosed); } to.append_child(elem); From 7d009923b71e3c30185e7872ca59bfe292ca2375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 16:54:03 +0100 Subject: [PATCH 071/111] Update author list --- Cargo.toml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e04d8b04cb2b8cb05a37ebd40c353480c4bb984e..9a29a616c03789fa943afccb27fdac14c6b5a4f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,13 @@ [package] name = "minidom" version = "0.7.0" -authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] +authors = [ + "lumi ", + "Emmanuel Gil Peyrot ", + "Bastien Orivel ", + "Astro ", + "Maxime “pep” Buquet ", +] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" From e250e9cf2c9e261ce0011713b1881c44dcde882a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 17:24:54 +0100 Subject: [PATCH 072/111] Add doctest for prefix fn --- src/element.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/element.rs b/src/element.rs index 4494d411782521ae73127c94142c53179dcd1c97..b2cde3a3243535cbbc810e6c6ba1bc141cc99790 100644 --- a/src/element.rs +++ b/src/element.rs @@ -192,6 +192,17 @@ impl Element { } /// Returns a reference to the prefix of this element. + /// + /// # Examples + /// ```rust + /// use minidom::Element; + /// + /// let elem = Element::builder("prefix:name") + /// .build(); + /// + /// assert_eq!(elem.name(), "name"); + /// assert_eq!(elem.prefix(), Some("prefix".to_owned())); + /// ``` pub fn prefix(&self) -> Option { self.prefix.clone() } From 9c498044ee15ee0afc8df7a7c8d32f5d49aea073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 31 Dec 2017 07:19:24 +0100 Subject: [PATCH 073/111] element: Add tests for Element::from_reader --- src/element.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/element.rs b/src/element.rs index b2cde3a3243535cbbc810e6c6ba1bc141cc99790..e363cbb6c674bb64b387aec7cd37d5746d8aeefb 100644 --- a/src/element.rs +++ b/src/element.rs @@ -843,3 +843,46 @@ fn test_element_new() { assert_eq!(elem.attr("name"), Some("value")); assert_eq!(elem.attr("inexistent"), None); } + +#[test] +fn test_from_reader_simple() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let elem2 = Element::builder("foo").build(); + + assert_eq!(elem.unwrap(), elem2); +} + +#[test] +fn test_from_reader_nested() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let nested = Element::builder("bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); + + assert_eq!(elem.unwrap(), elem2); +} + +#[test] +fn test_from_reader_with_prefix() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let nested = Element::builder("prefix:bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); + + assert_eq!(elem.unwrap(), elem2); +} From db70487a31e290ea04f44010f7b765406e271ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 31 Dec 2017 15:24:28 +0000 Subject: [PATCH 074/111] Change prefix fn to return Option<&str> --- src/element.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index e363cbb6c674bb64b387aec7cd37d5746d8aeefb..c96424b65c945d900785cf5a0a563d337988eda1 100644 --- a/src/element.rs +++ b/src/element.rs @@ -201,10 +201,10 @@ impl Element { /// .build(); /// /// assert_eq!(elem.name(), "name"); - /// assert_eq!(elem.prefix(), Some("prefix".to_owned())); + /// assert_eq!(elem.prefix(), Some("prefix")); /// ``` - pub fn prefix(&self) -> Option { - self.prefix.clone() + pub fn prefix(&self) -> Option<&str> { + self.prefix.as_ref().map(String::as_ref) } /// Returns a reference to the namespace of this element, if it has one, else `None`. @@ -335,7 +335,7 @@ impl Element { let elem = stack.pop().unwrap(); if let Some(to) = stack.last_mut() { let name = match elem.prefix() { - Some(ref prefix) => format!("{}:", prefix.clone()), + Some(ref prefix) => format!("{}:", prefix), None => String::from(""), } + elem.name(); if name.as_bytes() != e.name() { From 9959abbb256ba048e4f5e3e144cc13894a4a8bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 31 Dec 2017 16:31:10 +0000 Subject: [PATCH 075/111] Be explicit about quick-xml Events we are handling --- src/element.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/element.rs b/src/element.rs index e398d40f74d035817dcf79b9ffcf15b78fdb08bd..7d88d2d92c7386c567d75fab5205036998f3c636 100644 --- a/src/element.rs +++ b/src/element.rs @@ -295,7 +295,13 @@ impl Element { Event::Eof => { bail!(ErrorKind::EndOfDocument); }, - _ => (), // TODO: may need more errors + Event::Text { .. } | + Event::End { .. } | + Event::Comment { .. } | + Event::CData { .. } | + Event::Decl { .. } | + Event::PI { .. } | + Event::DocType { .. } => (), // TODO: may need more errors } }; @@ -334,7 +340,10 @@ impl Element { Event::Eof => { break; }, - _ => (), // TODO: may need to implement more + Event::Comment { .. } | + Event::Decl { .. } | + Event::PI { .. } | + Event::DocType { .. } => (), } } Ok(stack.pop().unwrap()) From cdb2cb8d861d91885efa9d39231c3d39a1400fe0 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 1 Jan 2018 14:01:03 +0100 Subject: [PATCH 076/111] try not to allocate memory when comparing ending tags with starting tags --- src/element.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/element.rs b/src/element.rs index 2dd08adddaea50b0e51fd07be7f26f13b9703808..cf9ee35cf42064c3c14d025a751985f0444ec377 100644 --- a/src/element.rs +++ b/src/element.rs @@ -340,12 +340,34 @@ impl Element { } let elem = stack.pop().unwrap(); if let Some(to) = stack.last_mut() { - let name = match elem.prefix() { - Some(ref prefix) => format!("{}:", prefix), - None => String::from(""), - } + elem.name(); - if name.as_bytes() != e.name() { - bail!(ErrorKind::InvalidElementClosed); + // TODO: check whether this is correct, we are comparing &[u8]s, not &strs + let elem_name = e.name(); + let mut split_iter = elem_name.splitn(2, |u| *u == 0x3A); + let possible_prefix = split_iter.next().unwrap(); // Can't be empty. + match split_iter.next() { + Some(name) => { + match elem.prefix() { + Some(prefix) => { + if possible_prefix != prefix.as_bytes() { + bail!(ErrorKind::InvalidElementClosed); + } + }, + None => { + bail!(ErrorKind::InvalidElementClosed); + }, + } + if name != elem.name().as_bytes() { + bail!(ErrorKind::InvalidElementClosed); + } + }, + None => { + if elem.prefix().is_some() { + bail!(ErrorKind::InvalidElementClosed); + } + if possible_prefix != elem.name().as_bytes() { + bail!(ErrorKind::InvalidElementClosed); + } + }, } to.append_child(elem); } From 118bbb54621a7023e326c6318e8b8737cb50993a Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 1 Jan 2018 14:28:17 +0100 Subject: [PATCH 077/111] add the spectest.xml from https://gitlab.com/lumi/minidom-rs/issues/8 --- src/element.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/element.rs b/src/element.rs index cf9ee35cf42064c3c14d025a751985f0444ec377..1f8a19039ea930bb8004f00e4984db9b544038b4 100644 --- a/src/element.rs +++ b/src/element.rs @@ -917,3 +917,14 @@ fn test_from_reader_with_prefix() { assert_eq!(elem.unwrap(), elem2); } + +#[test] +fn parses_spectest_xml() { // From: https://gitlab.com/lumi/minidom-rs/issues/8 + let xml = r#" + + + + "#; + let mut reader = EventReader::from_str(xml); + let _ = Element::from_reader(&mut reader); +} From d079f0503af7f1193488c77e1d661f1014b893b7 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 1 Jan 2018 14:34:24 +0100 Subject: [PATCH 078/111] make sure that my tests actually work... --- src/element.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index 1f8a19039ea930bb8004f00e4984db9b544038b4..d126aa1da18be2edd2797652caa68aada1844cb8 100644 --- a/src/element.rs +++ b/src/element.rs @@ -926,5 +926,5 @@ fn parses_spectest_xml() { // From: https://gitlab.com/lumi/minidom-rs/issues/8 "#; let mut reader = EventReader::from_str(xml); - let _ = Element::from_reader(&mut reader); + let _ = Element::from_reader(&mut reader).unwrap(); } From be3279a999b4008536319663a0e60ffda3e9433d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 1 Jan 2018 13:39:32 +0000 Subject: [PATCH 079/111] Update docker image used in CI --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0f54ef57dff651c4da0e8d1054489abc4653019e..c02e04ce55fee669db09a49bfa08f4aea2d5cfbc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: "scorpil/rust:nightly" +image: "pitkley/rust:nightly" before_script: - apt-get update -yqq From f456600efd9b5c2bf537ff37525cc737e83c3676 Mon Sep 17 00:00:00 2001 From: Yue Liu Date: Fri, 5 Jan 2018 20:20:12 -0800 Subject: [PATCH 080/111] Support comment and write to quick-xml Writer --- src/element.rs | 128 ++++++++++++++++++++++++++++++++++++------------- src/tests.rs | 33 ++++++++++++- 2 files changed, 125 insertions(+), 36 deletions(-) diff --git a/src/element.rs b/src/element.rs index d126aa1da18be2edd2797652caa68aada1844cb8..3cd3e824cd5eecec06d60a904779370ef6cd3383 100644 --- a/src/element.rs +++ b/src/element.rs @@ -10,7 +10,8 @@ use std::borrow::Cow; use error::{Error, ErrorKind, Result}; use quick_xml::reader::Reader as EventReader; -use quick_xml::events::{Event, BytesStart}; +use quick_xml::writer::Writer as EventWriter; +use quick_xml::events::{Event, BytesStart, BytesEnd, BytesText, BytesDecl}; use std::io::BufRead; @@ -21,20 +22,50 @@ use std::slice; use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; use namespace_set::NamespaceSet; -/// Escape XML text -pub fn write_escaped(writer: &mut W, input: &str) -> Result<()> { - for c in input.chars() { - match c { - '&' => write!(writer, "&")?, - '<' => write!(writer, "<")?, - '>' => write!(writer, ">")?, - '\'' => write!(writer, "'")?, - '"' => write!(writer, """)?, - _ => write!(writer, "{}", c)?, +/// helper function to escape a `&[u8]` and replace all +/// xml special characters (<, >, &, ', ") with their corresponding +/// xml escaped value. +pub fn escape(raw: &[u8]) -> Cow<[u8]> { + let mut escapes: Vec<(usize, &'static [u8])> = Vec::new(); + let mut bytes = raw.iter(); + fn to_escape(b: u8) -> bool { + match b { + b'<' | b'>' | b'\'' | b'&' | b'"' => true, + _ => false, } } - Ok(()) + let mut loc = 0; + while let Some(i) = bytes.position(|&b| to_escape(b)) { + loc += i; + match raw[loc] { + b'<' => escapes.push((loc, b"<")), + b'>' => escapes.push((loc, b">")), + b'\'' => escapes.push((loc, b"'")), + b'&' => escapes.push((loc, b"&")), + b'"' => escapes.push((loc, b""")), + _ => unreachable!("Only '<', '>','\', '&' and '\"' are escaped"), + } + loc += 1; + } + + if escapes.is_empty() { + Cow::Borrowed(raw) + } else { + let len = raw.len(); + let mut v = Vec::with_capacity(len); + let mut start = 0; + for (i, r) in escapes { + v.extend_from_slice(&raw[start..i]); + v.extend_from_slice(r); + start = i + 1; + } + + if start < len { + v.extend_from_slice(&raw[start..]); + } + Cow::Owned(v) + } } /// A node in an element tree. @@ -44,6 +75,8 @@ pub enum Node { Element(Element), /// A text node. Text(String), + /// A comment node. + Comment(String), } impl Node { @@ -64,6 +97,7 @@ impl Node { match *self { Node::Element(ref e) => Some(e), Node::Text(_) => None, + Node::Comment(_) => None, } } @@ -84,14 +118,22 @@ impl Node { match *self { Node::Element(_) => None, Node::Text(ref s) => Some(s), + Node::Comment(_) => None, } } - fn write_to_inner(&self, writer: &mut W) -> Result<()>{ + fn write_to_inner(&self, writer: &mut EventWriter) -> Result<()>{ match *self { Node::Element(ref elmt) => elmt.write_to_inner(writer)?, - Node::Text(ref s) => write_escaped(writer, s)?, - } + Node::Text(ref s) => { + writer.write_event(Event::Text(BytesText::from_str(s)))?; + () + }, + Node::Comment(ref s) => { + writer.write_event(Event::Comment(BytesText::from_str(s)))?; + () + }, + }; Ok(()) } @@ -382,7 +424,13 @@ impl Element { Event::Eof => { break; }, - Event::Comment { .. } | + Event::Comment(s) => { + let comment = reader.decode(&s).into_owned(); + if comment != "" { + let current_elem = stack.last_mut().unwrap(); + current_elem.append_comment_node(comment); + } + }, Event::Decl { .. } | Event::PI { .. } | Event::DocType { .. } => (), @@ -393,51 +441,48 @@ impl Element { /// Output a document to a `Writer`. pub fn write_to(&self, writer: &mut W) -> Result<()> { - write!(writer, "")?; + self.to_writer(&mut EventWriter::new(writer)) + } + + /// Output the document to quick-xml `Writer` + pub fn to_writer(&self, writer: &mut EventWriter) -> Result<()> { + writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"utf-8"), None)))?; self.write_to_inner(writer) } /// Like `write_to()` but without the `` prelude - pub fn write_to_inner(&self, writer: &mut W) -> Result<()> { + pub fn write_to_inner(&self, writer: &mut EventWriter) -> Result<()> { let name = match self.prefix { None => Cow::Borrowed(&self.name), Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)), }; - write!(writer, "<{}", name)?; + let mut start = BytesStart::borrowed(name.as_bytes(), name.len()); for (prefix, ns) in self.namespaces.declared_ns() { match *prefix { - None => { - write!(writer, " xmlns=\"")?; - write_escaped(writer, ns)?; - write!(writer, "\"")?; - }, + None => start.push_attribute(("xmlns", ns.as_ref())), Some(ref prefix) => { - write!(writer, " xmlns:{}=\"", prefix)?; - write_escaped(writer, ns)?; - write!(writer, "\"")?; + let key = format!("xmlns:{}", prefix); + start.push_attribute((key.as_bytes(), ns.as_bytes())) }, } } - for (key, value) in &self.attributes { - write!(writer, " {}=\"", key)?; - write_escaped(writer, value)?; - write!(writer, "\"")?; + start.push_attribute((key.as_bytes(), escape(value.as_bytes()).as_ref())); } if self.children.is_empty() { - write!(writer, " />")?; + writer.write_event(Event::Empty(start))?; return Ok(()) } - write!(writer, ">")?; + writer.write_event(Event::Start(start))?; for child in &self.children { child.write_to_inner(writer)?; } - write!(writer, "", name)?; + writer.write_event(Event::End(BytesEnd::borrowed(name.as_bytes())))?; Ok(()) } @@ -576,6 +621,21 @@ impl Element { self.children.push(Node::Text(child.into())); } + /// Appends a comment node to an `Element`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Element; + /// + /// let mut elem = Element::bare("node"); + /// + /// elem.append_comment_node("comment"); + /// ``` + pub fn append_comment_node>(&mut self, child: S) { + self.children.push(Node::Comment(child.into())); + } + /// Appends a node to an `Element`. /// /// # Examples diff --git a/src/tests.rs b/src/tests.rs index e787715830823ef66285348a0f67ae63ad6a0ad8..59ef0fedcc00725fb725d1d922a1fb88e9944b17 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -4,7 +4,7 @@ use quick_xml::reader::Reader; use element::Element; -const TEST_STRING: &'static str = r#"meownya"#; +const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { let mut root = Element::builder("root") @@ -27,6 +27,19 @@ fn build_test_tree() -> Element { root } +const COMMENT_TEST_STRING: &'static str = r#""#; + +fn build_comment_test_tree() -> Element { + let mut root = Element::builder("root").build(); + root.append_comment_node("This is a child."); + let mut child = Element::builder("child").attr("attr", "val").build(); + child.append_comment_node("This is a grandchild."); + let grand_child = Element::builder("grandchild").build(); + child.append_child(grand_child); + root.append_child(child); + root +} + #[test] fn reader_works() { let mut reader = Reader::from_str(TEST_STRING); @@ -53,7 +66,7 @@ fn writer_escapes_attributes() { root.write_to(&mut writer).unwrap(); } assert_eq!(String::from_utf8(writer).unwrap(), - r#""# + r#""# ); } @@ -197,3 +210,19 @@ fn namespace_inherited_prefixed2() { assert_eq!(child.name(), "message"); assert_eq!(child.ns(), Some("jabber:client".to_owned())); } + +#[test] +fn read_comments() { + let mut reader = Reader::from_str(COMMENT_TEST_STRING); + assert_eq!(Element::from_reader(&mut reader).unwrap(), build_comment_test_tree()); +} + +#[test] +fn write_comments() { + let root = build_comment_test_tree(); + let mut writer = Vec::new(); + { + root.write_to(&mut writer).unwrap(); + } + assert_eq!(String::from_utf8(writer).unwrap(), COMMENT_TEST_STRING); +} From 055caa8e24bc64da170e956c929200ccae5ada20 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 19:41:03 +0100 Subject: [PATCH 081/111] add tests for some XML errors --- src/tests.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index e787715830823ef66285348a0f67ae63ad6a0ad8..0db96b70af99f9843d8a769f6eeb5b9170de823e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -197,3 +197,24 @@ fn namespace_inherited_prefixed2() { assert_eq!(child.name(), "message"); assert_eq!(child.ns(), Some("jabber:client".to_owned())); } + +#[test] +fn xml_error() { + match "
".parse::() { + Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + err => panic!("No or wrong error: {:?}", err) + } + + match "() { + Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + err => panic!("No or wrong error: {:?}", err) + } +} + +#[test] +fn invalid_element_error() { + match "".parse::() { + Err(::error::Error(::error::ErrorKind::InvalidElement, _)) => (), + err => panic!("No or wrong error: {:?}", err) + } +} From 236582b24ea098afd7c78703d01dff1cac8f7891 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 19:54:09 +0100 Subject: [PATCH 082/111] update quick_xml and migrate from error-chain to failure --- Cargo.toml | 5 ++-- src/element.rs | 16 ++++++------ src/error.rs | 71 +++++++++++++++++++++++++++++++------------------- src/lib.rs | 5 ++-- src/tests.rs | 6 ++--- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a29a616c03789fa943afccb27fdac14c6b5a4f6..a4ea886e164dc8eed4fa6f664e8affed8808aa95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.10.0" -error-chain = "0.11.0" +quick-xml = "0.11.0" +failure = "0.1.1" +failure_derive = "0.1.1" diff --git a/src/element.rs b/src/element.rs index d126aa1da18be2edd2797652caa68aada1844cb8..0f3fa9d1bd53e2586b7ac0838292b52979d737c8 100644 --- a/src/element.rs +++ b/src/element.rs @@ -7,7 +7,7 @@ use std::str; use std::rc::Rc; use std::borrow::Cow; -use error::{Error, ErrorKind, Result}; +use error::{Error, Result}; use quick_xml::reader::Reader as EventReader; use quick_xml::events::{Event, BytesStart}; @@ -309,7 +309,7 @@ impl Element { break build_element(reader, e)?; }, Event::Eof => { - bail!(ErrorKind::EndOfDocument); + return Err(Error::EndOfDocument); }, Event::Text { .. } | Event::End { .. } | @@ -349,23 +349,23 @@ impl Element { match elem.prefix() { Some(prefix) => { if possible_prefix != prefix.as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, None => { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); }, } if name != elem.name().as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, None => { if elem.prefix().is_some() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } if possible_prefix != elem.name().as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, } @@ -676,7 +676,7 @@ fn split_element_name>(s: S) -> Result<(Option, String)> { match name_parts.len() { 2 => Ok((Some(name_parts[0].to_owned()), name_parts[1].to_owned())), 1 => Ok((None, name_parts[0].to_owned())), - _ => bail!(ErrorKind::InvalidElement), + _ => Err(Error::InvalidElement), } } diff --git a/src/error.rs b/src/error.rs index 4079365fa4d028181d140a6d3954421ca2764764..48f334403830eb1b89d06ed26f09c05ec2917ba8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,34 +2,51 @@ use std::convert::From; -error_chain! { - foreign_links { - XmlError(::quick_xml::errors::Error) - /// An error from quick_xml. - ; - Utf8Error(::std::str::Utf8Error) - /// An UTF-8 conversion error. - ; - IoError(::std::io::Error) - /// An I/O error, from std::io. - ; +/// Our main error type. +#[derive(Debug, Fail)] +pub enum Error { + /// An error from quick_xml. + #[fail(display = "XML error: {}", _0)] + XmlError(#[cause] ::quick_xml::errors::Error), + + /// An UTF-8 conversion error. + #[fail(display = "UTF-8 error: {}", _0)] + Utf8Error(#[cause] ::std::str::Utf8Error), + + /// An I/O error, from std::io. + #[fail(display = "IO error: {}", _0)] + IoError(#[cause] ::std::io::Error), + + /// An error which is returned when the end of the document was reached prematurely. + #[fail(display = "the end of the document has been reached prematurely")] + EndOfDocument, + + /// An error which is returned when an element is closed when it shouldn't be + #[fail(display = "the XML is invalid, an element was wrongly closed")] + InvalidElementClosed, + + /// An error which is returned when an elemet's name contains more than one colon + #[fail(display = "the XML element is invalid")] + InvalidElement, +} + +impl From<::quick_xml::errors::Error> for Error { + fn from(err: ::quick_xml::errors::Error) -> Error { + Error::XmlError(err) + } +} + +impl From<::std::str::Utf8Error> for Error { + fn from(err: ::std::str::Utf8Error) -> Error { + Error::Utf8Error(err) } +} - errors { - /// An error which is returned when the end of the document was reached prematurely. - EndOfDocument { - description("the end of the document has been reached prematurely") - display("the end of the document has been reached prematurely") - } - /// An error which is returned when an element is closed when it shouldn't be - InvalidElementClosed { - description("The XML is invalid, an element was wrongly closed") - display("the XML is invalid, an element was wrongly closed") - } - /// An error which is returned when an elemet's name contains more than one colon - InvalidElement { - description("The XML element is invalid") - display("the XML element is invalid") - } +impl From<::std::io::Error> for Error { + fn from(err: ::std::io::Error) -> Error { + Error::IoError(err) } } + +/// Our simplified Result type. +pub type Result = ::std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index b11fe0603ddf40c566a5af23675f0a5adab88b50..8cf4ab7e5b70ab0104e5b6e67a7b529b2ae4e3b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,8 @@ //! ``` extern crate quick_xml; -#[macro_use] extern crate error_chain; +extern crate failure; +#[macro_use] extern crate failure_derive; pub mod error; pub mod element; @@ -74,6 +75,6 @@ mod namespace_set; #[cfg(test)] mod tests; -pub use error::{Error, ErrorKind, Result, ResultExt}; +pub use error::{Error, Result}; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; diff --git a/src/tests.rs b/src/tests.rs index 0db96b70af99f9843d8a769f6eeb5b9170de823e..7d6c523e5e397686ceeb008a603ac02288a7192c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -201,12 +201,12 @@ fn namespace_inherited_prefixed2() { #[test] fn xml_error() { match "
".parse::() { - Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + Err(::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } match "() { - Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + Err(::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } } @@ -214,7 +214,7 @@ fn xml_error() { #[test] fn invalid_element_error() { match "".parse::() { - Err(::error::Error(::error::ErrorKind::InvalidElement, _)) => (), + Err(::error::Error::InvalidElement) => (), err => panic!("No or wrong error: {:?}", err) } } From 0eba989122a6df1d0d5c75b6707249b9e6425c44 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 20:56:40 +0100 Subject: [PATCH 083/111] bump version to 0.8.0 and add CHANGELOG.md entry --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 369ef8ad62d81000184f2cf47b2e713ac4d80326..2e1c16ab0eb36d62b31c9d6bb77a42fbd145a565 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version 0.8.0, released 2018-02-18: + * Additions + - Link Mauve replaced error\_chain with failure ( https://gitlab.com/lumi/minidom-rs/merge_requests/27 ) Version 0.6.2, released 2017-08-27: * Additions - Link Mauve added an implementation of IntoElements for all Into ( https://gitlab.com/lumi/minidom-rs/merge_requests/19 ) diff --git a/Cargo.toml b/Cargo.toml index a4ea886e164dc8eed4fa6f664e8affed8808aa95..a05552ca908f56a7a7e14a5c14ff7d58a89ecbee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.7.0" +version = "0.8.0" authors = [ "lumi ", "Emmanuel Gil Peyrot ", From e4dfa218b6c71320d4c4d2eee9ee8622d078447e Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 18 Feb 2018 21:50:32 +0100 Subject: [PATCH 084/111] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e1c16ab0eb36d62b31c9d6bb77a42fbd145a565..f2f831b7e80b5da7efe337813fd2a73dcf5d0885 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 0.8.0, released 2018-02-18: * Additions - Link Mauve replaced error\_chain with failure ( https://gitlab.com/lumi/minidom-rs/merge_requests/27 ) + - Yue Liu added support for writing comments and made the writing methods use quick-xml's EventWriter ( https://gitlab.com/lumi/minidom-rs/merge_requests/26 ) Version 0.6.2, released 2017-08-27: * Additions - Link Mauve added an implementation of IntoElements for all Into ( https://gitlab.com/lumi/minidom-rs/merge_requests/19 ) From 96f223b7e3b0ce21fe1a023f49527fa221bd4176 Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Tue, 10 Apr 2018 21:01:18 -0400 Subject: [PATCH 085/111] bump version to 0.9.0 and upgrade quick-xml to 0.12.1 --- CHANGELOG.md | 3 +++ Cargo.toml | 4 ++-- src/element.rs | 8 ++++---- src/error.rs | 6 +++--- src/tests.rs | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f831b7e80b5da7efe337813fd2a73dcf5d0885..d3fff023eabb6e81b577e0e92e1c9de4fcc5fe24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version 0.9.0, released 2018-04-10: + * Small changes + - Upgrade quick_xml to 0.12.1 Version 0.8.0, released 2018-02-18: * Additions - Link Mauve replaced error\_chain with failure ( https://gitlab.com/lumi/minidom-rs/merge_requests/27 ) diff --git a/Cargo.toml b/Cargo.toml index a05552ca908f56a7a7e14a5c14ff7d58a89ecbee..de970054f5c74f05b6359d5a990e109ccbab9c79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.8.0" +version = "0.9.0" authors = [ "lumi ", "Emmanuel Gil Peyrot ", @@ -20,6 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.11.0" +quick-xml = "0.12.1" failure = "0.1.1" failure_derive = "0.1.1" diff --git a/src/element.rs b/src/element.rs index 81e3fffc8a156f01ff1793b4831c5a1b28abebbe..d8a4a98a4762dbd52c8c7a60dd353fa777adb30e 100644 --- a/src/element.rs +++ b/src/element.rs @@ -9,8 +9,8 @@ use std::borrow::Cow; use error::{Error, Result}; -use quick_xml::reader::Reader as EventReader; -use quick_xml::writer::Writer as EventWriter; +use quick_xml::Reader as EventReader; +use quick_xml::Writer as EventWriter; use quick_xml::events::{Event, BytesStart, BytesEnd, BytesText, BytesDecl}; use std::io::BufRead; @@ -126,11 +126,11 @@ impl Node { match *self { Node::Element(ref elmt) => elmt.write_to_inner(writer)?, Node::Text(ref s) => { - writer.write_event(Event::Text(BytesText::from_str(s)))?; + writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; () }, Node::Comment(ref s) => { - writer.write_event(Event::Comment(BytesText::from_str(s)))?; + writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; () }, }; diff --git a/src/error.rs b/src/error.rs index 48f334403830eb1b89d06ed26f09c05ec2917ba8..b2ba473cd88c987cef13a6c334f6a0a2dada33b2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,7 +7,7 @@ use std::convert::From; pub enum Error { /// An error from quick_xml. #[fail(display = "XML error: {}", _0)] - XmlError(#[cause] ::quick_xml::errors::Error), + XmlError(#[cause] ::quick_xml::Error), /// An UTF-8 conversion error. #[fail(display = "UTF-8 error: {}", _0)] @@ -30,8 +30,8 @@ pub enum Error { InvalidElement, } -impl From<::quick_xml::errors::Error> for Error { - fn from(err: ::quick_xml::errors::Error) -> Error { +impl From<::quick_xml::Error> for Error { + fn from(err: ::quick_xml::Error) -> Error { Error::XmlError(err) } } diff --git a/src/tests.rs b/src/tests.rs index b57a2037322952dbbadf5034f43d498341bb0fbf..2acc97052ecc63c81761d0447dc9ae2c8eb6e0d0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,6 @@ use std::iter::Iterator; -use quick_xml::reader::Reader; +use quick_xml::Reader; use element::Element; From 4f588f615d513f23bbb094dd89871f27275de2af Mon Sep 17 00:00:00 2001 From: lumi Date: Tue, 29 May 2018 15:59:36 +0200 Subject: [PATCH 086/111] Fix CDATA handling, add relevant test, update CHANGELOG, bump version to 0.9.1. --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- src/element.rs | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3fff023eabb6e81b577e0e92e1c9de4fcc5fe24..8e7062a562ddf99f024ea07dcae61980f45385b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version 0.9.1, released 2018-05-29: + * Fixes + * Lumi fixed CDATA handling, minidom will not unescape CDATA bodies anymore. Version 0.9.0, released 2018-04-10: * Small changes - Upgrade quick_xml to 0.12.1 diff --git a/Cargo.toml b/Cargo.toml index de970054f5c74f05b6359d5a990e109ccbab9c79..3f49a66c3ea95c404741de098a78057f69b80e0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.9.0" +version = "0.9.1" authors = [ "lumi ", "Emmanuel Gil Peyrot ", diff --git a/src/element.rs b/src/element.rs index d8a4a98a4762dbd52c8c7a60dd353fa777adb30e..485c97ff69a1674c3a95a821b0c6993ab8dc6711 100644 --- a/src/element.rs +++ b/src/element.rs @@ -414,13 +414,20 @@ impl Element { to.append_child(elem); } }, - Event::Text(s) | Event::CData(s) => { + Event::Text(s) => { let text = s.unescape_and_decode(reader)?; if text != "" { let mut current_elem = stack.last_mut().unwrap(); current_elem.append_text_node(text); } }, + Event::CData(s) => { + let text = reader.decode(&s).into_owned(); + if text != "" { + let mut current_elem = stack.last_mut().unwrap(); + current_elem.append_text_node(text); + } + }, Event::Eof => { break; }, @@ -988,3 +995,11 @@ fn parses_spectest_xml() { // From: https://gitlab.com/lumi/minidom-rs/issues/8 let mut reader = EventReader::from_str(xml); let _ = Element::from_reader(&mut reader).unwrap(); } + +#[test] +fn does_not_unescape_cdata() { + let xml = "]]>"; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader).unwrap(); + assert_eq!(elem.text(), "'>blah"); +} From 7ed4be96ebe2e973a6f4643e13a6bc2d801059a4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 29 May 2018 16:01:11 +0200 Subject: [PATCH 087/111] impl IntoAttributeValue for std::net::IpAddr --- CHANGELOG.md | 2 ++ src/convert.rs | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e7062a562ddf99f024ea07dcae61980f45385b4..dc1fadc82edd186ee157db2277ec5d78ba611390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Version 0.9.1, released 2018-05-29: * Fixes * Lumi fixed CDATA handling, minidom will not unescape CDATA bodies anymore. + * Small changes + - Link Mauve implemented IntoAttributeValue on std::net::IpAddr. Version 0.9.0, released 2018-04-10: * Small changes - Upgrade quick_xml to 0.12.1 diff --git a/src/convert.rs b/src/convert.rs index 1b35414c12b0495e693b4d71d0238afd6f3345de..1a528111a7049651571368798d5609866e4b6173 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -103,7 +103,7 @@ macro_rules! impl_into_attribute_values { } } -impl_into_attribute_values!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8); +impl_into_attribute_values!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8, ::std::net::IpAddr); impl IntoAttributeValue for String { fn into_attribute_value(self) -> Option { @@ -132,6 +132,8 @@ impl IntoAttributeValue for Option { #[cfg(test)] mod tests { use super::IntoAttributeValue; + use std::net::IpAddr; + use std::str::FromStr; #[test] fn test_into_attribute_value_on_ints() { @@ -143,5 +145,6 @@ mod tests { assert_eq!((-17i16).into_attribute_value().unwrap(), "-17"); assert_eq!( 18i32.into_attribute_value().unwrap(), "18"); assert_eq!((-19i64).into_attribute_value().unwrap(), "-19"); + assert_eq!(IpAddr::from_str("127.000.0.1").unwrap().into_attribute_value().unwrap(), "127.0.0.1"); } } From fac64400f13a3aa8dd261cfd21429b111dc7c9e1 Mon Sep 17 00:00:00 2001 From: lumi Date: Tue, 29 May 2018 16:22:22 +0200 Subject: [PATCH 088/111] Put the tests in element.rs into a module block. --- src/element.rs | 134 +++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 65 deletions(-) diff --git a/src/element.rs b/src/element.rs index 485c97ff69a1674c3a95a821b0c6993ab8dc6711..e147a27d7b4fccdb6990386b4cd66129f63e1f5f 100644 --- a/src/element.rs +++ b/src/element.rs @@ -926,80 +926,84 @@ impl ElementBuilder { } #[cfg(test)] -#[test] -fn test_element_new() { - use std::iter::FromIterator; - - let elem = Element::new( "name".to_owned() - , None - , Some("namespace".to_owned()) - , BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() ) - , Vec::new() ); - - assert_eq!(elem.name(), "name"); - assert_eq!(elem.ns(), Some("namespace".to_owned())); - assert_eq!(elem.attr("name"), Some("value")); - assert_eq!(elem.attr("inexistent"), None); -} +mod tests { + use super::*; -#[test] -fn test_from_reader_simple() { - let xml = ""; - let mut reader = EventReader::from_str(xml); - let elem = Element::from_reader(&mut reader); + #[test] + fn test_element_new() { + use std::iter::FromIterator; - let elem2 = Element::builder("foo").build(); + let elem = Element::new( "name".to_owned() + , None + , Some("namespace".to_owned()) + , BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() ) + , Vec::new() ); - assert_eq!(elem.unwrap(), elem2); -} + assert_eq!(elem.name(), "name"); + assert_eq!(elem.ns(), Some("namespace".to_owned())); + assert_eq!(elem.attr("name"), Some("value")); + assert_eq!(elem.attr("inexistent"), None); + } -#[test] -fn test_from_reader_nested() { - let xml = ""; - let mut reader = EventReader::from_str(xml); - let elem = Element::from_reader(&mut reader); + #[test] + fn test_from_reader_simple() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); - let nested = Element::builder("bar") - .attr("baz", "qxx") - .build(); - let elem2 = Element::builder("foo") - .append(nested) - .build(); + let elem2 = Element::builder("foo").build(); - assert_eq!(elem.unwrap(), elem2); -} + assert_eq!(elem.unwrap(), elem2); + } -#[test] -fn test_from_reader_with_prefix() { - let xml = ""; - let mut reader = EventReader::from_str(xml); - let elem = Element::from_reader(&mut reader); + #[test] + fn test_from_reader_nested() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); - let nested = Element::builder("prefix:bar") - .attr("baz", "qxx") - .build(); - let elem2 = Element::builder("foo") - .append(nested) - .build(); + let nested = Element::builder("bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); - assert_eq!(elem.unwrap(), elem2); -} + assert_eq!(elem.unwrap(), elem2); + } -#[test] -fn parses_spectest_xml() { // From: https://gitlab.com/lumi/minidom-rs/issues/8 - let xml = r#" - - - - "#; - let mut reader = EventReader::from_str(xml); - let _ = Element::from_reader(&mut reader).unwrap(); -} + #[test] + fn test_from_reader_with_prefix() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let nested = Element::builder("prefix:bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); + + assert_eq!(elem.unwrap(), elem2); + } -#[test] -fn does_not_unescape_cdata() { - let xml = "]]>"; - let mut reader = EventReader::from_str(xml); - let elem = Element::from_reader(&mut reader).unwrap(); - assert_eq!(elem.text(), "'>blah"); + #[test] + fn parses_spectest_xml() { // From: https://gitlab.com/lumi/minidom-rs/issues/8 + let xml = r#" + + + + "#; + let mut reader = EventReader::from_str(xml); + let _ = Element::from_reader(&mut reader).unwrap(); + } + + #[test] + fn does_not_unescape_cdata() { + let xml = "]]>"; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader).unwrap(); + assert_eq!(elem.text(), "'>blah"); + } } From f1f5f5df113979a013d88c5c8cf7951024008525 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 23 Jun 2018 12:06:18 +0000 Subject: [PATCH 089/111] Fix doc to reflect the switch from xml-rs to quick-xml. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8cf4ab7e5b70ab0104e5b6e67a7b529b2ae4e3b6..32a18147e4d3ee9fbe19c5acde39b44ae1b381b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![deny(missing_docs)] -//! A minimal DOM crate built on top of xml-rs. +//! A minimal DOM crate built on top of quick-xml. //! //! This library exports an `Element` struct which represents a DOM tree. //! From 72c63920a8b0d20f956696add58d602855bed7bd Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Sun, 21 Oct 2018 20:24:32 +0200 Subject: [PATCH 090/111] Update quick-xml and bump version --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f49a66c3ea95c404741de098a78057f69b80e0d..fa66cfe377938713b8b89f75ae8a726ce78a861d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.9.1" +version = "0.10.0" authors = [ "lumi ", "Emmanuel Gil Peyrot ", @@ -20,6 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.12.1" +quick-xml = "0.13" failure = "0.1.1" failure_derive = "0.1.1" From c5c8dee20a951b6dc2b2aa9f22724a3044b5c376 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 23 Dec 2018 15:40:23 +0100 Subject: [PATCH 091/111] Get rid of `IntoElements`, replace it with `Into` and ` IntoIterator`. This is a backwards-incompatible change, but should require minimal changes. Doing this splits the `append` method in `ElementBuilder` into two methods. `append` now appends exactly one node, while `append_all` appends an iterator of nodes. Add `remove_child`, which removes the first child that has a specific name and namespace, then returns it if it exists. Add a lot of convenience methods on `Node`: `as_text_mut`, `as_element_mut`, `into_text`, `into_element`. --- src/convert.rs | 79 ----------------------- src/element.rs | 171 ++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 2 +- 3 files changed, 162 insertions(+), 90 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 1a528111a7049651571368798d5609866e4b6173..cabe86d5be2dff5c82d13848e0cda318ad65200a 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -2,85 +2,6 @@ use element::{Element, ElementBuilder}; -/// A struct which is used for emitting `Element`s and text nodes into an `Element` without -/// exposing more functionality than necessary. -pub struct ElementEmitter<'a>(&'a mut Element); - -impl<'a> ElementEmitter<'a> { - /// Creates a new `ElementEmitter`. - pub fn new(root: &'a mut Element) -> ElementEmitter<'a> { - ElementEmitter(root) - } - - /// Appends an `Element` to the target. - pub fn append_child(&mut self, element: Element) { - self.0.append_child(element); - } - - /// Appends a text node to the target. - pub fn append_text_node(&mut self, text: String) { - self.0.append_text_node(text); - } -} - -/// A trait for types which can be converted to one or multiple `Element`s. -pub trait IntoElements { - /// Emits this as a sequence of text nodes and `Element`s. - fn into_elements(self, emitter: &mut ElementEmitter); -} - -impl IntoElements for Vec { - fn into_elements(self, emitter: &mut ElementEmitter) { - for elem in self { - elem.into_elements(emitter); - } - } -} - -impl<'a, T: IntoElements + Clone> IntoElements for &'a [T] { - fn into_elements(self, emitter: &mut ElementEmitter) { - self.to_vec().into_elements(emitter); - } -} - -impl IntoElements for Option { - fn into_elements(self, emitter: &mut ElementEmitter) { - if let Some(e) = self { - e.into_elements(emitter); - } - } -} - -impl IntoElements for T where T: Into { - fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_child(self.into()); - } -} - -impl IntoElements for ElementBuilder { - fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_child(self.build()); - } -} - -impl IntoElements for String { - fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_text_node(self); - } -} - -impl<'a> IntoElements for &'a String { - fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_text_node(self.to_owned()); - } -} - -impl<'a> IntoElements for &'a str { - fn into_elements(self, emitter: &mut ElementEmitter) { - emitter.append_text_node(self.to_owned()); - } -} - /// A trait for types which can be converted to an attribute value. pub trait IntoAttributeValue { /// Turns this into an attribute string, or None if it shouldn't be added. diff --git a/src/element.rs b/src/element.rs index e147a27d7b4fccdb6990386b4cd66129f63e1f5f..88a048d12dd52dc104f51f6444c206e38c8c0a56 100644 --- a/src/element.rs +++ b/src/element.rs @@ -19,7 +19,7 @@ use std::str::FromStr; use std::slice; -use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; +use convert::IntoAttributeValue; use namespace_set::NamespaceSet; /// helper function to escape a `&[u8]` and replace all @@ -80,7 +80,8 @@ pub enum Node { } impl Node { - /// Turns this into an `Element` if possible, else returns None. + /// Turns this into a reference to an `Element` if this is an element node. + /// Else this returns `None`. /// /// # Examples /// @@ -101,7 +102,52 @@ impl Node { } } - /// Turns this into a `String` if possible, else returns None. + /// Turns this into a mutable reference of an `Element` if this is an element node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let mut elm = Node::Element("".parse().unwrap()); + /// let mut txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_element_mut().unwrap().name(), "meow"); + /// assert_eq!(txt.as_element_mut(), None); + /// ``` + pub fn as_element_mut(&mut self) -> Option<&mut Element> { + match *self { + Node::Element(ref mut e) => Some(e), + Node::Text(_) => None, + Node::Comment(_) => None, + } + } + + /// Turns this into an `Element`, consuming self, if this is an element node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.into_element().unwrap().name(), "meow"); + /// assert_eq!(txt.into_element(), None); + /// ``` + pub fn into_element(self) -> Option { + match self { + Node::Element(e) => Some(e), + Node::Text(_) => None, + Node::Comment(_) => None, + } + } + + /// Turns this into an `&str` if this is a text node. + /// Else this returns `None`. /// /// # Examples /// @@ -122,6 +168,56 @@ impl Node { } } + /// Turns this into an `&mut String` if this is a text node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let mut elm = Node::Element("".parse().unwrap()); + /// let mut txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_text_mut(), None); + /// { + /// let text_mut = txt.as_text_mut().unwrap(); + /// assert_eq!(text_mut, "meow"); + /// text_mut.push_str("zies"); + /// assert_eq!(text_mut, "meowzies"); + /// } + /// assert_eq!(txt.as_text().unwrap(), "meowzies"); + /// ``` + pub fn as_text_mut(&mut self) -> Option<&mut String> { + match *self { + Node::Element(_) => None, + Node::Text(ref mut s) => Some(s), + Node::Comment(_) => None, + } + } + + /// Turns this into an `String`, consuming self, if this is a text node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.into_text(), None); + /// assert_eq!(txt.into_text().unwrap(), "meow"); + /// ``` + pub fn into_text(self) -> Option { + match self { + Node::Element(_) => None, + Node::Text(s) => Some(s), + Node::Comment(_) => None, + } + } + fn write_to_inner(&self, writer: &mut EventWriter) -> Result<()>{ match *self { Node::Element(ref elmt) => elmt.write_to_inner(writer)?, @@ -139,6 +235,30 @@ impl Node { } } +impl From for Node { + fn from(elm: Element) -> Node { + Node::Element(elm) + } +} + +impl From for Node { + fn from(s: String) -> Node { + Node::Text(s) + } +} + +impl<'a> From<&'a str> for Node { + fn from(s: &'a str) -> Node { + Node::Text(s.to_owned()) + } +} + +impl From for Node { + fn from(builder: ElementBuilder) -> Node { + Node::Element(builder.build()) + } +} + #[derive(Clone, PartialEq, Eq, Debug)] /// A struct representing a DOM Element. pub struct Element { @@ -157,7 +277,6 @@ impl<'a> From<&'a Element> for String { } } - impl FromStr for Element { type Err = Error; @@ -736,6 +855,34 @@ impl Element { pub fn has_child, NS: AsRef>(&self, name: N, namespace: NS) -> bool { self.get_child(name, namespace).is_some() } + + /// Removes the first child with this name and namespace, if it exists, and returns an + /// `Option` containing this child if it succeeds. + /// Returns `None` if no child matches this name and namespace. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Element; + /// + /// let mut elem: Element = r#""#.parse().unwrap(); + /// + /// assert!(elem.remove_child("a", "ns").unwrap().is("a", "ns")); + /// assert!(elem.remove_child("a", "ns").is_none()); + /// assert!(elem.remove_child("inexistent", "inexistent").is_none()); + /// ``` + pub fn remove_child, NS: AsRef>(&mut self, name: N, namespace: NS) -> Option { + let name = name.as_ref(); + let namespace = namespace.as_ref(); + let idx = self.children.iter().position(|x| { + if let Node::Element(ref elm) = x { + elm.is(name, namespace) + } else { + false + } + })?; + self.children.remove(idx).into_element() + } } fn split_element_name>(s: S) -> Result<(Option, String)> { @@ -900,11 +1047,16 @@ impl ElementBuilder { self } - /// Appends anything implementing `IntoElements` into the tree. - pub fn append(mut self, into: T) -> ElementBuilder { - { - let mut emitter = ElementEmitter::new(&mut self.root); - into.into_elements(&mut emitter); + /// Appends anything implementing `Into` into the tree. + pub fn append>(mut self, node: T) -> ElementBuilder { + self.root.append_node(node.into()); + self + } + + /// Appends an iterator of things implementing `Into` into the tree. + pub fn append_all, I: IntoIterator>(mut self, iter: I) -> ElementBuilder { + for node in iter { + self.root.append_node(node.into()); } self } @@ -920,7 +1072,6 @@ impl ElementBuilder { e.namespaces.set_parent(Rc::clone(&element.namespaces)); } } - element } } diff --git a/src/lib.rs b/src/lib.rs index 32a18147e4d3ee9fbe19c5acde39b44ae1b381b1..20be49d3734e8f4a82c4ab4a1a6fae847a934ec3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,4 +77,4 @@ mod namespace_set; pub use error::{Error, Result}; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; -pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; +pub use convert::IntoAttributeValue; From f0dd03d63327ce0cd48eccb94783fc11751b1f54 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 23 Dec 2018 15:59:13 +0100 Subject: [PATCH 092/111] Split `Node` off into its own module: `node.rs` --- src/convert.rs | 2 - src/element.rs | 193 +--------------------------------------------- src/lib.rs | 4 +- src/node.rs | 202 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 194 deletions(-) create mode 100644 src/node.rs diff --git a/src/convert.rs b/src/convert.rs index cabe86d5be2dff5c82d13848e0cda318ad65200a..534073298b3d94fb206c68800a4e750d1558f28b 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,7 +1,5 @@ //! A module which exports a few traits for converting types to elements and attributes. -use element::{Element, ElementBuilder}; - /// A trait for types which can be converted to an attribute value. pub trait IntoAttributeValue { /// Turns this into an attribute string, or None if it shouldn't be added. diff --git a/src/element.rs b/src/element.rs index 88a048d12dd52dc104f51f6444c206e38c8c0a56..5db7727bec18581a013e339defad144279ed1c6e 100644 --- a/src/element.rs +++ b/src/element.rs @@ -11,7 +11,7 @@ use error::{Error, Result}; use quick_xml::Reader as EventReader; use quick_xml::Writer as EventWriter; -use quick_xml::events::{Event, BytesStart, BytesEnd, BytesText, BytesDecl}; +use quick_xml::events::{Event, BytesStart, BytesEnd, BytesDecl}; use std::io::BufRead; @@ -21,6 +21,7 @@ use std::slice; use convert::IntoAttributeValue; use namespace_set::NamespaceSet; +use node::Node; /// helper function to escape a `&[u8]` and replace all /// xml special characters (<, >, &, ', ") with their corresponding @@ -68,196 +69,6 @@ pub fn escape(raw: &[u8]) -> Cow<[u8]> { } } -/// A node in an element tree. -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Node { - /// An `Element`. - Element(Element), - /// A text node. - Text(String), - /// A comment node. - Comment(String), -} - -impl Node { - /// Turns this into a reference to an `Element` if this is an element node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let elm = Node::Element("".parse().unwrap()); - /// let txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.as_element().unwrap().name(), "meow"); - /// assert_eq!(txt.as_element(), None); - /// ``` - pub fn as_element(&self) -> Option<&Element> { - match *self { - Node::Element(ref e) => Some(e), - Node::Text(_) => None, - Node::Comment(_) => None, - } - } - - /// Turns this into a mutable reference of an `Element` if this is an element node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let mut elm = Node::Element("".parse().unwrap()); - /// let mut txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.as_element_mut().unwrap().name(), "meow"); - /// assert_eq!(txt.as_element_mut(), None); - /// ``` - pub fn as_element_mut(&mut self) -> Option<&mut Element> { - match *self { - Node::Element(ref mut e) => Some(e), - Node::Text(_) => None, - Node::Comment(_) => None, - } - } - - /// Turns this into an `Element`, consuming self, if this is an element node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let elm = Node::Element("".parse().unwrap()); - /// let txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.into_element().unwrap().name(), "meow"); - /// assert_eq!(txt.into_element(), None); - /// ``` - pub fn into_element(self) -> Option { - match self { - Node::Element(e) => Some(e), - Node::Text(_) => None, - Node::Comment(_) => None, - } - } - - /// Turns this into an `&str` if this is a text node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let elm = Node::Element("".parse().unwrap()); - /// let txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.as_text(), None); - /// assert_eq!(txt.as_text().unwrap(), "meow"); - /// ``` - pub fn as_text(&self) -> Option<&str> { - match *self { - Node::Element(_) => None, - Node::Text(ref s) => Some(s), - Node::Comment(_) => None, - } - } - - /// Turns this into an `&mut String` if this is a text node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let mut elm = Node::Element("".parse().unwrap()); - /// let mut txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.as_text_mut(), None); - /// { - /// let text_mut = txt.as_text_mut().unwrap(); - /// assert_eq!(text_mut, "meow"); - /// text_mut.push_str("zies"); - /// assert_eq!(text_mut, "meowzies"); - /// } - /// assert_eq!(txt.as_text().unwrap(), "meowzies"); - /// ``` - pub fn as_text_mut(&mut self) -> Option<&mut String> { - match *self { - Node::Element(_) => None, - Node::Text(ref mut s) => Some(s), - Node::Comment(_) => None, - } - } - - /// Turns this into an `String`, consuming self, if this is a text node. - /// Else this returns `None`. - /// - /// # Examples - /// - /// ```rust - /// use minidom::Node; - /// - /// let elm = Node::Element("".parse().unwrap()); - /// let txt = Node::Text("meow".to_owned()); - /// - /// assert_eq!(elm.into_text(), None); - /// assert_eq!(txt.into_text().unwrap(), "meow"); - /// ``` - pub fn into_text(self) -> Option { - match self { - Node::Element(_) => None, - Node::Text(s) => Some(s), - Node::Comment(_) => None, - } - } - - fn write_to_inner(&self, writer: &mut EventWriter) -> Result<()>{ - match *self { - Node::Element(ref elmt) => elmt.write_to_inner(writer)?, - Node::Text(ref s) => { - writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; - () - }, - Node::Comment(ref s) => { - writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; - () - }, - }; - - Ok(()) - } -} - -impl From for Node { - fn from(elm: Element) -> Node { - Node::Element(elm) - } -} - -impl From for Node { - fn from(s: String) -> Node { - Node::Text(s) - } -} - -impl<'a> From<&'a str> for Node { - fn from(s: &'a str) -> Node { - Node::Text(s.to_owned()) - } -} - -impl From for Node { - fn from(builder: ElementBuilder) -> Node { - Node::Element(builder.build()) - } -} #[derive(Clone, PartialEq, Eq, Debug)] /// A struct representing a DOM Element. diff --git a/src/lib.rs b/src/lib.rs index 20be49d3734e8f4a82c4ab4a1a6fae847a934ec3..5ea7e7063617163753b02f6550d957037be0fa13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,10 +71,12 @@ extern crate failure; pub mod error; pub mod element; pub mod convert; +pub mod node; mod namespace_set; #[cfg(test)] mod tests; pub use error::{Error, Result}; -pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; +pub use element::{Element, Children, ChildrenMut, ElementBuilder}; +pub use node::Node; pub use convert::IntoAttributeValue; diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 0000000000000000000000000000000000000000..7fe5ddadc70ca316d807c87dc4c1936c4dd6a833 --- /dev/null +++ b/src/node.rs @@ -0,0 +1,202 @@ +//! Provides the `Node` struct, which represents a node in the DOM. + +use std::io::Write; + +use quick_xml::Writer as EventWriter; +use quick_xml::events::{Event, BytesText}; + +use error::Result; + +use element::{Element, ElementBuilder}; + +/// A node in an element tree. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Node { + /// An `Element`. + Element(Element), + /// A text node. + Text(String), + /// A comment node. + Comment(String), +} + +impl Node { + /// Turns this into a reference to an `Element` if this is an element node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_element().unwrap().name(), "meow"); + /// assert_eq!(txt.as_element(), None); + /// ``` + pub fn as_element(&self) -> Option<&Element> { + match *self { + Node::Element(ref e) => Some(e), + Node::Text(_) => None, + Node::Comment(_) => None, + } + } + + /// Turns this into a mutable reference of an `Element` if this is an element node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let mut elm = Node::Element("".parse().unwrap()); + /// let mut txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_element_mut().unwrap().name(), "meow"); + /// assert_eq!(txt.as_element_mut(), None); + /// ``` + pub fn as_element_mut(&mut self) -> Option<&mut Element> { + match *self { + Node::Element(ref mut e) => Some(e), + Node::Text(_) => None, + Node::Comment(_) => None, + } + } + + /// Turns this into an `Element`, consuming self, if this is an element node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.into_element().unwrap().name(), "meow"); + /// assert_eq!(txt.into_element(), None); + /// ``` + pub fn into_element(self) -> Option { + match self { + Node::Element(e) => Some(e), + Node::Text(_) => None, + Node::Comment(_) => None, + } + } + + /// Turns this into an `&str` if this is a text node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_text(), None); + /// assert_eq!(txt.as_text().unwrap(), "meow"); + /// ``` + pub fn as_text(&self) -> Option<&str> { + match *self { + Node::Element(_) => None, + Node::Text(ref s) => Some(s), + Node::Comment(_) => None, + } + } + + /// Turns this into an `&mut String` if this is a text node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let mut elm = Node::Element("".parse().unwrap()); + /// let mut txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.as_text_mut(), None); + /// { + /// let text_mut = txt.as_text_mut().unwrap(); + /// assert_eq!(text_mut, "meow"); + /// text_mut.push_str("zies"); + /// assert_eq!(text_mut, "meowzies"); + /// } + /// assert_eq!(txt.as_text().unwrap(), "meowzies"); + /// ``` + pub fn as_text_mut(&mut self) -> Option<&mut String> { + match *self { + Node::Element(_) => None, + Node::Text(ref mut s) => Some(s), + Node::Comment(_) => None, + } + } + + /// Turns this into an `String`, consuming self, if this is a text node. + /// Else this returns `None`. + /// + /// # Examples + /// + /// ```rust + /// use minidom::Node; + /// + /// let elm = Node::Element("".parse().unwrap()); + /// let txt = Node::Text("meow".to_owned()); + /// + /// assert_eq!(elm.into_text(), None); + /// assert_eq!(txt.into_text().unwrap(), "meow"); + /// ``` + pub fn into_text(self) -> Option { + match self { + Node::Element(_) => None, + Node::Text(s) => Some(s), + Node::Comment(_) => None, + } + } + + #[doc(hidden)] + pub(crate) fn write_to_inner(&self, writer: &mut EventWriter) -> Result<()>{ + match *self { + Node::Element(ref elmt) => elmt.write_to_inner(writer)?, + Node::Text(ref s) => { + writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; + () + }, + Node::Comment(ref s) => { + writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; + () + }, + }; + + Ok(()) + } +} + +impl From for Node { + fn from(elm: Element) -> Node { + Node::Element(elm) + } +} + +impl From for Node { + fn from(s: String) -> Node { + Node::Text(s) + } +} + +impl<'a> From<&'a str> for Node { + fn from(s: &'a str) -> Node { + Node::Text(s.to_owned()) + } +} + +impl From for Node { + fn from(builder: ElementBuilder) -> Node { + Node::Element(builder.build()) + } +} From f68826057b094969e1ab1a45bfca0991bf2b1f11 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 21 Feb 2019 21:06:23 +0100 Subject: [PATCH 093/111] Fix clippy lints --- src/convert.rs | 2 +- src/element.rs | 4 ++-- src/namespace_set.rs | 6 +++--- src/node.rs | 4 +--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 534073298b3d94fb206c68800a4e750d1558f28b..7c6d314e56df62c13a3c6c9e62c6491859a14c54 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -44,7 +44,7 @@ impl<'a> IntoAttributeValue for &'a str { impl IntoAttributeValue for Option { fn into_attribute_value(self) -> Option { - self.and_then(|t| t.into_attribute_value()) + self.and_then(IntoAttributeValue::into_attribute_value) } } diff --git a/src/element.rs b/src/element.rs index 5db7727bec18581a013e339defad144279ed1c6e..a94c1cec998b0586d88a95e262adaba2637729dd 100644 --- a/src/element.rs +++ b/src/element.rs @@ -102,8 +102,8 @@ impl Element { Element { prefix, name, namespaces: Rc::new(namespaces.into()), - attributes: attributes, - children: children, + attributes, + children, } } diff --git a/src/namespace_set.rs b/src/namespace_set.rs index 767106280929bbccea5d4dbd4ce7c8b1e966b142..57d3156dc5883f75ce06faa70aa683892604e5b4 100644 --- a/src/namespace_set.rs +++ b/src/namespace_set.rs @@ -58,7 +58,7 @@ impl From, String>> for NamespaceSet { fn from(namespaces: BTreeMap, String>) -> Self { NamespaceSet { parent: RefCell::new(None), - namespaces: namespaces, + namespaces, } } } @@ -79,7 +79,7 @@ impl From for NamespaceSet { NamespaceSet { parent: RefCell::new(None), - namespaces: namespaces, + namespaces, } } } @@ -92,7 +92,7 @@ impl From<(Option, String)> for NamespaceSet { NamespaceSet { parent: RefCell::new(None), - namespaces: namespaces, + namespaces, } } } diff --git a/src/node.rs b/src/node.rs index 7fe5ddadc70ca316d807c87dc4c1936c4dd6a833..b136dbdce5bc44c002f8c6588415838aa006a8a3 100644 --- a/src/node.rs +++ b/src/node.rs @@ -165,13 +165,11 @@ impl Node { Node::Element(ref elmt) => elmt.write_to_inner(writer)?, Node::Text(ref s) => { writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; - () }, Node::Comment(ref s) => { writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; - () }, - }; + } Ok(()) } From 149681954625097448616ac88c277a5ba77b6638 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 27 Feb 2019 18:04:16 +0100 Subject: [PATCH 094/111] Implement a nicer Debug for NamespaceSet The existing one was quite hard to parse visually, this makes it a lot easier to understand what is what. --- src/namespace_set.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/namespace_set.rs b/src/namespace_set.rs index 57d3156dc5883f75ce06faa70aa683892604e5b4..a5401d1e5c94b6f599dab68efd6c7c022353f863 100644 --- a/src/namespace_set.rs +++ b/src/namespace_set.rs @@ -1,9 +1,10 @@ use std::collections::BTreeMap; use std::cell::RefCell; +use std::fmt; use std::rc::Rc; -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct NamespaceSet { parent: RefCell>>, namespaces: BTreeMap, String>, @@ -18,6 +19,19 @@ impl Default for NamespaceSet { } } +impl fmt::Debug for NamespaceSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "NamespaceSet(")?; + for (prefix, namespace) in &self.namespaces { + write!(f, "xmlns{}={:?}, ", match prefix { + None => String::new(), + Some(prefix) => format!(":{}", prefix), + }, namespace)?; + } + write!(f, "parent: {:?})", *self.parent.borrow()) + } +} + impl NamespaceSet { pub fn declared_ns(&self) -> &BTreeMap, String> { &self.namespaces @@ -107,7 +121,6 @@ impl From<(String, String)> for NamespaceSet { #[cfg(test)] mod tests { use super::*; - use std::rc::Rc; #[test] fn get_has() { @@ -147,4 +160,11 @@ mod tests { } } + #[test] + fn debug_looks_correct() { + let parent = NamespaceSet::from("http://www.w3.org/2000/svg".to_owned()); + let namespaces = NamespaceSet::from(("xhtml".to_owned(), "http://www.w3.org/1999/xhtml".to_owned())); + namespaces.set_parent(Rc::new(parent)); + assert_eq!(format!("{:?}", namespaces), "NamespaceSet(xmlns:xhtml=\"http://www.w3.org/1999/xhtml\", parent: Some(NamespaceSet(xmlns=\"http://www.w3.org/2000/svg\", parent: None)))"); + } } From aacfcc58b9c7a44bfa9932740bf5bd3781718cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 12 Jun 2019 18:22:51 +0200 Subject: [PATCH 095/111] Change unnecessary mut variable to default immutable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/element.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/element.rs b/src/element.rs index a94c1cec998b0586d88a95e262adaba2637729dd..47c8de96ba5589a5899e4ec9f15ef27aa1e370e4 100644 --- a/src/element.rs +++ b/src/element.rs @@ -347,14 +347,14 @@ impl Element { Event::Text(s) => { let text = s.unescape_and_decode(reader)?; if text != "" { - let mut current_elem = stack.last_mut().unwrap(); + let current_elem = stack.last_mut().unwrap(); current_elem.append_text_node(text); } }, Event::CData(s) => { let text = reader.decode(&s).into_owned(); if text != "" { - let mut current_elem = stack.last_mut().unwrap(); + let current_elem = stack.last_mut().unwrap(); current_elem.append_text_node(text); } }, From 769821141b94c295d035c85d9e813f3ecfcac030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 12 Jun 2019 18:23:32 +0200 Subject: [PATCH 096/111] Update quick-xml dependency to 0.14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fa66cfe377938713b8b89f75ae8a726ce78a861d..1ffa9b7921766ed93090e186c0bdc00108caf704 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.13" +quick-xml = "0.14" failure = "0.1.1" failure_derive = "0.1.1" From 019e700ab46797cae00c54603c1ba124bd64cd8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 12 Jun 2019 18:42:20 +0200 Subject: [PATCH 097/111] CHANGELOG: Add missing 0.10.0 entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc1fadc82edd186ee157db2277ec5d78ba611390..020f3f39cfbc0479da7a60a3ad306efd3692b1c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +Version 0.10.0, released 2018-10-21: + * Changes + * Update quick-xml to 0.13 + * Update doc to reflect switch from xml-rs to quick-xml. Version 0.9.1, released 2018-05-29: * Fixes * Lumi fixed CDATA handling, minidom will not unescape CDATA bodies anymore. From c5c330d77a9b9d92b7a40964c8e995b6ab1ddf8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 12 Jun 2019 18:43:42 +0200 Subject: [PATCH 098/111] Prepare for 0.11.0 release MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- CHANGELOG.md | 9 +++++++++ Cargo.toml | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 020f3f39cfbc0479da7a60a3ad306efd3692b1c9..65a72f9cb32445a0bee253077da633bc1c330f08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +Version 0.11.0, released 2019-06-14: + * Breaking + * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` + * Fixes + * Remote unused `mut` attribute on variable + * Changes + * Update quick-xml to 0.14 + * Split Node into its own module + * Nicer Debug implementation for NamespaceSet Version 0.10.0, released 2018-10-21: * Changes * Update quick-xml to 0.13 diff --git a/Cargo.toml b/Cargo.toml index 1ffa9b7921766ed93090e186c0bdc00108caf704..231949e761f9b8c3e16819627780aefc1c5bb4f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.10.0" +version = "0.11.0" authors = [ "lumi ", "Emmanuel Gil Peyrot ", From 1f96d82cb8a6fc5d4a185263ff674cff8372f631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 27 Jul 2019 13:14:37 +0200 Subject: [PATCH 099/111] Remove trailing whitespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/namespace_set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/namespace_set.rs b/src/namespace_set.rs index a5401d1e5c94b6f599dab68efd6c7c022353f863..163a87304aadb1320e4ff4731f2a8f66614d97d6 100644 --- a/src/namespace_set.rs +++ b/src/namespace_set.rs @@ -36,7 +36,7 @@ impl NamespaceSet { pub fn declared_ns(&self) -> &BTreeMap, String> { &self.namespaces } - + pub fn get(&self, prefix: &Option) -> Option { match self.namespaces.get(prefix) { Some(ns) => Some(ns.clone()), From aa8018d9999496d287e70039cf0241a4b4e7ff38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 21 Aug 2019 10:32:32 +0200 Subject: [PATCH 100/111] Update to quick-xml 0.15 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65a72f9cb32445a0bee253077da633bc1c330f08..f0dd53d3f9ea9714828896560a9a771b39c10e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version XXX, released YYY: + * Changes + * Update to quick-xml 0.15 Version 0.11.0, released 2019-06-14: * Breaking * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` diff --git a/Cargo.toml b/Cargo.toml index 231949e761f9b8c3e16819627780aefc1c5bb4f6..3c13bd807a65d57688f69944412d7ade58656741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.14" +quick-xml = "0.15" failure = "0.1.1" failure_derive = "0.1.1" From a91252c861ee61a390aac0a7907ae34f4482ea41 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 22 Aug 2019 18:04:47 +0200 Subject: [PATCH 101/111] Make comments optional. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a default "comments" feature to transform comments into errors when unset. This is so that XMPP implementations don’t have to care about comments, as they can’t happen in the stream. --- CHANGELOG.md | 1 + Cargo.toml | 4 ++++ src/element.rs | 11 ++++++++++- src/error.rs | 5 +++++ src/node.rs | 8 ++++++++ src/tests.rs | 4 ++++ 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0dd53d3f9ea9714828896560a9a771b39c10e40..4dc9482e07e49389541adfa7b1f551533ab5e086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version XXX, released YYY: * Changes * Update to quick-xml 0.15 + * Add a default "comments" feature to transform comments into errors when unset. Version 0.11.0, released 2019-06-14: * Breaking * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` diff --git a/Cargo.toml b/Cargo.toml index 3c13bd807a65d57688f69944412d7ade58656741..9ed80d59cf63cd19fe50694ffefe1e93f6d99e2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,7 @@ gitlab = { repository = "lumi/minidom-rs" } quick-xml = "0.15" failure = "0.1.1" failure_derive = "0.1.1" + +[features] +default = ["comments"] +comments = [] diff --git a/src/element.rs b/src/element.rs index 47c8de96ba5589a5899e4ec9f15ef27aa1e370e4..cc9f8e11d4409cdc3d05033687d59c2c15de937f 100644 --- a/src/element.rs +++ b/src/element.rs @@ -283,9 +283,14 @@ impl Element { Event::Eof => { return Err(Error::EndOfDocument); }, + #[cfg(not(feature = "comments"))] + Event::Comment { .. } => { + return Err(Error::CommentsDisabled); + } + #[cfg(feature = "comments")] + Event::Comment { .. } => (), Event::Text { .. } | Event::End { .. } | - Event::Comment { .. } | Event::CData { .. } | Event::Decl { .. } | Event::PI { .. } | @@ -361,6 +366,9 @@ impl Element { Event::Eof => { break; }, + #[cfg(not(feature = "comments"))] + Event::Comment(_) => return Err(Error::CommentsDisabled), + #[cfg(feature = "comments")] Event::Comment(s) => { let comment = reader.decode(&s).into_owned(); if comment != "" { @@ -569,6 +577,7 @@ impl Element { /// /// elem.append_comment_node("comment"); /// ``` + #[cfg(feature = "comments")] pub fn append_comment_node>(&mut self, child: S) { self.children.push(Node::Comment(child.into())); } diff --git a/src/error.rs b/src/error.rs index b2ba473cd88c987cef13a6c334f6a0a2dada33b2..773f214fa573a5434213c176ce6d03ee38b9a3b4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,6 +28,11 @@ pub enum Error { /// An error which is returned when an elemet's name contains more than one colon #[fail(display = "the XML element is invalid")] InvalidElement, + + /// An error which is returned when a comment is to be parsed by minidom + #[cfg(not(comments))] + #[fail(display = "a comment has been found even though comments are disabled by feature")] + CommentsDisabled, } impl From<::quick_xml::Error> for Error { diff --git a/src/node.rs b/src/node.rs index b136dbdce5bc44c002f8c6588415838aa006a8a3..56069d09f80f7a0960111a91914371389a00f717 100644 --- a/src/node.rs +++ b/src/node.rs @@ -16,6 +16,7 @@ pub enum Node { Element(Element), /// A text node. Text(String), + #[cfg(feature = "comments")] /// A comment node. Comment(String), } @@ -39,6 +40,7 @@ impl Node { match *self { Node::Element(ref e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -61,6 +63,7 @@ impl Node { match *self { Node::Element(ref mut e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -83,6 +86,7 @@ impl Node { match self { Node::Element(e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -105,6 +109,7 @@ impl Node { match *self { Node::Element(_) => None, Node::Text(ref s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -133,6 +138,7 @@ impl Node { match *self { Node::Element(_) => None, Node::Text(ref mut s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -155,6 +161,7 @@ impl Node { match self { Node::Element(_) => None, Node::Text(s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -166,6 +173,7 @@ impl Node { Node::Text(ref s) => { writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; }, + #[cfg(feature = "comments")] Node::Comment(ref s) => { writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; }, diff --git a/src/tests.rs b/src/tests.rs index 2acc97052ecc63c81761d0447dc9ae2c8eb6e0d0..1ff37e6ccfb816ea41f89d4f67cfd08da8b641ca 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -27,8 +27,10 @@ fn build_test_tree() -> Element { root } +#[cfg(feature = "comments")] const COMMENT_TEST_STRING: &'static str = r#""#; +#[cfg(feature = "comments")] fn build_comment_test_tree() -> Element { let mut root = Element::builder("root").build(); root.append_comment_node("This is a child."); @@ -211,12 +213,14 @@ fn namespace_inherited_prefixed2() { assert_eq!(child.ns(), Some("jabber:client".to_owned())); } +#[cfg(feature = "comments")] #[test] fn read_comments() { let mut reader = Reader::from_str(COMMENT_TEST_STRING); assert_eq!(Element::from_reader(&mut reader).unwrap(), build_comment_test_tree()); } +#[cfg(feature = "comments")] #[test] fn write_comments() { let root = build_comment_test_tree(); From b4035d122738e107d7f95c230ec3260026867e79 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 5 Sep 2019 20:06:17 +0200 Subject: [PATCH 102/111] Remove failure. --- Cargo.toml | 2 -- src/error.rs | 30 +++++++++++++++++++----------- src/lib.rs | 2 -- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9ed80d59cf63cd19fe50694ffefe1e93f6d99e2f..4e1632aa3cba811fcfbe9bfcf15587f94516b9cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,6 @@ gitlab = { repository = "lumi/minidom-rs" } [dependencies] quick-xml = "0.15" -failure = "0.1.1" -failure_derive = "0.1.1" [features] default = ["comments"] diff --git a/src/error.rs b/src/error.rs index 773f214fa573a5434213c176ce6d03ee38b9a3b4..fc96a08bd1b69638cf3a5c0580a80e02d7b48bac 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,38 +3,46 @@ use std::convert::From; /// Our main error type. -#[derive(Debug, Fail)] +#[derive(Debug)] pub enum Error { /// An error from quick_xml. - #[fail(display = "XML error: {}", _0)] - XmlError(#[cause] ::quick_xml::Error), + XmlError(::quick_xml::Error), /// An UTF-8 conversion error. - #[fail(display = "UTF-8 error: {}", _0)] - Utf8Error(#[cause] ::std::str::Utf8Error), + Utf8Error(::std::str::Utf8Error), /// An I/O error, from std::io. - #[fail(display = "IO error: {}", _0)] - IoError(#[cause] ::std::io::Error), + IoError(::std::io::Error), /// An error which is returned when the end of the document was reached prematurely. - #[fail(display = "the end of the document has been reached prematurely")] EndOfDocument, /// An error which is returned when an element is closed when it shouldn't be - #[fail(display = "the XML is invalid, an element was wrongly closed")] InvalidElementClosed, /// An error which is returned when an elemet's name contains more than one colon - #[fail(display = "the XML element is invalid")] InvalidElement, /// An error which is returned when a comment is to be parsed by minidom #[cfg(not(comments))] - #[fail(display = "a comment has been found even though comments are disabled by feature")] CommentsDisabled, } +impl std::fmt::Display for Error { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Error::XmlError(e) => write!(fmt, "XML error: {}", e), + Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e), + Error::IoError(e) => write!(fmt, "IO error: {}", e), + Error::EndOfDocument => write!(fmt, "the end of the document has been reached prematurely"), + Error::InvalidElementClosed => write!(fmt, "the XML is invalid, an element was wrongly closed"), + Error::InvalidElement => write!(fmt, "the XML element is invalid"), + #[cfg(not(comments))] + Error::CommentsDisabled => write!(fmt, "a comment has been found even though comments are disabled by feature"), + } + } +} + impl From<::quick_xml::Error> for Error { fn from(err: ::quick_xml::Error) -> Error { Error::XmlError(err) diff --git a/src/lib.rs b/src/lib.rs index 5ea7e7063617163753b02f6550d957037be0fa13..cf17cdac5cdcb07c112de154701fc382abeecc07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,8 +65,6 @@ //! ``` extern crate quick_xml; -extern crate failure; -#[macro_use] extern crate failure_derive; pub mod error; pub mod element; From 17f902b503c30c398db9612e98dc059d8e7e1846 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 5 Sep 2019 20:25:04 +0200 Subject: [PATCH 103/111] Update to quick-xml 0.16. --- Cargo.toml | 2 +- src/element.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e1632aa3cba811fcfbe9bfcf15587f94516b9cd..76b4b4dccbcc9f8ddfa3e5bd57e6c21beada66ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.15" +quick-xml = "0.16" [features] default = ["comments"] diff --git a/src/element.rs b/src/element.rs index cc9f8e11d4409cdc3d05033687d59c2c15de937f..68d445e27641b4ae73a28f3b3725ad7772eff03e 100644 --- a/src/element.rs +++ b/src/element.rs @@ -357,7 +357,7 @@ impl Element { } }, Event::CData(s) => { - let text = reader.decode(&s).into_owned(); + let text = reader.decode(&s)?.to_owned(); if text != "" { let current_elem = stack.last_mut().unwrap(); current_elem.append_text_node(text); @@ -370,7 +370,7 @@ impl Element { Event::Comment(_) => return Err(Error::CommentsDisabled), #[cfg(feature = "comments")] Event::Comment(s) => { - let comment = reader.decode(&s).into_owned(); + let comment = reader.decode(&s)?.to_owned(); if comment != "" { let current_elem = stack.last_mut().unwrap(); current_elem.append_comment_node(comment); From 0db94e554d551477b6ed4f61ed9dcc8d2dea43d0 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 6 Sep 2019 11:33:32 +0200 Subject: [PATCH 104/111] Prepare for 0.11.1 release. --- CHANGELOG.md | 5 +++-- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dc9482e07e49389541adfa7b1f551533ab5e086..72468faa528a78d3b5b33411a34ef48cf50e838a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -Version XXX, released YYY: +Version 0.11.1, released 2019-09-06: * Changes - * Update to quick-xml 0.15 + * Update to quick-xml 0.16 * Add a default "comments" feature to transform comments into errors when unset. + * Remove the mostly-unused failure dependency, to dramatically reduce compilation times. Version 0.11.0, released 2019-06-14: * Breaking * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` diff --git a/Cargo.toml b/Cargo.toml index 76b4b4dccbcc9f8ddfa3e5bd57e6c21beada66ec..204d729e3fd245b169dbd19a1650a0b8713d34b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.11.0" +version = "0.11.1" authors = [ "lumi ", "Emmanuel Gil Peyrot ", From 11a5c49470d1e3d8875a7ccefc0cbd34855eafe7 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 7 Sep 2019 16:02:40 +0200 Subject: [PATCH 105/111] Implement std::error::Error for Error. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was removed in 0.11.1 with the removal of failure, but is used by people so let’s reintroduce it. The cause of an XmlError is pending on this PR from quick-xml: https://github.com/tafia/quick-xml/pull/170 Fixes #15. Fixes #18. --- src/error.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/error.rs b/src/error.rs index fc96a08bd1b69638cf3a5c0580a80e02d7b48bac..579cee405aca8c68af1aa1f97dd0ff02329774df 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ //! Provides an error type for this crate. use std::convert::From; +use std::error::Error as StdError; /// Our main error type. #[derive(Debug)] @@ -28,6 +29,23 @@ pub enum Error { CommentsDisabled, } +impl StdError for Error { + fn cause(&self) -> Option<&dyn StdError> { + match self { + // TODO: return Some(e) for this case after the merge of + // https://github.com/tafia/quick-xml/pull/170 + Error::XmlError(_e) => None, + Error::Utf8Error(e) => Some(e), + Error::IoError(e) => Some(e), + Error::EndOfDocument => None, + Error::InvalidElementClosed => None, + Error::InvalidElement => None, + #[cfg(not(comments))] + Error::CommentsDisabled => None, + } + } +} + impl std::fmt::Display for Error { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { match self { From 5aee776f667af655d05076273cb21bb8d3214129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 13 Sep 2019 03:07:02 +0200 Subject: [PATCH 106/111] Update to edition 2018 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- CHANGELOG.md | 3 +++ Cargo.toml | 1 + src/element.rs | 11 +++++------ src/node.rs | 7 +++---- src/tests.rs | 10 ++++------ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72468faa528a78d3b5b33411a34ef48cf50e838a..f9d098f876e6b9b3c1cfd58658fe83135d370a77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version XXX, released YYY: + * Changes + * Update edition to 2018 Version 0.11.1, released 2019-09-06: * Changes * Update to quick-xml 0.16 diff --git a/Cargo.toml b/Cargo.toml index 204d729e3fd245b169dbd19a1650a0b8713d34b9..292b2e01c2eb7f6177bba6114ebc4c5237477cf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ documentation = "https://docs.rs/minidom" readme = "README.md" keywords = ["xml"] license = "MIT" +edition = "2018" [badges] gitlab = { repository = "lumi/minidom-rs" } diff --git a/src/element.rs b/src/element.rs index 68d445e27641b4ae73a28f3b3725ad7772eff03e..08ccc709ecad30354909a673d5eefd9321c349a8 100644 --- a/src/element.rs +++ b/src/element.rs @@ -1,5 +1,10 @@ //! Provides an `Element` type, which represents DOM nodes, and a builder to create them with. +use crate::convert::IntoAttributeValue; +use crate::error::{Error, Result}; +use crate::namespace_set::NamespaceSet; +use crate::node::Node; + use std::io:: Write; use std::collections::{btree_map, BTreeMap}; @@ -7,8 +12,6 @@ use std::str; use std::rc::Rc; use std::borrow::Cow; -use error::{Error, Result}; - use quick_xml::Reader as EventReader; use quick_xml::Writer as EventWriter; use quick_xml::events::{Event, BytesStart, BytesEnd, BytesDecl}; @@ -19,10 +22,6 @@ use std::str::FromStr; use std::slice; -use convert::IntoAttributeValue; -use namespace_set::NamespaceSet; -use node::Node; - /// helper function to escape a `&[u8]` and replace all /// xml special characters (<, >, &, ', ") with their corresponding /// xml escaped value. diff --git a/src/node.rs b/src/node.rs index 56069d09f80f7a0960111a91914371389a00f717..e0d9a9ebbcf5dd3c2d68a879664491b042c07568 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,14 +1,13 @@ //! Provides the `Node` struct, which represents a node in the DOM. +use crate::element::{Element, ElementBuilder}; +use crate::error::Result; + use std::io::Write; use quick_xml::Writer as EventWriter; use quick_xml::events::{Event, BytesText}; -use error::Result; - -use element::{Element, ElementBuilder}; - /// A node in an element tree. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Node { diff --git a/src/tests.rs b/src/tests.rs index 1ff37e6ccfb816ea41f89d4f67cfd08da8b641ca..046d8e5c817ac79febb208709c4f96b7671f8297 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,9 +1,7 @@ -use std::iter::Iterator; +use crate::element::Element; use quick_xml::Reader; -use element::Element; - const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { @@ -234,12 +232,12 @@ fn write_comments() { #[test] fn xml_error() { match "".parse::() { - Err(::error::Error::XmlError(_)) => (), + Err(crate::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } match "() { - Err(::error::Error::XmlError(_)) => (), + Err(crate::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } } @@ -247,7 +245,7 @@ fn xml_error() { #[test] fn invalid_element_error() { match "".parse::() { - Err(::error::Error::InvalidElement) => (), + Err(crate::error::Error::InvalidElement) => (), err => panic!("No or wrong error: {:?}", err) } } From 7a0ba4f39d247dff4e2a496a68d910d2f957f9d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 13 Sep 2019 03:10:06 +0200 Subject: [PATCH 107/111] CI: Import .gitlab-ci.yml from xmpp-rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- .gitlab-ci.yml | 56 +++++++++++++++++++++++++++++++++++++++++++------- CHANGELOG.md | 3 ++- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c02e04ce55fee669db09a49bfa08f4aea2d5cfbc..bc899d9fd2c40d66af66f5c346e972f69850a6d8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,10 +1,52 @@ -image: "pitkley/rust:nightly" +stages: + - build + - test -before_script: -- apt-get update -yqq -- apt-get install -yqq --no-install-recommends build-essential +variables: + FEATURES: "" + RUST_BACKTRACE: "full" -test:cargo: +.stable: + image: rust:latest + cache: + key: stable + paths: + - target/ + +.nightly: + image: rustlang/rust:nightly + cache: + key: nightly + paths: + - target/ + +.build: + stage: build script: - - rustc --version && cargo --version - - cargo test --verbose --jobs 1 --release + - cargo build --verbose --no-default-features --features=$FEATURES + +.test: + stage: test + script: + - cargo test --verbose --no-default-features --features=$FEATURES + +rust-latest-build: + extends: + - .build + - .stable + +rust-nightly-build: + extends: + - .build + - .nightly + + +rust-latest-test: + extends: + - .test + - .stable + +rust-nightly-test: + extends: + - .test + - .nightly diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d098f876e6b9b3c1cfd58658fe83135d370a77..275c0e69884a0b3638462e6353d571fe7390aad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ Version XXX, released YYY: * Changes * Update edition to 2018 + * Fixes + * Update old CI configuration with newer Rust images Version 0.11.1, released 2019-09-06: * Changes * Update to quick-xml 0.16 * Add a default "comments" feature to transform comments into errors when unset. - * Remove the mostly-unused failure dependency, to dramatically reduce compilation times. Version 0.11.0, released 2019-06-14: * Breaking * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` From 6ce9496fc301376b89aa244f96d8514cb40b9d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 13 Sep 2019 03:14:00 +0200 Subject: [PATCH 108/111] CI: Add jobs for 'comments' feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- .gitlab-ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bc899d9fd2c40d66af66f5c346e972f69850a6d8..efd57f1e923b2926fbf8cf39bbd5ef0727d7a1e9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -50,3 +50,13 @@ rust-nightly-test: extends: - .test - .nightly + +rust-latest-build with features=comments: + extends: rust-latest-build + variables: + FEATURES: "comments" + +rust-latest-test with features=comments: + extends: rust-latest-test + variables: + FEATURES: "comments" From 1d5276ccbdc36e280af6af4da2f2148df0a22385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 15 Oct 2019 23:50:35 +0200 Subject: [PATCH 109/111] Update quick-xml dep to 0.17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 292b2e01c2eb7f6177bba6114ebc4c5237477cf4..e46db5a346cca27dacd00c280ea00690bdc7016a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ edition = "2018" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.16" +quick-xml = "0.17" [features] default = ["comments"] From 1ee6609c97b9cca6a890b1ee6f5ea057dc2c1e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 15 Oct 2019 23:50:57 +0200 Subject: [PATCH 110/111] Re-export quick_xml dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cf17cdac5cdcb07c112de154701fc382abeecc07..c7f43b23010d693ef3df8a92e2969cb8c608aa41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ //! minidom = "*" //! ``` -extern crate quick_xml; +pub use quick_xml; pub mod error; pub mod element; From 57b2c6a13558c7da11a344047e738bc6ab86d203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 18 Oct 2019 14:27:53 +0200 Subject: [PATCH 111/111] Prepare for merge: Move all minidom-rs files into minidom-rs/ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- .gitignore => minidom-rs/.gitignore | 0 .gitlab-ci.yml => minidom-rs/.gitlab-ci.yml | 0 CHANGELOG.md => minidom-rs/CHANGELOG.md | 0 Cargo.toml => minidom-rs/Cargo.toml | 0 LICENSE => minidom-rs/LICENSE | 0 README.md => minidom-rs/README.md | 0 {examples => minidom-rs/examples}/articles.rs | 0 {src => minidom-rs/src}/convert.rs | 0 {src => minidom-rs/src}/element.rs | 0 {src => minidom-rs/src}/error.rs | 0 {src => minidom-rs/src}/lib.rs | 0 {src => minidom-rs/src}/namespace_set.rs | 0 {src => minidom-rs/src}/node.rs | 0 {src => minidom-rs/src}/tests.rs | 0 14 files changed, 0 insertions(+), 0 deletions(-) rename .gitignore => minidom-rs/.gitignore (100%) rename .gitlab-ci.yml => minidom-rs/.gitlab-ci.yml (100%) rename CHANGELOG.md => minidom-rs/CHANGELOG.md (100%) rename Cargo.toml => minidom-rs/Cargo.toml (100%) rename LICENSE => minidom-rs/LICENSE (100%) rename README.md => minidom-rs/README.md (100%) rename {examples => minidom-rs/examples}/articles.rs (100%) rename {src => minidom-rs/src}/convert.rs (100%) rename {src => minidom-rs/src}/element.rs (100%) rename {src => minidom-rs/src}/error.rs (100%) rename {src => minidom-rs/src}/lib.rs (100%) rename {src => minidom-rs/src}/namespace_set.rs (100%) rename {src => minidom-rs/src}/node.rs (100%) rename {src => minidom-rs/src}/tests.rs (100%) diff --git a/.gitignore b/minidom-rs/.gitignore similarity index 100% rename from .gitignore rename to minidom-rs/.gitignore diff --git a/.gitlab-ci.yml b/minidom-rs/.gitlab-ci.yml similarity index 100% rename from .gitlab-ci.yml rename to minidom-rs/.gitlab-ci.yml diff --git a/CHANGELOG.md b/minidom-rs/CHANGELOG.md similarity index 100% rename from CHANGELOG.md rename to minidom-rs/CHANGELOG.md diff --git a/Cargo.toml b/minidom-rs/Cargo.toml similarity index 100% rename from Cargo.toml rename to minidom-rs/Cargo.toml diff --git a/LICENSE b/minidom-rs/LICENSE similarity index 100% rename from LICENSE rename to minidom-rs/LICENSE diff --git a/README.md b/minidom-rs/README.md similarity index 100% rename from README.md rename to minidom-rs/README.md diff --git a/examples/articles.rs b/minidom-rs/examples/articles.rs similarity index 100% rename from examples/articles.rs rename to minidom-rs/examples/articles.rs diff --git a/src/convert.rs b/minidom-rs/src/convert.rs similarity index 100% rename from src/convert.rs rename to minidom-rs/src/convert.rs diff --git a/src/element.rs b/minidom-rs/src/element.rs similarity index 100% rename from src/element.rs rename to minidom-rs/src/element.rs diff --git a/src/error.rs b/minidom-rs/src/error.rs similarity index 100% rename from src/error.rs rename to minidom-rs/src/error.rs diff --git a/src/lib.rs b/minidom-rs/src/lib.rs similarity index 100% rename from src/lib.rs rename to minidom-rs/src/lib.rs diff --git a/src/namespace_set.rs b/minidom-rs/src/namespace_set.rs similarity index 100% rename from src/namespace_set.rs rename to minidom-rs/src/namespace_set.rs diff --git a/src/node.rs b/minidom-rs/src/node.rs similarity index 100% rename from src/node.rs rename to minidom-rs/src/node.rs diff --git a/src/tests.rs b/minidom-rs/src/tests.rs similarity index 100% rename from src/tests.rs rename to minidom-rs/src/tests.rs