pub type CalcNode = GenericCalcNode<CalcLengthPercentageLeaf>;
Expand description

The computed version of a calc() node for <length-percentage> values.

Aliased Type§

enum CalcNode {
    Leaf(CalcLengthPercentageLeaf),
    Negate(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
    Invert(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
    Sum(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
    Product(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
    MinMax(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>, MinMaxOp),
    Clamp {
        min: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
        center: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
        max: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
    },
    Round {
        strategy: RoundingStrategy,
        value: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
        step: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
    },
    ModRem {
        dividend: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
        divisor: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
        op: ModRemOp,
    },
    Hypot(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
    Abs(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
    Sign(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
}

Variants§

§

Leaf(CalcLengthPercentageLeaf)

A leaf node.

§

Negate(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)

A node that negates its child, e.g. Negate(1) == -1.

§

Invert(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)

A node that inverts its child, e.g. Invert(10) == 1 / 10 == 0.1. The child must always resolve to a number unit.

§

Sum(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>)

A sum node, representing a + b + c where a, b, and c are the arguments.

§

Product(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>)

A product node, representing a * b * c where a, b, and c are the arguments.

§

MinMax(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>, MinMaxOp)

A min or max function.

§

Clamp

A clamp() function.

§

Round

Fields

§strategy: RoundingStrategy

The rounding strategy.

A round() function.

§

ModRem

Fields

§dividend: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>

The dividend calculation.

§divisor: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>

The divisor calculation.

§op: ModRemOp

Is the function mod or rem?

A mod() or rem() function.

§

Hypot(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>)

A hypot() function

§

Abs(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)

An abs() function.

§

Sign(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)

A sign() function.

Implementations§

source§

impl<L: CalcNodeLeaf> CalcNode<L>

source

fn dummy() -> Self

Create a dummy CalcNode that can be used to do replacements of other nodes.

source

fn coerce_to_value(&mut self, value: f32)

Change all the leaf nodes to have the given value. This is useful when you have calc(1px * nan) and you want to replace the product node with calc(nan), in which case the unit will be retained.

source

pub fn is_product_distributive(&self) -> bool

Return true if a product is distributive over this node. Is distributive: (2 + 3) * 4 = 8 + 12 Not distributive: sign(2 + 3) * 4 != sign(8 + 12)

source

pub fn unit(&self) -> Result<CalcUnits, ()>

If the node has a valid unit outcome, then return it, otherwise fail.

source

pub fn negate(&mut self)

Negate the node inline. If the node is distributive, it is replaced by the result, otherwise the node is wrapped in a [Negate] node.

source

fn sort_key(&self) -> SortKey

source

pub fn as_leaf(&self) -> Option<&L>

Returns the leaf if we can (if simplification has allowed it).

source

fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>

Tries to merge one node into another using the sum, that is, perform x + y.

source

pub fn try_product_in_place(&mut self, other: &mut Self) -> bool

Tries to merge one node into another using the product, that is, perform x * y.

source

fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>where O: Fn(f32, f32) -> f32,

Tries to apply a generic arithmetic operator

source

pub fn map(&mut self, op: impl FnMut(f32) -> f32)

Map the value of this node with the given operation.

source

pub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>where O: CalcNodeLeaf, F: FnMut(&L) -> O,

Convert this CalcNode into a CalcNode with a different leaf kind.

source

fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>where O: CalcNodeLeaf, F: FnMut(&L) -> O,

source

pub fn resolve(&self) -> Result<L, ()>

Resolve this node into a value.

source

pub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>where F: FnMut(&L) -> Result<L, ()>,

Resolve this node into a value, given a function that maps the leaf values.

source

fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>where F: FnMut(&L) -> Result<L, ()>,

source

fn is_negative_leaf(&self) -> bool

source

fn is_zero_leaf(&self) -> bool

source

fn is_infinite_leaf(&self) -> bool

source

pub fn visit_depth_first(&mut self, f: impl FnMut(&mut Self))

Visits all the nodes in this calculation tree recursively, starting by the leaves and bubbling all the way up.

This is useful for simplification, but can also be used for validation and such.

source

fn visit_depth_first_internal(&mut self, f: &mut impl FnMut(&mut Self))

source

pub fn simplify_and_sort_direct_children(&mut self)

This function simplifies and sorts the calculation of the specified node. It simplifies directly nested nodes while assuming that all nodes below it have already been simplified. It is recommended to use this function in combination with visit_depth_first().

This function is necessary only if the node needs to be preserved after parsing, specifically for <length-percentage> cases where the calculation contains percentages or relative units. Otherwise, the node can be evaluated using resolve(), which will automatically provide a simplified value.

https://drafts.csswg.org/css-values-4/#calc-simplification

source

pub fn simplify_and_sort(&mut self)

Simplifies and sorts the kids in the whole calculation subtree.

source

fn to_css_impl<W>( &self, dest: &mut CssWriter<'_, W>, level: ArgumentLevel ) -> Resultwhere W: Write,

source

fn compare( &self, other: &Self, basis_positive: PositivePercentageBasis ) -> Option<Ordering>

source

fn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is greater than another.

source

fn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is less than another.

source

fn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is smaller or equal than another.

Trait Implementations§

source§

impl<L: Clone> Clone for GenericCalcNode<L>

source§

fn clone(&self) -> GenericCalcNode<L>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<L: Debug> Debug for GenericCalcNode<L>

source§

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

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

impl<'de, L> Deserialize<'de> for GenericCalcNode<L>where L: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<L> MallocSizeOf for GenericCalcNode<L>where L: MallocSizeOf,

source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.
source§

impl<L: PartialEq> PartialEq<GenericCalcNode<L>> for GenericCalcNode<L>

source§

fn eq(&self, other: &GenericCalcNode<L>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<L> Serialize for GenericCalcNode<L>where L: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<L> ToAnimatedZero for GenericCalcNode<L>where L: ToAnimatedZero,

source§

fn to_animated_zero(&self) -> Result<Self, ()>

Returns a value that, when added with an underlying value, will produce the underlying value. This is used for SMIL animation’s “by-animation” where SMIL first interpolates from the zero value to the ‘by’ value, and then adds the result to the underlying value. Read more
source§

impl<L: CalcNodeLeaf> ToCss for CalcNode<L>

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

fn to_css_string(&self) -> String

Serialize self in CSS syntax and return a string. Read more
source§

impl<L> ToResolvedValue for GenericCalcNode<L>where L: ToResolvedValue,

§

type ResolvedValue = GenericCalcNode<<L as ToResolvedValue>::ResolvedValue>

The resolved value type we’re going to be converted to.
source§

fn from_resolved_value(from: Self::ResolvedValue) -> Self

Convert a resolved value to resolved value form.
source§

fn to_resolved_value(self, context: &Context<'_>) -> Self::ResolvedValue

Convert a resolved value to a resolved value.
source§

impl<L> ToShmem for GenericCalcNode<L>where L: ToShmem,

source§

fn to_shmem(&self, builder: &mut SharedMemoryBuilder) -> Result<Self>

Clones this value into a form suitable for writing into a SharedMemoryBuilder. Read more
source§

impl<L> StructuralPartialEq for GenericCalcNode<L>