roxmltree-0.20.0/.cargo_vcs_info.json0000644000000001360000000000100131340ustar { "git": { "sha1": "6dffb22cb4113a168928ca9a2eec3c2961820a98" }, "path_in_vcs": "" }roxmltree-0.20.0/.github/workflows/main.yml000064400000000000000000000012031046102023000167640ustar 00000000000000name: Rust on: [push, pull_request] env: CARGO_TERM_COLOR: always jobs: build: runs-on: ubuntu-latest strategy: matrix: rust: - 1.60.0 - stable steps: - name: Checkout uses: actions/checkout@v2 - name: Install toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: ${{ matrix.rust }} override: true - name: Run tests run: cargo test - name: Run tests without positions run: cargo test --no-default-features --features std - name: Build without std run: cargo build --no-default-features roxmltree-0.20.0/.gitignore000064400000000000000000000000341046102023000137110ustar 00000000000000target Cargo.lock .DS_Store roxmltree-0.20.0/CHANGELOG.md000064400000000000000000000242231046102023000135400ustar 00000000000000# 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.20.0] - 2024-05-23 ### Added - `Attribute::range`, `Attribute::range_qname`, `Attribute::range_value`. Thanks to [@Jayonas](https://github.com/Jayonas) ### Changed - `Attribute::position` is deprecated now. - The `positions` build feature uses a bit more memory now to better preserve attribute ranges. ## [0.19.0] - 2023-11-18 ### Changed - `xmlparser` is no longer a dependency and its fork is used internally. - ~5% faster parsing. - Fallback to `Rc` when `Arc` isn't available. - Bump MSRV to 1.60 - Bump edition to 2021 - `Error` variants have changed quite a lot. - XML declaration validation was simplified. We no longer check for attributes content. Meaning that `version`, `encoding` and `standalone` can contain any value now. But we still do check attribute names and order.
And while we did validated those attributes before, they weren't really affecting the parser in any way. Therefore the parsing behavior is mostly unchanged. ### Fixed - `ParsingOptions::allow_dtd = false` would not trigger an error when an empty DTD was present. ### Removed - The `xmlparser` dependency. ## [0.18.1] - 2023-09-30 ### Added - `ExpandedName::from_static`. Thanks to [@WhyNotHugo](https://github.com/WhyNotHugo) ### Fixed - Replace `\r` in CDATA as well. - `no_std` build. Thanks to [@wenyuzhao](https://github.com/wenyuzhao) ## [0.18.0] - 2023-02-04 ### Added - `StringStorage` that exposes an internal string storage. - Allocated strings are stored as `Arc` and not `String` now. - `Node::text_storage` - `Node::tail_storage` - `Attribute::value_storage` - `Node::range` ### Removed - `Node::position`. Use `Node::range` instead. ### Fixed - Some methods return longer lifetimes now. Thanks to [@adamreichold](https://github.com/adamreichold) - Overly verbose Debug implementations. Thanks to [@adamreichold](https://github.com/adamreichold) ## [0.17.0] - 2023-01-06 ### Added - Stored namespaces are deduplicated now, which can reduce memory usage on large files up to 10%. Thanks to [@adamreichold](https://github.com/adamreichold) ### Changed - `Node::namespaces` returns an iterator and not a slice now. ### Fixed - Unclosed root element is an error now. Thanks to [@adamreichold](https://github.com/adamreichold) ## [0.16.0] - 2022-11-20 ### Added - `positions` build feature. - `Node::position` - `Attribute::position` - `ParsingOptions::nodes_limit` to limit the maximum number of nodes to parse. ### Changed - Peak memory usage reduces by up to 20% depending on an input. Thanks to [@adamreichold](https://github.com/adamreichold) and [@tomjw64](https://github.com/tomjw64). - `Node::attributes` returns an iterator and not a slice now. ### Removed - 4GB input limit. - Do not store Node and Attribute end range in the original document. - Do not store attribute value range in the original document. - `Node::range`. Use `position` instead. - `Attribute::range`. Use `position` instead. - `Attribute::value_range` ## [0.15.1] - 2022-10-18 ### Fixed - Bump `xmlparser` to prevent stack overflow on some input. ## [0.15.0] - 2022-08-15 ### Changed - Extend `Document::descendants` lifetime. Thanks to [@DBLouis](https://github.com/DBLouis). - Derive `Eq` and `Hash` for `NodeId`. - Remove `pretty_assertions` dev-dependency. - MSRV is now 1.36 ## [0.14.1] - 2021-04-01 ### Changed - The `std` feature is enabled by default now. ## [0.14.0] - 2020-12-27 ### Added - An ability to reject XML with DTD in it. - The library is no_std + alloc now. - `ParsingOptions` - `Document::parse_with_options` - `Error::DtdDetected` ## [0.13.1] - 2020-12-19 ### Added - `Debug` for all public types. ## [0.13.0] - 2020-06-20 ### Changed - Better `ExpandedName` lifetimes. Thanks to [@eduardosm](https://github.com/eduardosm). ## [0.12.0] - 2020-06-20 ### Changed - Re-release 0.11.1, since it had a breaking change. ## [0.11.1] - 2020-06-19 ### Changed - Extend `ExpandedName` lifetime. Thanks to [@rkusa](https://github.com/rkusa). ## [0.11.0] - 2020-04-19 ### Added - Implement `Ord`, `PartialOrd` and `Hash` for `Node`. Thanks to [@tomjw64]. - `NodeId`, `Document::get_node` and `Node::id`. Thanks to [@tomjw64]. ### Changed - The input data size is limited by 4GiB now. - `Node` can be accessed from multiple threads now. - Reduce `Node` memory usage. - Greatly optimized `Descendants` iterator. Up to 5x faster in some cases. Thanks to [@tomjw64]. - Heavily reduce memory usage when document has a lot of namespaces. Thanks to [@tomjw64]. ### Removed - `Node::traverse`, `Traverse` and `Edge`. Use `Node::descendants` instead. ## [0.10.1] - 2020-03-28 ### Fixed - `Node::prev_sibling_element` and `Node::next_sibling_element` were returning the current node. ## [0.10.0] - 2020-03-18 ### Added - `Document::input_text` ### Changed - `Ancestors`, `PrevSiblings`, `NextSiblings`, `FirstChildren` and `LastChildren` were replaced with `AxisIter`. ### Fixed - Root node range. ## [0.9.1] - 2020-02-09 ### Changed - A better entity loop detection. A document can have an unlimited number of references at zero depth now. ## [0.9.0] - 2020-01-07 ### Changed - Moved to Rust 2018. - `xmlparser` updated. ## [0.8.0] - 2019-12-21 ### Added - `Error::MalformedEntityReference` ### Changed - Malformed entity reference is an error now. - Escaped `<` in attribute inside an ENTITY is an error now. - `xmlparser` updated with multiple fixes. ## [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`. [@tomjw64]: https://github.com/tomjw64 [Unreleased]: https://github.com/RazrFalcon/roxmltree/compare/v0.20.0..HEAD [0.20.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.19.0...v0.20.0 [0.19.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.18.1...v0.19.0 [0.18.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.18.0...v0.18.1 [0.18.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.17.0...v0.18.0 [0.17.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.16.0...v0.17.0 [0.16.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.15.1...v0.16.0 [0.15.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.15.0...v0.15.1 [0.15.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.14.1...v0.15.0 [0.14.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.14.0...v0.14.1 [0.14.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.13.1...v0.14.0 [0.13.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.13.0...v0.13.1 [0.13.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.12.0...v0.13.0 [0.12.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.11.1...v0.12.0 [0.11.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.11.0...v0.11.1 [0.11.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.10.1...v0.11.0 [0.10.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.10.0...v0.10.1 [0.10.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.9.1...v0.10.0 [0.9.1]: https://github.com/RazrFalcon/roxmltree/compare/v0.9.0...v0.9.1 [0.9.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.8.0...v0.9.0 [0.8.0]: https://github.com/RazrFalcon/roxmltree/compare/v0.7.3...v0.8.0 [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.20.0/Cargo.lock0000644000000002320000000000100111040ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "roxmltree" version = "0.20.0" roxmltree-0.20.0/Cargo.toml0000644000000017420000000000100111360ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.60" name = "roxmltree" version = "0.20.0" authors = ["Yevhenii 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 OR Apache-2.0" repository = "https://github.com/RazrFalcon/roxmltree" [features] default = [ "std", "positions", ] positions = [] std = [] roxmltree-0.20.0/Cargo.toml.orig0000644000000014050000000000100120710ustar [package] name = "roxmltree" version = "0.20.0" authors = ["Yevhenii Reizner "] keywords = ["xml", "parser", "tree", "dom"] categories = ["parser-implementations"] license = "MIT OR 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" edition = "2021" rust-version = "1.60" [workspace] members = ["benches"] exclude = ["testing-tools"] [features] default = ["std", "positions"] std = [] # Enables Nodes and Attributes position in the original document preserving. # Increases memory usage by `Range` for each Node. # Increases memory usage by `Range` + `u16` + `u8` for each Attribute. positions = [] roxmltree-0.20.0/Cargo.toml.orig000064400000000000000000000014051046102023000146130ustar 00000000000000[package] name = "roxmltree" version = "0.20.0" authors = ["Yevhenii Reizner "] keywords = ["xml", "parser", "tree", "dom"] categories = ["parser-implementations"] license = "MIT OR 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" edition = "2021" rust-version = "1.60" [workspace] members = ["benches"] exclude = ["testing-tools"] [features] default = ["std", "positions"] std = [] # Enables Nodes and Attributes position in the original document preserving. # Increases memory usage by `Range` for each Node. # Increases memory usage by `Range` + `u16` + `u8` for each Attribute. positions = [] roxmltree-0.20.0/LICENSE-APACHE000064400000000000000000000251371046102023000136600ustar 00000000000000 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.20.0/LICENSE-MIT000064400000000000000000000020731046102023000133620ustar 00000000000000The MIT License (MIT) Copyright (c) 2018 Yevhenii Reizner 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.20.0/README.md000064400000000000000000000205551046102023000132120ustar 00000000000000# roxmltree ![Build Status](https://github.com/RazrFalcon/roxmltree/workflows/Rust/badge.svg) [![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.60+](https://img.shields.io/badge/rust-1.60+-orange.svg)](https://www.rust-lang.org) Represents an [XML](https://www.w3.org/TR/xml/) document as a read-only tree. ```rust // Find element by id. let doc = roxmltree::Document::parse("")?; let elem = doc.descendants().find(|n| n.attribute("id") == Some("rect1"))?; 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. ## Parsing behavior Sadly, XML can be parsed in many different ways. *roxmltree* tries to mimic the behavior of Python's [lxml](https://lxml.de/). For more details see [docs/parsing.md](https://github.com/RazrFalcon/roxmltree/blob/master/docs/parsing.md). ## Alternatives | Feature/Crate | roxmltree | [libxml2] | [xmltree] | [sxd-document] | | ------------------------------- | :--------------: | :-----------------: | :--------------: | :--------------: | | 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** | ✓ | | ✓ | | | Language | Rust | C | Rust | Rust | | Dependencies | **0** | - | 2 | 2 | | Tested version | 0.20.0 | Apple-provided | 0.10.3 | 0.3.2 | | License | MIT / Apache-2.0 | MIT | 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. There is also `elementtree` and `treexml` crates, but they are abandoned for a long time. [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 [sxd-document]: https://crates.io/crates/sxd-document ## Performance Here are some benchmarks comparing `roxmltree` to other XML tree libraries. ```text test huge_roxmltree ... bench: 2,997,887 ns/iter (+/- 48,976) test huge_libxml2 ... bench: 6,850,666 ns/iter (+/- 306,180) test huge_sdx_document ... bench: 9,440,412 ns/iter (+/- 117,106) test huge_xmltree ... bench: 41,662,316 ns/iter (+/- 850,360) test large_roxmltree ... bench: 1,494,886 ns/iter (+/- 30,384) test large_libxml2 ... bench: 3,250,606 ns/iter (+/- 140,201) test large_sdx_document ... bench: 4,242,162 ns/iter (+/- 99,740) test large_xmltree ... bench: 13,980,228 ns/iter (+/- 229,363) test medium_roxmltree ... bench: 421,137 ns/iter (+/- 13,855) test medium_libxml2 ... bench: 950,984 ns/iter (+/- 34,099) test medium_sdx_document ... bench: 1,618,270 ns/iter (+/- 23,466) test medium_xmltree ... bench: 4,315,974 ns/iter (+/- 31,849) test tiny_roxmltree ... bench: 2,522 ns/iter (+/- 31) test tiny_libxml2 ... bench: 8,931 ns/iter (+/- 235) test tiny_sdx_document ... bench: 11,658 ns/iter (+/- 82) test tiny_xmltree ... bench: 20,215 ns/iter (+/- 303) ``` When comparing to streaming XML parsers `roxmltree` is slightly slower than `quick-xml`, but still way faster than `xmlrs`. Note that streaming parsers usually do not provide a proper string unescaping, DTD resolving and namespaces support. ```text test huge_quick_xml ... bench: 2,997,887 ns/iter (+/- 48,976) test huge_roxmltree ... bench: 3,147,424 ns/iter (+/- 49,153) test huge_xmlrs ... bench: 36,258,312 ns/iter (+/- 180,438) test large_quick_xml ... bench: 1,250,053 ns/iter (+/- 21,943) test large_roxmltree ... bench: 1,494,886 ns/iter (+/- 30,384) test large_xmlrs ... bench: 11,239,516 ns/iter (+/- 76,937) test medium_quick_xml ... bench: 206,232 ns/iter (+/- 2,157) test medium_roxmltree ... bench: 421,137 ns/iter (+/- 13,855) test medium_xmlrs ... bench: 3,975,916 ns/iter (+/- 44,967) test tiny_quick_xml ... bench: 2,233 ns/iter (+/- 70) test tiny_roxmltree ... bench: 2,522 ns/iter (+/- 31) test tiny_xmlrs ... bench: 17,155 ns/iter (+/- 429) ``` ### Notes The benchmarks were taken on a Apple M1 Pro. You can try running the benchmarks yourself by running `cargo bench` in the `benches` dir. - Since all libraries have a different XML support, benchmarking is a bit pointless. - We bench *libxml2* using the *[rust-libxml]* wrapper crate [xml-rs]: https://crates.io/crates/xml-rs [quick-xml]: https://crates.io/crates/quick-xml [rust-libxml]: https://github.com/KWARC/rust-libxml ## Memory overhead `roxmltree` tries to use as little memory as possible to allow parsing very large (multi-GB) XML files. The peak memory usage doesn't directly correlate with the file size but rather with the amount of nodes and attributes a file has. How many attributes had to be normalized (i.e. allocated). And how many text nodes had to be preprocessed (i.e. allocated). `roxmltree` never allocates element and attribute names, processing instructions and comments. By disabling the `positions` feature, you can shave 8 bytes from each node and attribute. On average, the overhead is around 6-8x the file size. For example, our 1.1GB sample XML will peak at 7.6GB RAM with default features enabled and at 6.8GB RAM when `positions` is disabled. ## Safety - This library must not panic. Any panic should be considered a critical bug and reported. - This library forbids `unsafe` code. ## 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 mapped onto the 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.20.0/docs/parsing.md000064400000000000000000000040751046102023000146470ustar 00000000000000# 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.20.0/examples/ast.rs000064400000000000000000000010251046102023000146750ustar 00000000000000fn main() { let args: Vec<_> = std::env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example ast -- input.xml"); std::process::exit(1); } let text = std::fs::read_to_string(&args[1]).unwrap(); let opt = roxmltree::ParsingOptions { allow_dtd: true, ..roxmltree::ParsingOptions::default() }; match roxmltree::Document::parse_with_options(&text, opt) { Ok(doc) => print!("{:?}", doc), Err(e) => println!("Error: {}.", e), } } roxmltree-0.20.0/examples/print_pos.rs000064400000000000000000000016201046102023000161240ustar 00000000000000#[cfg(feature = "positions")] fn main() { let args: Vec<_> = std::env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example print_pos -- input.xml"); std::process::exit(1); } let text = std::fs::read_to_string(&args[1]).unwrap(); let opt = roxmltree::ParsingOptions { allow_dtd: true, ..roxmltree::ParsingOptions::default() }; let doc = match roxmltree::Document::parse_with_options(&text, opt) { 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) ); } } } #[cfg(not(feature = "positions"))] fn main() {} roxmltree-0.20.0/examples/stats.rs000064400000000000000000000033641046102023000152540ustar 00000000000000use std::collections::HashSet; fn main() { let args: Vec<_> = std::env::args().collect(); if args.len() != 2 { println!("Usage:\n\tcargo run --example stats -- input.xml"); std::process::exit(1); } let text = std::fs::read_to_string(&args[1]).unwrap(); let opt = roxmltree::ParsingOptions { allow_dtd: true, ..roxmltree::ParsingOptions::default() }; let doc = match roxmltree::Document::parse_with_options(&text, opt) { Ok(v) => v, Err(e) => { println!("Error: {}.", e); std::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()); } } roxmltree-0.20.0/src/lib.rs000064400000000000000000001414001046102023000136270ustar 00000000000000/*! Represent an [XML](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 the [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 */ #![no_std] #![forbid(unsafe_code)] #![warn(missing_docs)] #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] extern crate alloc; #[cfg(feature = "std")] extern crate std; use core::cmp::Ordering; use core::fmt; use core::hash::{Hash, Hasher}; use core::num::NonZeroU32; use core::ops::Range; use alloc::vec::Vec; mod parse; mod tokenizer; #[cfg(test)] mod tokenizer_tests; pub use crate::parse::*; /// The URI. pub const NS_XML_URI: &str = "http://www.w3.org/XML/1998/namespace"; /// The prefix 'xml', which is by definition bound to NS_XML_URI const NS_XML_PREFIX: &str = "xml"; /// The URI. pub const NS_XMLNS_URI: &str = "http://www.w3.org/2000/xmlns/"; /// The string 'xmlns', which is used to declare new namespaces const XMLNS: &str = "xmlns"; /// Position in text. /// /// Position indicates a row/line and a column in the original text. Starting from 1:1. #[allow(missing_docs)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct TextPos { pub row: u32, pub col: u32, } impl TextPos { /// Constructs a new `TextPos`. pub fn new(row: u32, col: u32) -> TextPos { TextPos { row, col } } } impl fmt::Display for TextPos { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}:{}", self.row, self.col) } } /// 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_at` methods. text: &'input str, nodes: Vec>, attributes: 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::new(0), d: &self.nodes[0], doc: self, } } /// Returns the node of the tree with the given NodeId. /// /// Note: NodeId::new(0) represents the root node /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse("\ ///

/// text ///

/// ").unwrap(); /// /// use roxmltree::NodeId; /// assert_eq!(doc.get_node(NodeId::new(0)).unwrap(), doc.root()); /// assert_eq!(doc.get_node(NodeId::new(1)), doc.descendants().find(|n| n.has_tag_name("p"))); /// assert_eq!(doc.get_node(NodeId::new(2)), doc.descendants().find(|n| n.is_text())); /// assert_eq!(doc.get_node(NodeId::new(3)), None); /// ``` #[inline] pub fn get_node<'a>(&'a self, id: NodeId) -> Option> { self.nodes.get(id.get_usize()).map(|data| Node { id, d: data, 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<'_, 'input> { 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 { tokenizer::Stream::new(self.text).gen_text_pos_from(pos) } /// Returns the input text of the original document. /// /// # Examples /// /// ``` /// use roxmltree::*; /// /// let doc = Document::parse("").unwrap(); /// /// assert_eq!(doc.input_text(), ""); /// ``` #[inline] pub fn input_text(&self) -> &'input str { self.text } } 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_into_iter< T: fmt::Debug, E: ExactSizeIterator, I: IntoIterator, >( prefix: &str, data: I, depth: usize, f: &mut fmt::Formatter, ) -> Result<(), fmt::Error> { let data = data.into_iter(); if data.len() == 0 { 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_into_iter("attributes", child.attributes(), depth + 1, f)?; print_into_iter("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(()) } } /// A list of supported node types. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum NodeType { /// The root node of the `Document`. Root, /// An element node. /// /// Only an element can have a tag name and attributes. Element, /// A processing instruction. PI, /// A comment node. Comment, /// A text node. Text, } /// A processing instruction. #[derive(Clone, Copy, PartialEq, Eq, Debug)] #[allow(missing_docs)] pub struct PI<'input> { pub target: &'input str, pub value: Option<&'input str>, } /// A short range. /// /// Just like Range, but only for `u32` and copyable. #[derive(Clone, Copy, Debug)] struct ShortRange { start: u32, end: u32, } impl From> for ShortRange { #[inline] fn from(range: Range) -> Self { debug_assert!(range.start <= core::u32::MAX as usize); debug_assert!(range.end <= core::u32::MAX as usize); ShortRange::new(range.start as u32, range.end as u32) } } impl ShortRange { #[inline] fn new(start: u32, end: u32) -> Self { ShortRange { start, end } } #[inline] fn to_urange(self) -> Range { self.start as usize..self.end as usize } } /// A node ID stored as `u32`. /// /// An index into a `Tree`-internal `Vec`. /// /// Note that this value should be used with care since `roxmltree` doesn't /// check that `NodeId` actually belongs to a selected `Document`. /// So you can end up in a situation, when `NodeId` produced by one `Document` /// is used to select a node in another `Document`. #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct NodeId(NonZeroU32); impl NodeId { /// Construct a new `NodeId` from a `u32`. #[inline] pub fn new(id: u32) -> Self { debug_assert!(id < core::u32::MAX); // We are using `NonZeroU32` to reduce overhead of `Option`. NodeId(NonZeroU32::new(id + 1).unwrap()) } /// Returns the `u32` representation of the `NodeId`. #[inline] pub fn get(self) -> u32 { self.0.get() - 1 } /// Returns the `usize` representation of the `NodeId`. #[inline] pub fn get_usize(self) -> usize { self.get() as usize } } impl From for NodeId { #[inline] fn from(id: u32) -> Self { NodeId::new(id) } } impl From for NodeId { #[inline] fn from(id: usize) -> Self { // We already checked that `id` is limited by u32::MAX. debug_assert!(id <= core::u32::MAX as usize); NodeId::new(id as u32) } } #[derive(Debug)] enum NodeKind<'input> { Root, Element { tag_name: ExpandedNameIndexed<'input>, attributes: ShortRange, namespaces: ShortRange, }, PI(PI<'input>), Comment(StringStorage<'input>), Text(StringStorage<'input>), } #[derive(Debug)] struct NodeData<'input> { parent: Option, prev_sibling: Option, next_subtree: Option, last_child: Option, kind: NodeKind<'input>, #[cfg(feature = "positions")] range: Range, } #[cfg(target_has_atomic = "ptr")] type OwnedSharedString = alloc::sync::Arc; #[cfg(not(target_has_atomic = "ptr"))] type OwnedSharedString = alloc::rc::Rc; /// A string storage. /// /// Used by text nodes and attributes values. /// /// We try our best not to allocate strings, referencing the input string as much as possible. /// But in some cases post-processing is necessary and we have to allocate them. /// /// All owned, allocated strings are stored as `Arc` or as `Rc` on targets /// were `Arc` isn't available. /// And unlike `Cow<&str>`, `StringStorage` is immutable and can be cheaply cloned. #[derive(Clone, Eq, Debug)] pub enum StringStorage<'input> { /// A raw slice of the input string. Borrowed(&'input str), /// A reference-counted string. Owned(OwnedSharedString), } impl StringStorage<'_> { /// Creates a new owned string from `&str` or `String`. pub fn new_owned>(s: T) -> Self { StringStorage::Owned(s.into()) } /// Returns a string slice. pub fn as_str(&self) -> &str { match self { StringStorage::Borrowed(s) => s, StringStorage::Owned(s) => s, } } } impl PartialEq for StringStorage<'_> { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() } } impl core::fmt::Display for StringStorage<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.as_str()) } } impl core::ops::Deref for StringStorage<'_> { type Target = str; fn deref(&self) -> &Self::Target { self.as_str() } } #[derive(Clone, Debug)] struct AttributeData<'input> { name: ExpandedNameIndexed<'input>, value: StringStorage<'input>, #[cfg(feature = "positions")] range: Range, #[cfg(feature = "positions")] qname_len: u16, #[cfg(feature = "positions")] eq_len: u8, // includes any surrounding spaces } /// An attribute. #[derive(Copy, Clone)] pub struct Attribute<'a, 'input: 'a> { doc: &'a Document<'input>, data: &'a AttributeData<'input>, } impl<'a, 'input> Attribute<'a, 'input> { /// Returns attribute's namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes().nth(0).unwrap().namespace(), None); /// assert_eq!(doc.root_element().attributes().nth(1).unwrap().namespace(), Some("http://www.w3.org")); /// ``` #[inline] pub fn namespace(&self) -> Option<&'a str> { self.data.name.namespace(self.doc).map(Namespace::uri) } /// Returns attribute's name. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes().nth(0).unwrap().name(), "a"); /// assert_eq!(doc.root_element().attributes().nth(1).unwrap().name(), "a"); /// ``` #[inline] pub fn name(&self) -> &'input str { self.data.name.local_name } /// Returns attribute's value. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().attributes().nth(0).unwrap().value(), "b"); /// assert_eq!(doc.root_element().attributes().nth(1).unwrap().value(), "c"); /// ``` #[inline] pub fn value(&self) -> &'a str { &self.data.value } /// Returns attribute's value storage. /// /// Useful when you need a more low-level access to an allocated string. #[inline] pub fn value_storage(&self) -> &StringStorage<'input> { &self.data.value } /// Returns attribute's position 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 #[deprecated(note="replaced by `range`")] #[cfg(feature = "positions")] #[inline] pub fn position(&self) -> usize { self.data.range.start } /// Returns attribute's range in bytes in the original document. /// /// ```text /// /// ^^^^^^^^^^^^^^ /// ``` #[cfg(feature = "positions")] #[inline] pub fn range(&self) -> Range { self.data.range.clone() } /// Returns attribute's qname's range in bytes in the original document. /// /// ```text /// /// ^^^^^^ /// ``` /// /// To reduce memory usage the qname length is limited by u16::MAX. /// If the attribute exceeds that limit then the end of the returned range will be incorrect. #[cfg(feature = "positions")] #[inline] pub fn range_qname(&self) -> Range { let end = self.data.range.start + usize::from(self.data.qname_len); self.data.range.start..end } /// Returns attribute's value's range in bytes in the original document, excluding the surrounding quotes. /// /// If the attribute's value is an empty string then the `start` and `end` of this `Range` are equal, and indicate the closing quote. /// /// ```text /// /// ^^^^^ /// ``` /// /// To reduce memory usage the qname length is limited by u16::MAX, /// and the number of spaces around the equal sign is limited by u8::MAX. /// If the attribute exceeds those limits then the start of the returned range will be incorrect. #[cfg(feature = "positions")] #[inline] pub fn range_value(&self) -> Range { // +1 on start and -1 on end are to exclude the quotes around the value (all valid quotes are 1 byte) let start = self.data.range.start + usize::from(self.data.qname_len) + usize::from(self.data.eq_len) + 1; let end = self.data.range.end - 1; start..end } } impl PartialEq for Attribute<'_, '_> { #[inline] fn eq(&self, other: &Attribute<'_, '_>) -> bool { self.data.name.as_expanded_name(self.doc) == other.data.name.as_expanded_name(other.doc) && self.data.value == other.data.value } } impl fmt::Debug for Attribute<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!( f, "Attribute {{ name: {:?}, value: {:?} }}", self.data.name.as_expanded_name(self.doc), self.data.value ) } } /// A namespace. /// /// Contains URI and *prefix* pair. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Namespace<'input> { name: Option<&'input str>, uri: StringStorage<'input>, } impl<'input> Namespace<'input> { /// Returns namespace name/prefix. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces().nth(0).unwrap().name(), Some("n")); /// ``` /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces().nth(0).unwrap().name(), None); /// ``` #[inline] pub fn name(&self) -> Option<&'input str> { self.name } /// Returns namespace URI. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces().nth(0).unwrap().uri(), "http://www.w3.org"); /// ``` #[inline] pub fn uri(&self) -> &str { self.uri.as_ref() } } #[derive(Default)] struct Namespaces<'input> { // Deduplicated namespace values used throughout the document values: Vec>, // Indices into the above in tree order as the document is parsed tree_order: Vec, // Indices into the above sorted by value used for deduplication sorted_order: Vec, } impl<'input> Namespaces<'input> { fn push_ns( &mut self, name: Option<&'input str>, uri: StringStorage<'input>, ) -> Result<(), Error> { debug_assert_ne!(name, Some("")); let idx = match self.sorted_order.binary_search_by(|idx| { let value = &self.values[idx.0 as usize]; (value.name, value.uri.as_ref()).cmp(&(name, uri.as_str())) }) { Ok(sorted_idx) => self.sorted_order[sorted_idx], Err(sorted_idx) => { if self.values.len() > core::u16::MAX as usize { return Err(Error::NamespacesLimitReached); } let idx = NamespaceIdx(self.values.len() as u16); self.values.push(Namespace { name, uri }); self.sorted_order.insert(sorted_idx, idx); idx } }; self.tree_order.push(idx); Ok(()) } #[inline] fn push_ref(&mut self, tree_idx: usize) { let idx = self.tree_order[tree_idx]; self.tree_order.push(idx); } #[inline] fn exists(&self, start: usize, prefix: Option<&str>) -> bool { self.tree_order[start..] .iter() .any(|idx| self.values[idx.0 as usize].name == prefix) } fn shrink_to_fit(&mut self) { self.values.shrink_to_fit(); self.tree_order.shrink_to_fit(); self.sorted_order.shrink_to_fit(); } #[inline] fn get(&self, idx: NamespaceIdx) -> &Namespace<'input> { &self.values[idx.0 as usize] } } #[derive(Clone, Copy, Debug)] #[repr(transparent)] struct NamespaceIdx(u16); #[derive(Clone, Copy, Debug)] struct ExpandedNameIndexed<'input> { namespace_idx: Option, local_name: &'input str, } impl<'input> ExpandedNameIndexed<'input> { #[inline] fn namespace<'a>(&self, doc: &'a Document<'input>) -> Option<&'a Namespace<'input>> { self.namespace_idx.map(|idx| doc.namespaces.get(idx)) } #[inline] fn as_expanded_name<'a>(&self, doc: &'a Document<'input>) -> ExpandedName<'a, 'input> { ExpandedName { uri: self.namespace(doc).map(Namespace::uri), name: self.local_name, } } } /// An expanded name. /// /// Contains an namespace URI and name pair. #[derive(Clone, Copy, PartialEq, Eq)] pub struct ExpandedName<'a, 'b> { uri: Option<&'a str>, name: &'b str, } impl<'a, 'b> ExpandedName<'a, 'b> { /// 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<&'a 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) -> &'b str { self.name } } impl ExpandedName<'static, 'static> { /// Create a new instance from static data. /// /// # Example /// /// ```rust /// use roxmltree::ExpandedName; /// const DAV_HREF: ExpandedName = /// ExpandedName::from_static("urn:ietf:params:xml:ns:caldav:", "calendar-data"); /// ``` pub const fn from_static(uri: &'static str, name: &'static str) -> Self { Self { uri: Some(uri), name, } } } impl fmt::Debug for ExpandedName<'_, '_> { 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<'a, 'b> From<&'b str> for ExpandedName<'a, 'b> { #[inline] fn from(v: &'b str) -> Self { ExpandedName { uri: None, name: v } } } impl<'a, 'b> From<(&'a str, &'b str)> for ExpandedName<'a, 'b> { #[inline] fn from(v: (&'a str, &'b str)) -> Self { ExpandedName { uri: Some(v.0), name: v.1, } } } /// A node in a document. /// /// # Document Order /// /// The implementation of the `Ord` traits for `Node` is based on the concept of *document-order*. /// In layman's terms, document-order is the order in which one would see each element if /// one opened a document in a text editor or web browser and scrolled down. /// Document-order convention is followed in XPath, CSS Counters, and DOM selectors API /// to ensure consistent results from selection. /// One difference in `roxmltree` is that there is the notion of more than one document /// in existence at a time. While Nodes within the same document are in document-order, /// Nodes in different documents will be grouped together, but not in any particular /// order. /// /// As an example, if we have a Document `a` with Nodes `[a0, a1, a2]` and a /// Document `b` with Nodes `[b0, b1]`, these Nodes in order could be either /// `[a0, a1, a2, b0, b1]` or `[b0, b1, a0, a1, a2]` and roxmltree makes no /// guarantee which it will be. /// /// Document-order is defined here in the /// [W3C XPath Recommendation](https://www.w3.org/TR/xpath-3/#id-document-order) /// The use of document-order in DOM Selectors is described here in the /// [W3C Selectors API Level 1](https://www.w3.org/TR/selectors-api/#the-apis) #[derive(Clone, Copy)] pub struct Node<'a, 'input: 'a> { /// Node's ID. id: NodeId, /// The tree containing the node. doc: &'a Document<'input>, /// Node's data. d: &'a NodeData<'input>, } impl Eq for Node<'_, '_> {} impl PartialEq for Node<'_, '_> { #[inline] fn eq(&self, other: &Self) -> bool { (self.id, self.doc as *const _) == (other.id, other.doc as *const _) } } impl PartialOrd for Node<'_, '_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Node<'_, '_> { fn cmp(&self, other: &Self) -> Ordering { (self.id.0, self.doc as *const _).cmp(&(other.id.0, other.doc as *const _)) } } impl Hash for Node<'_, '_> { fn hash(&self, state: &mut H) { self.id.0.hash(state); (self.doc as *const Document).hash(state); (self.d as *const NodeData).hash(state); } } 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, 'input> { match self.d.kind { NodeKind::Element { ref tag_name, .. } => tag_name.as_expanded_name(self.doc), _ => "".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, 'm, 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_expanded_name(self.doc) == name, None => tag_name.local_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() .find(|ns| ns.name.is_none()) .map(|v| v.uri.as_ref()) } /// 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<&'input str> { if uri == NS_XML_URI { return Some(NS_XML_PREFIX); } self.namespaces() .find(|ns| &*ns.uri == 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<&str>) -> Option<&'a str> { self.namespaces() .find(|ns| ns.name == prefix) .map(|v| v.uri.as_ref()) } /// 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, 'm, N>(&self, name: N) -> Option<&'a str> where N: Into>, { let name = name.into(); self.attributes() .find(|a| a.data.name.as_expanded_name(self.doc) == name) .map(|a| a.value()) } /// 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, 'm, N>(&self, name: N) -> Option> where N: Into>, { let name = name.into(); self.attributes() .find(|a| a.data.name.as_expanded_name(self.doc) == 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, 'm, N>(&self, name: N) -> bool where N: Into>, { let name = name.into(); self.attributes() .any(|a| a.data.name.as_expanded_name(self.doc) == 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) -> Attributes<'a, 'input> { Attributes::new(self) } /// Returns element's namespaces. /// /// # Examples /// /// ``` /// let doc = roxmltree::Document::parse( /// "" /// ).unwrap(); /// /// assert_eq!(doc.root_element().namespaces().len(), 1); /// ``` #[inline] pub fn namespaces(&self) -> NamespaceIter<'a, 'input> { let namespaces = match self.d.kind { NodeKind::Element { ref namespaces, .. } => { &self.doc.namespaces.tree_order[namespaces.to_urange()] } _ => &[], }; NamespaceIter { doc: self.doc, namespaces: namespaces.iter(), } } /// 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> { self.text_storage().map(|s| s.as_str()) } /// Returns node's text storage. /// /// Useful when you need a more low-level access to an allocated string. pub fn text_storage(&self) -> Option<&'a StringStorage<'input>> { match self.d.kind { NodeKind::Element { .. } => match self.first_child() { Some(child) if child.is_text() => match self.doc.nodes[child.id.get_usize()].kind { NodeKind::Text(ref text) => Some(text), _ => None, }, _ => None, }, NodeKind::Comment(ref 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> { self.tail_storage().map(|s| s.as_str()) } /// Returns element's tail text storage. /// /// Useful when you need a more low-level access to an allocated string. pub fn tail_storage(&self) -> Option<&'a StringStorage<'input>> { if !self.is_element() { return None; } match self.next_sibling().map(|n| n.id) { Some(id) => match self.doc.nodes[id.get_usize()].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, } } /// Returns the parent of this node. #[inline] pub fn parent(&self) -> Option { self.d.parent.map(|id| self.doc.get_node(id).unwrap()) } /// Returns the parent element of this node. pub fn parent_element(&self) -> Option { self.ancestors().skip(1).find(|n| n.is_element()) } /// Returns the previous sibling of this node. #[inline] pub fn prev_sibling(&self) -> Option { self.d.prev_sibling.map(|id| self.doc.get_node(id).unwrap()) } /// Returns the previous sibling element of this node. pub fn prev_sibling_element(&self) -> Option { self.prev_siblings().skip(1).find(|n| n.is_element()) } /// Returns the next sibling of this node. #[inline] pub fn next_sibling(&self) -> Option { self.d .next_subtree .map(|id| self.doc.get_node(id).unwrap()) .and_then(|node| { let possibly_self = node .d .prev_sibling .expect("next_subtree will always have a previous sibling"); if possibly_self == self.id { Some(node) } else { None } }) } /// Returns the next sibling element of this node. pub fn next_sibling_element(&self) -> Option { self.next_siblings().skip(1).find(|n| n.is_element()) } /// Returns the first child of this node. #[inline] pub fn first_child(&self) -> Option { self.d .last_child .map(|_| self.doc.get_node(NodeId::new(self.id.get() + 1)).unwrap()) } /// Returns the first element child of this node. pub fn first_element_child(&self) -> Option { self.children().find(|n| n.is_element()) } /// Returns the last child of this node. #[inline] pub fn last_child(&self) -> Option { self.d.last_child.map(|id| self.doc.get_node(id).unwrap()) } /// 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.next_sibling().is_some() } /// Returns true if this node has children. #[inline] pub fn has_children(&self) -> bool { self.d.last_child.is_some() } /// Returns an iterator over ancestor nodes starting at this node. #[inline] pub fn ancestors(&self) -> AxisIter<'a, 'input> { AxisIter { node: Some(*self), next: Node::parent, } } /// Returns an iterator over previous sibling nodes starting at this node. #[inline] pub fn prev_siblings(&self) -> AxisIter<'a, 'input> { AxisIter { node: Some(*self), next: Node::prev_sibling, } } /// Returns an iterator over next sibling nodes starting at this node. #[inline] pub fn next_siblings(&self) -> AxisIter<'a, 'input> { AxisIter { node: Some(*self), next: Node::next_sibling, } } /// Returns an iterator over first children nodes starting at this node. #[inline] pub fn first_children(&self) -> AxisIter<'a, 'input> { AxisIter { node: Some(*self), next: Node::first_child, } } /// Returns an iterator over last children nodes starting at this node. #[inline] pub fn last_children(&self) -> AxisIter<'a, 'input> { AxisIter { node: Some(*self), next: Node::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 over this node and its descendants. #[inline] pub fn descendants(&self) -> Descendants<'a, 'input> { Descendants::new(*self) } /// Returns node's range in bytes in the original document. #[cfg(feature = "positions")] #[inline] pub fn range(&self) -> Range { self.d.range.clone() } /// Returns node's NodeId #[inline] pub fn id(&self) -> NodeId { self.id } } 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(ref text) => write!(f, "Comment({:?})", text.as_str()), NodeKind::Text(ref text) => write!(f, "Text({:?})", text.as_str()), } } } /// Iterator over a node's attributes #[derive(Clone)] pub struct Attributes<'a, 'input> { doc: &'a Document<'input>, attrs: core::slice::Iter<'a, AttributeData<'input>>, } impl<'a, 'input> Attributes<'a, 'input> { #[inline] fn new(node: &Node<'a, 'input>) -> Attributes<'a, 'input> { let attrs = match node.d.kind { NodeKind::Element { ref attributes, .. } => { &node.doc.attributes[attributes.to_urange()] } _ => &[], }; Attributes { doc: node.doc, attrs: attrs.iter(), } } } impl<'a, 'input> Iterator for Attributes<'a, 'input> { type Item = Attribute<'a, 'input>; #[inline] fn next(&mut self) -> Option { self.attrs.next().map(|attr| Attribute { doc: self.doc, data: attr, }) } #[inline] fn nth(&mut self, n: usize) -> Option { self.attrs.nth(n).map(|attr| Attribute { doc: self.doc, data: attr, }) } #[inline] fn size_hint(&self) -> (usize, Option) { self.attrs.size_hint() } } impl<'a, 'input> DoubleEndedIterator for Attributes<'a, 'input> { #[inline] fn next_back(&mut self) -> Option { self.attrs.next_back().map(|attr| Attribute { doc: self.doc, data: attr, }) } } impl ExactSizeIterator for Attributes<'_, '_> {} impl fmt::Debug for Attributes<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.debug_struct("Attributes") .field("attrs", &self.attrs) .finish() } } /// Iterator over specified axis. #[derive(Clone)] pub struct AxisIter<'a, 'input: 'a> { node: Option>, next: fn(&Node<'a, 'input>) -> Option>, } impl<'a, 'input: 'a> Iterator for AxisIter<'a, 'input> { type Item = Node<'a, 'input>; #[inline] fn next(&mut self) -> Option { let node = self.node.take(); self.node = node.as_ref().and_then(self.next); node } } impl fmt::Debug for AxisIter<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.debug_struct("AxisIter") .field("node", &self.node) .field("next", &"fn()") .finish() } } /// Iterator over children. #[derive(Clone, Debug)] 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 } } } /// Iterator over a node and its descendants. #[derive(Clone)] pub struct Descendants<'a, 'input> { doc: &'a Document<'input>, nodes: core::iter::Enumerate>>, from: usize, } impl<'a, 'input> Descendants<'a, 'input> { #[inline] fn new(start: Node<'a, 'input>) -> Self { let from = start.id.get_usize(); let until = start .d .next_subtree .map(NodeId::get_usize) .unwrap_or(start.doc.nodes.len()); let nodes = start.doc.nodes[from..until].iter().enumerate(); Self { doc: start.doc, nodes, from, } } } impl<'a, 'input> Iterator for Descendants<'a, 'input> { type Item = Node<'a, 'input>; #[inline] fn next(&mut self) -> Option { self.nodes.next().map(|(idx, data)| Node { id: NodeId::from(self.from + idx), d: data, doc: self.doc, }) } #[inline] fn nth(&mut self, n: usize) -> Option { self.nodes.nth(n).map(|(idx, data)| Node { id: NodeId::from(self.from + idx), d: data, doc: self.doc, }) } #[inline] fn size_hint(&self) -> (usize, Option) { self.nodes.size_hint() } } impl<'a, 'input> DoubleEndedIterator for Descendants<'a, 'input> { #[inline] fn next_back(&mut self) -> Option { self.nodes.next_back().map(|(idx, data)| Node { id: NodeId::from(self.from + idx), d: data, doc: self.doc, }) } } impl ExactSizeIterator for Descendants<'_, '_> {} impl fmt::Debug for Descendants<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.debug_struct("Descendants") .field("nodes", &self.nodes) .field("from", &self.from) .finish() } } /// Iterator over the namespaces attached to a node. #[derive(Clone)] pub struct NamespaceIter<'a, 'input> { doc: &'a Document<'input>, namespaces: core::slice::Iter<'a, NamespaceIdx>, } impl<'a, 'input> Iterator for NamespaceIter<'a, 'input> { type Item = &'a Namespace<'input>; #[inline] fn next(&mut self) -> Option { self.namespaces .next() .map(|idx| self.doc.namespaces.get(*idx)) } #[inline] fn nth(&mut self, n: usize) -> Option { self.namespaces .nth(n) .map(|idx| self.doc.namespaces.get(*idx)) } #[inline] fn size_hint(&self) -> (usize, Option) { self.namespaces.size_hint() } } impl<'a, 'input> DoubleEndedIterator for NamespaceIter<'a, 'input> { #[inline] fn next_back(&mut self) -> Option { self.namespaces .next() .map(|idx| self.doc.namespaces.get(*idx)) } } impl ExactSizeIterator for NamespaceIter<'_, '_> {} impl fmt::Debug for NamespaceIter<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.debug_struct("NamespaceIter") .field("namespaces", &self.namespaces) .finish() } } roxmltree-0.20.0/src/parse.rs000064400000000000000000001236511046102023000142030ustar 00000000000000use alloc::string::{String, ToString}; use alloc::vec::Vec; use core::ops::Range; use crate::{ AttributeData, Document, ExpandedNameIndexed, NamespaceIdx, Namespaces, NodeData, NodeId, NodeKind, ShortRange, StringStorage, TextPos, NS_XMLNS_URI, NS_XML_PREFIX, NS_XML_URI, PI, XMLNS, }; use crate::tokenizer::{self, Reference, StrSpan, Stream}; type Result = core::result::Result; /// A list of all possible errors. #[derive(Clone, PartialEq, Eq, Hash, 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. /// /// expected, actual, position #[allow(missing_docs)] UnexpectedCloseTag(String, String, 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 malformed entity reference. /// /// A `&` character inside an attribute value or text indicates an entity reference. /// Otherwise, the document is not well-formed. MalformedEntityReference(TextPos), /// A possible entity reference loop. /// /// The current depth limit is 10. The max number of references per reference is 255. EntityReferenceLoop(TextPos), /// Attribute value cannot have a `<` character. InvalidAttributeValue(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, /// The root node was opened but never closed. UnclosedRootNode, /// An XML document can have only one XML declaration /// and it must be at the start of the document. UnexpectedDeclaration(TextPos), /// An XML with DTD detected. /// /// This error will be emitted only when `ParsingOptions::allow_dtd` is set to `false`. DtdDetected, /// Indicates that the [`ParsingOptions::nodes_limit`] was reached. NodesLimitReached, /// Indicates that too many attributes were parsed. AttributesLimitReached, /// Indicates that too many namespaces were parsed. NamespacesLimitReached, /// An invalid name. InvalidName(TextPos), /// A non-XML character has occurred. /// /// Valid characters are: NonXmlChar(char, TextPos), /// An invalid/unexpected character. /// /// expected, actual, position InvalidChar(u8, u8, TextPos), /// An invalid/unexpected character. /// /// expected, actual, position InvalidChar2(&'static str, u8, TextPos), /// An unexpected string. /// /// Contains what string was expected. InvalidString(&'static str, TextPos), /// An invalid ExternalID in the DTD. InvalidExternalID(TextPos), /// A comment cannot contain `--` or end with `-`. InvalidComment(TextPos), /// A Character Data node contains an invalid data. /// /// Currently, only `]]>` is not allowed. InvalidCharacterData(TextPos), /// An unknown token. UnknownToken(TextPos), /// The steam ended earlier than we expected. /// /// Should only appear on invalid input data. UnexpectedEndOfStream, } impl Error { /// Returns the error position. 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(_, pos) => pos, Error::UnknownNamespace(_, pos) => pos, Error::UnexpectedCloseTag(_, _, pos) => pos, Error::UnexpectedEntityCloseTag(pos) => pos, Error::UnknownEntityReference(_, pos) => pos, Error::MalformedEntityReference(pos) => pos, Error::EntityReferenceLoop(pos) => pos, Error::InvalidAttributeValue(pos) => pos, Error::DuplicatedAttribute(_, pos) => pos, Error::NoRootNode => TextPos::new(1, 1), Error::UnclosedRootNode => TextPos::new(1, 1), Error::UnexpectedDeclaration(pos) => pos, Error::DtdDetected => TextPos::new(1, 1), Error::NodesLimitReached => TextPos::new(1, 1), Error::AttributesLimitReached => TextPos::new(1, 1), Error::NamespacesLimitReached => TextPos::new(1, 1), Error::InvalidName(pos) => pos, Error::NonXmlChar(_, pos) => pos, Error::InvalidChar(_, _, pos) => pos, Error::InvalidChar2(_, _, pos) => pos, Error::InvalidString(_, pos) => pos, Error::InvalidExternalID(pos) => pos, Error::InvalidComment(pos) => pos, Error::InvalidCharacterData(pos) => pos, Error::UnknownToken(pos) => pos, Error::UnexpectedEndOfStream => TextPos::new(1, 1), } } } impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::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::MalformedEntityReference(pos) => { write!(f, "malformed entity reference 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::InvalidAttributeValue(pos) => { write!(f, "unescaped '<' found 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::UnclosedRootNode => { write!(f, "the root node was opened but never closed") } Error::UnexpectedDeclaration(pos) => { write!(f, "unexpected XML declaration at {}", pos) } Error::DtdDetected => { write!(f, "XML with DTD detected") } Error::NodesLimitReached => { write!(f, "nodes limit reached") } Error::AttributesLimitReached => { write!(f, "more than 2^32 attributes were parsed") } Error::NamespacesLimitReached => { write!(f, "more than 2^16 unique namespaces were parsed") } Error::InvalidName(pos) => { write!(f, "invalid name token at {}", pos) } Error::NonXmlChar(c, pos) => { write!(f, "a non-XML character {:?} found at {}", c, pos) } Error::InvalidChar(expected, actual, pos) => { write!( f, "expected '{}' not '{}' at {}", expected as char, actual as char, pos ) } Error::InvalidChar2(expected, actual, pos) => { write!( f, "expected {} not '{}' at {}", expected, actual as char, pos ) } Error::InvalidString(expected, pos) => { write!(f, "expected '{}' at {}", expected, pos) } Error::InvalidExternalID(pos) => { write!(f, "invalid ExternalID at {}", pos) } Error::InvalidComment(pos) => { write!(f, "comment at {} contains '--'", pos) } Error::InvalidCharacterData(pos) => { write!(f, "']]>' at {} is not allowed inside a character data", pos) } Error::UnknownToken(pos) => { write!(f, "unknown token at {}", pos) } Error::UnexpectedEndOfStream => { write!(f, "unexpected end of stream") } } } } #[cfg(feature = "std")] impl std::error::Error for Error { fn description(&self) -> &str { "an XML parsing error" } } /// Parsing options. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct ParsingOptions { /// Allow DTD parsing. /// /// When set to `false`, XML with DTD will cause an error. /// Empty DTD block is not an error. /// /// Currently, there is no option to simply skip DTD. /// Mainly because you will get `UnknownEntityReference` error later anyway. /// /// This flag is set to `false` by default for security reasons, /// but `roxmltree` still has checks for billion laughs attack, /// so this is just an extra security measure. /// /// Default: false pub allow_dtd: bool, /// Sets the maximum number of nodes to parse. /// /// Useful when dealing with random input to limit memory usage. /// /// Default: u32::MAX (no limit) pub nodes_limit: u32, } // Explicit for readability. #[allow(clippy::derivable_impls)] impl Default for ParsingOptions { fn default() -> Self { ParsingOptions { allow_dtd: false, nodes_limit: core::u32::MAX, } } } struct TempAttributeData<'input> { prefix: &'input str, local: &'input str, value: StringStorage<'input>, range: Range, #[allow(unused)] // only used for feature "positions" qname_len: u16, #[allow(unused)] // only used for feature "positions" eq_len: u8, } 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. /// /// This is a shorthand for `Document::parse_with_options(data, ParsingOptions::default())`. /// /// # 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 { Self::parse_with_options(text, ParsingOptions::default()) } /// Parses the input XML string using to selected options. /// /// We do not support `&[u8]` or `Reader` because the input must be an already allocated /// UTF-8 string. /// /// # Examples /// /// ``` /// let opt = roxmltree::ParsingOptions::default(); /// let doc = roxmltree::Document::parse_with_options("", opt).unwrap(); /// assert_eq!(doc.descendants().count(), 2); // root node + `e` element node /// ``` #[inline] pub fn parse_with_options(text: &str, opt: ParsingOptions) -> Result { parse(text, opt) } } struct Entity<'input> { name: &'input str, value: StrSpan<'input>, } #[derive(Clone, Copy)] struct TagNameSpan<'input> { prefix: &'input str, name: &'input str, pos: usize, prefix_pos: usize, } impl<'input> TagNameSpan<'input> { #[inline] fn new_null() -> Self { Self { prefix: "", name: "", pos: 0, prefix_pos: 0, } } } /// An entity loop detector. /// /// Limits: /// - Entities depth is 10. /// - Maximum number of entity references per entity reference is 255. /// /// Basically, if a text or an attribute has an entity reference and this reference /// has more than 10 nested references - this is an error. /// /// This is useful for simple loops like: /// /// ```text /// /// /// ``` /// /// And, if a text or an attribute has an entity reference and it references more /// than 255 references - this is an error. /// /// This is useful for cases like billion laughs attack, where depth can be pretty small, /// but the number of references is exponentially increasing: /// /// ```text /// /// /// /// /// /// ``` #[derive(Default)] struct LoopDetector { /// References depth. depth: u8, /// Number of references resolved by the root reference. references: u8, } impl LoopDetector { #[inline] fn inc_depth(&mut self, stream: &Stream) -> Result<()> { if self.depth < 10 { self.depth += 1; Ok(()) } else { Err(Error::EntityReferenceLoop(stream.gen_text_pos())) } } #[inline] fn dec_depth(&mut self) { if self.depth > 0 { self.depth -= 1; } // Reset references count after reaching zero depth. if self.depth == 0 { self.references = 0; } } #[inline] fn inc_references(&mut self, stream: &Stream) -> Result<()> { if self.depth == 0 { // Allow infinite amount of references at zero depth. Ok(()) } else { if self.references == core::u8::MAX { return Err(Error::EntityReferenceLoop(stream.gen_text_pos())); } self.references += 1; Ok(()) } } } struct Context<'input> { opt: ParsingOptions, namespace_start_idx: usize, current_attributes: Vec>, awaiting_subtree: Vec, parent_prefixes: Vec<&'input str>, entities: Vec>, after_text: bool, parent_id: NodeId, tag_name: TagNameSpan<'input>, loop_detector: LoopDetector, doc: Document<'input>, } impl<'input> Context<'input> { fn append_node(&mut self, kind: NodeKind<'input>, range: Range) -> Result { if self.doc.nodes.len() >= self.opt.nodes_limit as usize { return Err(Error::NodesLimitReached); } #[cfg(not(feature = "positions"))] let _ = range; let new_child_id = NodeId::from(self.doc.nodes.len()); let appending_element = matches!(kind, NodeKind::Element { .. }); self.doc.nodes.push(NodeData { parent: Some(self.parent_id), prev_sibling: None, next_subtree: None, last_child: None, kind, #[cfg(feature = "positions")] range, }); let last_child_id = self.doc.nodes[self.parent_id.get_usize()].last_child; self.doc.nodes[new_child_id.get_usize()].prev_sibling = last_child_id; self.doc.nodes[self.parent_id.get_usize()].last_child = Some(new_child_id); for id in &self.awaiting_subtree { self.doc.nodes[id.get_usize()].next_subtree = Some(new_child_id); } self.awaiting_subtree.clear(); if !appending_element { self.awaiting_subtree .push(NodeId::from(self.doc.nodes.len() - 1)); } Ok(new_child_id) } fn err_pos_at(&self, pos: usize) -> TextPos { self.doc.text_pos_at(pos) } } fn parse(text: &str, opt: ParsingOptions) -> Result { // 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), attributes: Vec::with_capacity(attributes_capacity), namespaces: Namespaces::default(), }; // Add a root node. doc.nodes.push(NodeData { parent: None, prev_sibling: None, next_subtree: None, last_child: None, kind: NodeKind::Root, #[cfg(feature = "positions")] range: 0..text.len(), }); doc.namespaces .push_ns(Some(NS_XML_PREFIX), StringStorage::Borrowed(NS_XML_URI))?; let mut ctx = Context { opt, namespace_start_idx: 1, current_attributes: Vec::with_capacity(16), entities: Vec::new(), awaiting_subtree: Vec::new(), parent_prefixes: Vec::new(), after_text: false, parent_id: NodeId::new(0), tag_name: TagNameSpan::new_null(), loop_detector: LoopDetector::default(), doc, }; ctx.parent_prefixes.push(""); tokenizer::parse(text, opt.allow_dtd, &mut ctx)?; let mut doc = ctx.doc; if !doc.root().children().any(|n| n.is_element()) { return Err(Error::NoRootNode); } if ctx.parent_prefixes.len() > 1 { return Err(Error::UnclosedRootNode); } doc.nodes.shrink_to_fit(); doc.attributes.shrink_to_fit(); doc.namespaces.shrink_to_fit(); Ok(doc) } impl<'input> tokenizer::XmlEvents<'input> for Context<'input> { fn token(&mut self, token: tokenizer::Token<'input>) -> Result<()> { match token { tokenizer::Token::ProcessingInstruction(target, value, range) => { let pi = NodeKind::PI(PI { target, value }); self.append_node(pi, range)?; self.after_text = false; } tokenizer::Token::Comment(text, range) => { self.append_node(NodeKind::Comment(StringStorage::Borrowed(text)), range)?; self.after_text = false; } tokenizer::Token::EntityDeclaration(name, definition) => { self.entities.push(Entity { name, value: definition, }); } tokenizer::Token::ElementStart(prefix, local, start) => { if prefix == XMLNS { let pos = self.err_pos_at(start + 1); return Err(Error::InvalidElementNamePrefix(pos)); } self.tag_name = TagNameSpan { prefix, name: local, pos: start, prefix_pos: start + 1, }; self.after_text = false; } tokenizer::Token::Attribute(range, qname_len, eq_len, prefix, local, value) => { process_attribute(range, qname_len, eq_len, prefix, local, value, self)?; } tokenizer::Token::ElementEnd(end, range) => { process_element(end, range, self)?; self.after_text = false; } tokenizer::Token::Text(text, range) => { process_text(text, range, self)?; } tokenizer::Token::Cdata(text, range) => { process_cdata(text, range, self)?; } } Ok(()) } } #[allow(clippy::too_many_arguments)] fn process_attribute<'input>( range: Range, qname_len: u16, eq_len: u8, prefix: &'input str, local: &'input str, value: StrSpan<'input>, ctx: &mut Context<'input>, ) -> Result<()> { let value = normalize_attribute(value, ctx)?; if prefix == XMLNS { // The xmlns namespace MUST NOT be declared as the default namespace. if value.as_str() == NS_XMLNS_URI { let pos = ctx.err_pos_at(range.start); return Err(Error::UnexpectedXmlnsUri(pos)); } let is_xml_ns_uri = value.as_str() == 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 == NS_XML_PREFIX { if !is_xml_ns_uri { let pos = ctx.err_pos_at(range.start); 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 = ctx.err_pos_at(range.start); return Err(Error::UnexpectedXmlUri(pos)); } } // Check for duplicated namespaces. if ctx .doc .namespaces .exists(ctx.namespace_start_idx, Some(local)) { let pos = ctx.err_pos_at(range.start); return Err(Error::DuplicatedNamespace(local.to_string(), pos)); } // Xml namespace should not be added to the namespaces. if !is_xml_ns_uri { ctx.doc.namespaces.push_ns(Some(local), value)?; } } else if local == XMLNS { // The xml namespace MUST NOT be declared as the default namespace. if value.as_str() == NS_XML_URI { let pos = ctx.err_pos_at(range.start); return Err(Error::UnexpectedXmlUri(pos)); } // The xmlns namespace MUST NOT be declared as the default namespace. if value.as_str() == NS_XMLNS_URI { let pos = ctx.err_pos_at(range.start); return Err(Error::UnexpectedXmlnsUri(pos)); } ctx.doc.namespaces.push_ns(None, value)?; } else { ctx.current_attributes.push(TempAttributeData { prefix, local, value, range, qname_len, eq_len, }); } Ok(()) } fn process_element<'input>( end_token: tokenizer::ElementEnd<'input>, token_range: Range, ctx: &mut Context<'input>, ) -> Result<()> { if ctx.tag_name.name.is_empty() { // May occur in XML like this: // '> ]> // &p; if let tokenizer::ElementEnd::Close(..) = end_token { return Err(Error::UnexpectedEntityCloseTag( ctx.err_pos_at(token_range.start), )); } else { unreachable!("should be already checked by the tokenizer"); } } let namespaces = ctx.resolve_namespaces(); ctx.namespace_start_idx = ctx.doc.namespaces.tree_order.len(); let attributes = resolve_attributes(namespaces, ctx)?; match end_token { tokenizer::ElementEnd::Empty => { let tag_ns_idx = get_ns_idx_by_prefix( namespaces, ctx.tag_name.prefix_pos, ctx.tag_name.prefix, ctx, )?; let new_element_id = ctx.append_node( NodeKind::Element { tag_name: ExpandedNameIndexed { namespace_idx: tag_ns_idx, local_name: ctx.tag_name.name, }, attributes, namespaces, }, ctx.tag_name.pos..token_range.end, )?; ctx.awaiting_subtree.push(new_element_id); } tokenizer::ElementEnd::Close(prefix, local) => { let parent_node = &mut ctx.doc.nodes[ctx.parent_id.get_usize()]; // should never panic as we start with the single prefix of the // root node and always push another one when changing the parent let parent_prefix = *ctx.parent_prefixes.last().unwrap(); #[cfg(feature = "positions")] { parent_node.range.end = token_range.end; } if let NodeKind::Element { ref tag_name, .. } = parent_node.kind { if prefix != parent_prefix || local != tag_name.local_name { return Err(Error::UnexpectedCloseTag( gen_qname_string(parent_prefix, tag_name.local_name), gen_qname_string(prefix, local), ctx.err_pos_at(token_range.start), )); } } ctx.awaiting_subtree.push(ctx.parent_id); if let Some(id) = parent_node.parent { ctx.parent_id = id; ctx.parent_prefixes.pop(); debug_assert!(!ctx.parent_prefixes.is_empty()); } else { unreachable!("should be already checked by the tokenizer"); } } tokenizer::ElementEnd::Open => { let tag_ns_idx = get_ns_idx_by_prefix( namespaces, ctx.tag_name.prefix_pos, ctx.tag_name.prefix, ctx, )?; ctx.parent_id = ctx.append_node( NodeKind::Element { tag_name: ExpandedNameIndexed { namespace_idx: tag_ns_idx, local_name: ctx.tag_name.name, }, attributes, namespaces, }, ctx.tag_name.pos..token_range.end, )?; ctx.parent_prefixes.push(ctx.tag_name.prefix); } } Ok(()) } impl Context<'_> { fn resolve_namespaces(&mut self) -> ShortRange { if let NodeKind::Element { ref namespaces, .. } = self.doc.nodes[self.parent_id.get_usize()].kind { let parent_ns = *namespaces; if self.namespace_start_idx == self.doc.namespaces.tree_order.len() { return parent_ns; } for i in parent_ns.to_urange() { if !self.doc.namespaces.exists( self.namespace_start_idx, self.doc .namespaces .get(self.doc.namespaces.tree_order[i]) .name, ) { self.doc.namespaces.push_ref(i); } } } (self.namespace_start_idx..self.doc.namespaces.tree_order.len()).into() } } fn resolve_attributes(namespaces: ShortRange, ctx: &mut Context) -> Result { if ctx.current_attributes.is_empty() { return Ok(ShortRange::new(0, 0)); } if ctx.doc.attributes.len() + ctx.current_attributes.len() >= core::u32::MAX as usize { return Err(Error::AttributesLimitReached); } let start_idx = ctx.doc.attributes.len(); let current_attributes = core::mem::take(&mut ctx.current_attributes); for attr in current_attributes { let namespace_idx = if attr.prefix == NS_XML_PREFIX { // The prefix 'xml' is by definition bound to the namespace name // http://www.w3.org/XML/1998/namespace. This namespace is added // to the document on creation and is always element 0. Some(NamespaceIdx(0)) } else if attr.prefix.is_empty() { // 'The namespace name for an unprefixed attribute name // always has no value.' None } else { get_ns_idx_by_prefix(namespaces, attr.range.start, attr.prefix, ctx)? }; let attr_name = ExpandedNameIndexed { namespace_idx, local_name: attr.local, }; // Check for duplicated attributes. if ctx.doc.attributes[start_idx..].iter().any(|attr| { attr.name.as_expanded_name(&ctx.doc) == attr_name.as_expanded_name(&ctx.doc) }) { let pos = ctx.err_pos_at(attr.range.start); return Err(Error::DuplicatedAttribute(attr.local.to_string(), pos)); } ctx.doc.attributes.push(AttributeData { name: attr_name, value: attr.value, #[cfg(feature = "positions")] range: attr.range, #[cfg(feature = "positions")] qname_len: attr.qname_len, #[cfg(feature = "positions")] eq_len: attr.eq_len, }); } Ok((start_idx..ctx.doc.attributes.len()).into()) } fn process_text<'input>( text: &'input str, range: Range, ctx: &mut Context<'input>, ) -> Result<()> { // Add text as is if it has only valid characters. if !text.bytes().any(|b| b == b'&' || b == b'\r') { append_text(StringStorage::Borrowed(text), range, ctx)?; ctx.after_text = true; return Ok(()); } let mut text_buffer = TextBuffer::new(); let mut is_as_is = false; // TODO: explain let mut stream = Stream::from_substr(ctx.doc.text, range.clone()); while !stream.at_end() { match parse_next_chunk(&mut stream, &ctx.entities)? { NextChunk::Byte(c) => { if is_as_is { text_buffer.push_raw(c); is_as_is = false; } else { text_buffer.push_from_text(c, stream.at_end()); } } NextChunk::Char(c) => { for b in CharToBytes::new(c) { if ctx.loop_detector.depth > 0 { text_buffer.push_from_text(b, stream.at_end()); } else { // Characters not from entity should be added as is. // Not sure why... At least `lxml` produces the same result. text_buffer.push_raw(b); is_as_is = true; } } } NextChunk::Text(fragment) => { is_as_is = false; if !text_buffer.is_empty() { let storage = StringStorage::new_owned(text_buffer.to_str()); append_text(storage, range.clone(), ctx)?; text_buffer.clear(); ctx.after_text = true; } ctx.loop_detector.inc_references(&stream)?; ctx.loop_detector.inc_depth(&stream)?; let mut stream = Stream::from_substr(ctx.doc.text, fragment.range()); let prev_tag_name = ctx.tag_name; ctx.tag_name = TagNameSpan::new_null(); tokenizer::parse_content(&mut stream, ctx)?; ctx.tag_name = prev_tag_name; text_buffer.clear(); ctx.loop_detector.dec_depth(); } } } if !text_buffer.is_empty() { append_text(StringStorage::new_owned(text_buffer.finish()), range, ctx)?; ctx.after_text = true; } Ok(()) } // While the whole purpose of CDATA is to indicate to an XML library that this text // has to be stored as is, carriage return (`\r`) is still has to be replaced with `\n`. fn process_cdata<'input>( text: &'input str, range: Range, ctx: &mut Context<'input>, ) -> Result<()> { // Add text as is if it has only valid characters. if !text.as_bytes().contains(&b'\r') { append_text(StringStorage::Borrowed(text), range, ctx)?; ctx.after_text = true; return Ok(()); } let mut text_buffer = TextBuffer::new(); let count = text.chars().count(); for (i, c) in text.chars().enumerate() { for b in CharToBytes::new(c) { text_buffer.push_from_text(b, i + 1 == count); } } if !text_buffer.is_empty() { append_text(StringStorage::new_owned(text_buffer.finish()), range, ctx)?; ctx.after_text = true; } Ok(()) } fn append_text<'input>( text: StringStorage<'input>, range: Range, ctx: &mut Context<'input>, ) -> Result<()> { if ctx.after_text { // Prepend to a previous text node. if let Some(node) = ctx.doc.nodes.last_mut() { if let NodeKind::Text(ref mut prev_text) = node.kind { let text_str = text.as_str(); let prev_text_str = prev_text.as_str(); let mut concat_text = String::with_capacity(text_str.len() + prev_text_str.len()); concat_text.push_str(prev_text_str); concat_text.push_str(text_str); *prev_text = StringStorage::new_owned(concat_text); } } } else { ctx.append_node(NodeKind::Text(text), range)?; } Ok(()) } enum NextChunk<'a> { Byte(u8), Char(char), Text(StrSpan<'a>), } fn parse_next_chunk<'a>(stream: &mut Stream<'a>, entities: &[Entity<'a>]) -> Result> { debug_assert!(!stream.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 = stream.curr_byte_unchecked(); // Check for character/entity references. if c == b'&' { let start = stream.pos(); match stream.try_consume_reference() { Some(Reference::Char(ch)) => Ok(NextChunk::Char(ch)), Some(Reference::Entity(name)) => entities .iter() .find(|e| e.name == name) .map(|e| NextChunk::Text(e.value)) .ok_or_else(|| { let pos = stream.gen_text_pos_from(start); Error::UnknownEntityReference(name.into(), pos) }), None => { let pos = stream.gen_text_pos_from(start); Err(Error::MalformedEntityReference(pos)) } } } else { stream.advance(1); Ok(NextChunk::Byte(c)) } } // https://www.w3.org/TR/REC-xml/#AVNormalize fn normalize_attribute<'input>( text: StrSpan<'input>, ctx: &mut Context<'input>, ) -> Result> { if is_normalization_required(&text) { let mut text_buffer = TextBuffer::new(); _normalize_attribute(text, &mut text_buffer, ctx)?; Ok(StringStorage::new_owned(text_buffer.finish())) } else { Ok(StringStorage::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 { matches!(c, b'&' | b'\t' | b'\n' | b'\r') } text.as_str().bytes().any(check) } fn _normalize_attribute(text: StrSpan, buffer: &mut TextBuffer, ctx: &mut Context) -> Result<()> { let mut stream = Stream::from_substr(ctx.doc.text, text.range()); while !stream.at_end() { // Safe, because we already checked that the stream is not at the end. let c = stream.curr_byte_unchecked(); if c != b'&' { stream.advance(1); buffer.push_from_attr(c, stream.curr_byte().ok()); continue; } // Check for character/entity references. let start = stream.pos(); match stream.try_consume_reference() { Some(Reference::Char(ch)) => { for b in CharToBytes::new(ch) { if ctx.loop_detector.depth > 0 { // Escaped `<` inside an ENTITY is an error. // Escaped `<` outside an ENTITY is ok. if b == b'<' { return Err(Error::InvalidAttributeValue( stream.gen_text_pos_from(start), )); } 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)) => match ctx.entities.iter().find(|e| e.name == name) { Some(entity) => { ctx.loop_detector.inc_references(&stream)?; ctx.loop_detector.inc_depth(&stream)?; _normalize_attribute(entity.value, buffer, ctx)?; ctx.loop_detector.dec_depth(); } None => { let pos = stream.gen_text_pos_from(start); return Err(Error::UnknownEntityReference(name.into(), pos)); } }, None => { let pos = stream.gen_text_pos_from(start); return Err(Error::MalformedEntityReference(pos)); } } } Ok(()) } fn get_ns_idx_by_prefix( namespaces: ShortRange, prefix_pos: usize, prefix: &str, ctx: &Context, ) -> Result> { // Prefix CAN be empty when the default namespace was defined. // // Example: // let prefix_opt = if prefix.is_empty() { None } else { Some(prefix) }; let idx = ctx.doc.namespaces.tree_order[namespaces.to_urange()] .iter() .find(|idx| ctx.doc.namespaces.get(**idx).name == prefix_opt); match idx { Some(idx) => Ok(Some(*idx)), 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 = ctx.err_pos_at(prefix_pos); Err(Error::UnknownNamespace(prefix.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) } } } } fn gen_qname_string(prefix: &str, local: &str) -> String { if prefix.is_empty() { local.to_string() } else { alloc::format!("{}:{}", prefix, local) } } /// Iterate over `char` by `u8`. struct CharToBytes { buf: [u8; 4], idx: u8, } impl CharToBytes { #[inline] 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 } } struct TextBuffer { buffer: Vec, } impl TextBuffer { #[inline] fn new() -> Self { TextBuffer { buffer: Vec::with_capacity(32), } } #[inline] fn push_raw(&mut self, c: u8) { self.buffer.push(c); } fn push_from_attr(&mut self, mut current: u8, next: Option) { // \r in \r\n should be ignored. if current == b'\r' && next == Some(b'\n') { return; } // \n, \r and \t should be converted into spaces. current = match current { b'\n' | b'\r' | b'\t' => b' ', _ => current, }; self.buffer.push(current); } // 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 fn push_from_text(&mut self, c: u8, at_end: bool) { if self.buffer.last() == Some(&b'\r') { let idx = self.buffer.len() - 1; self.buffer[idx] = b'\n'; if at_end && c == b'\r' { self.buffer.push(b'\n'); } else if c != b'\n' { self.buffer.push(c); } } else if at_end && c == b'\r' { self.buffer.push(b'\n'); } else { self.buffer.push(c); } } #[inline] fn clear(&mut self) { self.buffer.clear(); } #[inline] fn is_empty(&self) -> bool { self.buffer.is_empty() } #[inline] fn to_str(&self) -> &str { // `unwrap` is safe, because buffer must contain a valid UTF-8 string. core::str::from_utf8(&self.buffer).unwrap() } #[inline] fn finish(self) -> String { // `unwrap` is safe, because buffer must contain a valid UTF-8 string. String::from_utf8(self.buffer).unwrap() } } roxmltree-0.20.0/src/tokenizer.rs000064400000000000000000000750601046102023000151030ustar 00000000000000use core::ops::Range; use core::str; use crate::{Error, TextPos}; type Result = core::result::Result; /// Extension methods for XML-subset only operations. trait XmlCharExt { /// Checks if the value is within the /// [NameStartChar](https://www.w3.org/TR/xml/#NT-NameStartChar) range. fn is_xml_name_start(&self) -> bool; /// Checks if the value is within the /// [NameChar](https://www.w3.org/TR/xml/#NT-NameChar) range. fn is_xml_name(&self) -> bool; /// Checks if the value is within the /// [Char](https://www.w3.org/TR/xml/#NT-Char) range. fn is_xml_char(&self) -> bool; } impl XmlCharExt for char { #[inline] fn is_xml_name_start(&self) -> bool { // Check for ASCII first. if *self as u32 <= 128 { return matches!(*self as u8, b'A'..=b'Z' | b'a'..=b'z' | b':' | b'_'); } matches!(*self as u32, 0x0000C0..=0x0000D6 | 0x0000D8..=0x0000F6 | 0x0000F8..=0x0002FF | 0x000370..=0x00037D | 0x00037F..=0x001FFF | 0x00200C..=0x00200D | 0x002070..=0x00218F | 0x002C00..=0x002FEF | 0x003001..=0x00D7FF | 0x00F900..=0x00FDCF | 0x00FDF0..=0x00FFFD | 0x010000..=0x0EFFFF) } #[inline] fn is_xml_name(&self) -> bool { // Check for ASCII first. if *self as u32 <= 128 { return (*self as u8).is_xml_name(); } matches!(*self as u32, 0x0000B7 | 0x0000C0..=0x0000D6 | 0x0000D8..=0x0000F6 | 0x0000F8..=0x0002FF | 0x000300..=0x00036F | 0x000370..=0x00037D | 0x00037F..=0x001FFF | 0x00200C..=0x00200D | 0x00203F..=0x002040 | 0x002070..=0x00218F | 0x002C00..=0x002FEF | 0x003001..=0x00D7FF | 0x00F900..=0x00FDCF | 0x00FDF0..=0x00FFFD | 0x010000..=0x0EFFFF) } #[inline] fn is_xml_char(&self) -> bool { // Does not check for surrogate code points U+D800-U+DFFF, // since that check was performed by Rust when the `&str` was constructed. if (*self as u32) < 0x20 { return (*self as u8).is_xml_space(); } !matches!(*self as u32, 0xFFFF | 0xFFFE) } } trait XmlByteExt { /// Checks if byte is a space. /// /// `[ \r\n\t]` fn is_xml_space(&self) -> bool; /// Checks if byte is within the ASCII /// [Char](https://www.w3.org/TR/xml/#NT-Char) range. fn is_xml_name(&self) -> bool; } impl XmlByteExt for u8 { #[inline] fn is_xml_space(&self) -> bool { matches!(*self, b' ' | b'\t' | b'\n' | b'\r') } #[inline] fn is_xml_name(&self) -> bool { matches!(*self, b'A'..=b'Z' | b'a'..=b'z'| b'0'..=b'9'| b':' | b'_' | b'-' | b'.') } } /// A string slice. /// /// Like `&str`, but also contains the position in the input XML /// from which it was parsed. #[must_use] #[derive(Clone, Copy)] pub struct StrSpan<'input> { text: &'input str, start: usize, } impl<'input> From<&'input str> for StrSpan<'input> { #[inline] fn from(text: &'input str) -> Self { StrSpan { text, start: 0 } } } impl<'input> StrSpan<'input> { #[inline] pub fn from_substr(text: &str, start: usize, end: usize) -> StrSpan { debug_assert!(start <= end); StrSpan { text: &text[start..end], start, } } #[inline] pub fn range(&self) -> Range { self.start..(self.start + self.text.len()) } #[inline] pub fn as_str(&self) -> &'input str { self.text } #[inline] fn slice_region(&self, start: usize, end: usize) -> &'input str { &self.text[start..end] } } pub enum Token<'input> { // ProcessingInstruction(&'input str, Option<&'input str>, Range), // Comment(&'input str, Range), // EntityDeclaration(&'input str, StrSpan<'input>), // , u16, u8, &'input str, &'input str, StrSpan<'input>), ElementEnd(ElementEnd<'input>, Range), // Contains text between elements including whitespaces. // Basically everything between `>` and `<`. // Except `]]>`, which is not allowed and will lead to an error. Text(&'input str, Range), // Cdata(&'input str, Range), } /// `ElementEnd` token. #[derive(Clone, Copy)] pub enum ElementEnd<'input> { /// Indicates `>` Open, /// Indicates `` Close(&'input str, &'input str), /// Indicates `/>` Empty, } pub trait XmlEvents<'input> { fn token(&mut self, token: Token<'input>) -> Result<()>; } // document ::= prolog element Misc* pub fn parse<'input>( text: &'input str, allow_dtd: bool, events: &mut dyn XmlEvents<'input>, ) -> Result<()> { let s = &mut Stream::new(text); // Skip UTF-8 BOM. if s.starts_with(&[0xEF, 0xBB, 0xBF]) { s.advance(3); } if s.starts_with(b"(s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>) -> Result<()> { while !s.at_end() { s.skip_spaces(); if s.starts_with(b"' fn parse_comment<'input>(s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>) -> Result<()> { let start = s.pos(); s.advance(4); let text = s.consume_chars(|s, c| !(c == '-' && s.starts_with(b"-->")))?; s.skip_string(b"-->")?; if text.contains("--") { return Err(Error::InvalidComment(s.gen_text_pos_from(start))); } if text.ends_with('-') { return Err(Error::InvalidComment(s.gen_text_pos_from(start))); } let range = s.range_from(start); events.token(Token::Comment(text, range))?; Ok(()) } // PI ::= '' Char*)))? '?>' // PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) fn parse_pi<'input>(s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>) -> Result<()> { if s.starts_with(b"")))?; let content = if !content.is_empty() { Some(content) } else { None }; s.skip_string(b"?>")?; let range = s.range_from(start); events.token(Token::ProcessingInstruction(target, content, range))?; Ok(()) } fn parse_doctype<'input>(s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>) -> Result<()> { let start = s.pos(); parse_doctype_start(s)?; s.skip_spaces(); if s.curr_byte() == Ok(b'>') { s.advance(1); return Ok(()); } s.advance(1); // [ while !s.at_end() { s.skip_spaces(); if s.starts_with(b"', therefore we have to skip possible spaces. s.advance(1); s.skip_spaces(); match s.curr_byte() { Ok(b'>') => { s.advance(1); break; } Ok(c) => { return Err(Error::InvalidChar2("'>'", c, s.gen_text_pos())); } Err(_) => { return Err(Error::UnexpectedEndOfStream); } } } else if s.starts_with(b"' fn parse_doctype_start(s: &mut Stream) -> Result<()> { s.advance(9); s.consume_spaces()?; s.skip_name()?; s.skip_spaces(); let _ = parse_external_id(s)?; s.skip_spaces(); let c = s.curr_byte()?; if c != b'[' && c != b'>' { return Err(Error::InvalidChar2("'[' or '>'", c, s.gen_text_pos())); } Ok(()) } // ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral fn parse_external_id(s: &mut Stream) -> Result { let v = if s.starts_with(b"SYSTEM") || s.starts_with(b"PUBLIC") { let start = s.pos(); s.advance(6); let id = s.slice_back(start); s.consume_spaces()?; let quote = s.consume_quote()?; let _ = s.consume_bytes(|c| c != quote); s.consume_byte(quote)?; if id == "SYSTEM" { // Ok } else { s.consume_spaces()?; let quote = s.consume_quote()?; let _ = s.consume_bytes(|c| c != quote); s.consume_byte(quote)?; } true } else { false }; Ok(v) } // EntityDecl ::= GEDecl | PEDecl // GEDecl ::= '' // PEDecl ::= '' fn parse_entity_decl<'input>( s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>, ) -> Result<()> { s.advance(8); s.consume_spaces()?; let is_ge = if s.try_consume_byte(b'%') { s.consume_spaces()?; false } else { true }; let name = s.consume_name()?; s.consume_spaces()?; if let Some(definition) = parse_entity_def(s, is_ge)? { events.token(Token::EntityDeclaration(name, definition))?; } s.skip_spaces(); s.consume_byte(b'>')?; Ok(()) } // EntityDef ::= EntityValue | (ExternalID NDataDecl?) // PEDef ::= EntityValue | ExternalID // EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | "'" ([^%&'] // | PEReference | Reference)* "'" // ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral // NDataDecl ::= S 'NDATA' S Name fn parse_entity_def<'input>( s: &mut Stream<'input>, is_ge: bool, ) -> Result>> { let c = s.curr_byte()?; match c { b'"' | b'\'' => { let quote = s.consume_quote()?; let start = s.pos(); s.skip_bytes(|c| c != quote); let value = s.slice_back_span(start); s.consume_byte(quote)?; Ok(Some(value)) } b'S' | b'P' => { if parse_external_id(s)? { if is_ge { s.skip_spaces(); if s.starts_with(b"NDATA") { s.advance(5); s.consume_spaces()?; s.skip_name()?; // TODO: NDataDecl is not supported } } Ok(None) } else { Err(Error::InvalidExternalID(s.gen_text_pos())) } } _ => { let pos = s.gen_text_pos(); Err(Error::InvalidChar2("a quote, SYSTEM or PUBLIC", c, pos)) } } } fn consume_decl(s: &mut Stream) -> Result<()> { s.skip_bytes(|c| c != b'>'); s.consume_byte(b'>')?; Ok(()) } // element ::= EmptyElemTag | STag content ETag // '<' Name (S Attribute)* S? '>' fn parse_element<'input>(s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>) -> Result<()> { let start = s.pos(); s.advance(1); // < let (prefix, local) = s.consume_qname()?; events.token(Token::ElementStart(prefix, local, start))?; let mut open = false; while !s.at_end() { let has_space = s.starts_with_space(); s.skip_spaces(); let start = s.pos(); match s.curr_byte()? { b'/' => { s.advance(1); s.consume_byte(b'>')?; let range = s.range_from(start); events.token(Token::ElementEnd(ElementEnd::Empty, range))?; break; } b'>' => { s.advance(1); let range = s.range_from(start); events.token(Token::ElementEnd(ElementEnd::Open, range))?; open = true; break; } _ => { // An attribute must be preceded with a whitespace. if !has_space { // Will always trigger an error. Which is what we want. s.consume_spaces()?; } // Manual inlining of `parse_attribute` for performance. // We cannot mark `parse_attribute` as `#[inline(always)]` // because it will blow up the binary size. let (prefix, local) = s.consume_qname()?; let qname_end = s.pos(); let qname_len = u16::try_from(qname_end - start).unwrap_or(u16::MAX); s.consume_eq()?; let eq_len = u8::try_from(s.pos() - qname_end).unwrap_or(u8::MAX); let quote = s.consume_quote()?; let quote_c = quote as char; // The attribute value must not contain the < character. let value_start = s.pos(); s.skip_chars(|_, c| c != quote_c && c != '<')?; let value = s.slice_back_span(value_start); s.consume_byte(quote)?; let end = s.pos(); events.token(Token::Attribute(start..end, qname_len, eq_len, prefix, local, value))?; } } } if open { parse_content(s, events)?; } Ok(()) } // Attribute ::= Name Eq AttValue fn parse_attribute<'input>( s: &mut Stream<'input>, ) -> Result<(&'input str, &'input str, StrSpan<'input>)> { let (prefix, local) = s.consume_qname()?; s.consume_eq()?; let quote = s.consume_quote()?; let quote_c = quote as char; // The attribute value must not contain the < character. let value_start = s.pos(); s.skip_chars(|_, c| c != quote_c && c != '<')?; let value = s.slice_back_span(value_start); s.consume_byte(quote)?; Ok((prefix, local, value)) } // content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* pub fn parse_content<'input>( s: &mut Stream<'input>, events: &mut dyn XmlEvents<'input>, ) -> Result<()> { while !s.at_end() { match s.curr_byte() { Ok(b'<') => match s.next_byte() { Ok(b'!') => { if s.starts_with(b"", Token::Comment("comment", 0..14) ); test!(comment_02, "", Token::Comment("", 0..13)); test!(comment_03, "", Token::Comment("", Token::Comment("", Token::Comment("<", Token::Comment("<", Token::Comment("-->", Token::Comment("<>", 0..9)); test!(comment_09, "", Token::Comment("<", 0..8)); test!(comment_10, "", Token::Comment("", Token::Comment("", 0..7)); macro_rules! test_err { ($name:ident, $text:expr) => { #[test] fn $name() { let mut collector = EventsCollector { tokens: Vec::new() }; assert!(xml::parse($text, true, &mut collector).is_err()); } }; } test_err!(comment_err_01, ""); test_err!(comment_err_02, ""); test_err!(comment_err_05, ""); test_err!(comment_err_07, ""); test_err!(comment_err_15, ""); test_err!(comment_err_20, ""); test_err!(comment_err_27, ""); test_err!(comment_err_28, ""); test_err!(comment_err_29, ""); test_err!(comment_err_33, ""); test_err!(comment_err_34, ""); test_err!(comment_err_35, ""); // DTD test!( dtd_01, "", // nothing to parse ); test!( dtd_02, "", // nothing to parse ); test!( dtd_03, "", // nothing to parse ); test!( dtd_04, "", // nothing to parse ); test!(dtd_05, "",); test!( dtd_06, "", Token::ElementStart("", "a", 19), Token::ElementEnd(ElementEnd::Empty, 21..23) ); test!(dtd_07, "",); test!(dtd_08, "",); test!( dtd_entity_01, " ]>", Token::EntityDecl("ns_extend", "http://ns.adobe.com/Extensibility/1.0/") ); test!( dtd_entity_02, " ]>", Token::EntityDecl("Pub-Status", "This is a pre-release of the\nspecification.") ); test!( dtd_entity_03, " ]>", ); test!( dtd_entity_04, " ]>", ); // TODO: NDATA will be ignored test!( dtd_entity_05, " ]>", ); // TODO: unsupported data will be ignored test!( dtd_entity_06, " ]>", Token::EntityDecl("ns_extend", "http://ns.adobe.com/Extensibility/1.0/") ); // We do not support !ELEMENT DTD token and it will be skipped. // Previously, we were calling `Tokenizer::next` after the skip, // which is recursive and could cause a stack overflow when there are too many sequential // unsupported tokens. // This tests checks that the current code do not crash with stack overflow. #[test] fn dtd_entity_07() { let mut text = "\n"); } text.push_str("]>\n"); let mut collector = EventsCollector { tokens: Vec::new() }; xml::parse(&text, true, &mut collector).unwrap(); } test!( dtd_err_01, "\u{000a}<", Token::Error("expected a whitespace not 'E' at 1:10".to_string()) ); test!( dtd_err_02, "' not '!' at 1:16".to_string()) ); // Document test!(document_01, "",); test!(document_02, " ",); test!(document_03, " \n\t\r ",); // BOM test!( document_05, str::from_utf8(b"\xEF\xBB\xBF").unwrap(), Token::ElementStart("", "a", 3), Token::ElementEnd(ElementEnd::Empty, 5..7) ); test!( document_06, str::from_utf8(b"\xEF\xBB\xBF").unwrap(), ); test!( document_07, "\n\n\ ", Token::Comment(" comment ", 39..55) ); test!( document_08, "\n\ ", Token::PI("xml-stylesheet", None, 0..18) ); test!( document_09, "\n\n\ ", Token::PI("xml-stylesheet", None, 39..57) ); // TODO: better error test!( document_err_01, "", Token::Error("invalid name token at 1:2".to_string()) ); test!( document_err_02, " &www---------Ӥ+----------w-----www_", Token::Error("unknown token at 1:2".to_string()) ); test!( document_err_03, "q", Token::Error("unknown token at 1:1".to_string()) ); test!( document_err_04, "", Token::Error("invalid name token at 1:2".to_string()) ); // TODO: better error test!( document_err_05, "", Token::Error("invalid name token at 1:22".to_string()) ); test!( document_err_06, " ", Token::Error("unknown token at 1:1".to_string()) ); // Elements test!( element_01, "", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Empty, 2..4) ); test!( element_02, "", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::ElementEnd(ElementEnd::Close("", "a"), 3..7) ); test!( element_03, " \t \n ", Token::ElementStart("", "a", 5), Token::ElementEnd(ElementEnd::Empty, 7..9) ); test!( element_04, " \t \n ", Token::ElementStart("", "b", 5), Token::ElementEnd(ElementEnd::Open, 7..8), Token::ElementStart("", "a", 8), Token::ElementEnd(ElementEnd::Empty, 10..12), Token::ElementEnd(ElementEnd::Close("", "b"), 12..16) ); test!( element_06, "<俄语 լեզու=\"ռուսերեն\">данные", Token::ElementStart("", "俄语", 0), Token::Attribute("", "լեզու", "ռուսերեն"), Token::ElementEnd(ElementEnd::Open, 37..38), Token::Text("данные", 38..50), Token::ElementEnd(ElementEnd::Close("", "俄语"), 50..59) ); test!( element_07, "", Token::ElementStart("svg", "circle", 0), Token::ElementEnd(ElementEnd::Open, 11..12), Token::ElementEnd(ElementEnd::Close("svg", "circle"), 12..25) ); test!( element_08, "<:circle/>", Token::ElementStart("", "circle", 0), Token::ElementEnd(ElementEnd::Empty, 8..10) ); test!( element_err_01, "<>", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_02, "", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::ElementEnd(ElementEnd::Close("", "a"), 3..7), Token::Error("unknown token at 1:8".to_string()) ); test!( element_err_10, "", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Empty, 2..4), Token::Error("unknown token at 1:5".to_string()) ); test!( element_err_11, "
", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Error("expected '>' not '/' at 1:8".to_string()) ); test!( element_err_12, "", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_13, "\ ", Token::ElementStart("", "root", 0), Token::ElementEnd(ElementEnd::Open, 5..6), Token::Text("\n", 6..7), Token::ElementEnd(ElementEnd::Close("", "root"), 7..14), Token::Error("unknown token at 3:1".to_string()) ); test!( element_err_14, "<-svg/>", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_15, "", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_16, "", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_17, "", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_18, "<::svg/>", Token::Error("invalid name token at 1:2".to_string()) ); test!( element_err_19, "<", Token::ElementStart("", "a", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Error("unknown token at 1:4".to_string()) ); test!( attribute_01, "", Token::ElementStart("", "a", 0), Token::Attribute("", "ax", "test"), Token::ElementEnd(ElementEnd::Empty, 12..14) ); test!( attribute_02, "", Token::ElementStart("", "a", 0), Token::Attribute("", "ax", "test"), Token::ElementEnd(ElementEnd::Empty, 12..14) ); test!( attribute_03, "", Token::ElementStart("", "a", 0), Token::Attribute("", "b", "test1"), Token::Attribute("", "c", "test2"), Token::ElementEnd(ElementEnd::Empty, 22..24) ); test!( attribute_04, "", Token::ElementStart("", "a", 0), Token::Attribute("", "b", "\"test1\""), Token::Attribute("", "c", "'test2'"), Token::ElementEnd(ElementEnd::Empty, 26..28) ); test!( attribute_05, "", Token::ElementStart("", "c", 0), Token::Attribute("", "a", "test1' c='test2"), Token::Attribute("", "b", "test1\" c=\"test2"), Token::ElementEnd(ElementEnd::Empty, 42..44) ); test!( attribute_06, "", Token::ElementStart("", "c", 0), Token::Attribute("", "a", "test1"), Token::ElementEnd(ElementEnd::Empty, 26..28) ); test!( attribute_07, "", Token::ElementStart("", "c", 0), Token::Attribute("q", "a", "b"), Token::ElementEnd(ElementEnd::Empty, 10..12) ); test!( attribute_err_01, "", Token::ElementStart("", "c", 0), Token::Error("expected a quote not 't' at 1:7".to_string()) ); test!( attribute_err_02, "", Token::ElementStart("", "c", 0), Token::Error("expected \'=\' not \'>\' at 1:5".to_string()) ); test!( attribute_err_03, "", Token::ElementStart("", "c", 0), Token::Error("expected '=' not '/' at 1:5".to_string()) ); test!( attribute_err_04, "", Token::ElementStart("", "c", 0), Token::Attribute("", "a", "b"), Token::Error("expected '=' not '/' at 1:11".to_string()) ); test!( attribute_err_05, "", Token::ElementStart("", "c", 0), Token::Error("expected ''' not '<' at 1:7".to_string()) ); test!( attribute_err_06, "", Token::ElementStart("", "c", 0), Token::Error("a non-XML character '\\u{1}' found at 1:7".to_string()) ); test!( attribute_err_07, "", Token::ElementStart("", "c", 0), Token::Attribute("", "a", "v"), Token::Error("expected a whitespace not 'b' at 1:9".to_string()) ); // PI test!(pi_01, "", Token::PI("xslt", Some("ma"), 0..11)); test!( pi_02, "", Token::PI("xslt", Some("m"), 0..13) ); test!(pi_03, "", Token::PI("xslt", None, 0..8)); test!(pi_04, "", Token::PI("xslt", None, 0..9)); test!( pi_05, "", Token::PI("xml-stylesheet", None, 0..18) ); test!( pi_err_01, "", Token::Error("invalid name token at 1:3".to_string()) ); test!(declaration_01, "",); test!(declaration_02, "",); test!(declaration_03, "",); test!(declaration_04, "",); test!(declaration_05, "",); test!(declaration_06, "",); test!( declaration_07, "", ); test!( declaration_08, "", ); test!(declaration_09, "",); test!(declaration_10, "",); // Declaration with an invalid order test!( declaration_err_01, "", Token::Error("expected 'version' at 1:7".to_string()) ); test!( declaration_err_05, "", Token::Error("expected '?>' at 1:21".to_string()) ); test!( declaration_err_06, "", Token::Error("expected '?>' at 1:55".to_string()) ); test!( declaration_err_07, "\u{000a}' at 3:7".to_string()) ); test!( declaration_err_08, "", Token::Error("expected 'version' at 2:2".to_string()) ); test!( declaration_err_09, "", Token::Error("expected 'version' at 2:2".to_string()) ); // XML declaration allowed only at the start of the document. test!( declaration_err_10, " ", Token::Error("unexpected XML declaration at 1:2".to_string()) ); // XML declaration allowed only at the start of the document. test!( declaration_err_11, "", Token::Comment(" comment ", 0..16), Token::Error("unexpected XML declaration at 1:17".to_string()) ); // Duplicate. test!( declaration_err_12, "", Token::Error("unexpected XML declaration at 1:22".to_string()) ); test!( declaration_err_13, "", Token::Error("a non-XML character '\\u{1}' found at 1:10".to_string()) ); test!( declaration_err_14, "", Token::Error("expected a whitespace not 'e' at 1:20".to_string()) ); test!( declaration_err_15, "", Token::Error("expected a whitespace not 's' at 1:37".to_string()) ); test!( declaration_err_16, "' at 1:20".to_string()) ); // Text test!( text_01, "

text

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text("text", 3..7), Token::ElementEnd(ElementEnd::Close("", "p"), 7..11) ); test!( text_02, "

text

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text(" text ", 3..9), Token::ElementEnd(ElementEnd::Close("", "p"), 9..13) ); // 欄 is EF A4 9D. And EF can be mistreated for UTF-8 BOM. test!( text_03, "

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text("欄", 3..6), Token::ElementEnd(ElementEnd::Close("", "p"), 6..10) ); test!( text_04, "

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text(" ", 3..4), Token::ElementEnd(ElementEnd::Close("", "p"), 4..8) ); test!( text_05, "

\r\n\t

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text(" \r\n\t ", 3..8), Token::ElementEnd(ElementEnd::Close("", "p"), 8..12) ); test!( text_06, "

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text(" ", 3..9), Token::ElementEnd(ElementEnd::Close("", "p"), 9..13) ); test!( text_07, "

]>

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Text("]>", 3..5), Token::ElementEnd(ElementEnd::Close("", "p"), 5..9) ); test!( text_err_01, "

]]>

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Error("']]>' at 1:7 is not allowed inside a character data".to_string()) ); test!( text_err_02, "

\u{0c}

", Token::ElementStart("", "p", 0), Token::ElementEnd(ElementEnd::Open, 2..3), Token::Error("a non-XML character '\\u{c}' found at 1:4".to_string()) ); roxmltree-0.20.0/tests/api.rs000064400000000000000000000263301046102023000142110ustar 00000000000000#![allow(clippy::bool_assert_comparison)] use roxmltree::*; #[test] fn error_size() { assert!(::std::mem::size_of::() <= 64); } #[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); } #[cfg(feature = "positions")] #[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(doc.root().range().start), TextPos::new(1, 1) ); assert_eq!(doc.text_pos_at(doc.root().range().end), TextPos::new(5, 1)); 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.range().end), TextPos::new(1, 9)); assert_eq!(doc.text_pos_at(attr.range_qname().start), TextPos::new(1, 4)); assert_eq!(doc.text_pos_at(attr.range_qname().end), TextPos::new(1, 5)); assert_eq!(doc.text_pos_at(attr.range_value().start), TextPos::new(1, 7)); assert_eq!(doc.text_pos_at(attr.range_value().end), TextPos::new(1, 8)); } // 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)); let text = p.first_child().unwrap(); assert_eq!(doc.text_pos_at(text.range().start), TextPos::new(3, 8)); } #[cfg(feature = "positions")] #[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.range().end), TextPos::new(1, 44)); assert_eq!(doc.text_pos_at(attr.range_qname().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.range_qname().end), TextPos::new(1, 40)); assert_eq!(doc.text_pos_at(attr.range_value().start), TextPos::new(1, 42)); assert_eq!(doc.text_pos_at(attr.range_value().end), TextPos::new(1, 43)); } } #[cfg(feature = "positions")] #[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)); } #[cfg(feature = "positions")] #[test] fn text_pos_04() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); if let Some(attr) = node.attribute_node("a") { assert_eq!(doc.text_pos_at(attr.range().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.range().end), TextPos::new(1, 43)); assert_eq!(doc.text_pos_at(attr.range_qname().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.range_qname().end), TextPos::new(1, 40)); assert_eq!(doc.text_pos_at(attr.range_value().start), TextPos::new(1, 42)); assert_eq!(doc.text_pos_at(attr.range_value().end), TextPos::new(1, 42)); } } #[cfg(feature = "positions")] #[test] fn text_pos_05() { let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); if let Some(attr) = node.attribute_node("a") { assert_eq!(doc.text_pos_at(attr.range().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.range().end), TextPos::new(1, 48)); assert_eq!(doc.text_pos_at(attr.range_qname().start), TextPos::new(1, 36)); assert_eq!(doc.text_pos_at(attr.range_qname().end), TextPos::new(1, 40)); assert_eq!(doc.text_pos_at(attr.range_value().start), TextPos::new(1, 47)); assert_eq!(doc.text_pos_at(attr.range_value().end), TextPos::new(1, 48)); } } #[cfg(feature = "positions")] #[test] fn text_pos_06() { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 let data = ""; let doc = Document::parse(data).unwrap(); let node = doc.root_element(); 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.range().end), TextPos::new(1, 269)); assert_eq!(doc.text_pos_at(attr.range_qname().start), TextPos::new(1, 4)); assert_eq!(doc.text_pos_at(attr.range_qname().end), TextPos::new(1, 5)); attr.range_value(); // unreliable since >254 spaces around equal sign, but still shouldn't panic } } #[test] fn next_sibling_element_01() { let data = "
"; let doc = roxmltree::Document::parse(data).unwrap(); let root = doc.root_element(); let a = root.first_element_child().unwrap(); let b = a.next_sibling_element().unwrap(); let c = b.next_sibling_element().unwrap(); assert!(c.next_sibling_element().is_none()); assert_eq!(root.tag_name().name(), "root"); assert_eq!(a.tag_name().name(), "a"); assert_eq!(b.tag_name().name(), "b"); assert_eq!(c.tag_name().name(), "c"); } #[test] fn next_prev_element_01() { let data = ""; let doc = roxmltree::Document::parse(data).unwrap(); let root = doc.root_element(); let c = root.last_element_child().unwrap(); let b = c.prev_sibling_element().unwrap(); let a = b.prev_sibling_element().unwrap(); assert!(a.prev_sibling_element().is_none()); assert_eq!(root.tag_name().name(), "root"); assert_eq!(a.tag_name().name(), "a"); assert_eq!(b.tag_name().name(), "b"); assert_eq!(c.tag_name().name(), "c"); } #[test] fn nodes_document_order() { let data = ""; let doc = roxmltree::Document::parse(data).unwrap(); let root = doc.root_element(); let a = root.first_element_child().unwrap(); let b = a.next_sibling_element().unwrap(); let c = b.next_sibling_element().unwrap(); let mut elems = vec![&b, &c, &a]; elems.sort(); assert!(elems[0] == &a); assert!(elems[1] == &b); assert!(elems[2] == &c); } #[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()); } #[test] fn tag_name_lifetime() { fn get_tag_name<'a, 'input>(node: &'a Node<'a, 'input>) -> &'input str { node.tag_name().name() } let data = ""; let doc = roxmltree::Document::parse(data).unwrap(); let root = doc.root_element(); assert_eq!(get_tag_name(&root), "e"); } roxmltree-0.20.0/tests/ast.rs000064400000000000000000000201471046102023000142270ustar 00000000000000use roxmltree::*; use std::fmt; use std::fmt::Write; use std::fs; use std::io::Read; use std::path; #[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 } } } fn actual_test(path: &str) { let path = path::Path::new(path); let expected = load_file(&path.with_extension("yaml")); let opt = ParsingOptions { allow_dtd: true, ..roxmltree::ParsingOptions::default() }; let input_xml = load_file(path); let doc = match Document::parse_with_options(&input_xml, opt) { 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() ); } } let attributes = child.attributes(); if attributes.len() != 0 { let mut attrs = Vec::new(); for attr in 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); } } let namespaces = child.namespaces(); if namespaces.len() != 0 { let mut ns_list = Vec::new(); for ns in 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(()) } macro_rules! test { ($name:ident) => { #[test] fn $name() { actual_test(&format!("tests/files/{}.xml", stringify!($name))) } }; } test!(attrs_001); test!(attrs_002); test!(attrs_003); test!(attrs_004); test!(attrs_005); test!(attrs_006); test!(attrs_err_001); test!(attrs_err_002); test!(cdata_001); test!(cdata_002); test!(cdata_003); test!(cdata_004); test!(cdata_005); test!(cdata_006); test!(cdata_007); test!(comments_001); test!(elems_err_001); test!(elems_err_002); test!(entity_001); test!(entity_002); test!(entity_003); test!(entity_004); test!(entity_005); test!(entity_006); test!(entity_007); test!(entity_008); test!(entity_009); test!(entity_010); test!(entity_011); test!(entity_012); test!(entity_013); test!(entity_014); test!(entity_err_001); test!(entity_err_002); test!(entity_err_003); test!(entity_err_004); test!(entity_err_005); test!(entity_err_006); test!(entity_err_007); test!(entity_err_008); test!(entity_err_009); test!(ns_001); test!(ns_002); test!(ns_003); test!(ns_004); test!(ns_005); test!(ns_006); test!(ns_007); test!(ns_008); test!(ns_009); test!(ns_010); test!(ns_011); test!(ns_012); test!(ns_013); test!(ns_014); test!(ns_015); test!(ns_016); test!(ns_017); test!(ns_err_001); test!(ns_err_002); test!(ns_err_003); test!(ns_err_004); test!(ns_err_005); test!(ns_err_006); test!(ns_err_007); test!(ns_err_008); test!(ns_err_009); test!(ns_err_010); test!(ns_err_011); test!(ns_err_012); test!(ns_err_013); test!(text_001); test!(text_002); test!(text_003); test!(text_004); test!(text_005); test!(text_006); test!(text_007); test!(text_008); test!(text_009); test!(text_010); test!(text_011); test!(text_012); test!(tree_001); test!(tree_002); // test!(tree_003); // unsupported test!(tree_err_001); test!(tree_err_002); test!(tree_err_003); roxmltree-0.20.0/tests/dom-api.rs000064400000000000000000000073601046102023000147700ustar 00000000000000use 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.20.0/tests/files/README.md000064400000000000000000000072101046102023000154470ustar 00000000000000### 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_006 - escaped `<` - 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 - cdata_007 - with \r - 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_013 - many entity references in text - entity_014 - many entity references in attribute - 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 - entity_err_007 - malformed entity inside an attribute - entity_err_008 - malformed entity inside a character data/text - entity_err_009 - escaped `<` inside an attribute inside an entity is an error - 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 - text_012 - non-ASCII text - tree_001 - all node types - tree_002 - BOM - tree_003 - Windows-1251 encoding - tree_err_001 - no elements - tree_err_002 - root element not closed - tree_err_003 - child element not closed roxmltree-0.20.0/tests/files/attrs_001.xml000064400000000000000000000000131046102023000164210ustar 00000000000000

roxmltree-0.20.0/tests/files/attrs_001.yaml000064400000000000000000000001121046102023000165630ustar 00000000000000Document: - Element: tag_name: p attributes: a: "b" roxmltree-0.20.0/tests/files/attrs_002.xml000064400000000000000000000000171046102023000164260ustar 00000000000000

roxmltree-0.20.0/tests/files/attrs_002.yaml000064400000000000000000000001161046102023000165700ustar 00000000000000Document: - Element: tag_name: p attributes: a: " xyz" roxmltree-0.20.0/tests/files/attrs_003.xml000064400000000000000000000000221046102023000164230ustar 00000000000000

roxmltree-0.20.0/tests/files/attrs_003.yaml000064400000000000000000000001141046102023000165670ustar 00000000000000Document: - Element: tag_name: p attributes: a: "A B" roxmltree-0.20.0/tests/files/attrs_004.xml000064400000000000000000000000521046102023000164270ustar 00000000000000

roxmltree-0.20.0/tests/files/attrs_004.yaml000064400000000000000000000001271046102023000165740ustar 00000000000000Document: - Element: tag_name: p attributes: a: "\r\rA\n\nB\r\n" roxmltree-0.20.0/tests/files/attrs_005.xml000064400000000000000000000000201046102023000164230ustar 00000000000000 roxmltree-0.20.0/tests/files/attrs_005.yaml000064400000000000000000000001141046102023000165710ustar 00000000000000Document: - Element: tag_name: e attributes: a: " b " roxmltree-0.20.0/tests/files/attrs_006.xml000064400000000000000000000000171046102023000164320ustar 00000000000000 roxmltree-0.20.0/tests/files/attrs_006.yaml000064400000000000000000000001121046102023000165700ustar 00000000000000Document: - Element: tag_name: e attributes: a: "<" roxmltree-0.20.0/tests/files/attrs_err_001.xml000064400000000000000000000000231046102023000172720ustar 00000000000000 roxmltree-0.20.0/tests/files/attrs_err_001.yaml000064400000000000000000000000621046102023000174370ustar 00000000000000error: "attribute 'a' at 1:11 is already defined" roxmltree-0.20.0/tests/files/attrs_err_002.xml000064400000000000000000000001231046102023000172740ustar 00000000000000 roxmltree-0.20.0/tests/files/attrs_err_002.yaml000064400000000000000000000000621046102023000174400ustar 00000000000000error: "attribute 'a' at 1:72 is already defined" roxmltree-0.20.0/tests/files/cdata_001.xml000064400000000000000000000000741046102023000163470ustar 00000000000000 Hello, world!]]> roxmltree-0.20.0/tests/files/cdata_001.yaml000064400000000000000000000001641046102023000165110ustar 00000000000000Document: - Element: tag_name: doc children: - Text: "\nHello, world!\n" roxmltree-0.20.0/tests/files/cdata_002.xml000064400000000000000000000000721046102023000163460ustar 00000000000000]> roxmltree-0.20.0/tests/files/cdata_002.yaml000064400000000000000000000001451046102023000165110ustar 00000000000000Document: - Element: tag_name: e children: - Text: "\n \n " roxmltree-0.20.0/tests/files/cdata_003.xml000064400000000000000000000000321046102023000163430ustar 00000000000000

roxmltree-0.20.0/tests/files/cdata_003.yaml000064400000000000000000000001241046102023000165070ustar 00000000000000Document: - Element: tag_name: p children: - Text: "\n \n" roxmltree-0.20.0/tests/files/cdata_004.xml000064400000000000000000000001051046102023000163450ustar 00000000000000

some

text

roxmltree-0.20.0/tests/files/cdata_004.yaml000064400000000000000000000003511046102023000165120ustar 00000000000000Document: - Element: tag_name: p children: - Text: "\n some\n " - Element: tag_name: p children: - Text: "\n long\n " - Text: "\n text\n" roxmltree-0.20.0/tests/files/cdata_005.xml000064400000000000000000000000441046102023000163500ustar 00000000000000

Tx

roxmltree-0.20.0/tests/files/cdata_005.yaml000064400000000000000000000001201046102023000165050ustar 00000000000000Document: - Element: tag_name: p children: - Text: "Text" roxmltree-0.20.0/tests/files/cdata_006.xml000064400000000000000000000000771046102023000163570ustar 00000000000000 roxmltree-0.20.0/tests/files/cdata_006.yaml000064400000000000000000000003331046102023000165140ustar 00000000000000Document: - Element: tag_name: svg children: - Text: "\n " - Element: tag_name: style children: - Text: "\n text\n " - Text: "\n" roxmltree-0.20.0/tests/files/cdata_007.xml000064400000000000000000000000751046102023000163560ustar 00000000000000 roxmltree-0.20.0/tests/files/cdata_007.yaml000064400000000000000000000003311046102023000165130ustar 00000000000000Document: - Element: tag_name: svg children: - Text: "\n " - Element: tag_name: style children: - Text: "some\nlong\ntext\n" - Text: "\n" roxmltree-0.20.0/tests/files/comments_001.xml000064400000000000000000000000521046102023000171140ustar 00000000000000 roxmltree-0.20.0/tests/files/comments_001.yaml000064400000000000000000000001361046102023000172610ustar 00000000000000Document: - Comment: " comment " - Element: tag_name: root - Comment: " comment " roxmltree-0.20.0/tests/files/elems_err_001.xml000064400000000000000000000000321046102023000172420ustar 00000000000000 roxmltree-0.20.0/tests/files/elems_err_001.yaml000064400000000000000000000000601046102023000174050ustar 00000000000000error: "expected 'item' tag, not 'root' at 3:1" roxmltree-0.20.0/tests/files/elems_err_002.xml000064400000000000000000000000741046102023000172510ustar 00000000000000 roxmltree-0.20.0/tests/files/elems_err_002.yaml000064400000000000000000000000641046102023000174120ustar 00000000000000error: "expected 't:item' tag, not 't:root' at 3:1" roxmltree-0.20.0/tests/files/entity_001.xml000064400000000000000000000000741046102023000166070ustar 00000000000000'> ]> &p; roxmltree-0.20.0/tests/files/entity_001.yaml000064400000000000000000000001471046102023000167520ustar 00000000000000Document: - Element: tag_name: root children: - Element: tag_name: p roxmltree-0.20.0/tests/files/entity_002.xml000064400000000000000000000000651046102023000166100ustar 00000000000000 ]>

roxmltree-0.20.0/tests/files/entity_002.yaml000064400000000000000000000001121046102023000167430ustar 00000000000000Document: - Element: tag_name: p attributes: a: "b" roxmltree-0.20.0/tests/files/entity_003.xml000064400000000000000000000002041046102023000166040ustar 00000000000000 ]>

roxmltree-0.20.0/tests/files/entity_003.yaml000064400000000000000000000001221046102023000167450ustar 00000000000000Document: - Element: tag_name: p attributes: a: " A B " roxmltree-0.20.0/tests/files/entity_004.xml000064400000000000000000000000661046102023000166130ustar 00000000000000 ]>

&b;

roxmltree-0.20.0/tests/files/entity_004.yaml000064400000000000000000000001201046102023000167440ustar 00000000000000Document: - Element: tag_name: p children: - Text: "text" roxmltree-0.20.0/tests/files/entity_005.xml000064400000000000000000000000601046102023000166060ustar 00000000000000 ]> roxmltree-0.20.0/tests/files/entity_005.yaml000064400000000000000000000000511046102023000167500ustar 00000000000000Document: - Element: tag_name: e roxmltree-0.20.0/tests/files/entity_006.xml000064400000000000000000000001001046102023000166020ustar 00000000000000 ]>

&b;

roxmltree-0.20.0/tests/files/entity_006.yaml000064400000000000000000000001251046102023000167530ustar 00000000000000Document: - Element: tag_name: p children: - Text: "Some text" roxmltree-0.20.0/tests/files/entity_007.xml000064400000000000000000000001161046102023000166120ustar 00000000000000 ]> roxmltree-0.20.0/tests/files/entity_007.yaml000064400000000000000000000001151046102023000167530ustar 00000000000000Document: - Element: tag_name: e attributes: a: "text" roxmltree-0.20.0/tests/files/entity_008.xml000064400000000000000000000001021046102023000166060ustar 00000000000000 '> ]> &p; roxmltree-0.20.0/tests/files/entity_008.yaml000064400000000000000000000002231046102023000167540ustar 00000000000000Document: - Element: tag_name: root children: - Text: " " - Element: tag_name: p - Text: " " roxmltree-0.20.0/tests/files/entity_009.xml000064400000000000000000000001041046102023000166110ustar 00000000000000text'> ]> &p; roxmltree-0.20.0/tests/files/entity_009.yaml000064400000000000000000000002251046102023000167570ustar 00000000000000Document: - Element: tag_name: root children: - Text: "text" - Element: tag_name: p - Text: "text" roxmltree-0.20.0/tests/files/entity_010.xml000064400000000000000000000001301046102023000166000ustar 00000000000000 '> ]> &p; roxmltree-0.20.0/tests/files/entity_010.yaml000064400000000000000000000002241046102023000167460ustar 00000000000000Document: - Element: tag_name: root children: - Element: tag_name: p attributes: a: "b" roxmltree-0.20.0/tests/files/entity_011.xml000064400000000000000000000003151046102023000166060ustar 00000000000000 ]>

roxmltree-0.20.0/tests/files/entity_011.yaml000064400000000000000000000005151046102023000167520ustar 00000000000000Document: - 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.20.0/tests/files/entity_012.xml000064400000000000000000000015521046102023000166130ustar 00000000000000 ]>

&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.20.0/tests/files/entity_012.yaml000064400000000000000000000112241046102023000167520ustar 00000000000000Document: - 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.20.0/tests/files/entity_013.xml000064400000000000000000000002201046102023000166030ustar 00000000000000 ]> &q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q;&q; roxmltree-0.20.0/tests/files/entity_013.yaml000064400000000000000000000001551046102023000167540ustar 00000000000000Document: - Element: tag_name: root children: - Text: "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" roxmltree-0.20.0/tests/files/entity_014.xml000064400000000000000000000002171046102023000166120ustar 00000000000000 ]> roxmltree-0.20.0/tests/files/entity_014.yaml000064400000000000000000000001521046102023000167520ustar 00000000000000Document: - Element: tag_name: root attributes: a: "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" roxmltree-0.20.0/tests/files/entity_err_001.xml000064400000000000000000000000151046102023000174520ustar 00000000000000 roxmltree-0.20.0/tests/files/entity_err_001.yaml000064400000000000000000000000551046102023000176200ustar 00000000000000error: "unknown entity reference 'd' at 1:7" roxmltree-0.20.0/tests/files/entity_err_002.xml000064400000000000000000000001151046102023000174540ustar 00000000000000 ]> roxmltree-0.20.0/tests/files/entity_err_002.yaml000064400000000000000000000000761046102023000176240ustar 00000000000000error: "a possible entity reference loop is detected at 3:20" roxmltree-0.20.0/tests/files/entity_err_003.xml000064400000000000000000000000741046102023000174610ustar 00000000000000'> ]> &p; roxmltree-0.20.0/tests/files/entity_err_003.yaml000064400000000000000000000000461046102023000176220ustar 00000000000000error: "unexpected close tag at 2:17" roxmltree-0.20.0/tests/files/entity_err_004.xml000064400000000000000000000000771046102023000174650ustar 00000000000000'> ]>

&p; roxmltree-0.20.0/tests/files/entity_err_004.yaml000064400000000000000000000000461046102023000176230ustar 00000000000000error: "unexpected close tag at 2:17" roxmltree-0.20.0/tests/files/entity_err_005.xml000064400000000000000000000014301046102023000174600ustar 00000000000000 ]> &lol9; roxmltree-0.20.0/tests/files/entity_err_005.yaml000064400000000000000000000000761046102023000176270ustar 00000000000000error: "a possible entity reference loop is detected at 3:40" roxmltree-0.20.0/tests/files/entity_err_006.xml000064400000000000000000000014271046102023000174670ustar 00000000000000 ]> roxmltree-0.20.0/tests/files/entity_err_006.yaml000064400000000000000000000000761046102023000176300ustar 00000000000000error: "a possible entity reference loop is detected at 3:40" roxmltree-0.20.0/tests/files/entity_err_007.xml000064400000000000000000000000161046102023000174610ustar 00000000000000

roxmltree-0.20.0/tests/files/entity_err_007.yaml000064400000000000000000000000531046102023000176240ustar 00000000000000error: "malformed entity reference at 1:7" roxmltree-0.20.0/tests/files/entity_err_008.xml000064400000000000000000000000141046102023000174600ustar 00000000000000

&

roxmltree-0.20.0/tests/files/entity_err_008.yaml000064400000000000000000000000531046102023000176250ustar 00000000000000error: "malformed entity reference at 1:4" roxmltree-0.20.0/tests/files/entity_err_009.xml000064400000000000000000000000771046102023000174720ustar 00000000000000"> ]> &e; roxmltree-0.20.0/tests/files/entity_err_009.yaml000064400000000000000000000000451046102023000176270ustar 00000000000000error: "unescaped '<' found at 2:23" roxmltree-0.20.0/tests/files/ns_001.xml000064400000000000000000000001151046102023000157070ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_001.yaml000064400000000000000000000003171046102023000160550ustar 00000000000000Document: - 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.20.0/tests/files/ns_002.xml000064400000000000000000000000601046102023000157070ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_002.yaml000064400000000000000000000002421046102023000160530ustar 00000000000000Document: - Element: tag_name: svg@http://www.w3.org/2000/svg attributes: a: "b" namespaces: None: http://www.w3.org/2000/svg roxmltree-0.20.0/tests/files/ns_003.xml000064400000000000000000000001021046102023000157050ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_003.yaml000064400000000000000000000003131046102023000160530ustar 00000000000000Document: - 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.20.0/tests/files/ns_004.xml000064400000000000000000000001241046102023000157120ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_004.yaml000064400000000000000000000003011046102023000160510ustar 00000000000000Document: - 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.20.0/tests/files/ns_005.xml000064400000000000000000000000341046102023000157130ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_005.yaml000064400000000000000000000001741046102023000160620ustar 00000000000000Document: - Element: tag_name: svg attributes: space@http://www.w3.org/XML/1998/namespace: "preserve" roxmltree-0.20.0/tests/files/ns_006.xml000064400000000000000000000001151046102023000157140ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_006.yaml000064400000000000000000000001741046102023000160630ustar 00000000000000Document: - Element: tag_name: svg attributes: space@http://www.w3.org/XML/1998/namespace: "preserve" roxmltree-0.20.0/tests/files/ns_007.xml000064400000000000000000000002071046102023000157170ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_007.yaml000064400000000000000000000004431046102023000160630ustar 00000000000000Document: - 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.20.0/tests/files/ns_008.xml000064400000000000000000000000651046102023000157220ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_008.yaml000064400000000000000000000004541046102023000160660ustar 00000000000000Document: - 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.20.0/tests/files/ns_009.xml000064400000000000000000000001171046102023000157210ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_009.yaml000064400000000000000000000004541046102023000160670ustar 00000000000000Document: - 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.20.0/tests/files/ns_010.xml000064400000000000000000000001441046102023000157110ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_010.yaml000064400000000000000000000010271046102023000160540ustar 00000000000000Document: - 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.20.0/tests/files/ns_011.xml000064400000000000000000000000271046102023000157120ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_011.yaml000064400000000000000000000001601046102023000160520ustar 00000000000000Document: - Element: tag_name: root attributes: a: "b" namespaces: None: "" roxmltree-0.20.0/tests/files/ns_012.xml000064400000000000000000000001371046102023000157150ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_012.yaml000064400000000000000000000006471046102023000160650ustar 00000000000000Document: - 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.20.0/tests/files/ns_013.xml000064400000000000000000000001321046102023000157110ustar 00000000000000 ]>

roxmltree-0.20.0/tests/files/ns_013.yaml000064400000000000000000000002121046102023000160520ustar 00000000000000Document: - Element: tag_name: p@http://www.w3.org namespaces: None: http://www.w3.org t: http://www.w3.org roxmltree-0.20.0/tests/files/ns_014.xml000064400000000000000000000000171046102023000157140ustar 00000000000000

roxmltree-0.20.0/tests/files/ns_014.yaml000064400000000000000000000001161046102023000160560ustar 00000000000000Document: - Element: tag_name: p attributes: id: "test" roxmltree-0.20.0/tests/files/ns_015.xml000064400000000000000000000001131046102023000157120ustar 00000000000000

roxmltree-0.20.0/tests/files/ns_015.yaml000064400000000000000000000005461046102023000160660ustar 00000000000000Document: - 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.20.0/tests/files/ns_016.xml000064400000000000000000000001411046102023000157140ustar 00000000000000

roxmltree-0.20.0/tests/files/ns_016.yaml000064400000000000000000000011371046102023000160640ustar 00000000000000Document: - 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.20.0/tests/files/ns_017.xml000064400000000000000000000001241046102023000157160ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_017.yaml000064400000000000000000000006331046102023000160650ustar 00000000000000Document: - 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.20.0/tests/files/ns_err_001.xml000064400000000000000000000000711046102023000165600ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_001.yaml000064400000000000000000000000731046102023000167240ustar 00000000000000error: "'xml' namespace prefix mapped to wrong URI at 1:6" roxmltree-0.20.0/tests/files/ns_err_002.xml000064400000000000000000000000661046102023000165650ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_002.yaml000064400000000000000000000001051046102023000167210ustar 00000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.20.0/tests/files/ns_err_003.xml000064400000000000000000000000641046102023000165640ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_003.yaml000064400000000000000000000001051046102023000167220ustar 00000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.20.0/tests/files/ns_err_004.xml000064400000000000000000000000771046102023000165710ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_004.yaml000064400000000000000000000000621046102023000167250ustar 00000000000000error: "namespace 'n' at 1:34 is already defined" roxmltree-0.20.0/tests/files/ns_err_005.xml000064400000000000000000000000711046102023000165640ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_005.yaml000064400000000000000000000001051046102023000167240ustar 00000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.20.0/tests/files/ns_err_006.xml000064400000000000000000000000731046102023000165670ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_006.yaml000064400000000000000000000001051046102023000167250ustar 00000000000000error: "the 'xml' namespace URI is used for not 'xml' prefix at 1:6" roxmltree-0.20.0/tests/files/ns_err_007.xml000064400000000000000000000000551046102023000165700ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_007.yaml000064400000000000000000000001051046102023000167260ustar 00000000000000error: "the 'xmlns' URI is used at 1:6, but it must not be declared" roxmltree-0.20.0/tests/files/ns_err_008.xml000064400000000000000000000000571046102023000165730ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_008.yaml000064400000000000000000000001051046102023000167270ustar 00000000000000error: "the 'xmlns' URI is used at 1:6, but it must not be declared" roxmltree-0.20.0/tests/files/ns_err_009.xml000064400000000000000000000000151046102023000165660ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_009.yaml000064400000000000000000000000771046102023000167400ustar 00000000000000error: "the 'xmlns' prefix is used at 1:2, but it must not be" roxmltree-0.20.0/tests/files/ns_err_010.xml000064400000000000000000000000071046102023000165570ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_010.yaml000064400000000000000000000000601046102023000167200ustar 00000000000000error: "an unknown namespace prefix 'a' at 1:2" roxmltree-0.20.0/tests/files/ns_err_011.xml000064400000000000000000000000521046102023000165600ustar 00000000000000 roxmltree-0.20.0/tests/files/ns_err_011.yaml000064400000000000000000000000641046102023000167250ustar 00000000000000error: "an unknown namespace prefix 'test' at 1:30" roxmltree-0.20.0/tests/files/ns_err_012.xml000064400000000000000000000001041046102023000165570ustar 00000000000000
roxmltree-0.20.0/tests/files/ns_err_012.yaml000064400000000000000000000000621046102023000167240ustar 00000000000000error: "expected 'n:item' tag, not 'item' at 3:5" roxmltree-0.20.0/tests/files/ns_err_013.xml000064400000000000000000000001361046102023000165650ustar 00000000000000
roxmltree-0.20.0/tests/files/ns_err_013.yaml000064400000000000000000000000621046102023000167250ustar 00000000000000error: "expected 'n:item' tag, not 'item' at 3:5" roxmltree-0.20.0/tests/files/text_001.xml000064400000000000000000000000171046102023000162540ustar 00000000000000 roxmltree-0.20.0/tests/files/text_001.yaml000064400000000000000000000001201046102023000164110ustar 00000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.20.0/tests/files/text_002.xml000064400000000000000000000000241046102023000162530ustar 00000000000000 roxmltree-0.20.0/tests/files/text_002.yaml000064400000000000000000000001201046102023000164120ustar 00000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.20.0/tests/files/text_003.xml000064400000000000000000000000341046102023000162550ustar 00000000000000Some text roxmltree-0.20.0/tests/files/text_003.yaml000064400000000000000000000001301046102023000164140ustar 00000000000000Document: - Element: tag_name: root children: - Text: "Some text" roxmltree-0.20.0/tests/files/text_004.xml000064400000000000000000000000171046102023000162570ustar 00000000000000> roxmltree-0.20.0/tests/files/text_004.yaml000064400000000000000000000001201046102023000164140ustar 00000000000000Document: - Element: tag_name: root children: - Text: ">" roxmltree-0.20.0/tests/files/text_005.xml000064400000000000000000000000221046102023000162540ustar 00000000000000 roxmltree-0.20.0/tests/files/text_005.yaml000064400000000000000000000001251046102023000164220ustar 00000000000000Document: - Element: tag_name: root children: - Text: "\n\n\n" roxmltree-0.20.0/tests/files/text_006.xml000064400000000000000000000000211046102023000162540ustar 00000000000000 roxmltree-0.20.0/tests/files/text_006.yaml000064400000000000000000000001251046102023000164230ustar 00000000000000Document: - Element: tag_name: root children: - Text: "\n\n\n" roxmltree-0.20.0/tests/files/text_007.xml000064400000000000000000000000221046102023000162560ustar 00000000000000 roxmltree-0.20.0/tests/files/text_007.yaml000064400000000000000000000001231046102023000164220ustar 00000000000000Document: - Element: tag_name: root children: - Text: "\n\n" roxmltree-0.20.0/tests/files/text_008.xml000064400000000000000000000000221046102023000162570ustar 00000000000000 roxmltree-0.20.0/tests/files/text_008.yaml000064400000000000000000000001231046102023000164230ustar 00000000000000Document: - Element: tag_name: root children: - Text: " " roxmltree-0.20.0/tests/files/text_009.xml000064400000000000000000000000431046102023000162630ustar 000000000000000d > roxmltree-0.20.0/tests/files/text_009.yaml000064400000000000000000000001231046102023000164240ustar 00000000000000Document: - Element: tag_name: root children: - Text: "0d >" roxmltree-0.20.0/tests/files/text_010.xml000064400000000000000000000000731046102023000162560ustar 00000000000000 te xt roxmltree-0.20.0/tests/files/text_010.yaml000064400000000000000000000005031046102023000164160ustar 00000000000000Document: - 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.20.0/tests/files/text_011.xml000064400000000000000000000013251046102023000162600ustar 00000000000000

q

q

q

q

q

q

q q

q

q

q q

q

q

q q

roxmltree-0.20.0/tests/files/text_011.yaml000064400000000000000000000066211046102023000164260ustar 00000000000000Document: - 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.20.0/tests/files/text_012.xml000064400000000000000000000000341046102023000162550ustar 00000000000000 baño roxmltree-0.20.0/tests/files/text_012.yaml000064400000000000000000000001341046102023000164200ustar 00000000000000Document: - Element: tag_name: text children: - Text: "\n baño\n" roxmltree-0.20.0/tests/files/tree_001.xml000064400000000000000000000002311046102023000162250ustar 00000000000000 text roxmltree-0.20.0/tests/files/tree_001.yaml000064400000000000000000000005241046102023000163740ustar 00000000000000Document: - 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.20.0/tests/files/tree_002.xml000064400000000000000000000000131046102023000162240ustar 00000000000000 roxmltree-0.20.0/tests/files/tree_002.yaml000064400000000000000000000000541046102023000163730ustar 00000000000000Document: - Element: tag_name: root roxmltree-0.20.0/tests/files/tree_003.xml000064400000000000000000000001061046102023000162300ustar 00000000000000 roxmltree-0.20.0/tests/files/tree_003.yaml000064400000000000000000000002061046102023000163730ustar 00000000000000Document: - Element: tag_name: text children: - Text: "\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440" roxmltree-0.20.0/tests/files/tree_err_001.xml000064400000000000000000000001161046102023000170770ustar 00000000000000 roxmltree-0.20.0/tests/files/tree_err_001.yaml000064400000000000000000000000601046102023000172370ustar 00000000000000error: "the document does not have a root node" roxmltree-0.20.0/tests/files/tree_err_002.xml000064400000000000000000000001031046102023000170740ustar 00000000000000 Text roxmltree-0.20.0/tests/files/tree_err_002.yaml000064400000000000000000000000631046102023000172430ustar 00000000000000error: "the root node was opened but never closed" roxmltree-0.20.0/tests/files/tree_err_003.xml000064400000000000000000000001201046102023000170740ustar 00000000000000 Text roxmltree-0.20.0/tests/files/tree_err_003.yaml000064400000000000000000000000611046102023000172420ustar 00000000000000error: "expected 'open' tag, not 'root' at 2:17"