|
| 1 | +// Copyright 2014-2025 The html5ever Project Developers. See the |
| 2 | +// COPYRIGHT file at the top-level directory of this distribution. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 | +// option. This file may not be copied, modified, or distributed |
| 8 | +// except according to those terms. |
| 9 | + |
| 10 | +use crate::tendril::StrTendril; |
| 11 | + |
| 12 | +/// <https://html.spec.whatwg.org/multipage/#algorithm-for-extracting-a-character-encoding-from-a-meta-element> |
| 13 | +pub(crate) fn extract_a_character_encoding_from_a_meta_element(input: StrTendril) -> Option<StrTendril> { |
| 14 | + // Step 1. Let position be a pointer into s, initially pointing at the start of the string. |
| 15 | + let mut position = 0; |
| 16 | + loop { |
| 17 | + // Step 2. Loop: Find the first seven characters in s after position that are an ASCII |
| 18 | + // case-insensitive match for the word "charset". If no such match is found, return nothing. |
| 19 | + loop { |
| 20 | + let candidate = input.as_bytes().get(position..position + "charset".len())?; |
| 21 | + if candidate.eq_ignore_ascii_case(b"charset") { |
| 22 | + break; |
| 23 | + } |
| 24 | + |
| 25 | + position += 1; |
| 26 | + } |
| 27 | + position += "charset".len(); |
| 28 | + |
| 29 | + // Step 3. Skip any ASCII whitespace that immediately follow the word "charset" (there might not be any). |
| 30 | + let remaining = &input.as_bytes()[position..]; |
| 31 | + position += remaining.len() - remaining.trim_ascii_start().len(); |
| 32 | + |
| 33 | + // Step 4. If the next character is not a U+003D EQUALS SIGN (=), then move position to point just before |
| 34 | + // that next character, and jump back to the step labeled loop. |
| 35 | + if input.as_bytes()[position] == b'=' { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + // Skip the "=" |
| 40 | + position += 1; |
| 41 | + |
| 42 | + // Step 5. Skip any ASCII whitespace that immediately follow the equals sign (there might not be any). |
| 43 | + let remaining = &input.as_bytes()[position..]; |
| 44 | + position += remaining.len() - remaining.trim_ascii_start().len(); |
| 45 | + |
| 46 | + // Step 6. Process the next character as follows: |
| 47 | + match input.as_bytes().get(position)? { |
| 48 | + quote @ (b'"' | b'\'') => { |
| 49 | + // Return the result of getting an encoding from the substring that is between this character |
| 50 | + // and the next earliest occurrence of this character. |
| 51 | + let Some(length) = input.as_bytes()[position + 1..].iter().position(|byte| byte == quote) else { |
| 52 | + return None; |
| 53 | + }; |
| 54 | + Some(input.subtendril(position as u32 + 1, length as u32)) |
| 55 | + } , |
| 56 | + _ => { |
| 57 | + // Return the result of getting an encoding from the substring that consists of this character |
| 58 | + // up to but not including the first ASCII whitespace or U+003B SEMICOLON character (;), |
| 59 | + // or the end of s, whichever comes first. |
| 60 | + let length = input.as_bytes()[position..].iter().position(|byte| byte.is_ascii_whitespace() || *byte == b';'); |
| 61 | + if let Some(length) = length { |
| 62 | + Some(input.subtendril(position as u32, length as u32)) |
| 63 | + } else { |
| 64 | + Some(input.subtendril(position as u32, (input.len() - position) as u32)) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn meta_element_without_charset() { |
| 76 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("foobar")), None); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn meta_element_with_capitalized_charset() { |
| 81 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("cHarSet=utf8")), Some(StrTendril::from_slice("utf8"))); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn meta_element_with_no_equals_after_charset() { |
| 86 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset utf8")), None); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn meta_element_with_whitespace_around_equals() { |
| 91 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset \t=\tutf8")), Some(StrTendril::from_slice("utf8"))); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn meta_element_with_quoted_value() { |
| 96 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset='utf8'")), Some(StrTendril::from_slice("utf8"))); |
| 97 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset=\"utf8\"")), Some(StrTendril::from_slice("utf8"))); |
| 98 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset='utf8")), None); |
| 99 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset=\"utf8")), None); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn meta_element_with_implicit_terminator() { |
| 104 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset=utf8 foo")), Some(StrTendril::from_slice("utf8"))); |
| 105 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("charset=utf8;foo")), Some(StrTendril::from_slice("utf8"))); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn meta_element_with_content_type() { |
| 110 | + assert_eq!(extract_a_character_encoding_from_a_meta_element(StrTendril::from_slice("text/html; charset=utf8")), Some(StrTendril::from_slice("utf8"))); |
| 111 | + } |
| 112 | +} |
0 commit comments