roxmltree-0.16.0/.cargo_vcs_info.json 0000644 00000000136 00000000001 0013141 0 ustar {
"git": {
"sha1": "e5fa00eae4b5e121ed82b0d640d602dd4300563e"
},
"path_in_vcs": ""
} roxmltree-0.16.0/.github/workflows/main.yml 0000644 0000000 0000000 00000001203 10461020230 0016771 0 ustar 0000000 0000000 name: Rust
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.36.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.16.0/.gitignore 0000644 0000000 0000000 00000000063 10461020230 0013720 0 ustar 0000000 0000000 target
**/*.rs.bk
/.idea
/roxmltree.iml
Cargo.lock
roxmltree-0.16.0/CHANGELOG.md 0000644 0000000 0000000 00000016772 10461020230 0013557 0 ustar 0000000 0000000 # 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.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).
### 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.16.0..HEAD
[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.16.0/Cargo.lock 0000644 00000000575 00000000001 0011123 0 ustar # This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "roxmltree"
version = "0.16.0"
dependencies = [
"xmlparser",
]
[[package]]
name = "xmlparser"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d25c75bf9ea12c4040a97f829154768bbbce366287e2dc044af160cd79a13fd"
roxmltree-0.16.0/Cargo.toml 0000644 00000001771 00000000001 0011145 0 ustar # 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 = "2018"
name = "roxmltree"
version = "0.16.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"
[dependencies.xmlparser]
version = "0.13.5"
[features]
default = [
"std",
"positions",
]
positions = []
std = []
roxmltree-0.16.0/Cargo.toml.orig 0000644 0000000 0000000 00000001332 10461020230 0014617 0 ustar 0000000 0000000 [package]
name = "roxmltree"
version = "0.16.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 = "2018"
[workspace]
members = ["benches"]
exclude = ["test-apps", "testing-tools"]
[dependencies]
xmlparser = "0.13.5"
[features]
default = ["std", "positions"]
std = []
# Enables Nodes and Attributes position in the original document preserving.
# Increases memory usage by `usize` for each Node and Attribute.
positions = []
roxmltree-0.16.0/LICENSE-APACHE 0000644 0000000 0000000 00000025137 10461020230 0013665 0 ustar 0000000 0000000 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.16.0/LICENSE-MIT 0000644 0000000 0000000 00000002073 10461020230 0013367 0 ustar 0000000 0000000 The 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.16.0/README.md 0000644 0000000 0000000 00000024464 10461020230 0013222 0 ustar 0000000 0000000 # roxmltree

[](https://crates.io/crates/roxmltree)
[](https://docs.rs/roxmltree)
[](https://www.rust-lang.org)
Represents an [XML 1.0](https://www.w3.org/TR/xml/) document as a read-only tree.
```rust
// Find element by id.
let doc = roxmltree::Document::parse("").unwrap();
let elem = doc.descendants().find(|n| n.attribute("id") == Some("rect1")).unwrap();
assert!(elem.has_tag_name("rect"));
```
## Why read-only?
Because in some cases all you need is to retrieve some data from an XML document.
And for such cases, we can make a lot of optimizations.
As for *roxmltree*, it's fast not only because it's read-only, but also because
it uses [xmlparser], which is many times faster than [xml-rs].
See the [Performance](#performance) section for details.
## Parsing behavior
Sadly, XML can be parsed in many different ways. *roxmltree* tries to mimic the
behavior of Python's [lxml](https://lxml.de/).
But unlike *lxml*, *roxmltree* does support comments outside the root element.
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] | [minidom] |
| ------------------------------- | :--------------: | :-----------------: | :--------------: | :--------------: | :--------------: |
| Element namespace resolving | ✓ | ✓ | ✓ | ~1 | ✓ |
| Attribute namespace resolving | ✓ | ✓ | | ✓ | ✓ |
| [Entity references] | ✓ | ✓ | × | × | × |
| [Character references] | ✓ | ✓ | ✓ | ✓ | ✓ |
| [Attribute-Value normalization] | ✓ | ✓ | | | |
| Comments | ✓ | ✓ | | ✓ | |
| Processing instructions | ✓ | ✓ | ✓ | ✓ | |
| UTF-8 BOM | ✓ | ✓ | × | × | ✓ |
| Non UTF-8 input | | ✓ | | | |
| Complete DTD support | | ✓ | | | |
| Position preserving2 | ✓ | ✓ | | | |
| HTML support | | ✓ | | | |
| Tree modification | | ✓ | ✓ | ✓ | ✓ |
| Writing | | ✓ | ✓ | ✓ | ✓ |
| No **unsafe** | ✓ | | ✓ | | ~3 |
| Language | Rust | C | Rust | Rust | Rust |
| Size overhead4 | ~61KiB | ~1.4MiB5 | ~106KiB | ~121KiB | **~58KiB** |
| Dependencies | **1** | ?5 | 2 | 2 | 2 |
| Tested version | 0.16.0 | 2.9.8 | 0.10.2 | 0.3.2 | 0.12.0 |
| License | MIT / Apache-2.0 | MIT | 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.
3. In the `memchr` crate.
4. Binary size overhead according to [cargo-bloat](https://github.com/RazrFalcon/cargo-bloat).
5. Depends on build flags.
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
[minidom]: https://gitlab.com/xmpp-rs/xmpp-rs/-/tree/main/minidom
## Performance
### Parsing
```text
test large_roxmltree ... bench: 1,550,600 ns/iter (+/- 90,292)
test large_libxml ... bench: 3,257,226 ns/iter (+/- 203,441)
test large_sdx_document ... bench: 3,809,712 ns/iter (+/- 95,477)
test large_minidom ... bench: 5,393,247 ns/iter (+/- 152,617)
test large_xmltree ... bench: 16,149,937 ns/iter (+/- 464,889)
test medium_roxmltree ... bench: 431,244 ns/iter (+/- 33,706)
test medium_libxml ... bench: 937,626 ns/iter (+/- 25,933)
test medium_minidom ... bench: 1,258,619 ns/iter (+/- 15,863)
test medium_sdx_document ... bench: 1,497,293 ns/iter (+/- 44,276)
test medium_xmltree ... bench: 4,962,228 ns/iter (+/- 192,942)
test tiny_roxmltree ... bench: 2,378 ns/iter (+/- 140)
test tiny_minidom ... bench: 9,188 ns/iter (+/- 1,641)
test tiny_libxml ... bench: 9,238 ns/iter (+/- 240)
test tiny_sdx_document ... bench: 10,578 ns/iter (+/- 264)
test tiny_xmltree ... bench: 22,561 ns/iter (+/- 603)
```
*roxmltree* uses [xmlparser] internally,
while *sdx-document* uses its own implementation,
*xmltree* uses the [xml-rs]
and *minidom* uses [quick-xml].
Here is a comparison between *xmlparser*, *xml-rs* and *quick-xml*:
```text
test large_xmlparser ... bench: 760,960 ns/iter (+/- 15,320)
test large_quick_xml ... bench: 977,787 ns/iter (+/- 32,916)
test large_xmlrs ... bench: 13,112,612 ns/iter (+/- 311,702)
test medium_quick_xml ... bench: 229,251 ns/iter (+/- 10,529)
test medium_xmlparser ... bench: 263,752 ns/iter (+/- 12,571)
test medium_xmlrs ... bench: 4,671,499 ns/iter (+/- 97,025)
test tiny_quick_xml ... bench: 1,806 ns/iter (+/- 56)
test tiny_xmlparser ... bench: 1,070 ns/iter (+/- 15)
test tiny_xmlrs ... bench: 19,276 ns/iter (+/- 579)
```
### Iteration
```text
test xmltree_iter_descendants_expensive ... bench: 406,101 ns/iter (+/- 10,184)
test roxmltree_iter_descendants_expensive ... bench: 579,130 ns/iter (+/- 6,607)
test minidom_iter_descendants_expensive ... bench: 717,989 ns/iter (+/- 31,789)
test roxmltree_iter_descendants_inexpensive ... bench: 22,903 ns/iter (+/- 780)
test xmltree_iter_descendants_inexpensive ... bench: 127,131 ns/iter (+/- 3,250)
test minidom_iter_descendants_inexpensive ... bench: 143,045 ns/iter (+/- 16,683)
test roxmltree_iter_children ... bench: 1,398 ns/iter (+/- 68)
```
Where expensive refers to the matching done on each element. In these
benchmarks, *expensive* means searching for any node in the document which
contains a string. And *inexpensive* means searching for any element with a
particular name.
### Notes
The benchmarks were taken on a Apple M1.
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.
- Tree crates may use different *xml-rs* crate versions.
- We bench *libxml2* using the *[rust-libxml]* wrapper crate
- *quick-xml* is faster than *xmlparser* because it's more forgiving for the input,
while *xmlparser* is very strict and does a lot of checks, which are expensive.
So performance difference is mainly due to validation.
[xml-rs]: https://crates.io/crates/xml-rs
[quick-xml]: https://crates.io/crates/quick-xml
[xmlparser]: https://crates.io/crates/xmlparser
[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 correlates 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 by 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.
## Non-goals
- Complete XML support.
- Tree modification and writing.
- XPath/XQuery.
## API
This library uses Rust's idiomatic API based on iterators.
In case you are more familiar with browser/JS DOM APIs - you can check out
[tests/dom-api.rs](tests/dom-api.rs) to see how it can be converted into a Rust one.
## License
Licensed under either of
- [Apache License v2.0](LICENSE-APACHE)
- [MIT license](LICENSE-MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
roxmltree-0.16.0/docs/parsing.md 0000644 0000000 0000000 00000004075 10461020230 0014654 0 ustar 0000000 0000000 # 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
texttext
```
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.16.0/examples/ast.rs 0000644 0000000 0000000 00000001025 10461020230 0014702 0 ustar 0000000 0000000 fn 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.16.0/examples/print_pos.rs 0000644 0000000 0000000 00000001615 10461020230 0016135 0 ustar 0000000 0000000 #[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.position())
);
}
}
}
#[cfg(not(feature = "positions"))]
fn main() {}
roxmltree-0.16.0/examples/stats.rs 0000644 0000000 0000000 00000003364 10461020230 0015261 0 ustar 0000000 0000000 use 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.16.0/src/lib.rs 0000644 0000000 0000000 00000117673 10461020230 0013653 0 ustar 0000000 0000000 /*!
Represent an [XML 1.0](https://www.w3.org/TR/xml/) document as a read-only tree.
The root point of the documentations is [`Document::parse`].
You can find more details in the [README] and 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)]
// `matches!` available since 1.42, but we target 1.36 for now.
#![allow(clippy::match_like_matches_macro)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::num::NonZeroU32;
use core::ops::Deref;
pub use xmlparser::TextPos;
mod parse;
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";
type Range = core::ops::Range;
/// An XML tree container.
///
/// A tree consists of [`Nodes`].
/// There are no separate structs for each node type.
/// So you should check the current node type yourself via [`Node::node_type()`].
/// There are only [5 types](enum.NodeType.html):
/// Root, Element, PI, Comment and Text.
///
/// As you can see there are no XML declaration and CDATA types.
/// The XML declaration is basically skipped, since it doesn't contain any
/// valuable information (we support only UTF-8 anyway).
/// And CDATA will be converted into a Text node as is, without
/// any preprocessing (you can read more about it
/// [here](https://github.com/RazrFalcon/roxmltree/blob/master/docs/parsing.md)).
///
/// Also, the Text node data can be accessed from the text node itself or from
/// the parent element via [`Node::text()`] or [`Node::tail()`].
///
/// [`Nodes`]: struct.Node.html
/// [`Node::node_type()`]: struct.Node.html#method.node_type
/// [`Node::text()`]: struct.Node.html#method.text
/// [`Node::tail()`]: struct.Node.html#method.tail
pub struct Document<'input> {
/// An original data.
///
/// Required for `text_pos_at` methods.
text: &'input str,
nodes: Vec>,
attrs: Vec>,
namespaces: Namespaces<'input>,
}
impl<'input> Document<'input> {
/// Returns the root node.
///
/// # Examples
///
/// ```
/// let doc = roxmltree::Document::parse("").unwrap();
/// assert!(doc.root().is_root());
/// assert!(doc.root().first_child().unwrap().has_tag_name("e"));
/// ```
#[inline]
pub fn root<'a>(&'a self) -> Node<'a, 'input> {
Node {
id: NodeId::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 {
xmlparser::Stream::from(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 {
// Casting to `u32` should be safe since we have a 4GiB input data limit.
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`.
///
/// `u32` is more than enough since we have a 4GiB input data limit anyway.
#[inline]
pub fn new(id: u32) -> Self {
debug_assert!(id < core::u32::MAX);
// We are using `NonZeroUsize` 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)
}
}
enum NodeKind<'input> {
Root,
Element {
tag_name: ExpandedNameIndexed<'input>,
attributes: ShortRange,
namespaces: ShortRange,
},
PI(PI<'input>),
Comment(&'input str),
Text(Cow<'input, str>),
}
struct NodeData<'input> {
parent: Option,
prev_sibling: Option,
next_subtree: Option,
last_child: Option,
kind: NodeKind<'input>,
#[cfg(feature = "positions")]
pos: usize,
}
#[derive(Clone, Debug)]
struct AttributeData<'input> {
name: ExpandedNameIndexed<'input>,
value: Cow<'input, str>,
#[cfg(feature = "positions")]
pos: usize,
}
/// 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) -> &'a 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 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
#[cfg(feature = "positions")]
#[inline]
pub fn position(&self) -> usize {
self.data.pos
}
}
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(self.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: Cow<'input, str>,
}
impl<'input> Namespace<'input> {
/// Returns namespace name/prefix.
///
/// # Examples
///
/// ```
/// let doc = roxmltree::Document::parse(
/// ""
/// ).unwrap();
///
/// assert_eq!(doc.root_element().namespaces()[0].name(), Some("n"));
/// ```
///
/// ```
/// let doc = roxmltree::Document::parse(
/// ""
/// ).unwrap();
///
/// assert_eq!(doc.root_element().namespaces()[0].name(), None);
/// ```
#[inline]
pub fn name(&self) -> Option<&str> {
self.name
}
/// Returns namespace URI.
///
/// # Examples
///
/// ```
/// let doc = roxmltree::Document::parse(
/// ""
/// ).unwrap();
///
/// assert_eq!(doc.root_element().namespaces()[0].uri(), "http://www.w3.org");
/// ```
#[inline]
pub fn uri(&self) -> &str {
self.uri.as_ref()
}
}
struct Namespaces<'input>(Vec>);
impl<'input> Namespaces<'input> {
#[inline]
fn push_ns(&mut self, name: Option<&'input str>, uri: Cow<'input, str>) {
debug_assert_ne!(name, Some(""));
self.0.push(Namespace { name, uri });
}
#[inline]
fn exists(&self, start: usize, prefix: Option<&str>) -> bool {
self[start..].iter().any(|ns| ns.name == prefix)
}
}
impl<'input> Deref for Namespaces<'input> {
type Target = Vec>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[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[idx as usize])
}
#[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 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 == other.id
&& core::ptr::eq(self.doc as *const _, other.doc as *const _)
&& core::ptr::eq(self.d as *const _, other.d 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 {
let id_cmp = self.id.0.cmp(&other.id.0);
match id_cmp {
Ordering::Equal => {
let this_doc_ptr = self.doc as *const Document;
let other_doc_ptr = other.doc as *const Document;
this_doc_ptr.cmp(&other_doc_ptr)
}
_ => id_cmp,
}
}
}
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()
.iter()
.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<&'a str> {
if uri == NS_XML_URI {
return Some(NS_XML_PREFIX);
}
self.namespaces()
.iter()
.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<&'a str>) -> Option<&'a str> {
self.namespaces()
.iter()
.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) -> &'a [Namespace<'input>] {
match self.d.kind {
NodeKind::Element { ref namespaces, .. } => {
&self.doc.namespaces[namespaces.to_urange()]
}
_ => &[],
}
}
/// Returns node's text.
///
/// - for an element will return a first text child
/// - for a comment will return a self text
/// - for a text node will return a self text
///
/// # Examples
///
/// ```
/// let doc = roxmltree::Document::parse("\
///
/// text
///
/// ").unwrap();
///
/// assert_eq!(doc.root_element().text(),
/// Some("\n text\n"));
/// assert_eq!(doc.root_element().first_child().unwrap().text(),
/// Some("\n text\n"));
/// ```
///
/// ```
/// let doc = roxmltree::Document::parse("").unwrap();
///
/// assert_eq!(doc.root().first_child().unwrap().text(), Some(" comment "));
/// ```
#[inline]
pub fn text(&self) -> Option<&'a str> {
match self.d.kind {
NodeKind::Element { .. } => match self.first_child() {
Some(child) if child.is_text() => match self.doc.nodes[child.id.get_usize()].kind {
NodeKind::Text(ref text) => Some(text),
_ => None,
},
_ => None,
},
NodeKind::Comment(text) => Some(text),
NodeKind::Text(ref text) => Some(text),
_ => None,
}
}
/// Returns element's tail text.
///
/// # Examples
///
/// ```
/// let doc = roxmltree::Document::parse("\
///
/// text1
///
/// text2
///
/// ").unwrap();
///
/// let p = doc.descendants().find(|n| n.has_tag_name("p")).unwrap();
/// assert_eq!(p.tail(), Some("\n text2\n"));
/// ```
#[inline]
pub fn tail(&self) -> Option<&'a str> {
if !self.is_element() {
return None;
}
match self.next_sibling().map(|n| n.id) {
Some(id) => match self.doc.nodes[id.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 position in bytes in the original document.
#[cfg(feature = "positions")]
#[inline]
pub fn position(&self) -> usize {
self.d.pos
}
/// 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(text) => write!(f, "Comment({:?})", text),
NodeKind::Text(ref text) => write!(f, "Text({:?})", text),
}
}
}
/// Iterator over a node's attributes
#[derive(Clone, Debug)]
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.attrs[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<'_, '_> {}
/// 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, Debug)]
pub struct Descendants<'a, 'input> {
doc: &'a Document<'input>,
current: NodeId,
until: NodeId,
}
impl<'a, 'input> Descendants<'a, 'input> {
#[inline]
fn new(start: Node<'a, 'input>) -> Self {
Self {
doc: start.doc,
current: start.id,
until: start
.d
.next_subtree
.unwrap_or_else(|| NodeId::from(start.doc.nodes.len())),
}
}
}
impl<'a, 'input> Iterator for Descendants<'a, 'input> {
type Item = Node<'a, 'input>;
#[inline]
fn next(&mut self) -> Option {
let next = if self.current == self.until {
None
} else {
Some(self.doc.get_node(self.current).unwrap())
};
self.current = NodeId::new(self.current.get() + 1);
next
}
}
roxmltree-0.16.0/src/parse.rs 0000644 0000000 0000000 00000120335 10461020230 0014204 0 ustar 0000000 0000000 use alloc::borrow::{Cow, ToOwned};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use xmlparser::{self, Reference, StrSpan, Stream, TextPos};
use crate::{
AttributeData, Document, ExpandedNameIndexed, Namespaces, NodeData, NodeId, NodeKind,
ShortRange, NS_XMLNS_URI, NS_XML_PREFIX, NS_XML_URI, PI, XMLNS,
};
/// 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.
#[allow(missing_docs)]
UnexpectedCloseTag {
expected: String,
actual: String,
pos: TextPos,
},
/// Entity value starts with a close tag.
///
/// Example:
/// ```xml
/// '> ]>
/// &p;
/// ```
UnexpectedEntityCloseTag(TextPos),
/// A reference to an entity that was not defined in the DTD.
UnknownEntityReference(String, TextPos),
/// A 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,
/// 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,
/// Errors detected by the `xmlparser` crate.
ParserError(xmlparser::Error),
}
impl Error {
/// Returns the error position.
#[inline]
pub fn pos(&self) -> TextPos {
match *self {
Error::InvalidXmlPrefixUri(pos) => pos,
Error::UnexpectedXmlUri(pos) => pos,
Error::UnexpectedXmlnsUri(pos) => pos,
Error::InvalidElementNamePrefix(pos) => pos,
Error::DuplicatedNamespace(ref _name, pos) => pos,
Error::UnknownNamespace(ref _name, pos) => pos,
Error::UnexpectedCloseTag { pos, .. } => pos,
Error::UnexpectedEntityCloseTag(pos) => pos,
Error::UnknownEntityReference(ref _name, pos) => pos,
Error::MalformedEntityReference(pos) => pos,
Error::EntityReferenceLoop(pos) => pos,
Error::InvalidAttributeValue(pos) => pos,
Error::DuplicatedAttribute(ref _name, pos) => pos,
Error::ParserError(ref err) => err.pos(),
_ => TextPos::new(1, 1),
}
}
}
impl From for Error {
#[inline]
fn from(e: xmlparser::Error) -> Self {
Error::ParserError(e)
}
}
impl 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::DtdDetected => {
write!(f, "XML with DTD detected")
}
Error::NodesLimitReached => {
write!(f, "nodes limit reached")
}
Error::ParserError(ref err) => {
write!(f, "{}", err)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
#[inline]
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: StrSpan<'input>,
local: StrSpan<'input>,
value: Cow<'input, str>,
#[cfg(feature = "positions")]
pos: usize,
}
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)
}
fn append(
&mut self,
parent_id: NodeId,
kind: NodeKind<'input>,
pos: usize,
nodes_limit: u32,
awaiting_subtree: &mut Vec,
) -> Result {
if self.nodes.len() >= nodes_limit as usize {
return Err(Error::NodesLimitReached);
}
#[cfg(not(feature = "positions"))]
let _ = pos;
let new_child_id = NodeId::from(self.nodes.len());
let appending_element = match kind {
NodeKind::Element { .. } => true,
_ => false,
};
self.nodes.push(NodeData {
parent: Some(parent_id),
prev_sibling: None,
next_subtree: None,
last_child: None,
kind,
#[cfg(feature = "positions")]
pos,
});
let last_child_id = self.nodes[parent_id.get_usize()].last_child;
self.nodes[new_child_id.get_usize()].prev_sibling = last_child_id;
self.nodes[parent_id.get_usize()].last_child = Some(new_child_id);
awaiting_subtree.iter().for_each(|id| {
self.nodes[id.get_usize()].next_subtree = Some(new_child_id);
});
awaiting_subtree.clear();
if !appending_element {
awaiting_subtree.push(NodeId::from(self.nodes.len() - 1));
}
Ok(new_child_id)
}
}
struct Entity<'input> {
name: &'input str,
value: StrSpan<'input>,
}
struct ParserData<'input> {
opt: ParsingOptions,
ns_start_idx: usize,
tmp_attrs: Vec>,
awaiting_subtree: Vec,
parent_prefixes: Vec<&'input str>,
entities: Vec>,
buffer: TextBuffer,
after_text: bool,
}
#[derive(Clone, Copy)]
struct TagNameSpan<'input> {
prefix: StrSpan<'input>,
name: StrSpan<'input>,
span: StrSpan<'input>,
}
impl<'input> TagNameSpan<'input> {
#[inline]
fn new_null() -> Self {
Self {
prefix: StrSpan::from(""),
name: StrSpan::from(""),
span: StrSpan::from(""),
}
}
#[inline]
fn new(prefix: StrSpan<'input>, name: StrSpan<'input>, span: StrSpan<'input>) -> Self {
Self { prefix, name, span }
}
}
/// 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, s: &Stream) -> Result<(), Error> {
if self.depth < 10 {
self.depth += 1;
Ok(())
} else {
Err(Error::EntityReferenceLoop(s.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, s: &Stream) -> Result<(), Error> {
if self.depth == 0 {
// Allow infinite amount of references at zero depth.
Ok(())
} else {
if self.references == core::u8::MAX {
return Err(Error::EntityReferenceLoop(s.gen_text_pos()));
}
self.references += 1;
Ok(())
}
}
}
fn parse(text: &str, opt: ParsingOptions) -> Result {
let mut pd = ParserData {
opt,
ns_start_idx: 1,
tmp_attrs: Vec::with_capacity(16),
entities: Vec::new(),
awaiting_subtree: Vec::new(),
parent_prefixes: Vec::new(),
buffer: TextBuffer::new(),
after_text: false,
};
// Trying to guess rough nodes and attributes amount.
let nodes_capacity = text.bytes().filter(|c| *c == b'<').count();
let attributes_capacity = text.bytes().filter(|c| *c == b'=').count();
// Init document.
let mut doc = Document {
text,
nodes: Vec::with_capacity(nodes_capacity),
attrs: Vec::with_capacity(attributes_capacity),
namespaces: Namespaces(Vec::new()),
};
// Add a root node.
doc.nodes.push(NodeData {
parent: None,
prev_sibling: None,
next_subtree: None,
last_child: None,
kind: NodeKind::Root,
#[cfg(feature = "positions")]
pos: 0,
});
doc.namespaces
.push_ns(Some(NS_XML_PREFIX), Cow::Borrowed(NS_XML_URI));
let parser = xmlparser::Tokenizer::from(text);
let parent_id = doc.root().id;
pd.parent_prefixes.push("");
let mut tag_name = TagNameSpan::new_null();
process_tokens(
parser,
parent_id,
&mut LoopDetector::default(),
&mut tag_name,
&mut pd,
&mut doc,
)?;
if !doc.root().children().any(|n| n.is_element()) {
return Err(Error::NoRootNode);
}
doc.nodes.shrink_to_fit();
doc.attrs.shrink_to_fit();
doc.namespaces.0.shrink_to_fit();
Ok(doc)
}
#[allow(clippy::collapsible_match)]
fn process_tokens<'input>(
parser: xmlparser::Tokenizer<'input>,
mut parent_id: NodeId,
loop_detector: &mut LoopDetector,
tag_name: &mut TagNameSpan<'input>,
pd: &mut ParserData<'input>,
doc: &mut Document<'input>,
) -> Result<(), Error> {
for token in parser {
let token = token?;
match token {
xmlparser::Token::ProcessingInstruction {
target,
content,
span,
} => {
let pi = NodeKind::PI(PI {
target: target.as_str(),
value: content.map(|v| v.as_str()),
});
doc.append(
parent_id,
pi,
span.start(),
pd.opt.nodes_limit,
&mut pd.awaiting_subtree,
)?;
}
xmlparser::Token::Comment { text, span } => {
doc.append(
parent_id,
NodeKind::Comment(text.as_str()),
span.start(),
pd.opt.nodes_limit,
&mut pd.awaiting_subtree,
)?;
}
xmlparser::Token::Text { text } => {
process_text(text, parent_id, loop_detector, pd, doc)?;
}
xmlparser::Token::Cdata { text, span } => {
append_text(
BorrowedText::Input(text.as_str()),
parent_id,
span.start(),
pd.opt.nodes_limit,
pd.after_text,
doc,
&mut pd.awaiting_subtree,
)?;
pd.after_text = true;
}
xmlparser::Token::ElementStart {
prefix,
local,
span,
} => {
if prefix.as_str() == XMLNS {
let pos = err_pos_from_span(doc.text, prefix);
return Err(Error::InvalidElementNamePrefix(pos));
}
*tag_name = TagNameSpan::new(prefix, local, span);
}
xmlparser::Token::Attribute {
prefix,
local,
value,
span,
} => {
process_attribute(prefix, local, value, span, loop_detector, pd, doc)?;
}
xmlparser::Token::ElementEnd { end, span } => {
process_element(*tag_name, end, span, &mut parent_id, pd, doc)?;
}
xmlparser::Token::DtdStart { .. } => {
if !pd.opt.allow_dtd {
return Err(Error::DtdDetected);
}
}
xmlparser::Token::EntityDeclaration {
name, definition, ..
} => {
if let xmlparser::EntityDefinition::EntityValue(value) = definition {
pd.entities.push(Entity {
name: name.as_str(),
value,
});
}
}
_ => {}
}
match token {
xmlparser::Token::ProcessingInstruction { .. }
| xmlparser::Token::Comment { .. }
| xmlparser::Token::ElementStart { .. }
| xmlparser::Token::ElementEnd { .. } => {
pd.after_text = false;
}
_ => {}
}
}
Ok(())
}
fn process_attribute<'input>(
prefix: StrSpan<'input>,
local: StrSpan<'input>,
value: StrSpan<'input>,
token_span: StrSpan<'input>,
loop_detector: &mut LoopDetector,
pd: &mut ParserData<'input>,
doc: &mut Document<'input>,
) -> Result<(), Error> {
#[cfg(not(feature = "positions"))]
let _ = token_span;
#[cfg(feature = "positions")]
let pos = token_span.start();
let value = normalize_attribute(doc.text, value, &pd.entities, loop_detector, &mut pd.buffer)?;
if prefix.as_str() == XMLNS {
// The xmlns namespace MUST NOT be declared as the default namespace.
if value == NS_XMLNS_URI {
let pos = err_pos_from_qname(doc.text, prefix, local);
return Err(Error::UnexpectedXmlnsUri(pos));
}
let is_xml_ns_uri = value == NS_XML_URI;
// The prefix 'xml' is by definition bound to the namespace name
// http://www.w3.org/XML/1998/namespace.
// It MUST NOT be bound to any other namespace name.
if local.as_str() == NS_XML_PREFIX {
if !is_xml_ns_uri {
let pos = err_pos_from_span(doc.text, prefix);
return Err(Error::InvalidXmlPrefixUri(pos));
}
} else {
// The xml namespace MUST NOT be bound to a non-xml prefix.
if is_xml_ns_uri {
let pos = err_pos_from_span(doc.text, prefix);
return Err(Error::UnexpectedXmlUri(pos));
}
}
// Check for duplicated namespaces.
if doc.namespaces.exists(pd.ns_start_idx, Some(local.as_str())) {
let pos = err_pos_from_qname(doc.text, prefix, local);
return Err(Error::DuplicatedNamespace(local.as_str().to_string(), pos));
}
// Xml namespace should not be added to the namespaces.
if !is_xml_ns_uri {
doc.namespaces.push_ns(Some(local.as_str()), value);
}
} else if local.as_str() == XMLNS {
// The xml namespace MUST NOT be declared as the default namespace.
if value == NS_XML_URI {
let pos = err_pos_from_span(doc.text, local);
return Err(Error::UnexpectedXmlUri(pos));
}
// The xmlns namespace MUST NOT be declared as the default namespace.
if value == NS_XMLNS_URI {
let pos = err_pos_from_span(doc.text, local);
return Err(Error::UnexpectedXmlnsUri(pos));
}
doc.namespaces.push_ns(None, value);
} else {
pd.tmp_attrs.push(TempAttributeData {
prefix,
local,
value,
#[cfg(feature = "positions")]
pos,
});
}
Ok(())
}
fn process_element<'input>(
tag_name: TagNameSpan<'input>,
end_token: xmlparser::ElementEnd<'input>,
token_span: StrSpan<'input>,
parent_id: &mut NodeId,
pd: &mut ParserData<'input>,
doc: &mut Document<'input>,
) -> Result<(), Error> {
if tag_name.name.is_empty() {
// May occur in XML like this:
// '> ]>
// &p;
if let xmlparser::ElementEnd::Close(..) = end_token {
return Err(Error::UnexpectedEntityCloseTag(err_pos_from_span(
doc.text, token_span,
)));
} else {
unreachable!("should be already checked by the xmlparser");
}
}
let namespaces = resolve_namespaces(pd.ns_start_idx, *parent_id, doc);
pd.ns_start_idx = doc.namespaces.len();
let attributes = resolve_attributes(pd, namespaces, doc)?;
match end_token {
xmlparser::ElementEnd::Empty => {
let tag_ns_idx = get_ns_idx_by_prefix(doc, namespaces, tag_name.prefix)?;
let new_element_id = doc.append(
*parent_id,
NodeKind::Element {
tag_name: ExpandedNameIndexed {
namespace_idx: tag_ns_idx,
local_name: tag_name.name.as_str(),
},
attributes,
namespaces,
},
tag_name.span.start(),
pd.opt.nodes_limit,
&mut pd.awaiting_subtree,
)?;
pd.awaiting_subtree.push(new_element_id);
}
xmlparser::ElementEnd::Close(prefix, local) => {
let prefix = prefix.as_str();
let local = local.as_str();
let parent_node = &mut doc.nodes[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 = *pd.parent_prefixes.last().unwrap();
if let NodeKind::Element { ref tag_name, .. } = parent_node.kind {
if prefix != parent_prefix || local != tag_name.local_name {
return Err(Error::UnexpectedCloseTag {
expected: gen_qname_string(parent_prefix, tag_name.local_name),
actual: gen_qname_string(prefix, local),
pos: err_pos_from_span(doc.text, token_span),
});
}
}
pd.awaiting_subtree.push(*parent_id);
if let Some(id) = parent_node.parent {
*parent_id = id;
pd.parent_prefixes.pop();
debug_assert!(!pd.parent_prefixes.is_empty());
} else {
unreachable!("should be already checked by the xmlparser");
}
}
xmlparser::ElementEnd::Open => {
let tag_ns_idx = get_ns_idx_by_prefix(doc, namespaces, tag_name.prefix)?;
*parent_id = doc.append(
*parent_id,
NodeKind::Element {
tag_name: ExpandedNameIndexed {
namespace_idx: tag_ns_idx,
local_name: tag_name.name.as_str(),
},
attributes,
namespaces,
},
tag_name.span.start(),
pd.opt.nodes_limit,
&mut pd.awaiting_subtree,
)?;
pd.parent_prefixes.push(tag_name.prefix.as_str());
}
}
Ok(())
}
fn resolve_namespaces(start_idx: usize, parent_id: NodeId, doc: &mut Document) -> ShortRange {
if let NodeKind::Element { ref namespaces, .. } = doc.nodes[parent_id.get_usize()].kind {
let parent_ns = *namespaces;
if start_idx == doc.namespaces.len() {
return parent_ns;
}
for i in parent_ns.to_urange() {
if !doc.namespaces.exists(start_idx, doc.namespaces[i].name) {
let v = doc.namespaces[i].clone();
doc.namespaces.0.push(v);
}
}
}
(start_idx..doc.namespaces.len()).into()
}
fn resolve_attributes<'input>(
pd: &mut ParserData<'input>,
namespaces: ShortRange,
doc: &mut Document<'input>,
) -> Result {
if pd.tmp_attrs.is_empty() {
return Ok(ShortRange::new(0, 0));
}
if doc.attrs.len() + pd.tmp_attrs.len() >= core::u32::MAX as usize {
return Err(Error::NodesLimitReached);
}
let start_idx = doc.attrs.len();
for attr in pd.tmp_attrs.drain(..) {
let namespace_idx = if attr.prefix.as_str() == 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(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(doc, namespaces, attr.prefix)?
};
let attr_name = ExpandedNameIndexed {
namespace_idx,
local_name: attr.local.as_str(),
};
// Check for duplicated attributes.
if doc.attrs[start_idx..]
.iter()
.any(|attr| attr.name.as_expanded_name(doc) == attr_name.as_expanded_name(doc))
{
let pos = err_pos_from_qname(doc.text, attr.prefix, attr.local);
return Err(Error::DuplicatedAttribute(attr.local.to_string(), pos));
}
doc.attrs.push(AttributeData {
name: attr_name,
value: attr.value,
#[cfg(feature = "positions")]
pos: attr.pos,
});
}
Ok((start_idx..doc.attrs.len()).into())
}
fn process_text<'input>(
text: StrSpan<'input>,
parent_id: NodeId,
loop_detector: &mut LoopDetector,
pd: &mut ParserData<'input>,
doc: &mut Document<'input>,
) -> Result<(), Error> {
// Add text as is if it has only valid characters.
if !text.as_str().bytes().any(|b| b == b'&' || b == b'\r') {
append_text(
BorrowedText::Input(text.as_str()),
parent_id,
text.start(),
pd.opt.nodes_limit,
pd.after_text,
doc,
&mut pd.awaiting_subtree,
)?;
pd.after_text = true;
return Ok(());
}
pd.buffer.clear();
let mut is_as_is = false; // TODO: explain
let mut s = Stream::from_substr(doc.text, text.range());
while !s.at_end() {
match parse_next_chunk(&mut s, &pd.entities)? {
NextChunk::Byte(c) => {
if is_as_is {
pd.buffer.push_raw(c);
is_as_is = false;
} else {
pd.buffer.push_from_text(c, s.at_end());
}
}
NextChunk::Char(c) => {
for b in CharToBytes::new(c) {
if loop_detector.depth > 0 {
pd.buffer.push_from_text(b, s.at_end());
} else {
// Characters not from entity should be added as is.
// Not sure why... At least `lxml` produces the same result.
pd.buffer.push_raw(b);
is_as_is = true;
}
}
}
NextChunk::Text(fragment) => {
is_as_is = false;
if !pd.buffer.is_empty() {
append_text(
BorrowedText::Temp(pd.buffer.to_str()),
parent_id,
text.start(),
pd.opt.nodes_limit,
pd.after_text,
doc,
&mut pd.awaiting_subtree,
)?;
pd.after_text = true;
pd.buffer.clear();
}
loop_detector.inc_references(&s)?;
loop_detector.inc_depth(&s)?;
let parser = xmlparser::Tokenizer::from_fragment(doc.text, fragment.range());
let mut tag_name = TagNameSpan::new_null();
process_tokens(parser, parent_id, loop_detector, &mut tag_name, pd, doc)?;
pd.buffer.clear();
loop_detector.dec_depth();
}
}
}
if !pd.buffer.is_empty() {
append_text(
BorrowedText::Temp(pd.buffer.to_str()),
parent_id,
text.start(),
pd.opt.nodes_limit,
pd.after_text,
doc,
&mut pd.awaiting_subtree,
)?;
pd.after_text = true;
pd.buffer.clear();
}
Ok(())
}
enum BorrowedText<'input, 'temp> {
Input(&'input str),
Temp(&'temp str),
}
fn append_text<'input, 'temp>(
text: BorrowedText<'input, 'temp>,
parent_id: NodeId,
pos: usize,
nodes_limit: u32,
after_text: bool,
doc: &mut Document<'input>,
awaiting_subtree: &mut Vec,
) -> Result<(), Error> {
if after_text {
// Prepend to a previous text node.
if let Some(node) = doc.nodes.last_mut() {
if let NodeKind::Text(ref mut prev_text) = node.kind {
let text = match text {
BorrowedText::Input(text) => text,
BorrowedText::Temp(text) => text,
};
match prev_text {
Cow::Borrowed(s) => {
let mut concat_text = String::with_capacity(s.len() + text.len());
concat_text.push_str(s);
concat_text.push_str(text);
*prev_text = Cow::Owned(concat_text);
}
Cow::Owned(ref mut s) => {
s.push_str(text);
}
}
}
}
} else {
let text = match text {
BorrowedText::Input(text) => Cow::Borrowed(text),
BorrowedText::Temp(text) => Cow::Owned(text.to_owned()),
};
doc.append(
parent_id,
NodeKind::Text(text),
pos,
nodes_limit,
awaiting_subtree,
)?;
}
Ok(())
}
enum NextChunk<'a> {
Byte(u8),
Char(char),
Text(StrSpan<'a>),
}
fn parse_next_chunk<'a>(
s: &mut Stream<'a>,
entities: &[Entity<'a>],
) -> Result, Error> {
debug_assert!(!s.at_end());
// Safe, because we already checked that stream is not at the end.
// But we have an additional `debug_assert` above just in case.
let c = s.curr_byte_unchecked();
// Check for character/entity references.
if c == b'&' {
let start = s.pos();
match s.try_consume_reference() {
Some(Reference::Char(ch)) => Ok(NextChunk::Char(ch)),
Some(Reference::Entity(name)) => match entities.iter().find(|e| e.name == name) {
Some(entity) => Ok(NextChunk::Text(entity.value)),
None => {
let pos = s.gen_text_pos_from(start);
Err(Error::UnknownEntityReference(name.into(), pos))
}
},
None => {
let pos = s.gen_text_pos_from(start);
Err(Error::MalformedEntityReference(pos))
}
}
} else {
s.advance(1);
Ok(NextChunk::Byte(c))
}
}
// https://www.w3.org/TR/REC-xml/#AVNormalize
fn normalize_attribute<'input>(
input: &'input str,
text: StrSpan<'input>,
entities: &[Entity],
loop_detector: &mut LoopDetector,
buffer: &mut TextBuffer,
) -> Result, Error> {
if is_normalization_required(&text) {
buffer.clear();
_normalize_attribute(input, text, entities, loop_detector, buffer)?;
Ok(Cow::Owned(buffer.to_str().to_owned()))
} else {
Ok(Cow::Borrowed(text.as_str()))
}
}
#[inline]
fn is_normalization_required(text: &StrSpan) -> bool {
// We assume that `&` indicates an entity or a character reference.
// But in rare cases it can be just an another character.
fn check(c: u8) -> bool {
match c {
b'&' | b'\t' | b'\n' | b'\r' => true,
_ => false,
}
}
text.as_str().bytes().any(check)
}
fn _normalize_attribute(
input: &str,
text: StrSpan,
entities: &[Entity],
loop_detector: &mut LoopDetector,
buffer: &mut TextBuffer,
) -> Result<(), Error> {
let mut s = Stream::from_substr(input, text.range());
while !s.at_end() {
// Safe, because we already checked that the stream is not at the end.
let c = s.curr_byte_unchecked();
if c != b'&' {
s.advance(1);
buffer.push_from_attr(c, s.curr_byte().ok());
continue;
}
// Check for character/entity references.
let start = s.pos();
match s.try_consume_reference() {
Some(Reference::Char(ch)) => {
for b in CharToBytes::new(ch) {
if loop_detector.depth > 0 {
// Escaped `<` inside an ENTITY is an error.
// Escaped `<` outside an ENTITY is ok.
if b == b'<' {
return Err(Error::InvalidAttributeValue(s.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 entities.iter().find(|e| e.name == name) {
Some(entity) => {
loop_detector.inc_references(&s)?;
loop_detector.inc_depth(&s)?;
_normalize_attribute(input, entity.value, entities, loop_detector, buffer)?;
loop_detector.dec_depth();
}
None => {
let pos = s.gen_text_pos_from(start);
return Err(Error::UnknownEntityReference(name.into(), pos));
}
},
None => {
let pos = s.gen_text_pos_from(start);
return Err(Error::MalformedEntityReference(pos));
}
}
}
Ok(())
}
fn get_ns_idx_by_prefix<'input>(
doc: &Document<'input>,
range: ShortRange,
prefix: StrSpan,
) -> Result