Extend whitespace::T with additional helper methods

This commit is contained in:
Keith Yeung 2015-11-16 01:32:27 -08:00
parent a5babb89a0
commit 7de7cb4786
5 changed files with 47 additions and 48 deletions

View file

@ -1238,36 +1238,6 @@ impl Fragment {
self.style().get_inheritedtext().white_space self.style().get_inheritedtext().white_space
} }
pub fn white_space_allow_wrap(&self) -> bool {
match self.white_space() {
white_space::T::nowrap |
white_space::T::pre => false,
white_space::T::normal |
white_space::T::pre_wrap |
white_space::T::pre_line => true,
}
}
pub fn white_space_preserve_newlines(&self) -> bool {
match self.white_space() {
white_space::T::normal |
white_space::T::nowrap => false,
white_space::T::pre |
white_space::T::pre_wrap |
white_space::T::pre_line => true,
}
}
pub fn white_space_preserve_spaces(&self) -> bool {
match self.white_space() {
white_space::T::normal |
white_space::T::nowrap |
white_space::T::pre_line => false,
white_space::T::pre |
white_space::T::pre_wrap => true,
}
}
/// Returns the text decoration of this fragment, according to the style of the nearest ancestor /// Returns the text decoration of this fragment, according to the style of the nearest ancestor
/// element. /// element.
/// ///
@ -1297,7 +1267,7 @@ impl Fragment {
/// Returns true if this element can be split. This is true for text fragments, unless /// Returns true if this element can be split. This is true for text fragments, unless
/// `white-space: pre` or `white-space: nowrap` is set. /// `white-space: pre` or `white-space: nowrap` is set.
pub fn can_split(&self) -> bool { pub fn can_split(&self) -> bool {
self.is_scanned_text_fragment() && self.white_space_allow_wrap() self.is_scanned_text_fragment() && self.white_space().allow_wrap()
} }
/// Returns true if and only if this fragment is a generated content fragment. /// Returns true if and only if this fragment is a generated content fragment.
@ -1371,7 +1341,7 @@ impl Fragment {
.metrics_for_range(range) .metrics_for_range(range)
.advance_width; .advance_width;
let min_line_inline_size = if self.white_space_allow_wrap() { let min_line_inline_size = if self.white_space().allow_wrap() {
text_fragment_info.run.min_width_for_range(range) text_fragment_info.run.min_width_for_range(range)
} else { } else {
max_line_inline_size max_line_inline_size
@ -2224,7 +2194,7 @@ impl Fragment {
} }
pub fn strip_leading_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult { pub fn strip_leading_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult {
if self.white_space_preserve_spaces() { if self.white_space().preserve_spaces() {
return WhitespaceStrippingResult::RetainFragment return WhitespaceStrippingResult::RetainFragment
} }
@ -2287,7 +2257,7 @@ impl Fragment {
/// Returns true if the entire fragment was stripped. /// Returns true if the entire fragment was stripped.
pub fn strip_trailing_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult { pub fn strip_trailing_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult {
if self.white_space_preserve_spaces() { if self.white_space().preserve_spaces() {
return WhitespaceStrippingResult::RetainFragment return WhitespaceStrippingResult::RetainFragment
} }

View file

@ -534,7 +534,7 @@ impl LineBreaker {
self.pending_line.green_zone = line_bounds.size; self.pending_line.green_zone = line_bounds.size;
false false
} else { } else {
fragment.white_space_allow_wrap() fragment.white_space().allow_wrap()
}; };
debug!("LineBreaker: trying to append to line {} (fragment size: {:?}, green zone: {:?}): \ debug!("LineBreaker: trying to append to line {} (fragment size: {:?}, green zone: {:?}): \
@ -564,7 +564,7 @@ impl LineBreaker {
// If we must flush the line after finishing this fragment due to `white-space: pre`, // If we must flush the line after finishing this fragment due to `white-space: pre`,
// detect that. // detect that.
let line_flush_mode = if fragment.white_space_preserve_newlines() { let line_flush_mode = if fragment.white_space().preserve_newlines() {
if fragment.requires_line_break_afterward_if_wrapping_on_newlines() { if fragment.requires_line_break_afterward_if_wrapping_on_newlines() {
LineFlushMode::Flush LineFlushMode::Flush
} else { } else {
@ -588,7 +588,7 @@ impl LineBreaker {
// If the wrapping mode prevents us from splitting, then back up and split at the last // If the wrapping mode prevents us from splitting, then back up and split at the last
// known good split point. // known good split point.
if !fragment.white_space_allow_wrap() { if !fragment.white_space().allow_wrap() {
debug!("LineBreaker: fragment can't split; falling back to last known good split point"); debug!("LineBreaker: fragment can't split; falling back to last known good split point");
if !self.split_line_at_last_known_good_position() { if !self.split_line_at_last_known_good_position() {
// No line breaking opportunity exists at all for this line. Overflow. // No line breaking opportunity exists at all for this line. Overflow.

View file

@ -40,7 +40,7 @@ fn text(fragments: &LinkedList<Fragment>) -> String {
for fragment in fragments { for fragment in fragments {
match fragment.specific { match fragment.specific {
SpecificFragmentInfo::UnscannedText(ref info) => { SpecificFragmentInfo::UnscannedText(ref info) => {
if fragment.white_space_preserve_newlines() { if fragment.white_space().preserve_newlines() {
text.push_str(&info.text); text.push_str(&info.text);
} else { } else {
text.push_str(&info.text.replace("\n", " ")); text.push_str(&info.text.replace("\n", " "));
@ -411,7 +411,7 @@ fn split_first_fragment_at_newline_if_necessary(fragments: &mut LinkedList<Fragm
let string_before; let string_before;
let insertion_point_before; let insertion_point_before;
{ {
if !first_fragment.white_space_preserve_newlines() { if !first_fragment.white_space().preserve_newlines() {
return; return;
} }

View file

@ -63,7 +63,7 @@ use std::mem;
use std::sync::Arc; use std::sync::Arc;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
use style::computed_values::content::ContentItem; use style::computed_values::content::ContentItem;
use style::computed_values::{content, display, white_space}; use style::computed_values::{content, display};
use style::node::TElementAttributes; use style::node::TElementAttributes;
use style::properties::ComputedValues; use style::properties::ComputedValues;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock}; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock};
@ -960,13 +960,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
// //
// If you implement other values for this property, you will almost certainly // If you implement other values for this property, you will almost certainly
// want to update this check. // want to update this check.
match self.style().get_inheritedtext().white_space { !self.style().get_inheritedtext().white_space.preserve_newlines()
white_space::T::normal |
white_space::T::nowrap => true,
white_space::T::pre |
white_space::T::pre_wrap |
white_space::T::pre_line => false,
}
} }
} }

View file

@ -2331,7 +2331,42 @@ pub mod longhands {
} }
</%self:longhand> </%self:longhand>
${single_keyword("white-space", "normal pre nowrap pre-wrap pre-line")} <%self:single_keyword_computed name="white-space" values="normal pre nowrap pre-wrap pre-line">
use values::computed::ComputedValueAsSpecified;
impl ComputedValueAsSpecified for SpecifiedValue {}
impl SpecifiedValue {
pub fn allow_wrap(&self) -> bool {
match *self {
SpecifiedValue::nowrap |
SpecifiedValue::pre => false,
SpecifiedValue::normal |
SpecifiedValue::pre_wrap |
SpecifiedValue::pre_line => true,
}
}
pub fn preserve_newlines(&self) -> bool {
match *self {
SpecifiedValue::normal |
SpecifiedValue::nowrap => false,
SpecifiedValue::pre |
SpecifiedValue::pre_wrap |
SpecifiedValue::pre_line => true,
}
}
pub fn preserve_spaces(&self) -> bool {
match *self {
SpecifiedValue::normal |
SpecifiedValue::nowrap |
SpecifiedValue::pre_line => false,
SpecifiedValue::pre |
SpecifiedValue::pre_wrap => true,
}
}
}
</%self:single_keyword_computed>
// TODO(pcwalton): `full-width` // TODO(pcwalton): `full-width`
${single_keyword("text-transform", "none capitalize uppercase lowercase")} ${single_keyword("text-transform", "none capitalize uppercase lowercase")}