Skip to main content

script/dom/servoparser/
encoding.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::borrow::Cow;
6use std::mem;
7use std::time::{Duration, Instant};
8
9use encoding_rs::{Encoding, UTF_8, UTF_16BE, UTF_16LE, WINDOWS_1252, X_USER_DEFINED};
10use tendril::fmt::UTF8;
11use tendril::stream::LossyDecoder;
12use tendril::{ByteTendril, StrTendril, TendrilSink};
13
14use crate::dom::document::Document;
15
16#[derive(JSTraceable, MallocSizeOf)]
17pub(super) struct DetectingState {
18    /// The `charset` that was specified in the `Content-Type` header, if any.
19    #[no_trace]
20    encoding_hint_from_content_type: Option<&'static Encoding>,
21    /// The encoding of a same-origin container document, if this document is in an
22    /// `<iframe>`.
23    #[no_trace]
24    encoding_of_container_document: Option<&'static Encoding>,
25    start_timestamp: Instant,
26    attempted_bom_sniffing: bool,
27    buffered_bytes: Vec<u8>,
28}
29
30#[derive(JSTraceable, MallocSizeOf)]
31pub(super) struct DecodingState {
32    /// The actual decoder.
33    ///
34    /// This field is `None` after we've finished parsing, because `LossyDecoder::finish`
35    /// takes ownership of the decoder.
36    #[ignore_malloc_size_of = "Defined in tendril"]
37    #[no_trace]
38    decoder: Option<LossyDecoder<NetworkSink>>,
39    #[no_trace]
40    pub(super) encoding: &'static Encoding,
41}
42
43#[derive(JSTraceable, MallocSizeOf)]
44pub(super) enum NetworkDecoderState {
45    /// In this stage the decoder is buffering bytes until it has enough to determine the encoding.
46    Detecting(DetectingState),
47    Decoding(DecodingState),
48}
49
50impl DetectingState {
51    /// The maximum amount of bytes to buffer before attempting to determine the encoding
52    const BUFFER_THRESHOLD: usize = 1024;
53
54    /// The time threshold after which we will attempt to determine the encoding and start decoding,
55    /// even if there are less than [BUFFER_THRESHOLD] bytes in the buffer.
56    const MAX_TIME_TO_BUFFER: Duration = Duration::from_secs(1);
57
58    /// Appends some data to the internal buffer and attempts to [determine the character encoding].
59    ///
60    /// If an encoding was detected then it is returned. A return value of `None` indicates that
61    /// more bytes are required.
62    ///
63    /// [determine the character encoding]: https://html.spec.whatwg.org/multipage/#determining-the-character-encoding
64    fn buffer(
65        &mut self,
66        data: &[u8],
67        document: &Document,
68        is_at_end_of_file: AtEndOfFile,
69    ) -> Option<&'static Encoding> {
70        self.buffered_bytes.extend_from_slice(data);
71        let can_wait_longer = self.start_timestamp.elapsed() < Self::MAX_TIME_TO_BUFFER;
72        self.determine_the_character_encoding(document, can_wait_longer, is_at_end_of_file)
73    }
74
75    /// <https://html.spec.whatwg.org/multipage/#determining-the-character-encoding>
76    fn determine_the_character_encoding(
77        &mut self,
78        document: &Document,
79        potentially_wait_for_more_data: bool,
80        is_at_end_of_file: AtEndOfFile,
81    ) -> Option<&'static Encoding> {
82        // Step 1. If the result of BOM sniffing is an encoding, return that encoding with confidence certain.
83        if !self.attempted_bom_sniffing && self.buffered_bytes.len() > 2 {
84            self.attempted_bom_sniffing = true;
85
86            if let Some((encoding, _)) = Encoding::for_bom(self.buffered_bytes.as_slice()) {
87                log::debug!(
88                    "Determined that the document is {} via BOM-sniffing",
89                    encoding.name()
90                );
91                return Some(encoding);
92            }
93        }
94
95        // Step 2. If the user has explicitly instructed the user agent to override the document's character
96        // encoding with a specific encoding, optionally return that encoding with the confidence certain.
97        // NOTE: Our users have no way to do that.
98
99        // Step 3. The user agent may wait for more bytes of the resource to be available, either in this
100        // step or at any later step in this algorithm.
101        if potentially_wait_for_more_data && self.buffered_bytes.len() < Self::BUFFER_THRESHOLD {
102            return None;
103        }
104
105        // TODO: Step 4. If the transport layer specifies a character encoding, and it is supported, return that
106        // encoding with the confidence certain.
107        if let Some(encoding_hint_from_content_type) = self.encoding_hint_from_content_type {
108            log::debug!(
109                "Inferred encoding to be {} from the Content-Type header",
110                encoding_hint_from_content_type.name()
111            );
112            return Some(encoding_hint_from_content_type);
113        }
114
115        // Step 5. Optionally, prescan the byte stream to determine its encoding, with the end condition
116        // being when the user agent decides that scanning further bytes would not be efficient.
117        // NOTE: According to the spec, we should always try to get an xml encoding right after failing
118        // to prescan the byte stream
119        let bytes_to_prescan =
120            &self.buffered_bytes[..Self::BUFFER_THRESHOLD.min(self.buffered_bytes.len())];
121        let sniffed_encoding = if document.is_html_document() {
122            prescan_the_byte_stream_to_determine_the_encoding(bytes_to_prescan)
123                .or_else(|| get_xml_encoding(bytes_to_prescan))
124        } else {
125            get_xml_encoding(bytes_to_prescan)
126        };
127        if let Some(encoding) = sniffed_encoding {
128            log::debug!(
129                "Prescanning the byte stream determined that the encoding is {}",
130                encoding.name()
131            );
132            return Some(encoding);
133        }
134
135        if document.is_html_document() {
136            // Step 6. If the HTML parser for which this algorithm is being run is associated with a Document d
137            // whose container document is non-null, then:
138            // Step 6.1 Let parentDocument be d's container document.
139            // Step 6.2 If parentDocument's origin is same origin with d's origin and parentDocument's character encoding
140            // is not UTF-16BE/LE, then return parentDocument's character encoding, with the confidence tentative.
141            // NOTE: This should not happen for XML documents
142            if let Some(encoding) = self.encoding_of_container_document &&
143                encoding != UTF_16LE &&
144                encoding != UTF_16BE
145            {
146                log::debug!(
147                    "Inferred encoding to be that of the container document, which is {}",
148                    encoding.name()
149                );
150                return Some(encoding);
151            }
152
153            // Step 7. Otherwise, if the user agent has information on the likely encoding for this page, e.g.
154            // based on the encoding of the page when it was last visited, then return that encoding,
155            // with the confidence tentative.
156            // NOTE: We have no such information.
157
158            // Step 8. The user agent may attempt to autodetect the character encoding from applying frequency analysis
159            // or other algorithms to the data stream.
160            let mut encoding_detector = chardetng::EncodingDetector::new();
161            encoding_detector.feed(&self.buffered_bytes, is_at_end_of_file == AtEndOfFile::Yes);
162            let url = document.url();
163            let tld = url
164                .as_url()
165                .domain()
166                .and_then(|domain| domain.rsplit('.').next())
167                .map(|tld| tld.as_bytes());
168            let (guessed_encoding, is_probably_right) = encoding_detector.guess_assess(tld, true);
169            if is_probably_right {
170                log::debug!(
171                    "chardetng determined that the document encoding is {}",
172                    guessed_encoding.name()
173                );
174                return Some(guessed_encoding);
175            }
176        }
177
178        // Step 9. Otherwise, return an implementation-defined or user-specified default character encoding,
179        // with the confidence tentative.
180        // TODO: The spec has a cool table here for determining an appropriate fallback encoding based on the
181        // user locale. Use it!
182        log::debug!("Failed to determine encoding of byte stream, falling back to UTF-8");
183        Some(UTF_8)
184    }
185
186    fn finish(&mut self, document: &Document) -> &'static Encoding {
187        self.determine_the_character_encoding(document, false, AtEndOfFile::Yes)
188            .expect("Should always return character encoding when we're not allowed to wait")
189    }
190}
191
192impl NetworkDecoderState {
193    pub(super) fn new(
194        encoding_hint_from_content_type: Option<&'static Encoding>,
195        encoding_of_container_document: Option<&'static Encoding>,
196    ) -> Self {
197        Self::Detecting(DetectingState {
198            encoding_hint_from_content_type,
199            encoding_of_container_document,
200            start_timestamp: Instant::now(),
201            attempted_bom_sniffing: false,
202            buffered_bytes: vec![],
203        })
204    }
205
206    /// Feeds the network decoder a chunk of bytes.
207    ///
208    /// If a new encoding is detected, then the encoding of `document` is updated appropriately.
209    ///
210    /// The decoded bytes are returned to the caller. Note that there is not necessarily a 1:1
211    /// relation between `chunk` and the return value. In the beginning, the decoder will buffer
212    /// bytes and return `None`, then later it will flush them and return a large `StrTendril` all
213    /// at once.
214    pub(super) fn push(&mut self, chunk: &[u8], document: &Document) -> Option<StrTendril> {
215        match self {
216            Self::Detecting(encoding_detector) => {
217                if let Some(encoding) = encoding_detector.buffer(chunk, document, AtEndOfFile::No) {
218                    document.set_encoding(encoding);
219                    let buffered_bytes = mem::take(&mut encoding_detector.buffered_bytes);
220                    *self = Self::Decoding(DecodingState {
221                        decoder: Some(LossyDecoder::new_from_encoding_rs_decoder(
222                            encoding.new_decoder_without_bom_handling(),
223                            NetworkSink::default(),
224                        )),
225                        encoding,
226                    });
227                    return self.push(&buffered_bytes, document);
228                }
229
230                None
231            },
232            Self::Decoding(network_decoder) => {
233                let decoder = network_decoder
234                    .decoder
235                    .as_mut()
236                    .expect("Can't push after call to finish()");
237                decoder.process(ByteTendril::from(chunk));
238                Some(std::mem::take(&mut decoder.inner_sink_mut().output))
239            },
240        }
241    }
242
243    pub(super) fn finish(&mut self, document: &Document) -> StrTendril {
244        match self {
245            Self::Detecting(encoding_detector) => {
246                let encoding = encoding_detector.finish(document);
247                document.set_encoding(encoding);
248                let buffered_bytes = mem::take(&mut encoding_detector.buffered_bytes);
249                let mut decoder = LossyDecoder::new_from_encoding_rs_decoder(
250                    encoding.new_decoder_without_bom_handling(),
251                    NetworkSink::default(),
252                );
253                decoder.process(ByteTendril::from(&*buffered_bytes));
254                *self = Self::Decoding(DecodingState {
255                    // Important to set `None` here to indicate that we're done decoding
256                    decoder: None,
257                    encoding,
258                });
259                let mut chunk = std::mem::take(&mut decoder.inner_sink_mut().output);
260                chunk.push_tendril(&decoder.finish());
261                chunk
262            },
263            Self::Decoding(network_decoder) => network_decoder
264                .decoder
265                .take()
266                .map(|decoder| decoder.finish())
267                .unwrap_or_default(),
268        }
269    }
270
271    pub(super) fn is_finished(&self) -> bool {
272        match self {
273            Self::Detecting(_) => false,
274            Self::Decoding(network_decoder) => network_decoder.decoder.is_none(),
275        }
276    }
277}
278
279/// An implementor of `TendrilSink` with the sole purpose of buffering decoded data
280/// so we can take it later.
281#[derive(Default, JSTraceable)]
282pub(crate) struct NetworkSink {
283    #[no_trace]
284    pub(crate) output: StrTendril,
285}
286
287impl TendrilSink<UTF8> for NetworkSink {
288    type Output = StrTendril;
289
290    fn process(&mut self, tendril: StrTendril) {
291        if self.output.is_empty() {
292            self.output = tendril;
293        } else {
294            self.output.push_tendril(&tendril);
295        }
296    }
297
298    fn error(&mut self, _desc: Cow<'static, str>) {}
299
300    fn finish(self) -> Self::Output {
301        self.output
302    }
303}
304
305#[derive(Default)]
306struct Attribute {
307    name: Vec<u8>,
308    value: Vec<u8>,
309}
310
311/// <https://html.spec.whatwg.org/multipage/#prescan-a-byte-stream-to-determine-its-encoding>
312pub fn prescan_the_byte_stream_to_determine_the_encoding(
313    byte_stream: &[u8],
314) -> Option<&'static Encoding> {
315    // Step 1. Let position be a pointer to a byte in the input byte stream,
316    // initially pointing at the first byte.
317    let mut position = 0;
318
319    // Step 2. Prescan for UTF-16 XML declarations: If position points to:
320    match byte_stream {
321        // A sequence of bytes starting with: 0x3C, 0x0, 0x3F, 0x0, 0x78, 0x0
322        // (case-sensitive UTF-16 little-endian '<?x')
323        [0x3C, 0x0, 0x3F, 0x0, 0x78, 0x0, ..] => {
324            // Return UTF-16LE.
325            return Some(UTF_16LE);
326        },
327
328        // A sequence of bytes starting with: 0x0, 0x3C, 0x0, 0x3F, 0x0, 0x78
329        // (case-sensitive UTF-16 big-endian '<?x')
330        [0x0, 0x3C, 0x0, 0x3F, 0x0, 0x78, ..] => {
331            // Return UTF-16BE.
332            return Some(UTF_16BE);
333        },
334        _ => {},
335    }
336
337    loop {
338        // Step 3. Loop: If position points to:
339        let remaining_byte_stream = byte_stream.get(position..)?;
340
341        // A sequence of bytes starting with: 0x3C 0x21 0x2D 0x2D (`<!--`)
342        if remaining_byte_stream.starts_with(b"<!--") {
343            // Advance the position pointer so that it points at the first 0x3E byte which is preceded by two 0x2D bytes
344            // (i.e. at the end of an ASCII '-->' sequence) and comes after the 0x3C byte that was found.
345            // (The two 0x2D bytes can be the same as those in the '<!--' sequence.)
346            // NOTE: This is not very efficient, but likely not an issue...
347            position += remaining_byte_stream
348                .windows(3)
349                .position(|window| window == b"-->")?;
350        }
351        // A sequence of bytes starting with: 0x3C, 0x4D or 0x6D, 0x45 or 0x65, 0x54 or 0x74, 0x41 or 0x61,
352        // and one of 0x09, 0x0A, 0x0C, 0x0D, 0x20, 0x2F (case-insensitive ASCII '<meta' followed by a space or slash)
353        else if remaining_byte_stream
354            .get(..b"<meta ".len())
355            .is_some_and(|candidate| {
356                candidate[..b"<meta".len()].eq_ignore_ascii_case(b"<meta") &&
357                    candidate.last().is_some_and(|byte| {
358                        matches!(byte, 0x09 | 0x0A | 0x0C | 0x0D | 0x20 | 0x2F)
359                    })
360            })
361        {
362            // Step 1. Advance the position pointer so that it points at the next 0x09, 0x0A, 0x0C, 0x0D, 0x20,
363            // or 0x2F byte (the one in sequence of characters matched above).
364            position += b"<meta".len();
365
366            // Step 2. Let attribute list be an empty list of strings.
367            // NOTE: This is used to track which attributes we have already seen. As there are only
368            // three attributes that we care about, we instead use three booleans.
369            let mut have_seen_http_equiv_attribute = false;
370            let mut have_seen_content_attribute = false;
371            let mut have_seen_charset_attribute = false;
372
373            // Step 3. Let got pragma be false.
374            let mut got_pragma = false;
375
376            // Step 4. Let need pragma be null.
377            let mut need_pragma = None;
378
379            // Step 5. Let charset be the null value (which, for the purposes of this algorithm,
380            // is distinct from an unrecognized encoding or the empty string).
381            let mut charset = None;
382
383            // Step 6. Attributes: Get an attribute and its value. If no attribute was sniffed,
384            // then jump to the processing step below.
385            while let Some(attribute) = get_an_attribute(byte_stream, &mut position) {
386                // Step 7 If the attribute's name is already in attribute list,
387                // then return to the step labeled attributes.
388                // Step 8. Add the attribute's name to attribute list.
389                // NOTE: This happens in the match arms below
390                // Step 9. Run the appropriate step from the following list, if one applies:
391                match attribute.name.as_slice() {
392                    // If the attribute's name is "http-equiv"
393                    b"http-equiv" if !have_seen_http_equiv_attribute => {
394                        have_seen_http_equiv_attribute = true;
395
396                        // If the attribute's value is "content-type", then set got pragma to true.
397                        if attribute.value == b"content-type" {
398                            got_pragma = true;
399                        }
400                    },
401                    // If the attribute's name is "content"
402                    b"content" if !have_seen_content_attribute => {
403                        have_seen_content_attribute = true;
404
405                        // Apply the algorithm for extracting a character encoding from a meta element,
406                        // giving the attribute's value as the string to parse. If a character encoding
407                        // is returned, and if charset is still set to null, let charset be the encoding
408                        // returned, and set need pragma to true.
409                        if charset.is_none() &&
410                            let Some(extracted_charset) =
411                                extract_a_character_encoding_from_a_meta_element(
412                                    &attribute.value,
413                                )
414                        {
415                            need_pragma = Some(true);
416                            charset = Some(extracted_charset);
417                        }
418                    },
419                    // If the attribute's name is "charset"
420                    b"charset" if !have_seen_charset_attribute => {
421                        have_seen_charset_attribute = true;
422
423                        // Let charset be the result of getting an encoding from the attribute's value,
424                        // and set need pragma to false.
425                        if let Some(extracted_charset) = Encoding::for_label(&attribute.value) {
426                            charset = Some(extracted_charset);
427                        }
428
429                        need_pragma = Some(false);
430                    },
431                    _ => {},
432                }
433
434                // Step 10. Return to the step labeled attributes.
435            }
436
437            // Step 11. Processing: If need pragma is null, then jump to the step below labeled next byte.
438            if let Some(need_pragma) = need_pragma {
439                // Step 12. If need pragma is true but got pragma is false,
440                // then jump to the step below labeled next byte.
441                if !need_pragma || got_pragma {
442                    // Step 13. If charset is UTF-16BE/LE, then set charset to UTF-8.
443                    if charset.is_some_and(|charset| charset == UTF_16BE || charset == UTF_16LE) {
444                        charset = Some(UTF_8);
445                    }
446                    // Step 14. If charset is x-user-defined, then set charset to windows-1252.
447                    else if charset.is_some_and(|charset| charset == X_USER_DEFINED) {
448                        charset = Some(WINDOWS_1252);
449                    }
450
451                    // Step 15. Return charset.
452                    return charset;
453                }
454            }
455        }
456        // A sequence of bytes starting with a 0x3C byte (<), optionally a 0x2F byte (/),
457        // and finally a byte in the range 0x41-0x5A or 0x61-0x7A (A-Z or a-z)
458        else if *remaining_byte_stream.first()? == b'<' &&
459            remaining_byte_stream
460                .get(1)
461                .filter(|byte| **byte != b'=')
462                .or(remaining_byte_stream.get(2))?
463                .is_ascii_alphabetic()
464        {
465            // Step 1. Advance the position pointer so that it points at the next 0x09 (HT),
466            // 0x0A (LF), 0x0C (FF), 0x0D (CR), 0x20 (SP), or 0x3E (>) byte.
467            position += remaining_byte_stream
468                .iter()
469                .position(|byte| byte.is_ascii_whitespace() || *byte == b'>')?;
470
471            // Step 2. Repeatedly get an attribute until no further attributes can be found,
472            // then jump to the step below labeled next byte.
473            while get_an_attribute(byte_stream, &mut position).is_some() {}
474        }
475        // A sequence of bytes starting with: 0x3C 0x21 (`<!`)
476        // A sequence of bytes starting with: 0x3C 0x2F (`</`)
477        // A sequence of bytes starting with: 0x3C 0x3F (`<?`)
478        else if remaining_byte_stream.starts_with(b"<!") ||
479            remaining_byte_stream.starts_with(b"</") ||
480            remaining_byte_stream.starts_with(b"<?")
481        {
482            // Advance the position pointer so that it points at the first 0x3E byte (>) that comes after the 0x3C byte that was found.
483            position += remaining_byte_stream
484                .iter()
485                .position(|byte| *byte == b'>')?;
486        }
487        // Any other byte
488        else {
489            // Do nothing with that byte.
490        }
491
492        // Next byte: Move position so it points at the next byte in the input byte stream,
493        // and return to the step above labeled loop.
494        position += 1;
495    }
496}
497
498/// <https://html.spec.whatwg.org/multipage/#concept-get-attributes-when-sniffing>
499fn get_an_attribute(input: &[u8], position: &mut usize) -> Option<Attribute> {
500    // NOTE: If we reach the end of the input during parsing then we return "None"
501    // (because there obviously is no attribute). The caller will then also run
502    // out of bytes and invoke "get an xml encoding" as mandated by the spec.
503
504    // Step 1. If the byte at position is one of 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR),
505    // 0x20 (SP), or 0x2F (/), then advance position to the next byte and redo this step.
506    *position += &input[*position..]
507        .iter()
508        .position(|b| !matches!(b, 0x09 | 0x0A | 0x0C | 0x0D | 0x20 | 0x2F))?;
509
510    // Step 2. If the byte at position is 0x3E (>), then abort the get an attribute algorithm.
511    // There isn't one.
512    if input[*position] == 0x3E {
513        return None;
514    }
515
516    // Step 3. Otherwise, the byte at position is the start of the attribute name.
517    // Let attribute name and attribute value be the empty string.
518    let mut attribute = Attribute::default();
519    let mut have_spaces = false;
520    loop {
521        // Step 4. Process the byte at position as follows:
522        match *input.get(*position)? {
523            // If it is 0x3D (=), and the attribute name is longer than the empty string
524            b'=' if !attribute.name.is_empty() => {
525                // Advance position to the next byte and jump to the step below labeled value.
526                *position += 1;
527                break;
528            },
529
530            // If it is 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR), or 0x20 (SP)
531            0x09 | 0x0A | 0x0C | 0x0D | 0x20 => {
532                // Jump to the step below labeled spaces.
533                have_spaces = true;
534                break;
535            },
536
537            // If it is 0x2F (/) or 0x3E (>)
538            b'/' | b'>' => {
539                // Abort the get an attribute algorithm.
540                // The attribute's name is the value of attribute name, its value is the empty string.
541                return Some(attribute);
542            },
543
544            // If it is in the range 0x41 (A) to 0x5A (Z)
545            byte @ (b'A'..=b'Z') => {
546                // Append the code point b+0x20 to attribute name (where b is the value of the byte at position).
547                // (This converts the input to lowercase.)
548                attribute.name.push(byte + 0x20);
549            },
550
551            // Anything else
552            byte => {
553                // Append the code point with the same value as the byte at position to attribute name.
554                // (It doesn't actually matter how bytes outside the ASCII range are handled here, since only
555                // ASCII bytes can contribute to the detection of a character encoding.)
556                attribute.name.push(byte);
557            },
558        }
559
560        // Step 5. Advance position to the next byte and return to the previous step.
561        *position += 1;
562    }
563
564    if have_spaces {
565        // Step 6. Spaces: If the byte at position is one of 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR),
566        // or 0x20 (SP), then advance position to the next byte, then, repeat this step.
567        *position += &input[*position..]
568            .iter()
569            .position(|b| !b.is_ascii_whitespace())?;
570
571        // Step 7. If the byte at position is not 0x3D (=), abort the get an attribute algorithm.
572        // The attribute's name is the value of attribute name, its value is the empty string.
573        if input[*position] != b'=' {
574            return Some(attribute);
575        }
576
577        // Step 8. Advance position past the 0x3D (=) byte.
578        *position += 1;
579    }
580
581    // Step 9. Value: If the byte at position is one of 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR), or 0x20 (SP),
582    // then advance position to the next byte, then, repeat this step.
583    *position += &input[*position..]
584        .iter()
585        .position(|b| !b.is_ascii_whitespace())?;
586
587    // Step 10. Process the byte at position as follows:
588    match input[*position] {
589        // If it is 0x22 (") or 0x27 (')
590        b @ (b'"' | b'\'') => {
591            // Step 1. Let b be the value of the byte at position.
592            // NOTE: We already have b.
593            loop {
594                // Step 2. Quote loop: Advance position to the next byte.
595                *position += 1;
596
597                // Step 3. If the value of the byte at position is the value of b, then advance position to the next byte
598                // and abort the "get an attribute" algorithm. The attribute's name is the value of attribute name, and
599                // its value is the value of attribute value.
600                let byte_at_position = *input.get(*position)?;
601                if byte_at_position == b {
602                    *position += 1;
603                    return Some(attribute);
604                }
605                // Step 4. Otherwise, if the value of the byte at position is in the range 0x41 (A) to 0x5A (Z),
606                // then append a code point to attribute value whose value is 0x20 more than the value of the byte
607                // at position.
608                else if byte_at_position.is_ascii_uppercase() {
609                    attribute.value.push(byte_at_position + 0x20);
610                }
611                // Step 5. Otherwise, append a code point to attribute value whose value is the same
612                // as the value of the byte at position.
613                else {
614                    attribute.value.push(byte_at_position);
615                }
616
617                // Step 6. Return to the step above labeled quote loop.
618            }
619        },
620
621        // If it is 0x3E (>)
622        b'>' => {
623            // Abort the get an attribute algorithm. The attribute's name is the value of attribute name,
624            // its value is the empty string.
625            return Some(attribute);
626        },
627
628        // If it is in the range 0x41 (A) to 0x5A (Z)
629        b @ (b'A'..=b'Z') => {
630            // Append a code point b+0x20 to attribute value (where b is the value of the byte at position).
631            // Advance position to the next byte.
632            attribute.value.push(b + 0x20);
633            *position += 1;
634        },
635
636        // Anything else
637        b => {
638            // Append a code point with the same value as the byte at position to attribute value.
639            // Advance position to the next byte.
640            attribute.value.push(b);
641            *position += 1
642        },
643    }
644
645    loop {
646        // Step 11. Process the byte at position as follows:
647        match *input.get(*position)? {
648            // If it is 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR), 0x20 (SP), or 0x3E (>)
649            0x09 | 0x0A | 0x0C | 0x0D | 0x20 | 0x3E => {
650                // Abort the get an attribute algorithm. The attribute's name is the value of attribute name and
651                // its value is the value of attribute value.
652                return Some(attribute);
653            },
654
655            // If it is in the range 0x41 (A) to 0x5A (Z)
656            byte if byte.is_ascii_uppercase() => {
657                // Append a code point b+0x20 to attribute value (where b is the value of the byte at position).
658                attribute.value.push(byte + 0x20);
659            },
660
661            // Anything else
662            byte => {
663                // Append a code point with the same value as the byte at position to attribute value.
664                attribute.value.push(byte);
665            },
666        }
667
668        // Step 12. Advance position to the next byte and return to the previous step.
669        *position += 1;
670    }
671}
672
673/// <https://html.spec.whatwg.org/multipage/#algorithm-for-extracting-a-character-encoding-from-a-meta-element>
674fn extract_a_character_encoding_from_a_meta_element(input: &[u8]) -> Option<&'static Encoding> {
675    // Step 1. Let position be a pointer into s, initially pointing at the start of the string.
676    let mut position = 0;
677
678    loop {
679        // Step 2. Loop: Find the first seven characters in s after position that are an ASCII case-insensitive
680        // match for the word "charset". If no such match is found, return nothing.
681        // NOTE: In our case, the attribute value always comes from "get_an_attribute" and is already lowercased.
682        position += input[position..]
683            .windows(7)
684            .position(|window| window == b"charset")? +
685            b"charset".len();
686
687        // Step 3. Skip any ASCII whitespace that immediately follow the word "charset" (there might not be any).
688        position += &input[position..]
689            .iter()
690            .position(|byte| !byte.is_ascii_whitespace())?;
691
692        // Step 4. If the next character is not a U+003D EQUALS SIGN (=), then move position to point just before
693        // that next character, and jump back to the step labeled loop.
694        // NOTE: This is phrased very oddly, because position is already pointing to that character.
695        if *input.get(position)? == b'=' {
696            position += 1;
697            break;
698        }
699    }
700
701    // Step 5. Skip any ASCII whitespace that immediately follow the equals sign (there might not be any).
702    position += &input[position..]
703        .iter()
704        .position(|byte| !byte.is_ascii_whitespace())?;
705
706    // Step 6. Process the next character as follows:
707    let next_character = input.get(position)?;
708
709    // If it is a U+0022 QUOTATION MARK character (") and there is a later U+0022 QUOTATION MARK character (") in s
710    // If it is a U+0027 APOSTROPHE character (') and there is a later U+0027 APOSTROPHE character (') in s
711    if matches!(*next_character, b'"' | b'\'') {
712        // Return the result of getting an encoding from the substring that is between
713        // this character and the next earliest occurrence of this character.
714        let remaining = input.get(position + 1..)?;
715        let end = remaining.iter().position(|byte| byte == next_character)?;
716        Encoding::for_label(&remaining[..end])
717    }
718    // If it is an unmatched U+0022 QUOTATION MARK character (")
719    // If it is an unmatched U+0027 APOSTROPHE character (')
720    // If there is no next character
721    // NOTE: All of these cases are already covered above
722
723    // Otherwise
724    else {
725        // Return the result of getting an encoding from the substring that consists of this character up
726        // to but not including the first ASCII whitespace or U+003B SEMICOLON character (;), or the end of s,
727        // whichever comes first.
728        let remaining = input.get(position..)?;
729        let end = remaining
730            .iter()
731            .position(|byte| byte.is_ascii_whitespace() || *byte == b';')
732            .unwrap_or(remaining.len());
733
734        Encoding::for_label(&remaining[..end])
735    }
736}
737
738/// <https://html.spec.whatwg.org/multipage/#concept-get-xml-encoding-when-sniffing>
739pub fn get_xml_encoding(input: &[u8]) -> Option<&'static Encoding> {
740    // Step 1. Let encodingPosition be a pointer to the start of the stream.
741    // NOTE: We don't need this variable yet.
742    // Step 2. If encodingPosition does not point to the start of a byte sequence 0x3C, 0x3F, 0x78,
743    // 0x6D, 0x6C (`<?xml`), then return failure.
744    if !input.starts_with(b"<?xml") {
745        return None;
746    }
747
748    // Step 3. Let xmlDeclarationEnd be a pointer to the next byte in the input byte stream which is 0x3E (>).
749    // If there is no such byte, then return failure.
750    // NOTE: The spec does not use this variable but the intention is clear.
751    let xml_declaration_end = input.iter().position(|byte| *byte == b'>')?;
752    let input = &input[..xml_declaration_end];
753
754    // Step 4. Set encodingPosition to the position of the first occurrence of the subsequence of bytes 0x65, 0x6E,
755    // 0x63, 0x6F, 0x64, 0x69, 0x6E, 0x67 (`encoding`) at or after the current encodingPosition. If there is no
756    // such sequence, then return failure.
757    let mut encoding_position = input
758        .windows(b"encoding".len())
759        .position(|window| window == b"encoding")?;
760
761    // Step 5. Advance encodingPosition past the 0x67 (g) byte.
762    encoding_position += b"encoding".len();
763
764    // Step 6. While the byte at encodingPosition is less than or equal to 0x20 (i.e., it is either an
765    // ASCII space or control character), advance encodingPosition to the next byte.
766    while *input.get(encoding_position)? <= 0x20 {
767        encoding_position += 1;
768    }
769
770    // Step 7. If the byte at encodingPosition is not 0x3D (=), then return failure.
771    if *input.get(encoding_position)? != b'=' {
772        return None;
773    }
774
775    // Step 8. Advance encodingPosition to the next byte.
776    encoding_position += 1;
777
778    // Step 9. While the byte at encodingPosition is less than or equal to 0x20 (i.e., it is either an
779    // ASCII space or control character), advance encodingPosition to the next byte.
780    while *input.get(encoding_position)? <= 0x20 {
781        encoding_position += 1;
782    }
783
784    // Step 10. Let quoteMark be the byte at encodingPosition.
785    let quote_mark = *input.get(encoding_position)?;
786
787    // Step 11. If quoteMark is not either 0x22 (") or 0x27 ('), then return failure.
788    if !matches!(quote_mark, b'"' | b'\'') {
789        return None;
790    }
791
792    // Step 12. Advance encodingPosition to the next byte.
793    encoding_position += 1;
794
795    // Step 13. Let encodingEndPosition be the position of the next occurrence of quoteMark at or after
796    // encodingPosition. If quoteMark does not occur again, then return failure.
797    let encoding_end_position = input[encoding_position..]
798        .iter()
799        .position(|byte| *byte == quote_mark)?;
800
801    // Step 14. Let potentialEncoding be the sequence of the bytes between encodingPosition
802    // (inclusive) and encodingEndPosition (exclusive).
803    let potential_encoding = &input[encoding_position..][..encoding_end_position];
804
805    // Step 15. If potentialEncoding contains one or more bytes whose byte value is 0x20 or below,
806    // then return failure.
807    if potential_encoding.iter().any(|byte| *byte <= 0x20) {
808        return None;
809    }
810
811    // Step 16. Let encoding be the result of getting an encoding given potentialEncoding isomorphic decoded.
812    let encoding = Encoding::for_label(potential_encoding)?;
813
814    // Step 17. If the encoding is UTF-16BE/LE, then change it to UTF-8.
815    // Step 18. Return encoding.
816    if encoding == UTF_16BE || encoding == UTF_16LE {
817        Some(UTF_8)
818    } else {
819        Some(encoding)
820    }
821}
822
823#[derive(PartialEq)]
824enum AtEndOfFile {
825    Yes,
826    No,
827}