roxmltree-0.7.3/.gitignore010064400017500001750000000000631333633536300137620ustar0000000000000000target **/*.rs.bk /.idea /roxmltree.iml Cargo.lock roxmltree-0.7.3/.travis.yml010064400017500001750000000000531325676444000141050ustar0000000000000000language: rust rust: - 1.18.0 - stable roxmltree-0.7.3/CHANGELOG.md010064400017500001750000000065401356310533200136020ustar0000000000000000# Change Log All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ## [0.7.3] - 2019-11-14 ### Changed - Use unconstrained lifetimes for the `attribute` functions. By [myrrlyn](https://github.com/myrrlyn). ## [0.7.2] - 2019-11-07 ### Changed - Use longer lifetimes in `Document::root_element`. By [myrrlyn](https://github.com/myrrlyn). ## [0.7.1] - 2019-09-14 ### Changed - Update `xmlparser`. ## [0.7.0] - 2019-08-06 ### Added - `Node::prev_sibling_element` and `Node::next_sibling_element`. ### Changed - **(breaking)** `Node::ancestors` includes the current node now. - `Attribute` is cloneable now. ### Fixed - Namespaces resolving with equal URI's. ### Removed - `Node::resolve_tag_name_prefix`. ## [0.6.1] - 2019-06-18 ### Fixed - Namespace resolving. ## [0.6.0] - 2019-03-03 ### Added - `Error::UnknownNamespace`. ### Fixed - Unknown namespace prefixes will cause an error now. ## [0.5.0] - 2019-02-27 ### Added - `Node::range`. - `Node::attribute_node`. - `Attribute::range`. - `Attribute::value_range`. ### Changed - Rename `text_pos_from` into `text_pos_at`. ### Removed - `Node::pos`. Use `Node::range` instead. - `Node::node_pos`. Use `doc.text_pos_at(node.range().start)` instead. - `Node::attribute_pos`. - `Node::attribute_value_pos`. - `Attribute::pos`. Use `Attribute::range` instead. - `Attribute::value_pos`. Use `Attribute::value_range` instead. ## [0.4.1] - 2019-01-02 ### Changed - Use longer lifetimes in return types. By [tmiasko](https://github.com/tmiasko). ## [0.4.0] - 2018-12-13 ### Added - `Error::pos()`. ## [0.3.0] - 2018-10-29 ### Changed - Store text nodes as `&str` when possible. On an XML with a lot of simple text can be ~2x faster. - `Document` no longer implements `PartialEq`. ### Fixed - Entity and character references resolving inside a text. ## [0.2.0] - 2018-10-08 ### Added - `Error::EntityReferenceLoop`. - Nested entity references support. ### Changed - `Attribute::namespace` will return `Option` now. - `ExpandedName::namespace` will return `Option` now. - `Namespace::name` will return `Option` now. - `Node::resolve_tag_name_prefix` will return `Option` now. - `Node::lookup_namespace_uri` accepts `Option<&str>` and not `&str` now. - Performance optimizations. ### Removed - `ExpandedName::has_namespace`. `ExpandedName::namespace` will return `Option` now. - `Error::NestedEntityReference`. [Unreleased]: https://github.com/RazrFalcon/roxmltree/compare/v0.7.3...HEAD [0.7.3]: https://github.com/RazrFalcon/roxmltree/compare/v0.7.2...v0.7.3 [0.7.2]: https://github.com/RazrFalcon/roxmltree/compare/v0.7.1...v0.7.2 [0.7.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.7.0...v0.7.1 [0.7.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.6.1...v0.7.0 [0.6.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.6.0...v0.6.1 [0.6.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.5.0...v0.6.0 [0.5.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.4.1...v0.5.0 [0.4.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.4.0...v0.4.1 [0.4.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.3.0...v0.4.0 [0.3.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.2.0...v0.3.0 [0.2.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.1.0...v0.2.0 roxmltree-0.7.3/Cargo.toml.orig010064400017500001750000000012471356310533700146640ustar0000000000000000[package] name = "roxmltree" # When updating version, also modify html_root_url in the lib.rs version = "0.7.3" authors = ["Evgeniy Reizner "] keywords = ["xml", "parser", "tree", "dom"] categories = ["parser-implementations"] license = "MIT/Apache-2.0" description = "Represent an XML as a read-only tree." repository = "https://github.com/RazrFalcon/roxmltree" documentation = "https://docs.rs/roxmltree/" readme = "README.md" [workspace] members = ["benches"] exclude = ["testing-tools"] [badges] travis-ci = { repository = "RazrFalcon/roxmltree" } [lib] doctest = true [dependencies] xmlparser = "0.10" [dev-dependencies] pretty_assertions = "0.5" roxmltree-0.7.3/Cargo.toml0000644000000020730000000000000111230ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "roxmltree" version = "0.7.3" authors = ["Evgeniy Reizner "] description = "Represent an XML as a read-only tree." documentation = "https://docs.rs/roxmltree/" readme = "README.md" keywords = ["xml", "parser", "tree", "dom"] categories = ["parser-implementations"] license = "MIT/Apache-2.0" repository = "https://github.com/RazrFalcon/roxmltree" [lib] doctest = true [dependencies.xmlparser] version = "0.10" [dev-dependencies.pretty_assertions] version = "0.5" [badges.travis-ci] repository = "RazrFalcon/roxmltree" roxmltree-0.7.3/LICENSE-APACHE010064400017500001750000000251371322753041200137160ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. roxmltree-0.7.3/LICENSE-MIT010064400017500001750000000020721326664711400134320ustar0000000000000000The MIT License (MIT) Copyright (c) 2018 Reizner Evgeniy 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. roxmltree-0.7.3/README.md010064400017500001750000000212221354734771600132620ustar0000000000000000# roxmltree [![Build Status](https://travis-ci.org/RazrFalcon/roxmltree.svg?branch=master)](https://travis-ci.org/RazrFalcon/roxmltree) [![Crates.io](https://img.shields.io/crates/v/roxmltree.svg)](https://crates.io/crates/roxmltree) [![Documentation](https://docs.rs/roxmltree/badge.svg)](https://docs.rs/roxmltree) [![Rust 1.18+](https://img.shields.io/badge/rust-1.18+-orange.svg)](https://www.rust-lang.org) Represents an [XML 1.0](https://www.w3.org/TR/xml/) document as a read-only tree. ```rust // Find element by id. let doc = roxmltree::Document::parse("").unwrap(); let elem = doc.descendants().find(|n| n.attribute("id") == Some("rect1")).unwrap(); assert!(elem.has_tag_name("rect")); ``` ## Why read-only? Because in some cases all you need is to retrieve some data from an XML document. And for such cases, we can make a lot of optimizations. As for *roxmltree*, it's fast not only because it's read-only, but also because it uses [xmlparser], which is many times faster than [xml-rs]. See the [Performance](#performance) section for details. ## Parsing behavior Sadly, XML can be parsed in many different ways. *roxmltree* tries to mimic the behavior of Python's [lxml](https://lxml.de/). But unlike *lxml*, *roxmltree* does support comments outside the root element. Fo more details see [docs/parsing.md](https://github.com/RazrFalcon/roxmltree/blob/master/docs/parsing.md). ## Alternatives | Feature/Crate | roxmltree | [libxml2] | [xmltree] | [elementtree] | [sxd-document] | [treexml] | | ------------------------------- | :--------------: | :-----------------: | :--------------: | :--------------: | :--------------: | :--------------: | | Element namespace resolving | ✔ | ✔ | ✔ | ✔ | ~1 | | | Attribute namespace resolving | ✔ | ✔ | | | ✔ | | | [Entity references] | ✔ | ✔ | ⚠ | ⚠ | ⚠ | ⚠ | | [Character references] | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | | [Attribute-Value normalization] | ✔ | ✔ | | | | | | Comments | ✔ | ✔ | | | ✔ | | | Processing instructions | ✔ | ✔ | ⚠ | | ✔ | | | UTF-8 BOM | ✔ | ✔ | ⚠ | ⚠ | ⚠ | ⚠ | | Non UTF-8 input | | ✔ | | | | | | Complete DTD support | | ✔ | | | | | | Position preserving2 | ✔ | ✔ | | | | | | HTML support | | ✔ | | | | | | Tree modification | | ✔ | ✔ | ✔ | ✔ | ✔ | | Writing | | ✔ | ✔ | ✔ | ✔ | ✔ | | No **unsafe** | ✔ | | ✔ | ~3 | | ✔ | | Language | Rust | C | Rust | Rust | Rust | Rust | | Size overhead4 | **~73KiB** | ~1.4MiB5 | ~80KiB | ~96KiB | ~135KiB | ~110KiB | | Dependencies | **1** | ?5 | 2 | 18 | 2 | 14 | | Tested version | 0.7.0 | 2.9.8 | 0.8.0 | 0.5.0 | 0.3.0 | 0.7.0 | | License | MIT / Apache-2.0 | MIT | MIT | BSD-3-Clause | MIT | MIT | Legend: - ✔ - supported - ⚠ - parsing error - ~ - partial - *nothing* - not supported Notes: 1. No default namespace propagation. 2. *roxmltree* keeps all node and attribute positions in the original document, so you can easily retrieve it if you need it. See [examples/print_pos.rs](examples/print_pos.rs) for details. 3. In the `string_cache` crate. 4. Binary size overhead according to [cargo-bloat](https://github.com/RazrFalcon/cargo-bloat). 5. Depends on build flags. [Entity references]: https://www.w3.org/TR/REC-xml/#dt-entref [Character references]: https://www.w3.org/TR/REC-xml/#NT-CharRef [Attribute-Value Normalization]: https://www.w3.org/TR/REC-xml/#AVNormalize [libxml2]: http://xmlsoft.org/ [xmltree]: https://crates.io/crates/xmltree [elementtree]: https://crates.io/crates/elementtree [treexml]: https://crates.io/crates/treexml [sxd-document]: https://crates.io/crates/sxd-document ## Performance ```text test large_roxmltree ... bench: 5,147,695 ns/iter (+/- 75,762) test large_sdx_document ... bench: 9,452,579 ns/iter (+/- 37,298) test large_xmltree ... bench: 28,383,408 ns/iter (+/- 46,793) test large_treexml ... bench: 28,992,626 ns/iter (+/- 122,244) test large_elementtree ... bench: 29,991,730 ns/iter (+/- 58,134) test medium_roxmltree ... bench: 935,837 ns/iter (+/- 1,001) test medium_sdx_document ... bench: 3,619,042 ns/iter (+/- 8,863) test medium_xmltree ... bench: 10,181,629 ns/iter (+/- 13,994) test medium_treexml ... bench: 10,338,760 ns/iter (+/- 11,040) test medium_elementtree ... bench: 10,840,762 ns/iter (+/- 16,162) test tiny_roxmltree ... bench: 6,346 ns/iter (+/- 129) test tiny_sdx_document ... bench: 27,464 ns/iter (+/- 91) test tiny_xmltree ... bench: 43,838 ns/iter (+/- 107) test tiny_treexml ... bench: 44,794 ns/iter (+/- 263) test tiny_elementtree ... bench: 45,431 ns/iter (+/- 175) ``` *roxmltree* uses [xmlparser] internally, while *sdx-document* uses its own implementation and *xmltree*, *elementtree* and *treexml* use the [xml-rs] crate. Here is a comparison between *xmlparser* and *xml-rs*: ```text test large_xmlparser ... bench: 1,961,131 ns/iter (+/- 66,145) test large_xmlrs ... bench: 25,582,284 ns/iter (+/- 76,500) test medium_xmlparser ... bench: 451,180 ns/iter (+/- 1,523) test medium_xmlrs ... bench: 9,368,598 ns/iter (+/- 10,995) test tiny_xmlparser ... bench: 3,756 ns/iter (+/- 32) test tiny_xmlrs ... bench: 39,293 ns/iter (+/- 63) ``` You can try it yourself by running `cargo bench` in the `benches` dir. Notes: - Since all libraries have a different XML support, benchmarking is a bit pointless. - Tree crates may use different *xml-rs* crate versions. - We do not bench the libxml2, because `xmlReadFile()` will parse only an XML structure, without attributes normalization and stuff. So it's hard to compare. And we have to use a separate benchmark utility. [xml-rs]: https://crates.io/crates/xml-rs [xmlparser]: https://crates.io/crates/xmlparser ## Safety - This library must not panic. Any panic should be considered a critical bug and reported. - This library forbids `unsafe` code. ## Non-goals - Complete XML support. - Tree modification and writing. - XPath/XQuery. ## API This library uses Rust's idiomatic API based on iterators. In case you are more familiar with browser/JS DOM APIs - you can check out [tests/dom-api.rs](tests/dom-api.rs) to see how it can be converted into a Rust one. ## License Licensed under either of - [Apache License v2.0](LICENSE-APACHE) - [MIT license](LICENSE-MIT) at your option. ## Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. roxmltree-0.7.3/docs/parsing.md010064400017500001750000000040751341272017600147110ustar0000000000000000# roxmltree parsing strategy XML parsing is hard. Everyone knows that. But the other problem is that it can be represented in very different ways: - You can preserve comment or ignore them completely or partially. - You can represent text data as a separated node or embed it into the element node. - You can keep CDATA as a separated node or merge it into the text node. - You can preserve XML declaration or ignore it completely. - ... and many more. This document explains how *roxmltree* parses and represents the XML document. ## XML declaration [XML declaration](https://www.w3.org/TR/xml/#NT-XMLDecl) is completely ignored. Mostly because it doesn't contain any valuable information for us. - `version` is expected to be `1.*`. Otherwise an error will occur. - `encoding` is irrelevant since we are parsing only valid UTF-8 strings. - And no one really follow the `standalone` constraints. ## DTD Only `ENTITY` objects will be resolved. Everything else will be ignored at the moment. ```xml text'> ]> &a; ``` will be parsed into: ```xml text

text ``` Were `p` is an element, not a text. ## Comments All comment will be preserved. ## Processing instructions All processing instructions will be preserved. ## Whitespaces All whitespaces inside the root element will be preserved. ```xml

text

``` it will be parsed as `\n␣␣␣␣text\n`. Same goes to an escaped one: ```xml

text

``` it will be parsed as `␣␣text␣␣`. ## CDATA CDATA will be embedded to a text node: ```xml

t x

``` it will be parsed as `te xt`. ## Text Text will be unescaped. All entity references will be resolved. ```xml ]>

&b;

``` it will be parsed as `Some text`. ## Attribute-Value Normalization [Attribute-Value Normalization](https://www.w3.org/TR/xml/#AVNormalize) works as explained in the spec. ## Namespaces resolving *roxmltree* has a complete support for XML namespaces. roxmltree-0.7.3/examples/ast.rs010064400017500001750000000011651353713714500147520ustar0000000000000000extern crate roxmltree; use std::fs; use std::env; use std::io::Read; use std::process; fn main() { let args: Vec<_> = env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example ast -- input.xml"); process::exit(1); } let text = load_file(&args[1]); match roxmltree::Document::parse(&text) { Ok(doc) => print!("{:?}", doc), Err(e) => println!("Error: {}.", e), } } fn load_file(path: &str) -> String { let mut file = fs::File::open(&path).unwrap(); let mut text = String::new(); file.read_to_string(&mut text).unwrap(); text } roxmltree-0.7.3/examples/print_pos.rs010064400017500001750000000015451342512720400161710ustar0000000000000000extern crate roxmltree; use std::fs; use std::env; use std::io::Read; use std::process; fn main() { let args: Vec<_> = env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example print_pos -- input.xml"); process::exit(1); } let text = load_file(&args[1]); let doc = match roxmltree::Document::parse(&text) { Ok(doc) => doc, Err(e) => { println!("Error: {}.", e); return; }, }; // TODO: finish for node in doc.descendants() { if node.is_element() { println!("{:?} at {}", node.tag_name(), doc.text_pos_at(node.range().start)); } } } fn load_file(path: &str) -> String { let mut file = fs::File::open(&path).unwrap(); let mut text = String::new(); file.read_to_string(&mut text).unwrap(); text } roxmltree-0.7.3/examples/stats.rs010064400017500001750000000034161334724306500153200ustar0000000000000000extern crate roxmltree; use std::collections::HashSet; use std::fs; use std::env; use std::io::Read; use std::process; fn main() { let args: Vec<_> = env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example stats -- input.xml"); process::exit(1); } let text = load_file(&args[1]); let doc = match roxmltree::Document::parse(&text) { Ok(v) => v, Err(e) => { println!("Error: {}.", e); process::exit(1); } }; println!("Elements count: {}", doc.root().descendants().filter(|n| n.is_element()).count()); let attrs_count: usize = doc.root().descendants().map(|n| n.attributes().len()).sum(); println!("Attributes count: {}", attrs_count); let ns_count: usize = doc.root().descendants().map(|n| n.namespaces().len()).sum(); println!("Namespaces count: {}", ns_count); let mut uris = HashSet::new(); for node in doc.root().descendants() { for ns in node.namespaces() { uris.insert((ns.name().unwrap_or("\"\"").to_string(), ns.uri().to_string())); } } println!("Unique namespaces count: {}", uris.len()); if !uris.is_empty() { println!("Unique namespaces:"); for (key, value) in uris { println!(" {:?}: {}", key, value); } } println!("Comments count: {}", doc.root().descendants().filter(|n| n.is_comment()).count()); println!("Comments:"); for node in doc.root().descendants().filter(|n| n.is_comment()) { println!("{:?}", node.text().unwrap()); } } fn load_file(path: &str) -> String { let mut file = fs::File::open(&path).unwrap(); let mut text = String::new(); file.read_to_string(&mut text).unwrap(); text } roxmltree-0.7.3/src/lib.rs010064400017500001750000001031311356310534400136710ustar0000000000000000/*! Represent an [XML 1.0](https://www.w3.org/TR/xml/) document as a read-only tree. The root point of the documentations is [`Document::parse`]. You can find more details in the [README] and [parsing doc]. The tree structure itself is a heavily modified License: ISC. [`Document::parse`]: struct.Document.html#method.parse [README]: https://github.com/RazrFalcon/roxmltree/blob/master/README.md [parsing doc]: https://github.com/RazrFalcon/roxmltree/blob/master/docs/parsing.md */ #![doc(html_root_url = "https://docs.rs/roxmltree/0.7.3")] #![forbid(unsafe_code)] #![warn(missing_docs)] extern crate xmlparser; use std::borrow::Cow; use std::fmt; use std::ops::Deref; use std::rc::Rc; pub use xmlparser::TextPos; mod parse; pub use parse::*; /// The URI. pub const NS_XML_URI: &str = "http://www.w3.org/XML/1998/namespace"; /// The URI. pub const NS_XMLNS_URI: &str = "http://www.w3.org/2000/xmlns/"; type Range = std::ops::Range; /// An XML tree container. /// /// A tree consists of [`Nodes`]. /// There are no separate structs for each node type. /// So you should check the current node type yourself via [`Node::node_type()`]. /// There are only [5 types](enum.NodeType.html): /// Root, Element, PI, Comment and Text. /// /// As you can see there are no XML declaration and CDATA types. /// The XML declaration is basically skipped, since it doesn't contain any /// valuable information (we support only UTF-8 anyway). /// And CDATA will be converted into a Text node as is, without /// any preprocessing (you can read more about it /// [here](https://github.com/RazrFalcon/roxmltree/blob/master/docs/parsing.md)). /// /// Also, the Text node data can be accessed from the text node itself or from /// the parent element via [`Node::text()`] or [`Node::tail()`]. /// /// [`Nodes`]: struct.Node.html /// [`Node::node_type()`]: struct.Node.html#method.node_type /// [`Node::text()`]: struct.Node.html#method.text /// [`Node::tail()`]: struct.Node.html#method.tail pub struct Document<'input> { /// An original data. /// /// Required for `text_pos` methods. text: &'input str, nodes: Vec>, attrs: Vec>, namespaces: Namespaces<'input>, } impl<'input> Document<'input> { /// Returns the root node. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// assert!(doc.root().is_root()); /// assert!(doc.root().first_child().unwrap().has_tag_name("e")); /// ``` #[inline] pub fn root<'a>(&'a self) -> Node<'a, 'input> { Node { id: NodeId(0), d: &self.nodes[0], doc: self } } /// Returns the root element of the document. /// /// Unlike `root`, will return a first element node. /// /// The root element always exists. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// assert!(doc.root_element().has_tag_name("e")); /// ``` #[inline] pub fn root_element<'a>(&'a self) -> Node<'a, 'input> { // `expect` is safe, because the `Document` is guarantee to have at least one element. self.root().first_element_child().expect("XML documents must contain a root element") } /// Returns an iterator over document's descendant nodes. /// /// Shorthand for `doc.root().descendants()`. #[inline] pub fn descendants(&self) -> Descendants { self.root().descendants() } /// Calculates `TextPos` in the original document from position in bytes. /// /// **Note:** this operation is expensive. /// /// # Examples /// /// ``` /// use roxmltree::*; /// /// let doc = Document::parse("\ /// /// " /// ).unwrap(); /// /// assert_eq!(doc.text_pos_at(10), TextPos::new(1, 11)); /// assert_eq!(doc.text_pos_at(9999), TextPos::new(2, 5)); /// ``` #[inline] pub fn text_pos_at(&self, pos: usize) -> TextPos { xmlparser::Stream::from(self.text).gen_text_pos_from(pos) } } impl<'input> fmt::Debug for Document<'input> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { if !self.root().has_children() { return write!(f, "Document []"); } macro_rules! writeln_indented { ($depth:expr, $f:expr, $fmt:expr) => { for _ in 0..$depth { write!($f, " ")?; } writeln!($f, $fmt)?; }; ($depth:expr, $f:expr, $fmt:expr, $($arg:tt)*) => { for _ in 0..$depth { write!($f, " ")?; } writeln!($f, $fmt, $($arg)*)?; }; } fn print_vec(prefix: &str, data: &[T], depth: usize, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { if data.is_empty() { return Ok(()); } writeln_indented!(depth, f, "{}: [", prefix); for v in data { writeln_indented!(depth + 1, f, "{:?}", v); } writeln_indented!(depth, f, "]"); Ok(()) } fn print_children(parent: Node, depth: usize, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { for child in parent.children() { if child.is_element() { writeln_indented!(depth, f, "Element {{"); writeln_indented!(depth, f, " tag_name: {:?}", child.tag_name()); print_vec("attributes", child.attributes(), depth + 1, f)?; print_vec("namespaces", child.namespaces(), depth + 1, f)?; if child.has_children() { writeln_indented!(depth, f, " children: ["); print_children(child, depth + 2, f)?; writeln_indented!(depth, f, " ]"); } writeln_indented!(depth, f, "}}"); } else { writeln_indented!(depth, f, "{:?}", child); } } Ok(()) } writeln!(f, "Document [")?; print_children(self.root(), 1, f)?; writeln!(f, "]")?; Ok(()) } } /// List of supported node types. #[derive(Clone, Copy, PartialEq, Debug)] pub enum NodeType { /// The root node of the `Document`. Root, /// An element node. /// /// Only an element can have tag name and attributes. Element, /// A processing instruction. PI, /// A comment node. Comment, /// A text node. Text, } /// A processing instruction. #[derive(Clone, Copy, PartialEq, Debug)] #[allow(missing_docs)] pub struct PI<'input> { pub target: &'input str, pub value: Option<&'input str>, } /// Node ID. /// /// Index into a `Tree`-internal `Vec`. #[derive(Clone, Copy, PartialEq)] struct NodeId(usize); enum NodeKind<'input> { Root, Element { tag_name: ExpandedNameOwned<'input>, attributes: Range, namespaces: Range, }, PI(PI<'input>), Comment(&'input str), Text(Cow<'input, str>), } struct NodeData<'input> { parent: Option, prev_sibling: Option, next_sibling: Option, children: Option<(NodeId, NodeId)>, kind: NodeKind<'input>, range: Range, } /// An attribute. #[derive(Clone)] pub struct Attribute<'input> { name: ExpandedNameOwned<'input>, value: Cow<'input, str>, range: Range, value_range: Range, } impl<'input> Attribute<'input> { /// Returns attribute's namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes()[0].namespace(), None); /// assert_eq!(doc.root_element().attributes()[1].namespace(), Some("http://www.w3.org")); /// ``` #[inline] pub fn namespace(&self) -> Option<&str> { self.name.ns.as_ref().map(Uri::as_str) } /// Returns attribute's name. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes()[0].name(), "a"); /// assert_eq!(doc.root_element().attributes()[1].name(), "a"); /// ``` #[inline] pub fn name(&self) -> &str { self.name.name } /// Returns attribute's value. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes()[0].value(), "b"); /// assert_eq!(doc.root_element().attributes()[1].value(), "c"); /// ``` #[inline] pub fn value(&self) -> &str { &self.value } /// Returns attribute's name range in bytes in the original document. /// /// You can calculate a human-readable text position via [Document::text_pos_at]. /// /// ```text /// /// ^ /// ``` /// /// [Document::text_pos_at]: struct.Document.html#method.text_pos_at #[inline] pub fn range(&self) -> Range { self.range.clone() } /// Returns attribute's value range in bytes in the original document. /// /// You can calculate a human-readable text position via [Document::text_pos_at]. /// /// ```text /// /// ^ /// ``` /// /// [Document::text_pos_at]: struct.Document.html#method.text_pos_at #[inline] pub fn value_range(&self) -> Range { self.value_range.clone() } } impl<'input> PartialEq for Attribute<'input> { #[inline] fn eq(&self, other: &Attribute<'input>) -> bool { self.name == other.name && self.value == other.value } } impl<'input> fmt::Debug for Attribute<'input> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "Attribute {{ name: {:?}, value: {:?} }}", self.name, self.value) } } /// A namespace. /// /// Contains URI and *prefix* pair. #[derive(Clone, PartialEq, Debug)] pub struct Namespace<'input> { name: Option<&'input str>, uri: Uri, } impl<'input> Namespace<'input> { /// Returns namespace name/prefix. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces()[0].name(), Some("n")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces()[0].name(), None); /// ``` #[inline] pub fn name(&self) -> Option<&str> { self.name } /// Returns namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces()[0].uri(), "http://www.w3.org"); /// ``` #[inline] pub fn uri(&self) -> &str { self.uri.as_str() } } struct Namespaces<'input>(Vec>); impl<'input> Namespaces<'input> { #[inline] fn push_ns(&mut self, name: Option<&'input str>, uri: String) { debug_assert_ne!(name, Some("")); self.0.push(Namespace { name, uri: Uri::new(uri), }); } #[inline] fn xml_uri(&self) -> Uri { self[0].uri.clone() } #[inline] fn exists(&self, start: usize, prefix: Option<&str>) -> bool { self[start..].iter().any(|ns| ns.name == prefix) } } impl<'input> Deref for Namespaces<'input> { type Target = Vec>; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } struct Uri(Rc); impl Uri { #[inline] fn new(text: String) -> Self { Uri(Rc::new(text)) } #[inline] fn as_str(&self) -> &str { self.0.as_str() } } impl Clone for Uri { #[inline] fn clone(&self) -> Self { Uri(Rc::clone(&self.0)) } } impl PartialEq for Uri { #[inline] fn eq(&self, other: &Uri) -> bool { self.0.as_str() == other.0.as_str() } } impl fmt::Debug for Uri { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{:?}", self.0) } } #[derive(Clone, PartialEq)] struct ExpandedNameOwned<'input> { ns: Option, prefix: &'input str, // Used only for closing tags matching during parsing. name: &'input str, } impl<'input> ExpandedNameOwned<'input> { #[inline] fn as_ref(&self) -> ExpandedName { ExpandedName { uri: self.ns.as_ref().map(Uri::as_str), name: self.name, } } } impl<'input> fmt::Debug for ExpandedNameOwned<'input> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.ns { Some(ref ns) => write!(f, "{{{}}}{}", ns.as_str(), self.name), None => write!(f, "{}", self.name), } } } /// An expanded name. /// /// Contains an namespace URI and name pair. #[derive(Clone, Copy, PartialEq)] pub struct ExpandedName<'input> { uri: Option<&'input str>, name: &'input str, } impl<'input> ExpandedName<'input> { /// Returns a namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().tag_name().namespace(), Some("http://www.w3.org")); /// ``` #[inline] pub fn namespace(&self) -> Option<&'input str> { self.uri } /// Returns a local name. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().tag_name().name(), "e"); /// ``` #[inline] pub fn name(&self) -> &'input str { self.name } } impl<'input> fmt::Debug for ExpandedName<'input> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.namespace() { Some(ns) => write!(f, "{{{}}}{}", ns, self.name), None => write!(f, "{}", self.name), } } } impl<'input> From<&'input str> for ExpandedName<'input> { #[inline] fn from(v: &'input str) -> Self { ExpandedName { uri: None, name: v, } } } impl<'input> From<(&'input str, &'input str)> for ExpandedName<'input> { #[inline] fn from(v: (&'input str, &'input str)) -> Self { ExpandedName { uri: Some(v.0), name: v.1, } } } /// A node. #[derive(Clone, Copy)] pub struct Node<'a, 'input: 'a> { /// Node ID. id: NodeId, /// Tree containing the node. doc: &'a Document<'input>, d: &'a NodeData<'input>, } impl<'a, 'input> Eq for Node<'a, 'input> {} impl<'a, 'input> PartialEq for Node<'a, 'input> { #[inline] fn eq(&self, other: &Self) -> bool { self.id == other.id && self.doc as *const _ == other.doc as *const _ && self.d as *const _ == other.d as *const _ } } impl<'a, 'input: 'a> Node<'a, 'input> { /// Returns node's type. #[inline] pub fn node_type(&self) -> NodeType { match self.d.kind { NodeKind::Root => NodeType::Root, NodeKind::Element { .. } => NodeType::Element, NodeKind::PI { .. } => NodeType::PI, NodeKind::Comment(_) => NodeType::Comment, NodeKind::Text(_) => NodeType::Text, } } /// Checks that node is a root node. #[inline] pub fn is_root(&self) -> bool { self.node_type() == NodeType::Root } /// Checks that node is an element node. #[inline] pub fn is_element(&self) -> bool { self.node_type() == NodeType::Element } /// Checks that node is a processing instruction node. #[inline] pub fn is_pi(&self) -> bool { self.node_type() == NodeType::PI } /// Checks that node is a comment node. #[inline] pub fn is_comment(&self) -> bool { self.node_type() == NodeType::Comment } /// Checks that node is a text node. #[inline] pub fn is_text(&self) -> bool { self.node_type() == NodeType::Text } /// Returns node's document. #[inline] pub fn document(&self) -> &'a Document<'input> { self.doc } /// Returns node's tag name. /// /// Returns an empty name with no namespace if the current node is not an element. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().tag_name().namespace(), Some("http://www.w3.org")); /// assert_eq!(doc.root_element().tag_name().name(), "e"); /// ``` #[inline] pub fn tag_name(&self) -> ExpandedName<'a> { match self.d.kind { NodeKind::Element { ref tag_name, .. } => tag_name.as_ref(), _ => "".into() } } /// Checks that node has a specified tag name. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert!(doc.root_element().has_tag_name("e")); /// assert!(doc.root_element().has_tag_name(("http://www.w3.org", "e"))); /// /// assert!(!doc.root_element().has_tag_name("b")); /// assert!(!doc.root_element().has_tag_name(("http://www.w4.org", "e"))); /// ``` pub fn has_tag_name<'n, N>(&self, name: N) -> bool where N: Into>, { let name = name.into(); match self.d.kind { NodeKind::Element { ref tag_name, .. } => { match name.namespace() { Some(_) => tag_name.as_ref() == name, None => tag_name.name == name.name, } } _ => false, } } /// Returns node's default namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().default_namespace(), Some("http://www.w3.org")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().default_namespace(), None); /// ``` pub fn default_namespace(&self) -> Option<&'a str> { self.namespaces().iter().find(|ns| ns.name.is_none()).map(|v| v.uri.as_str()) } /// Returns a prefix for a given namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().lookup_prefix("http://www.w3.org"), Some("n")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().lookup_prefix(""), Some("n")); /// ``` pub fn lookup_prefix(&self, uri: &str) -> Option<&'a str> { if uri == NS_XML_URI { return Some("xml"); } self.namespaces().iter().find(|ns| ns.uri.as_str() == uri).map(|v| v.name).unwrap_or(None) } /// Returns an URI for a given prefix. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().lookup_namespace_uri(Some("n")), Some("http://www.w3.org")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().lookup_namespace_uri(None), Some("http://www.w3.org")); /// ``` pub fn lookup_namespace_uri(&self, prefix: Option<&'a str>) -> Option<&'a str> { self.namespaces().iter().find(|ns| ns.name == prefix).map(|v| v.uri.as_str()) } /// Returns element's attribute value. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root_element().attribute("a"), Some("b")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attribute("a"), Some("b")); /// assert_eq!(doc.root_element().attribute(("http://www.w3.org", "a")), Some("c")); /// ``` pub fn attribute<'n, N>(&self, name: N) -> Option<&'a str> where N: Into>, { let name = name.into(); self.attributes().iter().find(|a| a.name.as_ref() == name).map(|a| a.value.as_ref()) } /// Returns element's attribute object. /// /// The same as [`attribute()`], but returns the `Attribute` itself instead of a value string. /// /// [`attribute()`]: struct.Node.html#method.attribute pub fn attribute_node<'n, N>(&self, name: N) -> Option<&'a Attribute<'input>> where N: Into>, { let name = name.into(); self.attributes().iter().find(|a| a.name.as_ref() == name) } /// Checks that element has a specified attribute. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert!(doc.root_element().has_attribute("a")); /// assert!(doc.root_element().has_attribute(("http://www.w3.org", "a"))); /// /// assert!(!doc.root_element().has_attribute("b")); /// assert!(!doc.root_element().has_attribute(("http://www.w4.org", "a"))); /// ``` pub fn has_attribute<'n, N>(&self, name: N) -> bool where N: Into>, { let name = name.into(); self.attributes().iter().any(|a| a.name.as_ref() == name) } /// Returns element's attributes. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes().len(), 2); /// ``` #[inline] pub fn attributes(&self) -> &'a [Attribute<'input>] { match self.d.kind { NodeKind::Element { ref attributes, .. } => &self.doc.attrs[attributes.clone()], _ => &[], } } /// Returns element's namespaces. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces().len(), 1); /// ``` #[inline] pub fn namespaces(&self) -> &'a [Namespace<'input>] { match self.d.kind { NodeKind::Element { ref namespaces, .. } => { &self.doc.namespaces[namespaces.clone()] } _ => &[], } } /// Returns node's text. /// /// - for an element will return a first text child /// - for a comment will return a self text /// - for a text node will return a self text /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("\ ///

/// text ///

/// ").unwrap(); /// /// assert_eq!(doc.root_element().text(), /// Some("\n text\n")); /// assert_eq!(doc.root_element().first_child().unwrap().text(), /// Some("\n text\n")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// /// assert_eq!(doc.root().first_child().unwrap().text(), Some(" comment ")); /// ``` #[inline] pub fn text(&self) -> Option<&'a str> { match self.d.kind { NodeKind::Element { .. } => { match self.first_child() { Some(child) if child.is_text() => { match self.doc.nodes[child.id.0].kind { NodeKind::Text(ref text) => Some(text), _ => None } } _ => None, } } NodeKind::Comment(text) => Some(text), NodeKind::Text(ref text) => Some(text), _ => None, } } /// Returns element's tail text. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("\ /// /// text1 ///

/// text2 /// /// ").unwrap(); /// /// let p = doc.descendants().find(|n| n.has_tag_name("p")).unwrap(); /// assert_eq!(p.tail(), Some("\n text2\n")); /// ``` #[inline] pub fn tail(&self) -> Option<&'a str> { if !self.is_element() { return None; } match self.next_sibling().map(|n| n.id) { Some(id) => { match self.doc.nodes[id.0].kind { NodeKind::Text(ref text) => Some(text), _ => None } } None => None, } } /// Returns node as Processing Instruction. #[inline] pub fn pi(&self) -> Option> { match self.d.kind { NodeKind::PI(pi) => Some(pi), _ => None, } } #[inline] fn gen_node(&self, id: NodeId) -> Node<'a, 'input> { Node { id, d: &self.doc.nodes[id.0], doc: self.doc } } /// Returns the parent of this node. #[inline] pub fn parent(&self) -> Option { self.d.parent.map(|id| self.gen_node(id)) } /// Returns the parent element of this node. pub fn parent_element(&self) -> Option { self.ancestors().skip(1).filter(|n| n.is_element()).nth(0) } /// Returns the previous sibling of this node. #[inline] pub fn prev_sibling(&self) -> Option { self.d.prev_sibling.map(|id| self.gen_node(id)) } /// Returns the previous sibling element of this node. pub fn prev_sibling_element(&self) -> Option { self.prev_siblings().filter(|n| n.is_element()).nth(0) } /// Returns the next sibling of this node. #[inline] pub fn next_sibling(&self) -> Option { self.d.next_sibling.map(|id| self.gen_node(id)) } /// Returns the next sibling element of this node. pub fn next_sibling_element(&self) -> Option { self.next_siblings().filter(|n| n.is_element()).nth(0) } /// Returns the first child of this node. #[inline] pub fn first_child(&self) -> Option { self.d.children.map(|(id, _)| self.gen_node(id)) } /// Returns the first element child of this node. pub fn first_element_child(&self) -> Option { self.children().filter(|n| n.is_element()).nth(0) } /// Returns the last child of this node. #[inline] pub fn last_child(&self) -> Option { self.d.children.map(|(_, id)| self.gen_node(id)) } /// Returns the last element child of this node. pub fn last_element_child(&self) -> Option { self.children().filter(|n| n.is_element()).last() } /// Returns true if this node has siblings. #[inline] pub fn has_siblings(&self) -> bool { self.d.prev_sibling.is_some() || self.d.next_sibling.is_some() } /// Returns true if this node has children. #[inline] pub fn has_children(&self) -> bool { self.d.children.is_some() } /// Returns an iterator over ancestor nodes starting at this node. #[inline] pub fn ancestors(&self) -> Ancestors<'a, 'input> { Ancestors(Some(*self)) } /// Returns an iterator over previous sibling nodes. #[inline] pub fn prev_siblings(&self) -> PrevSiblings<'a, 'input> { PrevSiblings(self.prev_sibling()) } /// Returns an iterator over next sibling nodes. #[inline] pub fn next_siblings(&self) -> NextSiblings<'a, 'input> { NextSiblings(self.next_sibling()) } /// Returns an iterator over first children nodes. #[inline] pub fn first_children(&self) -> FirstChildren<'a, 'input> { FirstChildren(self.first_child()) } /// Returns an iterator over last children nodes. #[inline] pub fn last_children(&self) -> LastChildren<'a, 'input> { LastChildren(self.last_child()) } /// Returns an iterator over children nodes. #[inline] pub fn children(&self) -> Children<'a, 'input> { Children { front: self.first_child(), back: self.last_child() } } /// Returns an iterator which traverses the subtree starting at this node. #[inline] pub fn traverse(&self) -> Traverse<'a, 'input> { Traverse { root: *self, edge: None } } /// Returns an iterator over this node and its descendants. #[inline] pub fn descendants(&self) -> Descendants<'a, 'input> { Descendants(self.traverse()) } /// Returns node's range in bytes in the original document. #[inline] pub fn range(&self) -> Range { self.d.range.clone() } } impl<'a, 'input: 'a> fmt::Debug for Node<'a, 'input> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.d.kind { NodeKind::Root => write!(f, "Root"), NodeKind::Element { .. } => { write!(f, "Element {{ tag_name: {:?}, attributes: {:?}, namespaces: {:?} }}", self.tag_name(), self.attributes(), self.namespaces()) } NodeKind::PI(pi) => { write!(f, "PI {{ target: {:?}, value: {:?} }}", pi.target, pi.value) } NodeKind::Comment(text) => write!(f, "Comment({:?})", text), NodeKind::Text(ref text) => write!(f, "Text({:?})", text), } } } macro_rules! axis_iterators { ($(#[$m:meta] $i:ident($f:path);)*) => { $( #[$m] #[derive(Clone)] pub struct $i<'a, 'input: 'a>(Option>); impl<'a, 'input: 'a> Iterator for $i<'a, 'input> { type Item = Node<'a, 'input>; #[inline] fn next(&mut self) -> Option { let node = self.0.take(); self.0 = node.as_ref().and_then($f); node } } )* }; } axis_iterators! { /// Iterator over ancestors. Ancestors(Node::parent); /// Iterator over previous siblings. PrevSiblings(Node::prev_sibling); /// Iterator over next siblings. NextSiblings(Node::next_sibling); /// Iterator over first children. FirstChildren(Node::first_child); /// Iterator over last children. LastChildren(Node::last_child); } /// Iterator over children. #[derive(Clone)] pub struct Children<'a, 'input: 'a> { front: Option>, back: Option>, } impl<'a, 'input: 'a> Iterator for Children<'a, 'input> { type Item = Node<'a, 'input>; #[inline] fn next(&mut self) -> Option { if self.front == self.back { let node = self.front.take(); self.back = None; node } else { let node = self.front.take(); self.front = node.as_ref().and_then(Node::next_sibling); node } } } impl<'a, 'input: 'a> DoubleEndedIterator for Children<'a, 'input> { #[inline] fn next_back(&mut self) -> Option { if self.back == self.front { let node = self.back.take(); self.front = None; node } else { let node = self.back.take(); self.back = node.as_ref().and_then(Node::prev_sibling); node } } } /// Open or close edge of a node. #[derive(Clone, Copy, PartialEq, Debug)] pub enum Edge<'a, 'input: 'a> { /// Open. Open(Node<'a, 'input>), /// Close. Close(Node<'a, 'input>), } /// Iterator which traverses a subtree. #[derive(Clone)] pub struct Traverse<'a, 'input: 'a> { root: Node<'a, 'input>, edge: Option>, } impl<'a, 'input: 'a> Iterator for Traverse<'a, 'input> { type Item = Edge<'a, 'input>; #[inline] fn next(&mut self) -> Option { match self.edge { Some(Edge::Open(node)) => { self.edge = Some(match node.first_child() { Some(first_child) => Edge::Open(first_child), None => Edge::Close(node), }); } Some(Edge::Close(node)) => { if node == self.root { self.edge = None; } else if let Some(next_sibling) = node.next_sibling() { self.edge = Some(Edge::Open(next_sibling)); } else { self.edge = node.parent().map(Edge::Close); } } None => { self.edge = Some(Edge::Open(self.root)); } } self.edge } } /// Iterator over a node and its descendants. #[derive(Clone)] pub struct Descendants<'a, 'input: 'a>(Traverse<'a, 'input>); impl<'a, 'input: 'a> Iterator for Descendants<'a, 'input> { type Item = Node<'a, 'input>; #[inline] fn next(&mut self) -> Option { for edge in &mut self.0 { if let Edge::Open(node) = edge { return Some(node); } } None } } roxmltree-0.7.3/src/parse.rs010064400017500001750000000760201352422202700142360ustar0000000000000000use std::borrow::{Cow, Borrow}; use std::error; use std::fmt; use std::mem; use std::str; use xmlparser::{ self, Reference, Stream, StrSpan, TextPos, }; use { NS_XML_URI, NS_XMLNS_URI, Attribute, Document, ExpandedNameOwned, Namespaces, NodeData, NodeId, NodeKind, PI, Range, Uri, }; const ENTITY_DEPTH: u8 = 10; /// A list of all possible errors. #[derive(Debug)] pub enum Error { /// The `xmlns:xml` attribute must have an URI. InvalidXmlPrefixUri(TextPos), /// Only the `xmlns:xml` attribute can have the URI. UnexpectedXmlUri(TextPos), /// The URI must not be declared. UnexpectedXmlnsUri(TextPos), /// `xmlns` can't be used as an element prefix. InvalidElementNamePrefix(TextPos), /// A namespace was already defined on this element. DuplicatedNamespace(String, TextPos), /// An unknown namespace. /// /// Indicates that an element or an attribute has an unknown qualified name prefix. /// /// The first value is a prefix. UnknownNamespace(String, TextPos), /// Incorrect tree structure. #[allow(missing_docs)] UnexpectedCloseTag { expected: String, actual: String, pos: TextPos }, /// Entity value starts with a close tag. /// /// Example: /// ```xml /// '> ]> /// &p; /// ``` UnexpectedEntityCloseTag(TextPos), /// A reference to an entity that was not defined in the DTD. UnknownEntityReference(String, TextPos), /// A possible entity reference loop. EntityReferenceLoop(TextPos), /// An element has a duplicated attributes. /// /// This also includes namespaces resolving. /// So an element like this will lead to an error. /// ```xml /// /// ``` DuplicatedAttribute(String, TextPos), /// The XML document must have at least one element. NoRootNode, /// Errors detected by the `xmlparser` crate. ParserError(xmlparser::Error), } impl Error { /// Returns the error position. #[inline] pub fn pos(&self) -> TextPos { match *self { Error::InvalidXmlPrefixUri(pos) => pos, Error::UnexpectedXmlUri(pos) => pos, Error::UnexpectedXmlnsUri(pos) => pos, Error::InvalidElementNamePrefix(pos) => pos, Error::DuplicatedNamespace(ref _name, pos) => pos, Error::UnknownNamespace(ref _name, pos) => pos, Error::UnexpectedCloseTag { pos, .. } => pos, Error::UnexpectedEntityCloseTag(pos) => pos, Error::UnknownEntityReference(ref _name, pos) => pos, Error::EntityReferenceLoop(pos) => pos, Error::DuplicatedAttribute(ref _name, pos) => pos, Error::ParserError(ref err) => err.pos(), _ => TextPos::new(1, 1) } } } impl From for Error { #[inline] fn from(e: xmlparser::Error) -> Self { Error::ParserError(e) } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::InvalidXmlPrefixUri(pos) => { write!(f, "'xml' namespace prefix mapped to wrong URI at {}", pos) } Error::UnexpectedXmlUri(pos) => { write!(f, "the 'xml' namespace URI is used for not 'xml' prefix at {}", pos) } Error::UnexpectedXmlnsUri(pos) => { write!(f, "the 'xmlns' URI is used at {}, but it must not be declared", pos) } Error::InvalidElementNamePrefix(pos) => { write!(f, "the 'xmlns' prefix is used at {}, but it must not be", pos) } Error::DuplicatedNamespace(ref name, pos) => { write!(f, "namespace '{}' at {} is already defined", name, pos) } Error::UnknownNamespace(ref name, pos) => { write!(f, "an unknown namespace prefix '{}' at {}", name, pos) } Error::UnexpectedCloseTag { ref expected, ref actual, pos } => { write!(f, "expected '{}' tag, not '{}' at {}", expected, actual, pos) } Error::UnexpectedEntityCloseTag(pos) => { write!(f, "unexpected close tag at {}", pos) } Error::UnknownEntityReference(ref name, pos) => { write!(f, "unknown entity reference '{}' at {}", name, pos) } Error::EntityReferenceLoop(pos) => { write!(f, "a possible entity reference loop is detected at {}", pos) } Error::DuplicatedAttribute(ref name, pos) => { write!(f, "attribute '{}' at {} is already defined", name, pos) } Error::NoRootNode => { write!(f, "the document does not have a root node") } Error::ParserError(ref err) => { write!(f, "{}", err) } } } } impl error::Error for Error { #[inline] fn description(&self) -> &str { "an XML parsing error" } } struct AttributeData<'input> { prefix: StrSpan<'input>, local: StrSpan<'input>, value: Cow<'input, str>, range: Range, value_range: Range, } impl<'input> Document<'input> { /// Parses the input XML string. /// /// We do not support `&[u8]` or `Reader` because the input must be an already allocated /// UTF-8 string. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("").unwrap(); /// assert_eq!(doc.descendants().count(), 2); // root node + `e` element node /// ``` #[inline] pub fn parse(text: &str) -> Result { parse(text) } fn append(&mut self, parent_id: NodeId, kind: NodeKind<'input>, range: Range) -> NodeId { let new_child_id = NodeId(self.nodes.len()); self.nodes.push(NodeData { parent: Some(parent_id), prev_sibling: None, next_sibling: None, children: None, kind, range, }); let last_child_id = self.nodes[parent_id.0].children.map(|(_, id)| id); self.nodes[new_child_id.0].prev_sibling = last_child_id; if let Some(id) = last_child_id { self.nodes[id.0].next_sibling = Some(new_child_id); } self.nodes[parent_id.0].children = Some( if let Some((first_child_id, _)) = self.nodes[parent_id.0].children { (first_child_id, new_child_id) } else { (new_child_id, new_child_id) } ); new_child_id } } struct Entity<'input> { name: &'input str, value: StrSpan<'input>, } struct ParserData<'input> { attrs_start_idx: usize, ns_start_idx: usize, tmp_attrs: Vec>, entities: Vec>, buffer: TextBuffer, after_text: bool, } #[derive(Clone, Copy)] struct TagNameSpan<'input> { prefix: StrSpan<'input>, name: StrSpan<'input>, span: StrSpan<'input>, } impl<'input> TagNameSpan<'input> { #[inline] fn new_null() -> Self { Self { prefix: StrSpan::from(""), name: StrSpan::from(""), span: StrSpan::from(""), } } #[inline] fn new(prefix: StrSpan<'input>, name: StrSpan<'input>, span: StrSpan<'input>) -> Self { Self { prefix, name, span } } } fn parse(text: &str) -> Result { let mut pd = ParserData { attrs_start_idx: 0, ns_start_idx: 1, tmp_attrs: Vec::with_capacity(16), entities: Vec::new(), buffer: TextBuffer::new(), after_text: false, }; // Trying to guess rough nodes and attributes amount. let nodes_capacity = text.bytes().filter(|c| *c == b'<').count(); let attributes_capacity = text.bytes().filter(|c| *c == b'=').count(); // Init document. let mut doc = Document { text, nodes: Vec::with_capacity(nodes_capacity), attrs: Vec::with_capacity(attributes_capacity), namespaces: Namespaces(Vec::new()), }; // Add a root node. doc.nodes.push(NodeData { parent: None, prev_sibling: None, next_sibling: None, children: None, kind: NodeKind::Root, range: 0..0, }); doc.namespaces.push_ns(Some("xml"), NS_XML_URI.to_string()); let parser = xmlparser::Tokenizer::from(text); let parent_id = doc.root().id; let mut tag_name = TagNameSpan::new_null(); process_tokens(parser, 0, parent_id, &mut tag_name, &mut pd, &mut doc)?; if !doc.root().children().any(|n| n.is_element()) { return Err(Error::NoRootNode); } doc.nodes.shrink_to_fit(); doc.attrs.shrink_to_fit(); doc.namespaces.0.shrink_to_fit(); Ok(doc) } fn process_tokens<'input>( parser: xmlparser::Tokenizer<'input>, entity_depth: u8, mut parent_id: NodeId, tag_name: &mut TagNameSpan<'input>, pd: &mut ParserData<'input>, doc: &mut Document<'input>, ) -> Result<(), Error> { for token in parser { let token = token?; match token { xmlparser::Token::ProcessingInstruction { target, content, span } => { let pi = NodeKind::PI(PI { target: target.as_str(), value: content.map(|v| v.as_str()), }); doc.append(parent_id, pi, span.range()); } xmlparser::Token::Comment { text, span } => { doc.append(parent_id, NodeKind::Comment(text.as_str()), span.range()); } xmlparser::Token::Text { text } => { process_text(text, parent_id, entity_depth, pd, doc)?; } xmlparser::Token::Cdata { text, span } => { let cow_str = Cow::Borrowed(text.as_str()); append_text(cow_str, parent_id, span.range(), pd.after_text, doc); pd.after_text = true; } xmlparser::Token::ElementStart { prefix, local, span } => { if prefix.as_str() == "xmlns" { let pos = err_pos_from_span(prefix); return Err(Error::InvalidElementNamePrefix(pos)); } *tag_name = TagNameSpan::new(prefix, local, span); } xmlparser::Token::Attribute { prefix, local, value, span } => { process_attribute(entity_depth, prefix, local, value, span, pd, doc)?; } xmlparser::Token::ElementEnd { end, span } => { process_element(*tag_name, end, span, &mut parent_id, pd, doc)?; } xmlparser::Token::EntityDeclaration { name, definition, .. } => { if let xmlparser::EntityDefinition::EntityValue(value) = definition { pd.entities.push(Entity { name: name.as_str(), value }); } } _ => {} } match token { xmlparser::Token::ProcessingInstruction { .. } | xmlparser::Token::Comment { .. } | xmlparser::Token::ElementStart { .. } | xmlparser::Token::ElementEnd { .. } => { pd.after_text = false; } _ => {} } } Ok(()) } fn process_attribute<'input>( entity_depth: u8, prefix: StrSpan<'input>, local: StrSpan<'input>, value: StrSpan<'input>, token_span: StrSpan<'input>, pd: &mut ParserData<'input>, doc: &mut Document<'input>, ) -> Result<(), Error> { let range = token_span.range(); let value_range = value.range(); let value = normalize_attribute(entity_depth, value, &pd.entities, &mut pd.buffer)?; if prefix.as_str() == "xmlns" { // The xmlns namespace MUST NOT be declared as the default namespace. if value == NS_XMLNS_URI { let pos = err_pos_from_qname(prefix, local); return Err(Error::UnexpectedXmlnsUri(pos)); } let is_xml_ns_uri = value == NS_XML_URI; // The prefix 'xml' is by definition bound to the namespace name // http://www.w3.org/XML/1998/namespace. // It MUST NOT be bound to any other namespace name. if local.as_str() == "xml" { if !is_xml_ns_uri { let pos = err_pos_from_span(prefix); return Err(Error::InvalidXmlPrefixUri(pos)); } } else { // The xml namespace MUST NOT be bound to a non-xml prefix. if is_xml_ns_uri { let pos = err_pos_from_span(prefix); return Err(Error::UnexpectedXmlUri(pos)); } } // Check for duplicated namespaces. if doc.namespaces.exists(pd.ns_start_idx, Some(local.as_str())) { let pos = err_pos_from_qname(prefix, local); return Err(Error::DuplicatedNamespace(local.as_str().to_string(), pos)); } // Xml namespace should not be added to the namespaces. if !is_xml_ns_uri { doc.namespaces.push_ns(Some(local.as_str()), value.into()); } } else if local.as_str() == "xmlns" { // The xml namespace MUST NOT be declared as the default namespace. if value == NS_XML_URI { let pos = err_pos_from_span(local); return Err(Error::UnexpectedXmlUri(pos)); } // The xmlns namespace MUST NOT be declared as the default namespace. if value == NS_XMLNS_URI { let pos = err_pos_from_span(local); return Err(Error::UnexpectedXmlnsUri(pos)); } doc.namespaces.push_ns(None, value.into()); } else { pd.tmp_attrs.push(AttributeData { prefix, local, value, range, value_range }); } Ok(()) } fn process_element<'input>( tag_name: TagNameSpan<'input>, end_token: xmlparser::ElementEnd<'input>, token_span: StrSpan<'input>, parent_id: &mut NodeId, pd: &mut ParserData<'input>, doc: &mut Document<'input>, ) -> Result<(), Error> { if tag_name.name.is_empty() { // May occur in XML like this: // '> ]> // &p; if let xmlparser::ElementEnd::Close(..) = end_token { return Err(Error::UnexpectedEntityCloseTag(err_pos_from_span(token_span))); } else { unreachable!("should be already checked by the xmlparser"); } } let namespaces = resolve_namespaces(pd.ns_start_idx, *parent_id, doc); pd.ns_start_idx = doc.namespaces.len(); let attributes = resolve_attributes(pd.attrs_start_idx, namespaces.clone(), &mut pd.tmp_attrs, doc)?; pd.attrs_start_idx = doc.attrs.len(); pd.tmp_attrs.clear(); match end_token { xmlparser::ElementEnd::Empty => { let tag_ns_uri = get_ns_by_prefix(doc, namespaces.clone(), tag_name.prefix)?; doc.append(*parent_id, NodeKind::Element { tag_name: ExpandedNameOwned { ns: tag_ns_uri, prefix: tag_name.prefix.as_str(), name: tag_name.name.as_str(), }, attributes, namespaces, }, tag_name.span.start()..token_span.end() ); } xmlparser::ElementEnd::Close(prefix, local) => { let prefix = prefix.as_str(); let local = local.as_str(); doc.nodes[parent_id.0].range.end = token_span.end(); if let NodeKind::Element { ref tag_name, .. } = doc.nodes[parent_id.0].kind { if prefix != tag_name.prefix || local != tag_name.name { return Err(Error::UnexpectedCloseTag { expected: gen_qname_string(tag_name.prefix, tag_name.name), actual: gen_qname_string(prefix, local), pos: err_pos_from_span(token_span), }); } } if let Some(id) = doc.nodes[parent_id.0].parent { *parent_id = id; } else { unreachable!("should be already checked by the xmlparser"); } } xmlparser::ElementEnd::Open => { let tag_ns_uri = get_ns_by_prefix(doc, namespaces.clone(), tag_name.prefix)?; *parent_id = doc.append(*parent_id, NodeKind::Element { tag_name: ExpandedNameOwned { ns: tag_ns_uri, prefix: tag_name.prefix.as_str(), name: tag_name.name.as_str(), }, attributes, namespaces, }, tag_name.span.start()..token_span.end() ); } } Ok(()) } fn resolve_namespaces( start_idx: usize, parent_id: NodeId, doc: &mut Document, ) -> Range { let mut tmp_parent_id = parent_id.0; while tmp_parent_id != 0 { let curr_id = tmp_parent_id; tmp_parent_id = match doc.nodes[tmp_parent_id].parent { Some(id) => id.0, None => 0, }; let ns_range = match doc.nodes[curr_id].kind { NodeKind::Element { ref namespaces, .. } => namespaces.clone(), _ => continue, }; for i in ns_range { if !doc.namespaces.exists(start_idx, doc.namespaces[i].name) { let v = doc.namespaces[i].clone(); doc.namespaces.0.push(v); } } } if start_idx != doc.namespaces.len() { start_idx..doc.namespaces.len() } else { 0..0 } } fn resolve_attributes<'input>( start_idx: usize, namespaces: Range, tmp_attrs: &mut [AttributeData<'input>], doc: &mut Document<'input>, ) -> Result { if tmp_attrs.is_empty() { return Ok(0..0); } for attr in tmp_attrs { let ns = if attr.prefix.as_str() == "xml" { // The prefix 'xml' is by definition bound to the namespace name // http://www.w3.org/XML/1998/namespace. Some(doc.namespaces.xml_uri()) } else if attr.prefix.is_empty() { // 'The namespace name for an unprefixed attribute name // always has no value.' None } else { get_ns_by_prefix(doc, namespaces.clone(), attr.prefix)? }; // We do not store attribute prefixes since `ExpandedNameOwned::prefix` // is used only for closing tags matching during parsing. let attr_name = ExpandedNameOwned { ns, prefix: "", name: attr.local.as_str() }; // Check for duplicated attributes. if doc.attrs[start_idx..].iter().any(|attr| attr.name == attr_name) { let pos = err_pos_from_qname(attr.prefix, attr.local); return Err(Error::DuplicatedAttribute(attr.local.to_string(), pos)); } doc.attrs.push(Attribute { name: attr_name, // Takes a value from a slice without consuming the slice. value: mem::replace(&mut attr.value, Cow::Borrowed("")), range: attr.range.clone(), value_range: attr.value_range.clone(), }); } Ok(start_idx..doc.attrs.len()) } fn process_text<'input>( text: StrSpan<'input>, parent_id: NodeId, entity_depth: u8, pd: &mut ParserData<'input>, doc: &mut Document<'input>, ) -> Result<(), Error> { // Add text as is if it has only valid characters. if !text.as_str().bytes().any(|b| b == b'&' || b == b'\r') { append_text(Cow::Borrowed(text.as_str()), parent_id, text.range(), pd.after_text, doc); pd.after_text = true; return Ok(()); } fn _append_text(parent_id: NodeId, range: Range, pd: &mut ParserData, doc: &mut Document) { let cow_text = Cow::Owned(pd.buffer.to_str().to_owned()); append_text(cow_text, parent_id, range, pd.after_text, doc); pd.after_text = true; pd.buffer.clear(); } pd.buffer.clear(); let mut entity_depth = entity_depth; let mut is_as_is = false; // TODO: explain let mut s = Stream::from(text); while !s.at_end() { match parse_next_chunk(&mut s, &pd.entities)? { NextChunk::Byte(c) => { if is_as_is { pd.buffer.push_raw(c); is_as_is = false; } else { pd.buffer.push_from_text(c, s.at_end()); } } NextChunk::Char(c) => { for b in CharToBytes::new(c) { if entity_depth > 0 { pd.buffer.push_from_text(b, s.at_end()); } else { // Characters not from entity should be added as is. // Not sure why... At least `lxml` produces the same results. pd.buffer.push_raw(b); is_as_is = true; } } } NextChunk::Text(fragment) => { is_as_is = false; if entity_depth > ENTITY_DEPTH { let pos = s.gen_text_pos(); return Err(Error::EntityReferenceLoop(pos)); } if !pd.buffer.is_empty() { _append_text(parent_id, text.range(), pd, doc); } let mut parser = xmlparser::Tokenizer::from(fragment); parser.enable_fragment_mode(); let mut tag_name = TagNameSpan::new_null(); entity_depth += 1; // TODO: explain process_tokens(parser, entity_depth, parent_id, &mut tag_name, pd, doc)?; pd.buffer.clear(); } } } if !pd.buffer.is_empty() { _append_text(parent_id, text.range(), pd, doc); } Ok(()) } fn append_text<'input>( text: Cow<'input, str>, parent_id: NodeId, range: Range, after_text: bool, doc: &mut Document<'input>, ) { if after_text { // Prepend to a previous text node. if let Some(node) = doc.nodes.iter_mut().last() { if let NodeKind::Text(ref mut prev_text) = node.kind { match *prev_text { Cow::Borrowed(..) => { *prev_text = Cow::Owned(prev_text.to_string() + text.borrow()); } Cow::Owned(ref mut s) => { s.push_str(text.borrow()); } } } } } else { doc.append(parent_id, NodeKind::Text(text), range); } } enum NextChunk<'a> { Byte(u8), Char(char), Text(StrSpan<'a>), } fn parse_next_chunk<'a>( s: &mut Stream<'a>, entities: &[Entity<'a>], ) -> Result, Error> { debug_assert!(!s.at_end()); // Safe, because we already checked that stream is not at the end. // But we have an additional `debug_assert` above just in case. let c = s.curr_byte_unchecked(); // Check for character/entity references. if c == b'&' { let start = s.pos(); match s.try_consume_reference() { Some(Reference::Char(ch)) => { Ok(NextChunk::Char(ch)) } Some(Reference::Entity(name)) => { match entities.iter().find(|e| e.name == name) { Some(entity) => { Ok(NextChunk::Text(entity.value)) } None => { let pos = s.gen_text_pos_from(start); Err(Error::UnknownEntityReference(name.into(), pos)) } } } None => { s.advance(1); Ok(NextChunk::Byte(c)) } } } else { s.advance(1); Ok(NextChunk::Byte(c)) } } // https://www.w3.org/TR/REC-xml/#AVNormalize fn normalize_attribute<'input>( entity_depth: u8, text: StrSpan<'input>, entities: &[Entity], buffer: &mut TextBuffer, ) -> Result, Error> { if is_normalization_required(&text) { buffer.clear(); _normalize_attribute(text, entities, entity_depth, buffer)?; Ok(Cow::Owned(buffer.to_str().to_owned())) } else { Ok(Cow::Borrowed(text.as_str())) } } #[inline] fn is_normalization_required(text: &StrSpan) -> bool { // We assume that `&` indicates an entity or a character reference. // But in rare cases it can be just an another character. fn check(c: u8) -> bool { match c { b'&' | b'\t' | b'\n' | b'\r' => true, _ => false, } } text.as_str().bytes().any(check) } fn _normalize_attribute( text: StrSpan, entities: &[Entity], entity_depth: u8, buffer: &mut TextBuffer, ) -> Result<(), Error> { let mut entity_depth = entity_depth; let mut s = Stream::from(text); while !s.at_end() { // Safe, because we already checked that the stream is not at the end. let c = s.curr_byte_unchecked(); if c != b'&' { s.advance(1); buffer.push_from_attr(c, s.curr_byte().ok()); continue; } // Check for character/entity references. let start = s.pos(); match s.try_consume_reference() { Some(Reference::Char(ch)) => { for b in CharToBytes::new(ch) { if entity_depth > 0 { buffer.push_from_attr(b, None); } else { // Characters not from entity should be added as is. // Not sure why... At least `lxml` produces the same results. buffer.push_raw(b); } } } Some(Reference::Entity(name)) => { if entity_depth > ENTITY_DEPTH { let pos = s.gen_text_pos(); return Err(Error::EntityReferenceLoop(pos)); } match entities.iter().find(|e| e.name == name) { Some(entity) => { entity_depth += 1; _normalize_attribute(entity.value, entities, entity_depth, buffer)?; } None => { let pos = s.gen_text_pos_from(start); return Err(Error::UnknownEntityReference(name.into(), pos)); } } } None => { s.advance(1); buffer.push_from_attr(c, s.curr_byte().ok()); } } } Ok(()) } fn get_ns_by_prefix( doc: &Document, range: Range, prefix: StrSpan, ) -> Result, Error> { // Prefix CAN be empty when the default namespace was defined. // // Example: // let prefix_opt = if prefix.is_empty() { None } else { Some(prefix.as_str()) }; let uri = doc.namespaces[range].iter() .find(|ns| ns.name == prefix_opt) .map(|ns| ns.uri.clone()); match uri { Some(v) => Ok(Some(v)), None => { if !prefix.is_empty() { // If an URI was not found and prefix IS NOT empty than // we have an unknown namespace. // // Example: // let pos = err_pos_from_span(prefix); Err(Error::UnknownNamespace(prefix.as_str().to_string(), pos)) } else { // If an URI was not found and prefix IS empty than // an element or an attribute doesn't have a namespace. // // Example: // Ok(None) } } } } #[inline] fn gen_qname_string(prefix: &str, local: &str) -> String { if prefix.is_empty() { local.to_string() } else { format!("{}:{}", prefix, local) } } #[inline] fn err_pos_from_span(text: StrSpan) -> TextPos { Stream::from(text).gen_text_pos() } #[inline] fn err_pos_from_qname(prefix: StrSpan, local: StrSpan) -> TextPos { let err_span = if prefix.is_empty() { local } else { prefix }; err_pos_from_span(err_span) } mod internals { /// Iterate over `char` by `u8`. pub struct CharToBytes { buf: [u8; 4], idx: u8, } impl CharToBytes { #[inline] pub fn new(c: char) -> Self { let mut buf = [0xFF; 4]; c.encode_utf8(&mut buf); CharToBytes { buf, idx: 0, } } } impl Iterator for CharToBytes { type Item = u8; #[inline] fn next(&mut self) -> Option { if self.idx < 4 { let b = self.buf[self.idx as usize]; if b != 0xFF { self.idx += 1; return Some(b); } else { self.idx = 4; } } None } } pub struct TextBuffer { buf: Vec, } impl TextBuffer { #[inline] pub fn new() -> Self { TextBuffer { buf: Vec::with_capacity(32), } } #[inline] pub fn push_raw(&mut self, c: u8) { self.buf.push(c); } pub fn push_from_attr(&mut self, mut c: u8, c2: Option) { // \r in \r\n should be ignored. if c == b'\r' && c2 == Some(b'\n') { return; } // \n, \r and \t should be converted into spaces. c = match c { b'\n' | b'\r' | b'\t' => b' ', _ => c, }; self.buf.push(c); } // Translate \r\n and any \r that is not followed by \n into a single \n character. // // https://www.w3.org/TR/xml/#sec-line-ends pub fn push_from_text(&mut self, c: u8, at_end: bool) { if self.buf.last() == Some(&b'\r') { let idx = self.buf.len() - 1; self.buf[idx] = b'\n'; if at_end && c == b'\r' { self.buf.push(b'\n'); } else if c != b'\n' { self.buf.push(c); } } else if at_end && c == b'\r' { self.buf.push(b'\n'); } else { self.buf.push(c); } } #[inline] pub fn clear(&mut self) { self.buf.clear(); } #[inline] pub fn is_empty(&self) -> bool { self.buf.is_empty() } #[inline] pub fn to_str(&self) -> &str { use std::str; // `unwrap` is safe, because buffer must contain a valid UTF-8 string. str::from_utf8(&self.buf).unwrap() } } } use self::internals::*; roxmltree-0.7.3/testing-tools/lxml-ast.py010075500017500001750000000053721336071612100167230ustar0000000000000000#!/usr/bin/env python3 import sys from lxml import etree def escape_text(text): return text.encode('unicode_escape').decode("utf-8") def split_qname(name): if name[0] == '{': return name[1:].split('}') else: return [None, name] def print_ind(depth, *args, **kwargs): indent = ' ' * depth indent = indent[:-1] print(indent, *args, **kwargs) def print_node(node, depth): if node.tag is etree.Comment: print_ind(depth, '- Comment: "{}"'.format(escape_text(node.text))) if node.tail: print_ind(depth, '- Text: "{}"'.format(escape_text(node.tail))) return if node.tag is etree.PI: print_ind(depth, '- PI:') print_ind(depth + 2, 'target: "{}"'.format(node.target)) print_ind(depth + 2, 'value: "{}"'.format(escape_text(node.text))) if node.tail: print_ind(depth, '- Text: "{}"'.format(escape_text(node.tail))) return print_ind(depth, '- Element:') if node.tag[0] == '{': uri, tag = split_qname(node.tag) print_ind(depth + 2, 'tag_name: {}@{}'.format(tag, uri)) else: print_ind(depth + 2, 'tag_name:', node.tag) if node.attrib: print_ind(depth + 2, 'attributes:') attrs = [] for name, value in node.attrib.items(): uri, tag = split_qname(name) if uri: attrs.append([tag + '@' + uri, value]) else: attrs.append([tag, value]) attrs = sorted(attrs, key=lambda x: x[0]) for name, value in attrs: print_ind(depth + 3, '{}: "{}"'.format(name, escape_text(value))) if node.nsmap: print_ind(depth + 2, 'namespaces:') ns_list = [] for name, value in node.nsmap.items(): if not name and not value: ns_list.append(['None', '""']) elif not name: ns_list.append(['None', value]) elif not value: ns_list.append([name, '""']) else: ns_list.append([name, value]) ns_list = sorted(ns_list, key=lambda x: x[0]) for name, value in ns_list: print_ind(depth + 3, '{}: {}'.format(name, value)) if len(node): print_ind(depth + 2, 'children:') if node.text: print_ind(depth + 3, '- Text: "{}"'.format(escape_text(node.text))) for child in node: print_node(child, depth + 3) elif node.text: print_ind(depth + 2, 'children:') print_ind(depth + 3, '- Text: "{}"'.format(escape_text(node.text))) if node.tail: print_ind(depth, '- Text: "{}"'.format(escape_text(node.tail))) tree = etree.parse(sys.argv[1]) root = tree.getroot() print('Document:') print_node(root, 1) roxmltree-0.7.3/testing-tools/test-lxml.py010075500017500001750000000021761333656237100171230ustar0000000000000000#!/usr/bin/env python3 import os import subprocess import fnmatch from pathlib import Path ignore = [ "comments_001.xml", "tree_001.xml", ] input_dir = '../tests/files/' files = os.listdir(input_dir) files = fnmatch.filter(files, '*.xml') files = sorted(files) is_ok = True for idx, xml_file_name in enumerate(files): xml_file_path = input_dir + xml_file_name res_file_path = input_dir + Path(input_dir + xml_file_name).stem + '.yaml' print('Test {} of {}: {}'.format(idx + 1, len(files), xml_file_name)) if xml_file_name in ignore: continue with open(res_file_path, 'r') as f: expected_ast = f.read() try: lxml_ast = subprocess.check_output(['./lxml-ast.py', xml_file_path], stderr=subprocess.DEVNULL).decode('utf-8') except subprocess.CalledProcessError as e: if expected_ast.startswith('error:'): continue else: is_ok = False print('Failed') break if lxml_ast != expected_ast: is_ok = False print('Failed') break if not is_ok: exit(1) roxmltree-0.7.3/tests/api.rs010064400017500001750000000134401352215305500142470ustar0000000000000000extern crate roxmltree; #[macro_use] extern crate pretty_assertions; use roxmltree::*; #[test] fn root_element_01() { let data = "\ "; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(node.tag_name().name(), "e"); } #[test] fn get_text_01() { let data = "\ Text1 Text2 Text3 "; let doc = Document::parse(data).unwrap(); let root = doc.root_element(); assert_eq!(root.text(), Some("\n Text1\n ")); assert_eq!(root.tail(), None); let item = root.children().nth(1).unwrap(); assert_eq!(item.text(), Some("\n Text2\n ")); assert_eq!(item.tail(), Some("\n Text3\n")); } #[test] fn get_text_02() { let data = "'"; let doc = Document::parse(data).unwrap(); let root = doc.root_element(); assert_eq!(root.text(), Some("'")); } #[test] fn api_01() { let data = "\ "; let doc = Document::parse(data).unwrap(); let p = doc.root_element(); assert_eq!(p.attribute("attr"), Some("no_ns")); assert_eq!(p.has_attribute("attr"), true); assert_eq!(p.attribute(("http://www.w3.org", "attr")), Some("a_ns")); assert_eq!(p.has_attribute(("http://www.w3.org", "attr")), true); assert_eq!(p.attribute("attr2"), None); assert_eq!(p.has_attribute("attr2"), false); assert_eq!(p.attribute(("http://www.w2.org", "attr")), None); assert_eq!(p.has_attribute(("http://www.w2.org", "attr")), false); assert_eq!(p.attribute("b"), None); assert_eq!(p.has_attribute("b"), false); assert_eq!(p.attribute("xmlns"), None); assert_eq!(p.has_attribute("xmlns"), false); } #[test] fn get_pi() { let data = "\ "; let doc = Document::parse(data).unwrap(); let node = doc.root().first_child().unwrap(); assert_eq!(node.pi(), Some(PI { target: "target", value: Some("value") })); } #[test] fn lookup_prefix_01() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(node.lookup_prefix("http://www.w3.org"), Some("n1")); assert_eq!(node.lookup_prefix("http://www.w4.org"), None); } #[test] fn lookup_prefix_02() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(node.lookup_prefix(NS_XML_URI), Some("xml")); } #[test] fn lookup_namespace_uri() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(node.lookup_namespace_uri(Some("n1")), Some("http://www.w3.org")); assert_eq!(node.lookup_namespace_uri(None), Some("http://www.w4.org")); assert_eq!(node.lookup_namespace_uri(Some("n2")), None); } #[test] fn text_pos_01() { let data = "\

Text

"; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(doc.text_pos_at(node.range().start), TextPos::new(1, 1)); assert_eq!(doc.text_pos_at(node.range().end), TextPos::new(4, 5)); if let Some(attr) = node.attribute_node("a") { assert_eq!(doc.text_pos_at(attr.range().start), TextPos::new(1, 4)); assert_eq!(doc.text_pos_at(attr.value_range().start), TextPos::new(1, 7)); } // first child is a text/whitespace, not a comment let comm = node.first_child().unwrap().next_sibling().unwrap(); assert_eq!(doc.text_pos_at(comm.range().start), TextPos::new(2, 5)); let p = comm.next_sibling().unwrap().next_sibling().unwrap(); assert_eq!(doc.text_pos_at(p.range().start), TextPos::new(3, 5)); assert_eq!(doc.text_pos_at(p.range().end), TextPos::new(3, 16)); let text = p.first_child().unwrap(); assert_eq!(doc.text_pos_at(text.range().start), TextPos::new(3, 8)); } #[test] fn text_pos_02() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(doc.text_pos_at(node.range().start), TextPos::new(1, 1)); if let Some(attr) = node.attribute_node(("http://www.w3.org", "a")) { assert_eq!(doc.text_pos_at(attr.range().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.value_range().start), TextPos::new(1, 42)); } } #[test] fn text_pos_03() { let data = "\ "; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); assert_eq!(doc.text_pos_at(node.range().start), TextPos::new(2, 1)); assert_eq!(doc.text_pos_at(node.range().end), TextPos::new(2, 5)); } #[test] fn lifetimes() { fn f<'a, 'd, F, R>(doc: &'a roxmltree::Document<'d>, fun: F) -> R where F: Fn(&'a roxmltree::Document<'d>) -> R { fun(doc) } let doc = roxmltree::Document::parse("").unwrap(); let _ = f(&doc, |d| d.root()); let _ = f(&doc, |d| d.root().document()); let _ = f(&doc, |d| d.root().tag_name()); let _ = f(&doc, |d| d.root().tag_name().namespace()); let _ = f(&doc, |d| d.root().tag_name().name()); let _ = f(&doc, |d| d.root().default_namespace()); let _ = f(&doc, |d| d.root().lookup_prefix("")); let _ = f(&doc, |d| d.root().lookup_namespace_uri(None)); let _ = f(&doc, |d| d.root().attribute("a")); let _ = f(&doc, |d| d.root().attribute_node("a")); let _ = f(&doc, |d| d.root().attributes()); let _ = f(&doc, |d| d.root().namespaces()); let _ = f(&doc, |d| d.root().text()); let _ = f(&doc, |d| d.root().tail()); let _ = f(&doc, |d| d.root().pi()); } roxmltree-0.7.3/tests/ast.rs010064400017500001750000000137671353173637100143110ustar0000000000000000extern crate roxmltree; #[macro_use] extern crate pretty_assertions; use roxmltree::*; use std::path; use std::fs; use std::io::Read; use std::fmt::Write; use std::fmt; #[derive(Clone, Copy, PartialEq)] struct TStr<'a>(pub &'a str); impl<'a> fmt::Debug for TStr<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } trait HasExtension { fn has_extension(&self, ext: &str) -> bool; } impl HasExtension for path::Path { fn has_extension(&self, ext: &str) -> bool { if let Some(e) = self.extension() { e == ext } else { false } } } // List of not yet supported test cases. static IGNORE: &[&str] = &[ "tree_003.xml", ]; #[test] fn compare_ast() { for entry in fs::read_dir("tests/files").unwrap() { let entry = entry.unwrap(); if !entry.path().has_extension("xml") { continue; } let file_name = entry.path().file_name().unwrap().to_str().unwrap().to_string(); if !IGNORE.contains(&file_name.as_str()) { actual_test(entry.path()); } } } fn actual_test(path: path::PathBuf) { let expected = load_file(&path.with_extension("yaml")); let input_xml = load_file(&path); let doc = match Document::parse(&input_xml) { Ok(v) => v, Err(e) => { assert_eq!(TStr(&format!("error: \"{}\"", e)), TStr(expected.trim())); return; } }; assert_eq!(TStr(&to_yaml(&doc)), TStr(&expected)); } fn load_file(path: &path::Path) -> String { let mut file = fs::File::open(&path).unwrap(); let mut text = String::new(); file.read_to_string(&mut text).unwrap(); text } fn to_yaml(doc: &Document) -> String { let mut s = String::new(); _to_yaml(doc, &mut s).unwrap(); s } fn _to_yaml(doc: &Document, s: &mut String) -> Result<(), fmt::Error> { if !doc.root().has_children() { return write!(s, "Document:"); } macro_rules! writeln_indented { ($depth:expr, $f:expr, $fmt:expr) => { for _ in 0..$depth { write!($f, " ")?; } writeln!($f, $fmt)?; }; ($depth:expr, $f:expr, $fmt:expr, $($arg:tt)*) => { for _ in 0..$depth { write!($f, " ")?; } writeln!($f, $fmt, $($arg)*)?; }; } fn print_children(parent: Node, depth: usize, s: &mut String) -> Result<(), fmt::Error> { for child in parent.children() { match child.node_type() { NodeType::Element => { writeln_indented!(depth, s, "- Element:"); match child.tag_name().namespace() { Some(ns) => { if ns.is_empty() { writeln_indented!(depth + 2, s, "tag_name: {}", child.tag_name().name()); } else { writeln_indented!(depth + 2, s, "tag_name: {}@{}", child.tag_name().name(), ns); } } None => { writeln_indented!(depth + 2, s, "tag_name: {}", child.tag_name().name()); } } if !child.attributes().is_empty() { let mut attrs = Vec::new(); for attr in child.attributes() { match attr.namespace() { Some(ns) => { attrs.push((format!("{}@{}", attr.name(), ns), attr.value())); } None => { attrs.push((attr.name().to_string(), attr.value())); } } } attrs.sort_by(|a, b| a.0.cmp(&b.0)); writeln_indented!(depth + 2, s, "attributes:"); for (name, value) in attrs { writeln_indented!(depth + 3, s, "{}: {:?}", name, value); } } if !child.namespaces().is_empty() { let mut ns_list = Vec::new(); for ns in child.namespaces() { let name = ns.name().unwrap_or("None"); let uri = if ns.uri().is_empty() { "\"\"" } else { ns.uri() }; ns_list.push((name, uri)); } ns_list.sort_by(|a, b| a.0.cmp(&b.0)); writeln_indented!(depth + 2, s, "namespaces:"); for (name, uri) in ns_list { writeln_indented!(depth + 3, s, "{}: {}", name, uri); } } if child.has_children() { writeln_indented!(depth + 2, s, "children:"); print_children(child, depth + 3, s)?; } } NodeType::Text => { writeln_indented!(depth, s, "- Text: {:?}", child.text().unwrap()); } NodeType::Comment => { writeln_indented!(depth, s, "- Comment: {:?}", child.text().unwrap()); } NodeType::PI => { if child.parent().unwrap().is_root() { continue; } writeln_indented!(depth, s, "- PI:"); let pi = child.pi().unwrap(); writeln_indented!(depth + 2, s, "target: {:?}", pi.target); if let Some(value) = pi.value { writeln_indented!(depth + 2, s, "value: {:?}", value); } } NodeType::Root => {} } } Ok(()) } writeln!(s, "Document:")?; print_children(doc.root(), 1, s)?; Ok(()) } roxmltree-0.7.3/tests/dom-api.rs010064400017500001750000000071601354734766000150440ustar0000000000000000extern crate roxmltree; use roxmltree::*; // Document.getElementsByTagName() #[test] fn get_elements_by_tag_name() { let data = "\ Text "; let doc = Document::parse(data).unwrap(); let nodes: Vec = doc.descendants().filter(|n| n.has_tag_name("rect")).collect(); assert_eq!(nodes.len(), 2); } // Document.getElementsByTagNameNS() #[test] fn get_elements_by_tag_name_ns() { let data = "\ Text "; let doc = Document::parse(data).unwrap(); let nodes: Vec = doc.descendants() .filter(|n| n.has_tag_name(("http://www.w3.org/", "rect"))).collect(); assert_eq!(nodes.len(), 1); } // ParentNode.childElementCount #[test] fn child_element_count() { let data = "\ "; let doc = Document::parse(data).unwrap(); let svg_elem = doc.root_element(); let count = svg_elem.children().filter(|n| n.is_element()).count(); assert_eq!(count, 3); } // ParentNode.children #[test] fn children() { let data = "\ "; let doc = Document::parse(data).unwrap(); let svg_elem = doc.root_element(); let count = svg_elem.children().filter(|n| n.is_element()).count(); assert_eq!(count, 3); } // ParentNode.firstElementChild #[test] fn first_element_child() { let data = "\ "; let doc = Document::parse(data).unwrap(); let svg_elem = doc.root_element(); let elem = svg_elem.first_element_child().unwrap(); assert!(elem.has_tag_name("rect")); // or let elem = svg_elem.children().find(|n| n.is_element()).unwrap(); assert!(elem.has_tag_name("rect")); } // ParentNode.lastElementChild #[test] fn last_element_child() { let data = "\ "; let doc = Document::parse(data).unwrap(); let svg_elem = doc.root_element(); let elem = svg_elem.last_element_child().unwrap(); assert!(elem.has_tag_name("rect")); // or let elem = svg_elem.children().filter(|n| n.is_element()).last().unwrap(); assert!(elem.has_tag_name("rect")); } // Document.getElementById #[test] fn get_element_by_id() { let data = "\ "; let doc = Document::parse(data).unwrap(); let elem = doc.descendants().find(|n| n.attribute("id") == Some("rect1")).unwrap(); assert!(elem.has_tag_name("rect")); } // Node.ownerDocument #[test] fn owner_document() { let doc = Document::parse("").unwrap(); let _elem = doc.root_element(); } // Node.parentElement #[test] fn parent_element() { let data = "\ "; let doc = Document::parse(data).unwrap(); let rect = doc.descendants().find(|n| n.has_tag_name("rect")).unwrap(); assert!(rect.parent_element().unwrap().has_tag_name("svg")); // or assert!(rect.ancestors().skip(1).find(|n| n.is_element()).unwrap().has_tag_name("svg")); } // Node.contains #[test] fn contains() { let data = "\ "; let doc = Document::parse(data).unwrap(); let svg = doc.root_element(); let rect = svg.first_child().unwrap(); assert!(svg.descendants().any(|n| n == rect)); } roxmltree-0.7.3/tests/files/README.md010064400017500001750000000062771352215305500155230ustar0000000000000000### Tests - attrs_001 - simple attribute - attrs_002 - attribute value with new lines - attrs_003 - attribute value with escaped text - attrs_004 - attribute value with escaped text - attrs_005 - attribute value with \r\n - attrs_err_001 - duplicated attributes - attrs_err_002 - duplicated attributes via namespaces - cdata_001 - simple case - cdata_002 - simple case - cdata_003 - empty - cdata_004 - simple case - cdata_005 - mixed text and cdata - cdata_006 - simple case - comments_001 - comment before and after the root element - elems_err_001 - invalid tree structure - elems_err_002 - invalid tree structure with namespace - entity_001 - entity reference to an element - entity_002 - entity reference to an attribute value - entity_003 - many entity references to an attribute value - entity_004 - entity reference to a text - entity_005 - unused entity reference - entity_006 - entity reference to an escaped text - entity_007 - indirect entity reference to an attribute value - entity_008 - entity reference to an element - entity_009 - entity reference to a mixed content - entity_010 - entity reference to an element with an entity reference - entity_011 - character and entity references in attributes - entity_012 - mixed entity references in text - entity_err_001 - unknown entity reference - entity_err_002 - recursive entity references - entity_err_003 - reference to a close tag - entity_err_004 - reference to a close tag - entity_err_005 - billion laughs - entity_err_006 - billion laughs - ns_001 - attributes with different namespaces - ns_002 - attribute is not affected by the default namespace - ns_003 - attributes with different namespaces - ns_004 - `href` with a custom prefix - ns_005 - `xml` namespace resolving - ns_006 - `xml` namespace overriding - ns_007 - many namespaces - ns_008 - namespace propagation - ns_009 - namespace overwriting - ns_010 - indirect namespace propagation - ns_011 - empty URI - ns_012 - namespace propagation - ns_013 - namespace from entity - ns_014 - no namespaces - ns_015 - duplicated namespaces with different prefixes and a child element without prefix - ns_016 - an empty element with namespace on parent - ns_017 - duplicated namespaces with different prefixes and a child element with prefix - ns_err_001 - invalid `xml` URI - ns_err_002 - reserved URI - ns_err_003 - reserved URI - ns_err_004 - duplicated namespaces - ns_err_005 - escaped namespace - ns_err_006 - escaped namespace - ns_err_007 - reserved URI - ns_err_008 - reserved URI - ns_err_009 - `xmlns` cannot be used as an element prefix - ns_err_010 - an element with an unknown namespace - ns_err_011 - an attribute with an unknown namespace - ns_err_012 - closing tag with missing namespace prefix - ns_err_013 - closing tag with missing namespace prefix and default namespace - text_001 - single space text - text_002 - single escaped space text - text_003 - escaped text - text_004 - '>' text - text_005 - '\n\r\r\n' text - text_006 - '\r\r\r' text - text_007 - '\r\n\r\n' text - text_008 - only whitespaces - text_009 - escaped text - text_010 - text around elements - text_011 - mixed character references in text - tree_001 - all node types - tree_002 - BOM - tree_003 - Windows-1251 encoding - tree_err_001 - no elements roxmltree-0.7.3/tests/files/attrs_001.xml010064400017500001750000000000131331616737700164760ustar0000000000000000

roxmltree-0.7.3/tests/files/attrs_001.yaml010064400017500001750000000001121353173640200166260ustar0000000000000000Document: - Element: tag_name: p attributes: a: "b" roxmltree-0.7.3/tests/files/attrs_002.xml010064400017500001750000000000171331616744400164760ustar0000000000000000

roxmltree-0.7.3/tests/files/attrs_002.yaml010064400017500001750000000001161331620556700166370ustar0000000000000000Document: - Element: tag_name: p attributes: a: " xyz" roxmltree-0.7.3/tests/files/attrs_003.xml010064400017500001750000000000221331616763000164700ustar0000000000000000

roxmltree-0.7.3/tests/files/attrs_003.yaml010064400017500001750000000001141331620560700166310ustar0000000000000000Document: - Element: tag_name: p attributes: a: "A B" roxmltree-0.7.3/tests/files/attrs_004.xml010064400017500001750000000000521331616766300165020ustar0000000000000000

roxmltree-0.7.3/tests/files/attrs_004.yaml010064400017500001750000000001271331620563000166320ustar0000000000000000Document: - Element: tag_name: p attributes: a: "\r\rA\n\nB\r\n" roxmltree-0.7.3/tests/files/attrs_005.xml010064400017500001750000000000201331617055500164700ustar0000000000000000 roxmltree-0.7.3/tests/files/attrs_005.yaml010064400017500001750000000001141331620564700166370ustar0000000000000000Document: - Element: tag_name: e attributes: a: " b " roxmltree-0.7.3/tests/files/attrs_err_001.xml010064400017500001750000000000231331617000000173200ustar0000000000000000 roxmltree-0.7.3/tests/files/attrs_err_001.yaml010064400017500001750000000000621331623745300175050ustar0000000000000000error: "attribute 'a' at 1:11 is already defined" roxmltree-0.7.3/tests/files/attrs_err_002.xml010064400017500001750000000001231331617003400173310ustar0000000000000000 roxmltree-0.7.3/tests/files/attrs_err_002.yaml010064400017500001750000000000621331623746100175050ustar0000000000000000error: "attribute 'a' at 1:72 is already defined" roxmltree-0.7.3/tests/files/cdata_001.xml010064400017500001750000000000741331271257100164100ustar0000000000000000 Hello, world!]]> roxmltree-0.7.3/tests/files/cdata_001.yaml010064400017500001750000000001641333635152700165600ustar0000000000000000Document: - Element: tag_name: doc children: - Text: "\nHello, world!\n" roxmltree-0.7.3/tests/files/cdata_002.xml010064400017500001750000000000721333635147700164210ustar0000000000000000]> roxmltree-0.7.3/tests/files/cdata_002.yaml010064400017500001750000000001451333635151000165500ustar0000000000000000Document: - Element: tag_name: e children: - Text: "\n \n " roxmltree-0.7.3/tests/files/cdata_003.xml010064400017500001750000000000321333635105400164050ustar0000000000000000

roxmltree-0.7.3/tests/files/cdata_003.yaml010064400017500001750000000001241333635106600165540ustar0000000000000000Document: - Element: tag_name: p children: - Text: "\n \n" roxmltree-0.7.3/tests/files/cdata_004.xml010064400017500001750000000001051333647464600164240ustar0000000000000000

some

text

roxmltree-0.7.3/tests/files/cdata_004.yaml010064400017500001750000000003511333647471300165640ustar0000000000000000Document: - Element: tag_name: p children: - Text: "\n some\n " - Element: tag_name: p children: - Text: "\n long\n " - Text: "\n text\n" roxmltree-0.7.3/tests/files/cdata_005.xml010064400017500001750000000000441333650344400164140ustar0000000000000000

Tx

roxmltree-0.7.3/tests/files/cdata_005.yaml010064400017500001750000000001201333650346000165470ustar0000000000000000Document: - Element: tag_name: p children: - Text: "Text" roxmltree-0.7.3/tests/files/cdata_006.xml010064400017500001750000000000771333657447200164340ustar0000000000000000 roxmltree-0.7.3/tests/files/cdata_006.yaml010064400017500001750000000003331333657450000165610ustar0000000000000000Document: - Element: tag_name: svg children: - Text: "\n " - Element: tag_name: style children: - Text: "\n text\n " - Text: "\n" roxmltree-0.7.3/tests/files/comments_001.xml010064400017500001750000000000521333656125200171610ustar0000000000000000 roxmltree-0.7.3/tests/files/comments_001.yaml010064400017500001750000000001361333656243100173260ustar0000000000000000Document: - Comment: " comment " - Element: tag_name: root - Comment: " comment " roxmltree-0.7.3/tests/files/elems_err_001.xml010064400017500001750000000000321331617215600173060ustar0000000000000000 roxmltree-0.7.3/tests/files/elems_err_001.yaml010064400017500001750000000000601333656005600174530ustar0000000000000000error: "expected 'item' tag, not 'root' at 3:1" roxmltree-0.7.3/tests/files/elems_err_002.xml010064400017500001750000000000741331617221100173050ustar0000000000000000 roxmltree-0.7.3/tests/files/elems_err_002.yaml010064400017500001750000000000641333656007000174540ustar0000000000000000error: "expected 't:item' tag, not 't:root' at 3:1" roxmltree-0.7.3/tests/files/entity_001.xml010064400017500001750000000000741331613706700166550ustar0000000000000000'> ]> &p; roxmltree-0.7.3/tests/files/entity_001.yaml010064400017500001750000000001471331620636400170150ustar0000000000000000Document: - Element: tag_name: root children: - Element: tag_name: p roxmltree-0.7.3/tests/files/entity_002.xml010064400017500001750000000000651331616747500166640ustar0000000000000000 ]>

roxmltree-0.7.3/tests/files/entity_002.yaml010064400017500001750000000001121331620633000167770ustar0000000000000000Document: - Element: tag_name: p attributes: a: "b" roxmltree-0.7.3/tests/files/entity_003.xml010064400017500001750000000002041336071343400166470ustar0000000000000000 ]>

roxmltree-0.7.3/tests/files/entity_003.yaml010064400017500001750000000001221336071344300170100ustar0000000000000000Document: - Element: tag_name: p attributes: a: " A B " roxmltree-0.7.3/tests/files/entity_004.xml010064400017500001750000000000661333631206200166510ustar0000000000000000 ]>

&b;

roxmltree-0.7.3/tests/files/entity_004.yaml010064400017500001750000000001201333631171200170030ustar0000000000000000Document: - Element: tag_name: p children: - Text: "text" roxmltree-0.7.3/tests/files/entity_005.xml010064400017500001750000000000601333631201700166440ustar0000000000000000 ]> roxmltree-0.7.3/tests/files/entity_005.yaml010064400017500001750000000000511333631202200170020ustar0000000000000000Document: - Element: tag_name: e roxmltree-0.7.3/tests/files/entity_006.xml010064400017500001750000000001001333631231200166360ustar0000000000000000 ]>

&b;

roxmltree-0.7.3/tests/files/entity_006.yaml010064400017500001750000000001251333631232500170130ustar0000000000000000Document: - Element: tag_name: p children: - Text: "Some text" roxmltree-0.7.3/tests/files/entity_007.xml010064400017500001750000000001161331617236000166530ustar0000000000000000 ]> roxmltree-0.7.3/tests/files/entity_007.yaml010064400017500001750000000001151333631266400170210ustar0000000000000000Document: - Element: tag_name: e attributes: a: "text" roxmltree-0.7.3/tests/files/entity_008.xml010064400017500001750000000001021333632273500166540ustar0000000000000000 '> ]> &p; roxmltree-0.7.3/tests/files/entity_008.yaml010064400017500001750000000002231333632300500170110ustar0000000000000000Document: - Element: tag_name: root children: - Text: " " - Element: tag_name: p - Text: " " roxmltree-0.7.3/tests/files/entity_009.xml010064400017500001750000000001041333632514500166550ustar0000000000000000text'> ]> &p; roxmltree-0.7.3/tests/files/entity_009.yaml010064400017500001750000000002251333632517400170250ustar0000000000000000Document: - Element: tag_name: root children: - Text: "text" - Element: tag_name: p - Text: "text" roxmltree-0.7.3/tests/files/entity_010.xml010064400017500001750000000001301333632531100166370ustar0000000000000000 '> ]> &p; roxmltree-0.7.3/tests/files/entity_010.yaml010064400017500001750000000002241333632531700170130ustar0000000000000000Document: - Element: tag_name: root children: - Element: tag_name: p attributes: a: "b" roxmltree-0.7.3/tests/files/entity_011.xml010064400017500001750000000003151336071642600166550ustar0000000000000000 ]>

roxmltree-0.7.3/tests/files/entity_011.yaml010064400017500001750000000005151336071643600170220ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n " - Element: tag_name: p attributes: a: " A B " - Text: "\n " - Element: tag_name: p attributes: a: "\r\rA\n\nB\r\n\r\n" - Text: "\n" roxmltree-0.7.3/tests/files/entity_012.xml010064400017500001750000000015521336555153600166660ustar0000000000000000 ]>

&a;

&a;&a;

&a;&a;&a;

&d;

&d;&d;

&d;&d;&d;

&a;&d;

&d;&a;

&a;&d;&a;

&d;&a;&d;

&a;q&a;

&d;q&d;

&a;q&d;

&d;q&a;

&a;&da;

&da;&a;

&d;&da;

&da;&d;

&da;&da;

&a;&a;q

q&a;&a;

q&a;&a;q

&d;&d;q

q&d;&d;

q&d;&d;q

&da;&da;q

q&da;&da;

q&da;&da;q

roxmltree-0.7.3/tests/files/entity_012.yaml010064400017500001750000000112241336555153600170250ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n " - Element: tag_name: p attributes: id: "1" children: - Text: "\n" - Text: "\n " - Element: tag_name: p attributes: id: "2" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "3" children: - Text: "\n\n\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "4" children: - Text: "\n" - Text: "\n " - Element: tag_name: p attributes: id: "5" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "6" children: - Text: "\n\n\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "7" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "8" children: - Text: "\n\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "9" children: - Text: "\n\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "10" children: - Text: "\n\n\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "11" children: - Text: "\nq\n" - Text: "\n " - Element: tag_name: p attributes: id: "12" children: - Text: "\nq\n" - Text: "\n " - Element: tag_name: p attributes: id: "13" children: - Text: "\nq\n" - Text: "\n " - Element: tag_name: p attributes: id: "14" children: - Text: "\nq\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "15" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "16" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "17" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "18" children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "19" children: - Text: "\n\n" - Text: "\n\n " - Element: tag_name: p attributes: id: "20" children: - Text: "\n\nq" - Text: "\n " - Element: tag_name: p attributes: id: "21" children: - Text: "q\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "22" children: - Text: "q\n\nq" - Text: "\n\n " - Element: tag_name: p attributes: id: "23" children: - Text: "\n\nq" - Text: "\n " - Element: tag_name: p attributes: id: "24" children: - Text: "q\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "25" children: - Text: "q\n\nq" - Text: "\n\n " - Element: tag_name: p attributes: id: "26" children: - Text: "\n\nq" - Text: "\n " - Element: tag_name: p attributes: id: "27" children: - Text: "q\n\n" - Text: "\n " - Element: tag_name: p attributes: id: "28" children: - Text: "q\n\nq" - Text: "\n" roxmltree-0.7.3/tests/files/entity_err_001.xml010064400017500001750000000000151331617227200175150ustar0000000000000000 roxmltree-0.7.3/tests/files/entity_err_001.yaml010064400017500001750000000000551343532034200176560ustar0000000000000000error: "unknown entity reference 'd' at 1:7" roxmltree-0.7.3/tests/files/entity_err_002.xml010064400017500001750000000001151331617241100175120ustar0000000000000000 ]> roxmltree-0.7.3/tests/files/entity_err_002.yaml010064400017500001750000000000761334745757100177030ustar0000000000000000error: "a possible entity reference loop is detected at 2:20" roxmltree-0.7.3/tests/files/entity_err_003.xml010064400017500001750000000000741333655550400175310ustar0000000000000000'> ]> &p; roxmltree-0.7.3/tests/files/entity_err_003.yaml010064400017500001750000000000461333656025600176720ustar0000000000000000error: "unexpected close tag at 2:17" roxmltree-0.7.3/tests/files/entity_err_004.xml010064400017500001750000000000771333655564500175430ustar0000000000000000'> ]>

&p; roxmltree-0.7.3/tests/files/entity_err_004.yaml010064400017500001750000000000461333656026100176670ustar0000000000000000error: "unexpected close tag at 2:17" roxmltree-0.7.3/tests/files/entity_err_005.xml010064400017500001750000000014301334745775600175440ustar0000000000000000 ]> &lol9; roxmltree-0.7.3/tests/files/entity_err_005.yaml010064400017500001750000000000761334746161100176740ustar0000000000000000error: "a possible entity reference loop is detected at 3:35" roxmltree-0.7.3/tests/files/entity_err_006.xml010064400017500001750000000014271334746165400175430ustar0000000000000000 ]> roxmltree-0.7.3/tests/files/entity_err_006.yaml010064400017500001750000000000761334746161100176750ustar0000000000000000error: "a possible entity reference loop is detected at 3:35" roxmltree-0.7.3/tests/files/ns_001.xml010064400017500001750000000001151331617075100157520ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_001.yaml010064400017500001750000000003171334724032700161210ustar0000000000000000Document: - Element: tag_name: e@http://www.w3.org attributes: a: "b2" a@http://www.w3.org: "b1" namespaces: None: http://www.w3.org n1: http://www.w3.org roxmltree-0.7.3/tests/files/ns_002.xml010064400017500001750000000000601331617105600157510ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_002.yaml010064400017500001750000000002421334724032400161140ustar0000000000000000Document: - Element: tag_name: svg@http://www.w3.org/2000/svg attributes: a: "b" namespaces: None: http://www.w3.org/2000/svg roxmltree-0.7.3/tests/files/ns_003.xml010064400017500001750000000001021331617107400157470ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_003.yaml010064400017500001750000000003131331620624500161140ustar0000000000000000Document: - Element: tag_name: svg@http://www.w3.org/2000/svg attributes: a: "b" a@http://www.w3.org/2000/svg: "b" namespaces: svg: http://www.w3.org/2000/svg roxmltree-0.7.3/tests/files/ns_004.xml010064400017500001750000000001241331617114300157510ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_004.yaml010064400017500001750000000003011331620663300161130ustar0000000000000000Document: - Element: tag_name: svg attributes: href@http://www.w3.org/1999/xlink: "https://www.w3.org/TR/SVG11/" namespaces: t: http://www.w3.org/1999/xlink roxmltree-0.7.3/tests/files/ns_005.xml010064400017500001750000000000341331617117100157530ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_005.yaml010064400017500001750000000001741331620641200161170ustar0000000000000000Document: - Element: tag_name: svg attributes: space@http://www.w3.org/XML/1998/namespace: "preserve" roxmltree-0.7.3/tests/files/ns_006.xml010064400017500001750000000001151331617123300157530ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_006.yaml010064400017500001750000000001741331620626400161250ustar0000000000000000Document: - Element: tag_name: svg attributes: space@http://www.w3.org/XML/1998/namespace: "preserve" roxmltree-0.7.3/tests/files/ns_007.xml010064400017500001750000000002071342776253400157730ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_007.yaml010064400017500001750000000004431342775660000161340ustar0000000000000000Document: - Element: tag_name: e@http://www.uvic.ca attributes: attr: "no_ns" attr@http://www.ietf.org: "b_ns" attr@http://www.w3.org: "a_ns" namespaces: None: http://www.uvic.ca a: http://www.w3.org b: http://www.ietf.org roxmltree-0.7.3/tests/files/ns_008.xml010064400017500001750000000000651331617131100157560ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_008.yaml010064400017500001750000000004541334724031100161230ustar0000000000000000Document: - Element: tag_name: root@http://www.w3.org namespaces: None: http://www.w3.org children: - Text: "\n " - Element: tag_name: item@http://www.w3.org namespaces: None: http://www.w3.org - Text: "\n" roxmltree-0.7.3/tests/files/ns_009.xml010064400017500001750000000001171331617134600157650ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_009.yaml010064400017500001750000000004541334724030400161260ustar0000000000000000Document: - Element: tag_name: root@http://www.w3.org namespaces: None: http://www.w3.org children: - Text: "\n " - Element: tag_name: item@http://www.w2.org namespaces: None: http://www.w2.org - Text: "\n" roxmltree-0.7.3/tests/files/ns_010.xml010064400017500001750000000001441331617137200157540ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_010.yaml010064400017500001750000000010271334724027600161230ustar0000000000000000Document: - Element: tag_name: root@http://www.w3.org namespaces: None: http://www.w3.org children: - Text: "\n " - Element: tag_name: g@http://www.w3.org namespaces: None: http://www.w3.org children: - Text: "\n " - Element: tag_name: item@http://www.w2.org namespaces: None: http://www.w2.org - Text: "\n " - Text: "\n" roxmltree-0.7.3/tests/files/ns_011.xml010064400017500001750000000000271331617142000157470ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_011.yaml010064400017500001750000000001601334724027000161130ustar0000000000000000Document: - Element: tag_name: root attributes: a: "b" namespaces: None: "" roxmltree-0.7.3/tests/files/ns_012.xml010064400017500001750000000001371331622445300157570ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_012.yaml010064400017500001750000000006471334724026400161310ustar0000000000000000Document: - Element: tag_name: root@http://www.w3.org namespaces: None: http://www.w3.org test: http://www.w3.org children: - Text: "\n " - Element: tag_name: item@http://www.w3.org namespaces: None: http://www.w3.org test: http://www.w3.org children: - Text: "\n " - Text: "\n" roxmltree-0.7.3/tests/files/ns_013.xml010064400017500001750000000001321331617147400157570ustar0000000000000000 ]>

roxmltree-0.7.3/tests/files/ns_013.yaml010064400017500001750000000002121334724025300161140ustar0000000000000000Document: - Element: tag_name: p@http://www.w3.org namespaces: None: http://www.w3.org t: http://www.w3.org roxmltree-0.7.3/tests/files/ns_014.xml010064400017500001750000000000171333651274700157660ustar0000000000000000

roxmltree-0.7.3/tests/files/ns_014.yaml010064400017500001750000000001161333651276200161250ustar0000000000000000Document: - Element: tag_name: p attributes: id: "test" roxmltree-0.7.3/tests/files/ns_015.xml010064400017500001750000000001131334725045400157600ustar0000000000000000

roxmltree-0.7.3/tests/files/ns_015.yaml010064400017500001750000000005461334725046700161400ustar0000000000000000Document: - Element: tag_name: e@http://www.w3.org namespaces: None: http://www.w3.org n: http://www.w3.org children: - Text: "\n " - Element: tag_name: p@http://www.w3.org namespaces: None: http://www.w3.org n: http://www.w3.org - Text: "\n" roxmltree-0.7.3/tests/files/ns_016.xml010064400017500001750000000001411350220575000157520ustar0000000000000000

roxmltree-0.7.3/tests/files/ns_016.yaml010064400017500001750000000011371350221174500161230ustar0000000000000000Document: - Element: tag_name: e@http://www.w3.org namespaces: None: http://www.w3.org children: - Text: "\n " - Element: tag_name: p@http://www.w3.org namespaces: None: http://www.w3.org n: http://www.w4.org children: - Text: "\n " - Element: tag_name: div@http://www.w4.org namespaces: None: http://www.w3.org n: http://www.w4.org - Text: "\n " - Text: "\n" roxmltree-0.7.3/tests/files/ns_017.xml010064400017500001750000000001241352215305500157560ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_017.yaml010064400017500001750000000006331352215305500161250ustar0000000000000000Document: - Element: tag_name: e@http://www.w3.org namespaces: None: http://www.w3.org n: http://www.w3.org children: - Text: "\n " - Element: tag_name: p@http://www.w3.org namespaces: None: http://www.w3.org n: http://www.w3.org children: - Text: "\n " - Text: "\n" roxmltree-0.7.3/tests/files/ns_err_001.xml010064400017500001750000000000711331617155600166270ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_001.yaml010064400017500001750000000000731331623751700167730ustar0000000000000000error: "'xml' namespace prefix mapped to wrong URI at 1:6" roxmltree-0.7.3/tests/files/ns_err_002.xml010064400017500001750000000000661331617157700166370ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_002.yaml010064400017500001750000000001051331623752400167660ustar0000000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.7.3/tests/files/ns_err_003.xml010064400017500001750000000000641331617162100166240ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_003.yaml010064400017500001750000000001051331623752700167720ustar0000000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.7.3/tests/files/ns_err_004.xml010064400017500001750000000000771331617164200166340ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_004.yaml010064400017500001750000000000621331623753200167710ustar0000000000000000error: "namespace 'n' at 1:34 is already defined" roxmltree-0.7.3/tests/files/ns_err_005.xml010064400017500001750000000000711331617166500166340ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_005.yaml010064400017500001750000000001051331623753700167750ustar0000000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.7.3/tests/files/ns_err_006.xml010064400017500001750000000000731331617170600166330ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_006.yaml010064400017500001750000000001051331623754200167720ustar0000000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.7.3/tests/files/ns_err_007.xml010064400017500001750000000000551331617172700166370ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_007.yaml010064400017500001750000000001051331623754600167770ustar0000000000000000error: "the 'xmlns' URI is used at 1:6, but it must not be declared" roxmltree-0.7.3/tests/files/ns_err_008.xml010064400017500001750000000000571331617175500166430ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_008.yaml010064400017500001750000000001051331623755200167750ustar0000000000000000error: "the 'xmlns' URI is used at 1:6, but it must not be declared" roxmltree-0.7.3/tests/files/ns_err_009.xml010064400017500001750000000000151331617177300166360ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_009.yaml010064400017500001750000000000771334222163500170000ustar0000000000000000error: "the 'xmlns' prefix is used at 1:2, but it must not be" roxmltree-0.7.3/tests/files/ns_err_010.xml010064400017500001750000000000071343702351000166140ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_010.yaml010064400017500001750000000000601343702356300167650ustar0000000000000000error: "an unknown namespace prefix 'a' at 1:2" roxmltree-0.7.3/tests/files/ns_err_011.xml010064400017500001750000000000521343702166500166260ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_011.yaml010064400017500001750000000000641343702356000167670ustar0000000000000000error: "an unknown namespace prefix 'test' at 1:30" roxmltree-0.7.3/tests/files/ns_err_012.xml010064400017500001750000000001041352215305500166170ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_012.yaml010064400017500001750000000000621352215305500167640ustar0000000000000000error: "expected 'n:item' tag, not 'item' at 3:5" roxmltree-0.7.3/tests/files/ns_err_013.xml010064400017500001750000000001361352215305500166250ustar0000000000000000 roxmltree-0.7.3/tests/files/ns_err_013.yaml010064400017500001750000000000621352215305500167650ustar0000000000000000error: "expected 'n:item' tag, not 'item' at 3:5" roxmltree-0.7.3/tests/files/text_001.xml010064400017500001750000000000171333631101200163040ustar0000000000000000 roxmltree-0.7.3/tests/files/text_001.yaml010064400017500001750000000001201333631106500164510ustar0000000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.7.3/tests/files/text_002.xml010064400017500001750000000000241333631116200163110ustar0000000000000000 roxmltree-0.7.3/tests/files/text_002.yaml010064400017500001750000000001201333631117100164500ustar0000000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.7.3/tests/files/text_003.xml010064400017500001750000000000341333631123700163160ustar0000000000000000Some text roxmltree-0.7.3/tests/files/text_003.yaml010064400017500001750000000001301333631124400164530ustar0000000000000000Document: - Element: tag_name: root children: - Text: "Some text" roxmltree-0.7.3/tests/files/text_004.xml010064400017500001750000000000171333631556400163270ustar0000000000000000> roxmltree-0.7.3/tests/files/text_004.yaml010064400017500001750000000001201333631557600164670ustar0000000000000000Document: - Element: tag_name: root children: - Text: ">" roxmltree-0.7.3/tests/files/text_005.xml010064400017500001750000000000221333631664400163240ustar0000000000000000 roxmltree-0.7.3/tests/files/text_005.yaml010064400017500001750000000001251333631710200164600ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n\n\n" roxmltree-0.7.3/tests/files/text_006.xml010064400017500001750000000000211333632016200163120ustar0000000000000000 roxmltree-0.7.3/tests/files/text_006.yaml010064400017500001750000000001251333631710200164610ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n\n\n" roxmltree-0.7.3/tests/files/text_007.xml010064400017500001750000000000221333632036600163220ustar0000000000000000 roxmltree-0.7.3/tests/files/text_007.yaml010064400017500001750000000001231333632041600164620ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n\n" roxmltree-0.7.3/tests/files/text_008.xml010064400017500001750000000000221333633453600163270ustar0000000000000000 roxmltree-0.7.3/tests/files/text_008.yaml010064400017500001750000000001231333633457100164720ustar0000000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.7.3/tests/files/text_009.xml010064400017500001750000000000431333650322400163230ustar00000000000000000d > roxmltree-0.7.3/tests/files/text_009.yaml010064400017500001750000000001231333650323600164670ustar0000000000000000Document: - Element: tag_name: root children: - Text: "0d >" roxmltree-0.7.3/tests/files/text_010.xml010064400017500001750000000000731336071557200163260ustar0000000000000000 te xt roxmltree-0.7.3/tests/files/text_010.yaml010064400017500001750000000005031336071605600164640ustar0000000000000000Document: - Element: tag_name: text children: - Text: "\n " - Element: tag_name: tspan children: - Text: "t" - Text: "e\n " - Element: tag_name: tspan children: - Text: "x" - Text: "t\n" roxmltree-0.7.3/tests/files/text_011.xml010064400017500001750000000013251336555153600163330ustar0000000000000000

q

q

q

q

q

q

q q

q

q

q q

q

q

q q

roxmltree-0.7.3/tests/files/text_011.yaml010064400017500001750000000066211336555153600165010ustar0000000000000000Document: - Element: tag_name: root children: - Text: "\n " - Element: tag_name: p children: - Text: "\n" - Text: "\n " - Element: tag_name: p children: - Text: "\n\n" - Text: "\n " - Element: tag_name: p children: - Text: "\n\n\n" - Text: "\n\n " - Element: tag_name: p children: - Text: "\r" - Text: "\n " - Element: tag_name: p children: - Text: "\r\r" - Text: "\n " - Element: tag_name: p children: - Text: "\r\r\r" - Text: "\n\n " - Element: tag_name: p children: - Text: "\n\r" - Text: "\n " - Element: tag_name: p children: - Text: "\r\n" - Text: "\n\n " - Element: tag_name: p children: - Text: "\n\r\n" - Text: "\n " - Element: tag_name: p children: - Text: "\r\n\r" - Text: "\n\n " - Element: tag_name: p children: - Text: "\nq\n" - Text: "\n " - Element: tag_name: p children: - Text: "\rq\r" - Text: "\n " - Element: tag_name: p children: - Text: "\nq\r" - Text: "\n " - Element: tag_name: p children: - Text: "\rq\n" - Text: "\n\n " - Element: tag_name: p children: - Text: "\n\r\n" - Text: "\n " - Element: tag_name: p children: - Text: "\r\n\n" - Text: "\n " - Element: tag_name: p children: - Text: "\r\r\n" - Text: "\n " - Element: tag_name: p children: - Text: "\r\n\r" - Text: "\n " - Element: tag_name: p children: - Text: "\r\n\r\n" - Text: "\n\n " - Element: tag_name: p children: - Text: "\n\nq" - Text: "\n " - Element: tag_name: p children: - Text: "q\n\n" - Text: "\n " - Element: tag_name: p children: - Text: "q\n\nq" - Text: "\n\n " - Element: tag_name: p children: - Text: "\r\rq" - Text: "\n " - Element: tag_name: p children: - Text: "q\r\r" - Text: "\n " - Element: tag_name: p children: - Text: "q\r\rq" - Text: "\n\n " - Element: tag_name: p children: - Text: "\r\n\r\nq" - Text: "\n " - Element: tag_name: p children: - Text: "q\r\n\r\n" - Text: "\n " - Element: tag_name: p children: - Text: "q\r\n\r\nq" - Text: "\n" roxmltree-0.7.3/tests/files/tree_001.xml010064400017500001750000000002311331621445000162630ustar0000000000000000 text roxmltree-0.7.3/tests/files/tree_001.yaml010064400017500001750000000005241333656234600164460ustar0000000000000000Document: - Comment: " comment1 " - Element: tag_name: root children: - Text: "\n " - Comment: " comment2 " - Text: "\n " - PI: target: "target" value: "value" - Text: "\n " - Element: tag_name: item - Text: "\n text\n" roxmltree-0.7.3/tests/files/tree_002.xml010064400017500001750000000000131331417427300162700ustar0000000000000000 roxmltree-0.7.3/tests/files/tree_002.yaml010064400017500001750000000000541333633507700164440ustar0000000000000000Document: - Element: tag_name: root roxmltree-0.7.3/tests/files/tree_003.xml010064400017500001750000000001061331416260300162660ustar0000000000000000 roxmltree-0.7.3/tests/files/tree_003.yaml010064400017500001750000000002061333633525400164410ustar0000000000000000Document: - Element: tag_name: text children: - Text: "\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440" roxmltree-0.7.3/tests/files/tree_err_001.xml010064400017500001750000000001161333651157500171470ustar0000000000000000 roxmltree-0.7.3/tests/files/tree_err_001.yaml010064400017500001750000000000601333651212600173000ustar0000000000000000error: "the document does not have a root node" roxmltree-0.7.3/.cargo_vcs_info.json0000644000000001120000000000000131150ustar00{ "git": { "sha1": "78d716339aa4560bf78647b677c22b0cb8f59337" } } roxmltree-0.7.3/Cargo.lock0000644000000053110000000000000110760ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "ansi_term" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "difference" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "pretty_assertions" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "roxmltree" version = "0.7.3" dependencies = [ "pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "xmlparser 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "winapi" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "xmlparser" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a029430f0d744bc3d15dd474d591bed2402b645d024583082b9f63bb936dac6" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum xmlparser 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8110496c5bcc0d966b0b2da38d5a791aa139eeb0b80e7840a7463c2b806921eb"