tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

background.rs (6939B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 use parsing::parse;
      6 use style::properties::longhands::background_size;
      7 use style::properties::longhands::{
      8    background_attachment, background_clip, background_color, background_image,
      9 };
     10 use style::properties::longhands::{
     11    background_origin, background_position_x, background_position_y, background_repeat,
     12 };
     13 use style::properties::shorthands::background;
     14 
     15 #[test]
     16 fn background_shorthand_should_parse_all_available_properties_when_specified() {
     17    let input = "url(\"http://servo/test.png\") top center / 200px 200px repeat-x fixed padding-box content-box red";
     18    let result = parse(background::parse_value, input).unwrap();
     19 
     20    assert_eq!(
     21        result.background_image,
     22        parse_longhand!(background_image, "url(\"http://servo/test.png\")")
     23    );
     24    assert_eq!(
     25        result.background_position_x,
     26        parse_longhand!(background_position_x, "center")
     27    );
     28    assert_eq!(
     29        result.background_position_y,
     30        parse_longhand!(background_position_y, "top")
     31    );
     32    assert_eq!(
     33        result.background_size,
     34        parse_longhand!(background_size, "200px 200px")
     35    );
     36    assert_eq!(
     37        result.background_repeat,
     38        parse_longhand!(background_repeat, "repeat-x")
     39    );
     40    assert_eq!(
     41        result.background_attachment,
     42        parse_longhand!(background_attachment, "fixed")
     43    );
     44    assert_eq!(
     45        result.background_origin,
     46        parse_longhand!(background_origin, "padding-box")
     47    );
     48    assert_eq!(
     49        result.background_clip,
     50        parse_longhand!(background_clip, "content-box")
     51    );
     52    assert_eq!(
     53        result.background_color,
     54        parse_longhand!(background_color, "red")
     55    );
     56 }
     57 
     58 #[test]
     59 fn background_shorthand_should_parse_when_some_fields_set() {
     60    let result = parse(background::parse_value, "14px 40px repeat-y").unwrap();
     61 
     62    assert_eq!(
     63        result.background_position_x,
     64        parse_longhand!(background_position_x, "14px")
     65    );
     66    assert_eq!(
     67        result.background_position_y,
     68        parse_longhand!(background_position_y, "40px")
     69    );
     70    assert_eq!(
     71        result.background_repeat,
     72        parse_longhand!(background_repeat, "repeat-y")
     73    );
     74 
     75    let result = parse(
     76        background::parse_value,
     77        "url(\"http://servo/test.png\") repeat blue",
     78    )
     79    .unwrap();
     80 
     81    assert_eq!(
     82        result.background_image,
     83        parse_longhand!(background_image, "url(\"http://servo/test.png\")")
     84    );
     85    assert_eq!(
     86        result.background_repeat,
     87        parse_longhand!(background_repeat, "repeat")
     88    );
     89    assert_eq!(
     90        result.background_color,
     91        parse_longhand!(background_color, "blue")
     92    );
     93 
     94    let result = parse(background::parse_value, "padding-box").unwrap();
     95 
     96    assert_eq!(
     97        result.background_origin,
     98        parse_longhand!(background_origin, "padding-box")
     99    );
    100    assert_eq!(
    101        result.background_clip,
    102        parse_longhand!(background_clip, "padding-box")
    103    );
    104 
    105    let result = parse(background::parse_value, "url(\"http://servo/test.png\")").unwrap();
    106 
    107    assert_eq!(
    108        result.background_image,
    109        parse_longhand!(background_image, "url(\"http://servo/test.png\")")
    110    );
    111 }
    112 
    113 #[test]
    114 fn background_shorthand_should_parse_comma_separated_declarations() {
    115    let input =
    116        "url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
    117        center / 100% 100% no-repeat, white";
    118    let result = parse(background::parse_value, input).unwrap();
    119 
    120    assert_eq!(
    121        result.background_image,
    122        parse_longhand!(
    123            background_image,
    124            "url(\"http://servo/test.png\"), \
    125        url(\"http://servo/test.png\"), none"
    126        )
    127    );
    128    assert_eq!(
    129        result.background_position_x,
    130        parse_longhand!(background_position_x, "left, center, 0%")
    131    );
    132    assert_eq!(
    133        result.background_position_y,
    134        parse_longhand!(background_position_y, "top, center, 0%")
    135    );
    136    assert_eq!(
    137        result.background_repeat,
    138        parse_longhand!(background_repeat, "no-repeat, no-repeat, repeat")
    139    );
    140    assert_eq!(
    141        result.background_clip,
    142        parse_longhand!(background_clip, "border-box, border-box, border-box")
    143    );
    144    assert_eq!(
    145        result.background_origin,
    146        parse_longhand!(
    147            background_origin,
    148            "padding-box, padding-box, \
    149        padding-box"
    150        )
    151    );
    152    assert_eq!(
    153        result.background_size,
    154        parse_longhand!(background_size, "auto auto, 100% 100%, auto auto")
    155    );
    156    assert_eq!(
    157        result.background_attachment,
    158        parse_longhand!(background_attachment, "scroll, scroll, scroll")
    159    );
    160    assert_eq!(
    161        result.background_color,
    162        parse_longhand!(background_color, "white")
    163    );
    164 }
    165 
    166 #[test]
    167 fn background_shorthand_should_parse_position_and_size_correctly() {
    168    let result = parse(background::parse_value, "7px 4px").unwrap();
    169 
    170    assert_eq!(
    171        result.background_position_x,
    172        parse_longhand!(background_position_x, "7px")
    173    );
    174    assert_eq!(
    175        result.background_position_y,
    176        parse_longhand!(background_position_y, "4px")
    177    );
    178 
    179    let result = parse(background::parse_value, "7px 4px / 30px 20px").unwrap();
    180 
    181    assert_eq!(
    182        result.background_position_x,
    183        parse_longhand!(background_position_x, "7px")
    184    );
    185    assert_eq!(
    186        result.background_position_y,
    187        parse_longhand!(background_position_y, "4px")
    188    );
    189    assert_eq!(
    190        result.background_size,
    191        parse_longhand!(background_size, "30px 20px")
    192    );
    193 
    194    assert!(parse(background::parse_value, "/ 30px 20px").is_err());
    195 
    196    assert!(parse(background::parse_value, "repeat-x / 30px 20px").is_err());
    197 }
    198 
    199 #[test]
    200 fn background_shorthand_should_parse_origin_and_clip_correctly() {
    201    let result = parse(background::parse_value, "padding-box content-box").unwrap();
    202 
    203    assert_eq!(
    204        result.background_origin,
    205        parse_longhand!(background_origin, "padding-box")
    206    );
    207    assert_eq!(
    208        result.background_clip,
    209        parse_longhand!(background_clip, "content-box")
    210    );
    211 
    212    let result = parse(background::parse_value, "padding-box padding-box").unwrap();
    213 
    214    assert_eq!(
    215        result.background_origin,
    216        parse_longhand!(background_origin, "padding-box")
    217    );
    218    assert_eq!(
    219        result.background_clip,
    220        parse_longhand!(background_clip, "padding-box")
    221    );
    222 
    223    let result = parse(background::parse_value, "padding-box").unwrap();
    224 
    225    assert_eq!(
    226        result.background_origin,
    227        parse_longhand!(background_origin, "padding-box")
    228    );
    229    assert_eq!(
    230        result.background_clip,
    231        parse_longhand!(background_clip, "padding-box")
    232    );
    233 }