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
use super::Blob;

use std::borrow::ToOwned;
use std::default::Default;
use std::io;
use std::fmt;

use mimetype::MimeType;

use parser::base::{DecodeResult, DecodeError, XmlElement};
use schema::{FromSchemaReader, Mergeable};


/// Text construct defined in :rfc:`4287#section-3.1` (section 3.1).
///
/// RFC: <https://tools.ietf.org/html/rfc4287#section-3.1>
///
/// Note: It currently does not support `xhtml`.
#[derive(PartialEq, Eq, Debug)]
pub enum Text {
    /// The plain text content.  It corresponds to :rfc:`4287#section-3.1.1.1` (section 3.1.1.1).
    ///
    /// [rfc-text-1.1]: https://tools.ietf.org/html/rfc4287#section-3.1.1.1
    Plain(String),

    /// The HTML content.  It corresponds to :rfc:`4287#section-3.1.1.2` (section 3.1.1.2).
    ///
    /// [rfc-text-1.2]: https://tools.ietf.org/html/rfc4287#section-3.1.1.2
    Html(String),
}

impl Text {
    pub fn new<T>(type_: &str, value: T) -> Text
        where T: Into<String>
    {
        match type_ {
            "text" => Text::plain(value),
            "html" => Text::html(value),
            _ => Text::plain(value),
        }
    }

    pub fn plain<T>(value: T) -> Text
        where T: Into<String>
    {
        Text::Plain(value.into())
    }

    pub fn html<T>(value: T) -> Text
        where T: Into<String>
    {
        Text::Html(value.into())
    }

    /// The type of the text.  It corresponds to :rfc:`4287#section-3.1.1` (section 3.1.1).
    ///
    /// [rfc-text-1]: https://tools.ietf.org/html/rfc4287#section-3.1.1
    pub fn type_(&self) -> &'static str {
        match *self {
            Text::Plain(_) => "text",
            Text::Html(_) => "html",
        }
    }
}

impl Default for Text {
    fn default() -> Text {
        Text::Plain("".to_owned())
    }
}

impl fmt::Display for Text {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Text::Plain(ref value) => write!(f, "{}", value),
            // TODO: use sanitizer::clean_html()
            Text::Html(ref value) => write!(f, "{}", value),
        }
    }
}

#[cfg(html_sanitizer)]
impl<'a> fmt::Display for ForHtml<'a, Text> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.sanitized_html(None))
    }
}

impl Blob for Text {
    fn mimetype(&self) -> MimeType {
        match *self {
            Text::Plain(_) => MimeType::Text,
            Text::Html(_) => MimeType::Html,
        }
    }

    fn is_text(&self) -> bool { true }

    fn as_bytes(&self) -> &[u8] { self.as_str().unwrap().as_bytes() }

    fn as_str(&self) -> Option<&str> {
        let value = match *self {
            Text::Plain(ref value) => value,
            Text::Html(ref value) => value,
        };
        Some(&value)
    }
}

#[cfg(html_sanitizer)]
impl super::HtmlBlob for Text {
    fn sanitized_html<'a>(&'a self, base_uri: Option<&'a str>) ->
        Box<fmt::Display + 'a>
    {
        match *self {
            Text::Plain(ref value) => {
                let s = sanitizer::Escape(&value, sanitizer::QUOTE_BR);
                Box::new(s) as Box<fmt::Display>
            }
            Text::Html(ref value) =>
                Box::new(sanitizer::sanitize_html(&value, base_uri)) as Box<fmt::Display>,
        }
    }
}

impl FromSchemaReader for Text {
    fn read_from<B: io::BufRead>(&mut self, element: XmlElement<B>)
                            -> DecodeResult<()>
    {
        let type_ = match element.get_attr("type") {
            Ok("text") => "text",
            Ok("html") => "html",
            Ok(_type) => {
                // TODO: should be warned
                "text"
            }
            Err(DecodeError::AttributeNotFound(_)) => "text",
            Err(e) => { return Err(e); }
        };
        *self = Text::new(type_, try!(element.read_whole_text()));
        Ok(())
    }
}

impl Mergeable for Text { }


#[cfg(test)]
mod test {
    use super::Text;

    #[test]
    fn test_text_str() {
        assert_eq!(Text::plain("Hello world").to_string(), "Hello world");
        assert_eq!(Text::plain("<p>Hello <em>world</em></p>").to_string(),
                   "<p>Hello <em>world</em></p>");
        /* TODO: should be enabled after sanitizer is always available
        assert_eq!(Text::html("Hello world").to_string(), "Hello world");
        assert_eq!(Text::html("<p>Hello <em>world</em></p>").to_string(),
                   "Hello world");
        assert_eq!(Text::html("<p>안녕 <em>세상</em>아</p>").to_string(),
                   "안녕 세상아");
        */
    }

    macro_rules! assert_sanitized {
        ($text:expr, $expected:expr) => (
            assert_eq!($text.sanitized_html(None).to_string(), $expected);
        );
        ($text:expr, $base_uri:expr, $expected:expr) => (
            assert_eq!($text.sanitized_html(Some($base_uri)).to_string(), $expected);
        )
    }
}

#[cfg(all(test, html_sanitizer))]
mod test_sanitization {
    use super::Text;

    use feed::Blob;

    #[test]
    fn test_get_sanitized_html() {
        let text = Text::plain("Hello world");
        assert_sanitized!(text, "Hello world");
        let text = Text::plain("Hello\nworld");
        assert_sanitized!(text, "Hello<br>\nworld");
        let text = Text::plain("<p>Hello <em>world</em></p>");
        assert_sanitized!(text, concat!("&lt;p&gt;Hello &lt;em&gt;",
                                        "world&lt;/em&gt;&lt;/p&gt;"));
        let text = Text::plain("<p>안녕 <em>세상</em>아</p>");
        assert_sanitized!(text, concat!("&lt;p&gt;안녕 &lt;em&gt;",
                                        "세상&lt;/em&gt;아&lt;/p&gt;"));
        let text = Text::html("Hello world");
        assert_sanitized!(text, "Hello world");
        let text = Text::html("<p>Hello <em>world</em></p>");
        assert_sanitized!(text, "<p>Hello <em>world</em></p>");
        let text = Text::html("<p>Hello</p><script>alert(1);</script>");
        assert_sanitized!(text, "<p>Hello</p>");
        let text = Text::html("<p>Hello</p><hr noshade>");
        assert_sanitized!(text, "<p>Hello</p><hr noshade>");
        let text = Text::html("<a href=\"/abspath\">abspath</a>");
        assert_sanitized!(text, "http://localhost/path/",
                          concat!("<a href=\"http://localhost/abspath\">",
                                  "abspath</a>"));
    }
}