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
use crate::GenericNode;
use delegate::delegate;
use std::convert::TryFrom;

pub struct StyleSheet {
    inner: web_sys::StyleSheet,
}

impl StyleSheet {
    delegate! {
        target self.inner {
            pub fn disabled(&self) -> bool;

            pub fn set_disabled(&self, disabled: bool);

            pub fn title(&self) -> Option<String>;
        }
    }

    pub fn href(&self) -> Option<String> {
        self.inner.href().ok().flatten()
    }

    pub fn style_sheet_type(&self) -> String {
        self.inner.type_()
    }

    pub fn owner_node(&self) -> Option<GenericNode> {
        self.inner.owner_node().map(|n| n.into())
    }

    pub fn parent_style_sheet(&self) -> Option<StyleSheet> {
        self.inner.parent_style_sheet().map(|s| s.into())
    }

    pub fn media(&self) -> StylesheetMedia {
        StylesheetMedia {
            inner: self.inner.media(),
        }
    }

    pub fn set_media(&self, media: &str) {
        self.inner.media().set_media_text(media);
    }
}

impl AsRef<web_sys::StyleSheet> for StyleSheet {
    fn as_ref(&self) -> &web_sys::StyleSheet {
        &self.inner
    }
}

impl From<web_sys::StyleSheet> for StyleSheet {
    fn from(inner: web_sys::StyleSheet) -> Self {
        StyleSheet { inner }
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct InvalidMedium(String);

pub struct StylesheetMedia {
    inner: web_sys::MediaList,
}

impl StylesheetMedia {
    pub fn get(&self, index: usize) -> Option<String> {
        u32::try_from(index)
            .ok()
            .and_then(|index| self.inner.get(index))
    }

    pub fn len(&self) -> usize {
        self.inner.length() as usize
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn is_not_empty(&self) -> bool {
        !self.is_empty()
    }

    pub fn first(&self) -> Option<String> {
        self.get(0)
    }

    pub fn last(&self) -> Option<String> {
        let len = self.len();

        if len > 0 {
            self.get(len - 1)
        } else {
            None
        }
    }

    pub fn push(&self, medium: &str) -> Result<(), InvalidMedium> {
        self.inner
            .append_medium(medium)
            .map_err(|_| InvalidMedium(medium.to_string()))
    }

    pub fn remove(&self, medium: &str) -> bool {
        self.inner.delete_medium(medium).is_ok()
    }

    pub fn iter(&self) -> StylesheetMediaIter {
        StylesheetMediaIter {
            stylesheet_media: self,
            current: 0,
        }
    }
}

impl IntoIterator for StylesheetMedia {
    type Item = String;
    type IntoIter = StylesheetMediaIntoIter;

    fn into_iter(self) -> Self::IntoIter {
        StylesheetMediaIntoIter {
            stylesheet_media: self,
            current: 0,
        }
    }
}

pub struct StylesheetMediaIter<'a> {
    stylesheet_media: &'a StylesheetMedia,
    current: usize,
}

impl<'a> Iterator for StylesheetMediaIter<'a> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.current;

        self.current += 1;

        self.stylesheet_media.get(current)
    }
}

pub struct StylesheetMediaIntoIter {
    stylesheet_media: StylesheetMedia,
    current: usize,
}

impl Iterator for StylesheetMediaIntoIter {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.current;

        self.current += 1;

        self.stylesheet_media.get(current)
    }
}