private void CalculateBlockWidth(Dimensions containingBlock) { var style = GetStyleNode(); var auto = new Css.Keyword("auto"); var width = style.GetValue("width") ?? auto; var zero = Css.Length.Zero(Css.Unit.Px); var marginLeft = style.Lookup("margin-left", "margin", zero); var marginRight = style.Lookup("margin-right", "margin", zero); var borderLeft = style.Lookup("border-left-width", "border-width", zero); var borderRight = style.Lookup("border-right-width", "border-width", zero); var paddingLeft = style.Lookup("padding-left", "padding", zero); var paddingRight = style.Lookup("padding-right", "padding", zero); var total = new Css.Value[] { marginLeft, marginRight, borderLeft, borderRight, paddingLeft, paddingRight, width }.Select(v => v.ToPx()).Sum(); if (width != auto && total > containingBlock.Content.Width) { if (marginLeft == auto) { marginLeft = Css.Length.Zero(Css.Unit.Px); } if (marginRight == auto) { marginRight = Css.Length.Zero(Css.Unit.Px); } } var underflow = containingBlock.Content.Width - total; if (width != auto && marginLeft != auto && marginRight != auto) { marginRight = new Css.Length(marginRight.ToPx(), Css.Unit.Px); } else if (width != auto && marginLeft != auto && marginRight == auto) { marginRight = new Css.Length(underflow, Css.Unit.Px); } else if (width != auto && marginLeft == auto && marginRight != auto) { marginRight = new Css.Length(underflow, Css.Unit.Px); } else if (width == auto) { if (marginLeft == auto) { marginLeft = Css.Length.Zero(Css.Unit.Px); } if (marginRight == auto) { marginRight = Css.Length.Zero(Css.Unit.Px); } if (underflow >= 0) { width = new Css.Length(underflow, Css.Unit.Px); } else { width = Css.Length.Zero(Css.Unit.Px); marginRight = new Css.Length(marginRight.ToPx() + underflow, Css.Unit.Px); } } else if (width != auto && marginLeft == auto && marginRight == auto) { marginLeft = new Css.Length(underflow / 2, Css.Unit.Px); marginRight = new Css.Length(underflow / 2, Css.Unit.Px); } Dimensions.Content.Width = width.ToPx(); Dimensions.Padding.Left = paddingLeft.ToPx(); Dimensions.Padding.Right = paddingRight.ToPx(); Dimensions.Border.Left = borderLeft.ToPx(); Dimensions.Border.Right = borderRight.ToPx(); Dimensions.Margin.Left = marginLeft.ToPx(); Dimensions.Margin.Right = marginRight.ToPx(); }
public Css.Value Lookup(string name, string fallbackName, Css.Value defaultValue) { return(GetValue(name) ?? GetValue(fallbackName) ?? defaultValue); }