1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* 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 http://mozilla.org/MPL/2.0/. */

//! Specified types for text properties.

use cssparser::{Parser, Token};
use parser::{Parse, ParserContext};
#[cfg(feature = "servo")]
use properties::{longhands, PropertyDeclaration};
use selectors::parser::SelectorParseErrorKind;
#[allow(unused_imports)] use std::ascii::AsciiExt;
use std::fmt;
use style_traits::{ParseError, StyleParseErrorKind, ToCss};
use values::computed::{Context, ToComputedValue};
use values::computed::text::LineHeight as ComputedLineHeight;
use values::computed::text::TextOverflow as ComputedTextOverflow;
use values::generics::text::InitialLetter as GenericInitialLetter;
use values::generics::text::LineHeight as GenericLineHeight;
use values::generics::text::Spacing;
use values::specified::{AllowQuirks, Integer, NonNegativeNumber, Number};
use values::specified::length::{FontRelativeLength, Length, LengthOrPercentage, NoCalcLength};
use values::specified::length::NonNegativeLengthOrPercentage;

/// A specified type for the `initial-letter` property.
pub type InitialLetter = GenericInitialLetter<Number, Integer>;

/// A specified value for the `letter-spacing` property.
pub type LetterSpacing = Spacing<Length>;

/// A specified value for the `word-spacing` property.
pub type WordSpacing = Spacing<LengthOrPercentage>;

/// A specified value for the `line-height` property.
pub type LineHeight = GenericLineHeight<NonNegativeNumber, NonNegativeLengthOrPercentage>;

impl Parse for InitialLetter {
    fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        if input.try(|i| i.expect_ident_matching("normal")).is_ok() {
            return Ok(GenericInitialLetter::Normal);
        }
        let size = Number::parse_at_least_one(context, input)?;
        let sink = input.try(|i| Integer::parse_positive(context, i)).ok();
        Ok(GenericInitialLetter::Specified(size, sink))
    }
}

impl Parse for LetterSpacing {
    fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        Spacing::parse_with(context, input, |c, i| {
            Length::parse_quirky(c, i, AllowQuirks::Yes)
        })
    }
}

impl Parse for WordSpacing {
    fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        Spacing::parse_with(context, input, |c, i| {
            LengthOrPercentage::parse_quirky(c, i, AllowQuirks::Yes)
        })
    }
}

impl Parse for LineHeight {
    fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        if let Ok(number) = input.try(|i| NonNegativeNumber::parse(context, i)) {
            return Ok(GenericLineHeight::Number(number))
        }
        if let Ok(nlop) = input.try(|i| NonNegativeLengthOrPercentage::parse(context, i)) {
            return Ok(GenericLineHeight::Length(nlop))
        }
        let location = input.current_source_location();
        let ident = input.expect_ident()?;
        match ident {
            ref ident if ident.eq_ignore_ascii_case("normal") => {
                Ok(GenericLineHeight::Normal)
            },
            #[cfg(feature = "gecko")]
            ref ident if ident.eq_ignore_ascii_case("-moz-block-height") => {
                Ok(GenericLineHeight::MozBlockHeight)
            },
            ident => Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))),
        }
    }
}

impl ToComputedValue for LineHeight {
    type ComputedValue = ComputedLineHeight;

    #[inline]
    fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
        use values::computed::Length as ComputedLength;
        use values::specified::length::FontBaseSize;
        match *self {
            GenericLineHeight::Normal => {
                GenericLineHeight::Normal
            },
            #[cfg(feature = "gecko")]
            GenericLineHeight::MozBlockHeight => {
                GenericLineHeight::MozBlockHeight
            },
            GenericLineHeight::Number(number) => {
                GenericLineHeight::Number(number.to_computed_value(context))
            },
            GenericLineHeight::Length(ref non_negative_lop) => {
                let result = match non_negative_lop.0 {
                    LengthOrPercentage::Length(NoCalcLength::Absolute(ref abs)) => {
                        context.maybe_zoom_text(abs.to_computed_value(context).into()).0
                    }
                    LengthOrPercentage::Length(ref length) => {
                        length.to_computed_value(context)
                    },
                    LengthOrPercentage::Percentage(ref p) => {
                        FontRelativeLength::Em(p.0)
                            .to_computed_value(
                                context,
                                FontBaseSize::CurrentStyle,
                            )
                    }
                    LengthOrPercentage::Calc(ref calc) => {
                        let computed_calc =
                            calc.to_computed_value_zoomed(context, FontBaseSize::CurrentStyle);
                        let font_relative_length =
                            FontRelativeLength::Em(computed_calc.percentage())
                                .to_computed_value(
                                    context,
                                    FontBaseSize::CurrentStyle,
                                ).px();

                        let absolute_length = computed_calc.unclamped_length().px();
                        let pixel = computed_calc
                            .clamping_mode
                            .clamp(absolute_length + font_relative_length);
                        ComputedLength::new(pixel)
                    }
                };
                GenericLineHeight::Length(result.into())
            }
        }
    }

    #[inline]
    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
        match *computed {
            GenericLineHeight::Normal => {
                GenericLineHeight::Normal
            },
            #[cfg(feature = "gecko")]
            GenericLineHeight::MozBlockHeight => {
                GenericLineHeight::MozBlockHeight
            },
            GenericLineHeight::Number(ref number) => {
                GenericLineHeight::Number(NonNegativeNumber::from_computed_value(number))
            },
            GenericLineHeight::Length(ref length) => {
                GenericLineHeight::Length(NoCalcLength::from_computed_value(&length.0).into())
            }
        }
    }
}

/// A generic value for the `text-overflow` property.
#[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToCss)]
pub enum TextOverflowSide {
    /// Clip inline content.
    Clip,
    /// Render ellipsis to represent clipped inline content.
    Ellipsis,
    /// Render a given string to represent clipped inline content.
    String(Box<str>),
}

impl Parse for TextOverflowSide {
    fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
                        -> Result<TextOverflowSide, ParseError<'i>> {
        let location = input.current_source_location();
        match *input.next()? {
            Token::Ident(ref ident) => {
                match_ignore_ascii_case! { ident,
                    "clip" => Ok(TextOverflowSide::Clip),
                    "ellipsis" => Ok(TextOverflowSide::Ellipsis),
                    _ => Err(location.new_custom_error(
                        SelectorParseErrorKind::UnexpectedIdent(ident.clone())
                    ))
                }
            }
            Token::QuotedString(ref v) => {
                Ok(TextOverflowSide::String(v.as_ref().to_owned().into_boxed_str()))
            }
            ref t => Err(location.new_unexpected_token_error(t.clone())),
        }
    }
}

#[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToCss)]
/// text-overflow. Specifies rendering when inline content overflows its line box edge.
pub struct TextOverflow {
    /// First value. Applies to end line box edge if no second is supplied; line-left edge otherwise.
    pub first: TextOverflowSide,
    /// Second value. Applies to the line-right edge if supplied.
    pub second: Option<TextOverflowSide>,
}

impl Parse for TextOverflow {
    fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<TextOverflow, ParseError<'i>> {
        let first = TextOverflowSide::parse(context, input)?;
        let second = input.try(|input| TextOverflowSide::parse(context, input)).ok();
        Ok(TextOverflow { first, second })
    }
}

impl ToComputedValue for TextOverflow {
    type ComputedValue = ComputedTextOverflow;

    #[inline]
    fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue {
        if let Some(ref second) = self.second {
            Self::ComputedValue {
                first: self.first.clone(),
                second: second.clone(),
                sides_are_logical: false,
            }
        } else {
            Self::ComputedValue {
                first: TextOverflowSide::Clip,
                second: self.first.clone(),
                sides_are_logical: true,
            }
        }
    }

    #[inline]
    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
        if computed.sides_are_logical {
            assert!(computed.first == TextOverflowSide::Clip);
            TextOverflow {
                first: computed.second.clone(),
                second: None,
            }
        } else {
            TextOverflow {
                first: computed.first.clone(),
                second: Some(computed.second.clone()),
            }
        }
    }
}

bitflags! {
    #[derive(MallocSizeOf, ToComputedValue)]
    /// Specified keyword values for the text-decoration-line property.
    pub struct TextDecorationLine: u8 {
        /// No text decoration line is specified
        const NONE = 0;
        /// Underline
        const UNDERLINE = 0x01;
        /// Overline
        const OVERLINE = 0x02;
        /// Line through
        const LINE_THROUGH = 0x04;
        /// Blink
        const BLINK = 0x08;
        #[cfg(feature = "gecko")]
        /// Only set by presentation attributes
        ///
        /// Setting this will mean that text-decorations use the color
        /// specified by `color` in quirks mode.
        ///
        /// For example, this gives <a href=foo><font color="red">text</font></a>
        /// a red text decoration
        const COLOR_OVERRIDE = 0x10;
    }
}

#[cfg(feature = "gecko")]
impl_bitflags_conversions!(TextDecorationLine);

impl TextDecorationLine {
    #[inline]
    /// Returns the initial value of text-decoration-line
    pub fn none() -> Self {
        TextDecorationLine::NONE
    }

    #[cfg(feature = "servo")]
    #[inline]
    /// Custom cascade for the text-decoration-line property in servo
    pub fn cascade_property_custom(_declaration: &PropertyDeclaration, context: &mut Context) {
        longhands::_servo_text_decorations_in_effect::derive_from_text_decoration(context);
    }
}

impl Parse for TextDecorationLine {
    /// none | [ underline || overline || line-through || blink ]
    fn parse<'i, 't>(
        _context: &ParserContext,
        input: &mut Parser<'i, 't>
    ) -> Result<TextDecorationLine, ParseError<'i>> {
        let mut result = TextDecorationLine::NONE;
        if input.try(|input| input.expect_ident_matching("none")).is_ok() {
            return Ok(result)
        }

        loop {
            let result: Result<_, ParseError> = input.try(|input| {
                try_match_ident_ignore_ascii_case! { input,
                    "underline" => {
                        if result.contains(TextDecorationLine::UNDERLINE) {
                            Err(())
                        } else {
                            result.insert(TextDecorationLine::UNDERLINE);
                            Ok(())
                        }
                    }
                    "overline" => {
                        if result.contains(TextDecorationLine::OVERLINE) {
                            Err(())
                        } else {
                            result.insert(TextDecorationLine::OVERLINE);
                            Ok(())
                        }
                    }
                    "line-through" => {
                        if result.contains(TextDecorationLine::LINE_THROUGH) {
                            Err(())
                        } else {
                            result.insert(TextDecorationLine::LINE_THROUGH);
                            Ok(())
                        }
                    }
                    "blink" => {
                        if result.contains(TextDecorationLine::BLINK) {
                            Err(())
                        } else {
                            result.insert(TextDecorationLine::BLINK);
                            Ok(())
                        }
                    }
                }
            });
            if result.is_err() {
                break;
            }
        }

        if !result.is_empty() {
            Ok(result)
        } else {
            Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
        }
    }
}

macro_rules! define_text_align_keyword {
    ($($name: ident => $discriminant: expr,)+) => {
        /// Specified value of text-align keyword value.
        #[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, Parse, PartialEq, ToComputedValue, ToCss)]
        #[allow(missing_docs)]
        pub enum TextAlignKeyword {
            $(
                $name = $discriminant,
            )+
        }

        impl TextAlignKeyword {
            /// Construct a TextAlignKeyword from u32.
            pub fn from_u32(discriminant: u32) -> Option<TextAlignKeyword> {
                match discriminant {
                    $(
                        $discriminant => Some(TextAlignKeyword::$name),
                    )+
                    _ => None
                }
            }
        }
    }
}

// FIXME(emilio): Why reinventing the world?
#[cfg(feature = "gecko")]
define_text_align_keyword! {
    Start => 0,
    End => 1,
    Left => 2,
    Right => 3,
    Center => 4,
    Justify => 5,
    MozCenter => 6,
    MozLeft => 7,
    MozRight => 8,
    Char => 10,
}

#[cfg(feature = "servo")]
define_text_align_keyword! {
    Start => 0,
    End => 1,
    Left => 2,
    Right => 3,
    Center => 4,
    Justify => 5,
    ServoCenter => 6,
    ServoLeft => 7,
    ServoRight => 8,
}

impl TextAlignKeyword {
    /// Return the initial value of TextAlignKeyword.
    #[inline]
    pub fn start() -> TextAlignKeyword {
        TextAlignKeyword::Start
    }
}

/// Specified value of text-align property.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TextAlign {
    /// Keyword value of text-align property.
    Keyword(TextAlignKeyword),
    /// `match-parent` value of text-align property. It has a different handling
    /// unlike other keywords.
    #[cfg(feature = "gecko")]
    MatchParent,
    /// `MozCenterOrInherit` value of text-align property. It cannot be parsed,
    /// only set directly on the elements and it has a different handling
    /// unlike other values.
    #[cfg(feature = "gecko")]
    MozCenterOrInherit,

}

impl Parse for TextAlign {
    fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        // MozCenterOrInherit cannot be parsed, only set directly on the elements
        if let Ok(key) = input.try(TextAlignKeyword::parse) {
            return Ok(TextAlign::Keyword(key));
        }
        #[cfg(feature = "gecko")]
        {
            input.expect_ident_matching("match-parent")?;
            return Ok(TextAlign::MatchParent);
        }
        #[cfg(feature = "servo")]
        {
            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }
    }
}

impl ToCss for TextAlign {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
        match *self {
            TextAlign::Keyword(key) => key.to_css(dest),
            #[cfg(feature = "gecko")]
            TextAlign::MatchParent => dest.write_str("match-parent"),
            #[cfg(feature = "gecko")]
            TextAlign::MozCenterOrInherit => Ok(()),
        }
    }
}

impl TextAlign {
    /// Convert an enumerated value coming from Gecko to a `TextAlign`.
    #[cfg(feature = "gecko")]
    pub fn from_gecko_keyword(kw: u32) -> Self {
        use gecko_bindings::structs::NS_STYLE_TEXT_ALIGN_MATCH_PARENT;
        if kw == NS_STYLE_TEXT_ALIGN_MATCH_PARENT {
            TextAlign::MatchParent
        } else {
            TextAlign::Keyword(TextAlignKeyword::from_gecko_keyword(kw))
        }
    }
}

impl ToComputedValue for TextAlign {
    type ComputedValue = TextAlignKeyword;

    #[inline]
    fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue {
        match *self {
            TextAlign::Keyword(key) => key,
            #[cfg(feature = "gecko")]
            TextAlign::MatchParent => {
                // on the root <html> element we should still respect the dir
                // but the parent dir of that element is LTR even if it's <html dir=rtl>
                // and will only be RTL if certain prefs have been set.
                // In that case, the default behavior here will set it to left,
                // but we want to set it to right -- instead set it to the default (`start`),
                // which will do the right thing in this case (but not the general case)
                if _context.is_root_element {
                    return TextAlignKeyword::start();
                }
                let parent = _context.builder.get_parent_inheritedtext().clone_text_align();
                let ltr = _context.builder.inherited_writing_mode().is_bidi_ltr();
                match (parent, ltr) {
                    (TextAlignKeyword::Start, true) => TextAlignKeyword::Left,
                    (TextAlignKeyword::Start, false) => TextAlignKeyword::Right,
                    (TextAlignKeyword::End, true) => TextAlignKeyword::Right,
                    (TextAlignKeyword::End, false) => TextAlignKeyword::Left,
                    _ => parent
                }
            },
            #[cfg(feature = "gecko")]
            TextAlign::MozCenterOrInherit => {
                let parent = _context.builder.get_parent_inheritedtext().clone_text_align();
                if parent == TextAlignKeyword::Start {
                    TextAlignKeyword::Center
                } else {
                    parent
                }
            }
        }
    }

    #[inline]
    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
        TextAlign::Keyword(*computed)
    }
}