Skip to main content

Translator

Struct Translator 

Source
pub struct Translator { /* private fields */ }
Expand description

A reusable translator for one Mode, optionally carrying a default namespace prefix.

Every flavour difference — which pseudo-class overrides apply, whether names are ASCII-lowercased, where :lang() reads from — is derived from the mode by the private accessors below. Casing is applied here in the translator, never via Servo’s parser settings.

Default is Mode::Generic with no default namespace: the plain translator, the same as Translator::new(Mode::Generic).

Implementations§

Source§

impl Translator

Source

pub const fn new(mode: Mode) -> Self

Build a translator for one of the three Mode flavours, with no default namespace — see Translator::with_default_namespace_prefix.

The result is immutable and holds no per-selector state, so a single translator can be reused for any number of translations.

Source

pub fn with_default_namespace_prefix( self, prefix: impl Into<Cow<'static, str>>, ) -> Self

Put unprefixed type selectors in a default namespace, the way a stylesheet’s @namespace url(…) does.

This crate never sees namespace URLs, so the default namespace is named by the prefix the emitted XPath should use — one the caller’s namespace map binds, exactly as a written h|p prefix would be:

use css_to_xpath::{Mode, Translator};

let t = Translator::new(Mode::Xhtml).with_default_namespace_prefix("h");
assert_eq!(t.css_to_xpath("body > p", "").unwrap(), "h:body/h:p");
assert_eq!(t.css_to_xpath("p:is(a, b)", "").unwrap(), "h:p[self::h:a or self::h:b]");

The semantics are the CSS Namespaces 3 ones. A default namespace applies to type selectors and to the implicit universal selector of a compound that has none (.c becomes h:*[…], * becomes h:*), but never to attribute selectors — an unprefixed attribute name has no namespace by definition. |e still means “no namespace” and *|e still means “any namespace”; and, per Selectors Level 4, the implicit universal selector of an :is() / :where() / :not() argument is not qualified, so :is(p) picks up the default namespace but :is(.c) does not.

The prefix is checked exactly as a written one is, when the translation reaches it: one that is not a usable XPath name is an Error rather than a guess. An empty prefix means no default namespace.

Source

pub const fn mode(&self) -> Mode

The Mode this translator was built for.

Source

pub fn default_namespace_prefix(&self) -> Option<&str>

The default namespace prefix set by Translator::with_default_namespace_prefix, if any.

Source

pub fn css_to_xpath(&self, css: &str, prefix: &str) -> Result<String, Error>

Translate comma-separated selector groups, each prefixed, joined with “ | “.

prefix is prepended verbatim to every selector-group branch, so it must end in something a node test can follow: an axis ("descendant-or-self::", crate::DESCENDANT_OR_SELF) or a step separator ("//", crate::WHOLE_DOCUMENT). Pass "" for a bare relative expression. Nothing validates it — a prefix like "/html/body " yields /html/body div, which XPath reads as a division, not a path.

A selector group anchored on :scope ignores prefix and anchors on the self:: axis instead, since :scope names the context node the XPath is evaluated from.

§Errors

Returns an Error when the selector is syntactically invalid or uses an unsupported construct.

Trait Implementations§

Source§

impl Clone for Translator

Source§

fn clone(&self) -> Translator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Translator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Translator

Source§

fn default() -> Translator

Returns the “default value” for a type. Read more
Source§

impl Eq for Translator

Source§

impl Hash for Translator

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Translator

Source§

fn eq(&self, other: &Translator) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Translator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.