Repository: servo/servo
File: components/script/dom/servoparser/encoding.rs (Rust)

Code before:
        // encoding with the confidence certain.
        if let Some(encoding_hint_from_content_type) = self.encoding_hint_from_content_type {
            log::debug!(
                "Inferred encoding to be {} from the Content-Type header",
                encoding_hint_from_content_type.name()
            );
            return Some(encoding_hint_from_content_type);
        }

        // Step 5. Optionally, prescan the byte stream to determine its encoding, with the end condition
        // being when the user agent decides that scanning further bytes would not be efficient.
        // NOTE: According to the spec, we should always try to get an xml encoding right after failing
        // to prescan the byte stream
        let bytes_to_prescan =
            &self.buffered_bytes[..Self::BUFFER_THRESHOLD.min(self.buffered_bytes.len())];
        if let Some(encoding) = prescan_the_byte_stream_to_determine_the_encoding(bytes_to_prescan)
            .or_else(|| get_xml_encoding(bytes_to_prescan))
        {
            log::debug!(
                "Prescanning the byte stream determined that the encoding is {}",
                encoding.name()
            );
            return Some(encoding);
        }

        // TODO: Step 6. If the HTML parser for which this algorithm is being run is associated with a Document d
        // whose container document is non-null, then:
        // Step 6.1 Let parentDocument be d's container document.
        // Step 6.2 If parentDocument's origin is same origin with d's origin and parentDocument's character encoding
        // is not UTF-16BE/LE, then return parentDocument's character encoding, with the confidence tentative.
        if let Some(encoding) = self.encoding_of_container_document {
            if encoding != UTF_16LE && encoding != UTF_16BE {
                log::debug!(
                    "Inferred encoding to be that of the container document, which is {}",
                    encoding.name()
                );
                return Some(encoding);
            }
        }

        // Step 7. Otherwise, if the user agent has information on the likely encoding for this page, e.g.
        // based on the encoding of the page when it was last visited, then return that encoding,
        // with the confidence tentative.
        // NOTE: We have no such information.

        // Step 8. The user agent may attempt to autodetect the character encoding from applying frequency analysis
        // or other algorithms to the data stream.
        let mut encoding_detector = chardetng::EncodingDetector::new();
        encoding_detector.feed(&self.buffered_bytes, is_at_end_of_file == AtEndOfFile::Yes);
        let url = document.url();
        let tld = url

Review comment (suggestion):
```suggestion
        // Step 6. If the HTML parser for which this algorithm is being run is associated with a Document d
```

Code after:
        // encoding with the confidence certain.
        if let Some(encoding_hint_from_content_type) = self.encoding_hint_from_content_type {
            log::debug!(
                "Inferred encoding to be {} from the Content-Type header",
                encoding_hint_from_content_type.name()
            );
            return Some(encoding_hint_from_content_type);
        }

        // Step 5. Optionally, prescan the byte stream to determine its encoding, with the end condition
        // being when the user agent decides that scanning further bytes would not be efficient.
        // NOTE: According to the spec, we should always try to get an xml encoding right after failing
        // to prescan the byte stream
        let bytes_to_prescan =
            &self.buffered_bytes[..Self::BUFFER_THRESHOLD.min(self.buffered_bytes.len())];
        if let Some(encoding) = prescan_the_byte_stream_to_determine_the_encoding(bytes_to_prescan)
            .or_else(|| get_xml_encoding(bytes_to_prescan))
        {
            log::debug!(
                "Prescanning the byte stream determined that the encoding is {}",
                encoding.name()
            );
            return Some(encoding);
        }

        // Step 6. If the HTML parser for which this algorithm is being run is associated with a Document d
        // whose container document is non-null, then:
        // Step 6.1 Let parentDocument be d's container document.
        // Step 6.2 If parentDocument's origin is same origin with d's origin and parentDocument's character encoding
        // is not UTF-16BE/LE, then return parentDocument's character encoding, with the confidence tentative.
        if let Some(encoding) = self.encoding_of_container_document {
            if encoding != UTF_16LE && encoding != UTF_16BE {
                log::debug!(
                    "Inferred encoding to be that of the container document, which is {}",
                    encoding.name()
                );
                return Some(encoding);
            }
        }

        // Step 7. Otherwise, if the user agent has information on the likely encoding for this page, e.g.
        // based on the encoding of the page when it was last visited, then return that encoding,
        // with the confidence tentative.
        // NOTE: We have no such information.

        // Step 8. The user agent may attempt to autodetect the character encoding from applying frequency analysis
        // or other algorithms to the data stream.
        let mut encoding_detector = chardetng::EncodingDetector::new();
        encoding_detector.feed(&self.buffered_bytes, is_at_end_of_file == AtEndOfFile::Yes);
        let url = document.url();
        let tld = url