servo/components/script/dom/servoparser/xml.rs
Pi-Cla 160c7c0b0f
Fix some clippy warnings in components/gfx and components/script (#32215)
* clippy: Squish warnings and errors in gfx

warning: redundant closure (gfx/font.rs:415:18)

warning: useless conversion to the same type (gfx/font.rs:534:9)

warning: the following explicit lifetimes could be elided: 'a (gfx/font.rs:619:16)

error: this loop never actually loops (gfx/font_cache_thread.rs:112:9)

warning: this expression creates a reference which is immediately dereferenced by the compiler  (gfx/font_cache_thread.rs:229:51)

warning: redundant closure (gfx/font_cache_thread.rs:551:18)

3 instances of:
warning: casting integer literal to `f64` is unnecessary (gfx/platform/freetype/font_list.rs:271-273)

* clippy: methods called `from_*` usually take no `self`

It reports that by standard convention, from_* methods should not take any `&self` parameter

* clippy: you should consider adding a `Default` implementation

It reports that public types with a pub fn new() -> Self should have a Default implementation since they can be constructed without arguments

* clippy: casting to the same type is unnecessary (`f32` -> `f32`)

* clippy: use of `unwrap_or_else` to construct default value

* clippy: methods called `is_*` usually take `self` by mutable reference or `self` by reference or no `self`

* clippy: manual `!RangeInclusive::contains` implementation

contains expresses the intent better and has less failure modes (such as fencepost errors or using || instead of &&)

* clippy: this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`

* clippy: Fix some new warnings

warning: this `if` statement can be collapsed (gfx/font.rs:468:130)

warning: this lifetime isn't used in the impl (gfx/platform/freetype/font.rs:341:6)

warning: field assignment outside of initializer for an instance created with Default::default() (compositor.rs:881:17)
2024-05-02 20:02:21 +00:00

81 lines
2.5 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![allow(crown::unrooted_must_root)]
use html5ever::tokenizer::TokenizerResult;
use js::jsapi::JSTracer;
use servo_url::ServoUrl;
use xml5ever::buffer_queue::BufferQueue;
use xml5ever::tokenizer::XmlTokenizer;
use xml5ever::tree_builder::{Tracer as XmlTracer, XmlTreeBuilder};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::trace::{CustomTraceable, JSTraceable};
use crate::dom::document::Document;
use crate::dom::htmlscriptelement::HTMLScriptElement;
use crate::dom::node::Node;
use crate::dom::servoparser::{ParsingAlgorithm, Sink};
#[derive(JSTraceable, MallocSizeOf)]
#[crown::unrooted_must_root_lint::must_root]
pub struct Tokenizer {
#[ignore_malloc_size_of = "Defined in xml5ever"]
inner: XmlTokenizer<XmlTreeBuilder<Dom<Node>, Sink>>,
}
impl Tokenizer {
pub fn new(document: &Document, url: ServoUrl) -> Self {
let sink = Sink {
base_url: url,
document: Dom::from_ref(document),
current_line: 1,
script: Default::default(),
parsing_algorithm: ParsingAlgorithm::Normal,
};
let tb = XmlTreeBuilder::new(sink, Default::default());
let tok = XmlTokenizer::new(tb, Default::default());
Tokenizer { inner: tok }
}
pub fn feed(&mut self, input: &mut BufferQueue) -> TokenizerResult<DomRoot<HTMLScriptElement>> {
self.inner.run(input);
match self.inner.sink.sink.script.take() {
Some(script) => TokenizerResult::Script(script),
None => TokenizerResult::Done,
}
}
pub fn end(&mut self) {
self.inner.end()
}
pub fn url(&self) -> &ServoUrl {
&self.inner.sink.sink.base_url
}
}
#[allow(unsafe_code)]
unsafe impl CustomTraceable for XmlTokenizer<XmlTreeBuilder<Dom<Node>, Sink>> {
unsafe fn trace(&self, trc: *mut JSTracer) {
struct Tracer(*mut JSTracer);
let tracer = Tracer(trc);
impl XmlTracer for Tracer {
type Handle = Dom<Node>;
#[allow(crown::unrooted_must_root)]
fn trace_handle(&self, node: &Dom<Node>) {
unsafe {
node.trace(self.0);
}
}
}
let tree_builder = &self.sink;
tree_builder.trace_handles(&tracer);
tree_builder.sink.trace(trc);
}
}