summaryrefslogtreecommitdiffstats
path: root/third_party/rust/xmldecl
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/xmldecl
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/xmldecl')
-rw-r--r--third_party/rust/xmldecl/.cargo-checksum.json1
-rw-r--r--third_party/rust/xmldecl/COPYRIGHT20
-rw-r--r--third_party/rust/xmldecl/Cargo.toml26
-rw-r--r--third_party/rust/xmldecl/LICENSE-APACHE202
-rw-r--r--third_party/rust/xmldecl/LICENSE-MIT25
-rw-r--r--third_party/rust/xmldecl/README.md38
-rw-r--r--third_party/rust/xmldecl/src/lib.rs371
7 files changed, 683 insertions, 0 deletions
diff --git a/third_party/rust/xmldecl/.cargo-checksum.json b/third_party/rust/xmldecl/.cargo-checksum.json
new file mode 100644
index 0000000000..e5a94a1b57
--- /dev/null
+++ b/third_party/rust/xmldecl/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"COPYRIGHT":"12d3af8e034aee1d1955deb3797cb32fd7e014d1540a8714971238a35201fa0f","Cargo.toml":"0bb4ff44435bd07eda06de25599dc80a3954b7a55ab317a7276d9c2b239f49a5","LICENSE-APACHE":"cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30","LICENSE-MIT":"3fa4ca83dcc9237839b1bdeb2e6d16bdfb5ec0c5ce42b24694d8bbf0dcbef72c","README.md":"039f7f51a3e74690d16fbbc35edf42ff7ee23024aeff60a1320fc19117c69e8a","src/lib.rs":"da20ff71c6d81c6a7f066d15ded76bdb8d447784a843a10fc78ca50c7a4f4015"},"package":"efeb408acbc94f7459f1a3ee3620c108ebea5e5baf93a4641c07d57f59f5ffd1"} \ No newline at end of file
diff --git a/third_party/rust/xmldecl/COPYRIGHT b/third_party/rust/xmldecl/COPYRIGHT
new file mode 100644
index 0000000000..d0201d26e2
--- /dev/null
+++ b/third_party/rust/xmldecl/COPYRIGHT
@@ -0,0 +1,20 @@
+Copyright Mozilla Foundation
+
+Licensed under the Apache License (Version 2.0), or the MIT license,
+(the "Licenses") at your option. You may not use this file except in
+compliance with one of the Licenses. You may obtain copies of the
+Licenses at:
+
+ http://www.apache.org/licenses/LICENSE-2.0
+ http://opensource.org/licenses/MIT
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the Licenses is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the Licenses for the specific language governing permissions and
+limitations under the Licenses.
+
+--
+
+Test code is dedicated to the Public Domain when so designated
+(see source code for PD/CC0-dedicated sections).
diff --git a/third_party/rust/xmldecl/Cargo.toml b/third_party/rust/xmldecl/Cargo.toml
new file mode 100644
index 0000000000..ef055713f5
--- /dev/null
+++ b/third_party/rust/xmldecl/Cargo.toml
@@ -0,0 +1,26 @@
+# 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 = "xmldecl"
+version = "0.2.0"
+authors = ["Henri Sivonen <hsivonen@hsivonen.fi>"]
+description = "Extracts an encoding from an ASCII-based bogo-XML declaration in text/html in a Web-compatible way"
+homepage = "https://docs.rs/xmldecl/"
+documentation = "https://docs.rs/xmldecl/"
+readme = "README.md"
+keywords = ["encoding", "web", "unicode", "charset"]
+categories = ["text-processing", "encoding", "web-programming", "internationalization"]
+license = "Apache-2.0 OR MIT"
+repository = "https://github.com/hsivonen/xmldecl"
+[dependencies.encoding_rs]
+version = "0.8.0"
diff --git a/third_party/rust/xmldecl/LICENSE-APACHE b/third_party/rust/xmldecl/LICENSE-APACHE
new file mode 100644
index 0000000000..d645695673
--- /dev/null
+++ b/third_party/rust/xmldecl/LICENSE-APACHE
@@ -0,0 +1,202 @@
+
+ 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.
diff --git a/third_party/rust/xmldecl/LICENSE-MIT b/third_party/rust/xmldecl/LICENSE-MIT
new file mode 100644
index 0000000000..3317c82e2f
--- /dev/null
+++ b/third_party/rust/xmldecl/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright Mozilla Foundation
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/third_party/rust/xmldecl/README.md b/third_party/rust/xmldecl/README.md
new file mode 100644
index 0000000000..9adfefd7e6
--- /dev/null
+++ b/third_party/rust/xmldecl/README.md
@@ -0,0 +1,38 @@
+# xmldecl
+
+[![crates.io](https://img.shields.io/crates/v/xmldecl.svg)](https://crates.io/crates/xmldecl)
+[![docs.rs](https://docs.rs/xmldecl/badge.svg)](https://docs.rs/xmldecl/)
+[![Apache 2 OR MIT dual-licensed](https://img.shields.io/badge/license-Apache%202%20%2F%20MIT-blue.svg)](https://github.com/hsivonen/xmldecl/blob/main/COPYRIGHT)
+
+`xmldecl::parse()` extracts an encoding from an ASCII-based bogo-XML
+declaration in `text/html` in a Web-compatible way.
+
+See https://github.com/whatwg/html/pull/1752
+
+## Licensing
+
+Please see the file named
+[COPYRIGHT](https://github.com/hsivonen/xmldecl/blob/main/COPYRIGHT).
+
+## Documentation
+
+Generated [API documentation](https://docs.rs/xmldecl/) is available
+online.
+
+## Minimum Supported Rust Version
+
+MSRV: 1.51.0
+
+## Release Notes
+
+### 0.2.0
+
+* Remove the 1024-byte limit.
+
+### 0.1.1
+
+* Map UTF-16LE and UTF-16BE to UTF-8 as in `meta`.
+
+### 0.1.0
+
+* The initial release.
diff --git a/third_party/rust/xmldecl/src/lib.rs b/third_party/rust/xmldecl/src/lib.rs
new file mode 100644
index 0000000000..ed4fb43a85
--- /dev/null
+++ b/third_party/rust/xmldecl/src/lib.rs
@@ -0,0 +1,371 @@
+// Copyright Mozilla Foundation
+//
+// Licensed under the Apache License (Version 2.0), or the MIT license,
+// (the "Licenses") at your option. You may not use this file except in
+// compliance with one of the Licenses. You may obtain copies of the
+// Licenses at:
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the Licenses is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the Licenses for the specific language governing permissions and
+// limitations under the Licenses.
+
+//! `xmldecl::parse()` extracts an encoding from an ASCII-based bogo-XML
+//! declaration in `text/html` in a WebKit-compatible way.
+
+extern crate encoding_rs;
+
+fn position(needle: u8, haystack: &[u8]) -> Option<usize> {
+ haystack.iter().position(|&x| x == needle)
+}
+
+// The standard library lacks subslice search.
+// Since our needle, "encoding" is short, 'g' occurs in it only once,
+// and the other letters we expect to skip over are "version", let's
+// search for 'g' and verify match.
+fn skip_encoding(hay: &[u8]) -> Option<&[u8]> {
+ let mut haystack = hay;
+ loop {
+ if let Some(g) = position(b'g', haystack) {
+ let (head, tail) = haystack.split_at(g + 1);
+ if let Some(_) = head.strip_suffix(b"encoding") {
+ return Some(tail);
+ }
+ haystack = tail;
+ } else {
+ return None;
+ }
+ }
+}
+
+/// Extracts an encoding from an ASCII-based bogo-XML declaration.
+/// `bytes` must the prefix of a `text/html` resource.
+///
+/// The intended use is that when the `meta` prescan fails, the HTML
+/// parser will have buffered the head section or the first 1024
+/// bytes (whichever is larger) at which point the should be passed to
+/// this function.
+pub fn parse(bytes: &[u8]) -> Option<&'static encoding_rs::Encoding> {
+ if let Some(after_xml) = bytes.strip_prefix(b"<?xml") {
+ if let Some(gt) = position(b'>', after_xml) {
+ let until_gt = &after_xml[..gt];
+ if let Some(tail) = skip_encoding(until_gt) {
+ let mut pos = 0;
+ loop {
+ if pos >= tail.len() {
+ return None;
+ }
+ let c = tail[pos];
+ pos += 1;
+ if c == b'=' {
+ break;
+ }
+ if c <= b' ' {
+ continue;
+ }
+ return None;
+ }
+ // pos is now the index of the byte after =
+ let is_single_quoted;
+ let label_start;
+ loop {
+ if pos >= tail.len() {
+ return None;
+ }
+ let c = tail[pos];
+ pos += 1;
+ if c == b'"' {
+ is_single_quoted = false;
+ label_start = pos;
+ break;
+ }
+ if c == b'\'' {
+ is_single_quoted = true;
+ label_start = pos;
+ break;
+ }
+ if c <= b' ' {
+ continue;
+ }
+ return None;
+ }
+ loop {
+ if pos >= tail.len() {
+ return None;
+ }
+ let c = tail[pos];
+ if c <= b' ' {
+ return None;
+ }
+ if (c == b'"' && !is_single_quoted) || (c == b'\'' && is_single_quoted) {
+ let encoding = encoding_rs::Encoding::for_label(&tail[label_start..pos]);
+ if encoding == Some(encoding_rs::UTF_16LE)
+ || encoding == Some(encoding_rs::UTF_16BE)
+ {
+ return Some(encoding_rs::UTF_8);
+ }
+ return encoding;
+ }
+ pos += 1;
+ }
+ }
+ }
+ }
+ None
+}
+
+// Any copyright to the test code below this comment is dedicated to the
+// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
+
+#[cfg(test)]
+mod tests {
+ use super::parse;
+ #[test]
+ fn baseline() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn meta_encoding_before_charset() {
+ assert_eq!(parse(b"<?xml version=\"1.0\" <meta encoding=\"windows-1251\" charset=\"windows-1253\"?>AAAA"), Some(encoding_rs::WINDOWS_1251));
+ }
+ #[test]
+ fn lt() {
+ assert_eq!(
+ parse(b"<?xml<encoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn unmatched_quotes() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"windows-1251'?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn no_version() {
+ assert_eq!(
+ parse(b"<?xml encoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_quotes_space() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=windows-1251 ?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn no_quotes() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=windows-1251?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn no_space_no_version_line_breaks_trailing_body() {
+ assert_eq!(
+ parse(b"<?xmlencoding \n = \n 'windows-1251'<body>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn space_before_label() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\" windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn space_after_label() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"windows-1251 \"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn one_around_label() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"\x01windows-1251\x01\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn one_around_equals() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding\x01=\x01\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space_trailing_lt_without_question_mark() {
+ assert_eq!(
+ parse(b"<?xmlencoding=\"windows-1251\"<>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space_spaces_around_equals_single_quotes_trailing_body() {
+ assert_eq!(
+ parse(b"<?xmlencoding = 'windows-1251'<body>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space_single_quotes_trailing_body() {
+ assert_eq!(
+ parse(b"<?xmlencoding='windows-1251'<body>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space_double_quotes_trailing_body() {
+ assert_eq!(
+ parse(b"<?xmlencoding=\"windows-1251\"<body>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space_no_trailing_question_mark() {
+ assert_eq!(
+ parse(b"<?xmlencoding=\"windows-1251\">AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn no_version_no_space() {
+ assert_eq!(
+ parse(b"<?xmlencoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn upper_case_xml() {
+ assert_eq!(
+ parse(b"<?XML version=\"1.0\" encoding=\"windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn meta_charset_before_encoding() {
+ assert_eq!(parse(b"<?xml version=\"1.0\" <meta charset=\"windows-1253\" encoding=\"windows-1251\"?>AAAA"), Some(encoding_rs::WINDOWS_1251));
+ }
+ #[test]
+ fn lt_between_xml_and_encoding() {
+ assert_eq!(
+ parse(b"<?xml<encoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn letter_between_xml_and_encoding() {
+ assert_eq!(
+ parse(b"<?xmlaencoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn gt_between_xml_and_encoding() {
+ assert_eq!(parse(b"<?xml>encoding=\"windows-1251\"?>"), None);
+ }
+ #[test]
+ fn non_primary_label() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"cp1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn upper_case_label() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn upper_case_version() {
+ assert_eq!(
+ parse(b"<?xml VERSION=\"1.0\" encoding=\"windows-1251\"?>AAAA"),
+ Some(encoding_rs::WINDOWS_1251)
+ );
+ }
+ #[test]
+ fn upper_case_encoding() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" ENCODING=\"windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn space_before() {
+ assert_eq!(
+ parse(b" <?xml version=\"1.0\" encoding=\"windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn encoding_equals_encoding() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=encoding=\"windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn encodingencoding() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encodingencoding=\"windows-1251\"?>AAAA"),
+ None
+ );
+ }
+ #[test]
+ fn utf16() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"UTF-16\"?>AAAA"),
+ Some(encoding_rs::UTF_8)
+ );
+ }
+ #[test]
+ fn utf16le() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"UTF-16LE\"?>AAAA"),
+ Some(encoding_rs::UTF_8)
+ );
+ }
+ #[test]
+ fn utf16be() {
+ assert_eq!(
+ parse(b"<?xml version=\"1.0\" encoding=\"UTF-16BE\"?>AAAA"),
+ Some(encoding_rs::UTF_8)
+ );
+ }
+ #[test]
+ fn bytes_1024() {
+ let mut v = Vec::new();
+ v.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"windows-1251\"");
+ while v.len() < 1022 {
+ v.push(b' ');
+ }
+ v.extend_from_slice(b"?>AAAA");
+ assert_eq!(v.len(), 1028);
+ assert_eq!(parse(&v), Some(encoding_rs::WINDOWS_1251));
+ }
+ #[test]
+ fn bytes_1025() {
+ let mut v = Vec::new();
+ v.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"windows-1251\"");
+ while v.len() < 1023 {
+ v.push(b' ');
+ }
+ v.extend_from_slice(b"?>AAAA");
+ assert_eq!(v.len(), 1029);
+ assert_eq!(parse(&v), Some(encoding_rs::WINDOWS_1251));
+ }
+}