ferri-pdf 0.1.0

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
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
use crate::dom::state::is_html_whitespace;
use crate::dom::{
    Element, HTML_NAMESPACE, InitialState, SVG_NAMESPACE, TreeRelation, non_negative_integer_digits,
};

const SINGLE_SELECT_SIZE: u8 = 1;
const GREGORIAN_CYCLE: u32 = 400;
const MONTHS_PER_YEAR: u8 = 12;
const HOURS_PER_DAY: u8 = 24;
const MINUTES_PER_HOUR: u8 = 60;
const SECONDS_PER_MINUTE: u8 = 60;
const MIN_YEAR_DIGITS: usize = 4;
const DECIMAL_RADIX: u8 = 10;
const DIGIT_PAIR_LENGTH: usize = 2;
const LEAP_PERIOD: u32 = 4;
const CENTURY: u32 = 100;
const DAYS_PER_WEEK: u32 = 7;
const WEDNESDAY: u32 = 3;
const THURSDAY: u32 = 4;
const REGULAR_WEEKS: u8 = 52;
const LONG_WEEKS: u8 = 53;

#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ControlAncestry {
    fieldset_inherited: bool,
    fieldset_descendants: bool,
    first_legend: Option<ego_tree::NodeId>,
    select_disabled: bool,
    option_disabled: bool,
    option_children_disabled: bool,
}

pub(crate) fn initial_disabled(
    element: Element<'_>,
    parent: Option<&InitialState>,
) -> (Option<bool>, ControlAncestry) {
    let parent = parent.map_or_else(ControlAncestry::default, |state| state.control_ancestry);
    let inherited = if parent.first_legend == Some(element.raw.id()) {
        parent.fieldset_inherited
    } else {
        parent.fieldset_descendants
    };
    let html = element.namespace() == HTML_NAMESPACE;
    let own = html && element.attr("disabled").is_some();
    let option_disabled =
        html_element(element, "option") && (own || parent.option_children_disabled);
    let disabled = if html {
        match element.name() {
            "button" | "input" | "select" | "textarea" | "fieldset" => Some(own || inherited),
            "optgroup" => Some(own || parent.select_disabled),
            "option" => Some(option_disabled || parent.select_disabled),
            _ => None,
        }
    } else {
        None
    };
    let fieldset = html && element.name() == "fieldset";
    let first_legend = fieldset
        .then(|| {
            element
                .children(TreeRelation::Selector)
                .find(|child| html_element(*child, "legend"))
                .map(|child| child.raw.id())
        })
        .flatten();
    let ancestry = ControlAncestry {
        fieldset_inherited: inherited,
        fieldset_descendants: inherited || (fieldset && own),
        first_legend,
        select_disabled: if html && element.name() == "select" {
            disabled == Some(true)
        } else {
            parent.select_disabled
        },
        option_disabled,
        option_children_disabled: if html {
            match element.name() {
                "optgroup" => own,
                "select" | "hr" | "datalist" | "option" => false,
                _ => parent.option_children_disabled,
            }
        } else {
            parent.option_children_disabled
        },
    };
    (disabled, ancestry)
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum InputType {
    Hidden,
    Text,
    Search,
    Tel,
    Url,
    Email,
    Password,
    Date,
    Month,
    Week,
    Time,
    DateTimeLocal,
    Number,
    Range,
    Color,
    Checkbox,
    Radio,
    File,
    Submit,
    Image,
    Reset,
    Button,
}
impl InputType {
    pub(crate) fn of(element: Element<'_>) -> Self {
        match element
            .attr("type")
            .unwrap_or_default()
            .to_ascii_lowercase()
            .as_str()
        {
            "hidden" => Self::Hidden,
            "search" => Self::Search,
            "tel" => Self::Tel,
            "url" => Self::Url,
            "email" => Self::Email,
            "password" => Self::Password,
            "date" => Self::Date,
            "month" => Self::Month,
            "week" => Self::Week,
            "time" => Self::Time,
            "datetime-local" => Self::DateTimeLocal,
            "number" => Self::Number,
            "range" => Self::Range,
            "color" => Self::Color,
            "checkbox" => Self::Checkbox,
            "radio" => Self::Radio,
            "file" => Self::File,
            "submit" => Self::Submit,
            "image" => Self::Image,
            "reset" => Self::Reset,
            "button" => Self::Button,
            _ => Self::Text,
        }
    }
    pub(crate) fn auto_direction_value(self, element: Element<'_>) -> Option<String> {
        let value = element.attr("value").unwrap_or_default();
        match self {
            Self::Text | Self::Search | Self::Tel | Self::Password | Self::Url | Self::Email => {
                let value: String = value
                    .chars()
                    .filter(|ch| !matches!(ch, '\r' | '\n'))
                    .collect();
                Some(if matches!(self, Self::Url | Self::Email) {
                    value.trim_matches(is_html_whitespace).to_owned()
                } else {
                    value
                })
            }
            Self::Hidden | Self::Submit | Self::Reset | Self::Button => Some(value.to_owned()),
            _ => None,
        }
    }
}

fn html_element(element: Element<'_>, name: &str) -> bool {
    element.namespace() == HTML_NAMESPACE && element.name() == name
}

impl Element<'_> {
    pub(crate) fn input_type(self) -> Option<InputType> {
        self.initial_state().input_type
    }
    fn tree_elements(self) -> impl Iterator<Item = Self> {
        let mut root = self.as_node();
        while let Some(parent) = root.parent(TreeRelation::Selector) {
            root = parent;
        }
        root.inclusive_descendants(TreeRelation::Selector)
            .filter_map(super::tree::Node::element)
    }
    fn form_owner(self) -> Option<Self> {
        if let Some(id) = self.attr("form") {
            if id.is_empty() {
                return None;
            }
            return self
                .tree_elements()
                .find(|element| element.attr("id") == Some(id))
                .filter(|element| html_element(*element, "form"));
        }
        self.ancestors(TreeRelation::Selector)
            .find(|element| html_element(*element, "form"))
    }

    pub(crate) fn initially_checked(self) -> bool {
        if !html_element(self, "input") || self.attr("checked").is_none() {
            return false;
        }
        match self.input_type() {
            Some(InputType::Checkbox) => true,
            Some(InputType::Radio) => {
                let Some(name) = self.attr("name").filter(|name| !name.is_empty()) else {
                    return true;
                };
                let owner = self.form_owner();
                self.tree_elements()
                    .filter(|element| {
                        html_element(*element, "input")
                            && element.input_type() == Some(InputType::Radio)
                            && element.attr("name") == Some(name)
                            && element.form_owner() == owner
                            && element.attr("checked").is_some()
                    })
                    .last()
                    .is_some_and(|element| element == self)
            }
            _ => false,
        }
    }

    pub(crate) fn initial_selected_options(self) -> Vec<Self> {
        if !html_element(self, "select") {
            return Vec::new();
        }
        let options = self.select_options();
        let mut selected: Vec<_> = options
            .iter()
            .copied()
            .filter(|option| option.attr("selected").is_some())
            .collect();
        if self.attr("multiple").is_some() {
            return selected;
        }
        if let Some(last) = selected.pop() {
            return vec![last];
        }
        let size = self
            .attr("size")
            .and_then(non_negative_integer_digits)
            .map(|digits| {
                digits.bytes().fold(0u8, |value, digit| {
                    value
                        .saturating_mul(DECIMAL_RADIX)
                        .saturating_add(digit - b'0')
                })
            })
            .filter(|size| *size != 0)
            .unwrap_or(SINGLE_SELECT_SIZE);
        if size != SINGLE_SELECT_SIZE {
            return Vec::new();
        }
        options
            .into_iter()
            .find(|option| !option.initial_state().control_ancestry.option_disabled)
            .into_iter()
            .collect()
    }

    fn select_options(self) -> Vec<Self> {
        let mut pending: Vec<_> = self
            .children(TreeRelation::Selector)
            .rev()
            .map(|element| (element, false))
            .collect();
        let mut options = Vec::new();
        while let Some((element, in_group)) = pending.pop() {
            let html = element.namespace() == HTML_NAMESPACE;
            let group = html && element.name() == "optgroup";
            if html && element.name() == "option" {
                options.push(element);
            }
            if (html && matches!(element.name(), "select" | "hr" | "option" | "datalist"))
                || (group && in_group)
            {
                continue;
            }
            pending.extend(
                element
                    .children(TreeRelation::Selector)
                    .rev()
                    .map(|child| (child, in_group || group)),
            );
        }
        options
    }

    pub(crate) fn option_value(self) -> Option<String> {
        if !html_element(self, "option") {
            return None;
        }
        if let Some(value) = self.attr("value") {
            return Some(value.to_owned());
        }
        let text: String = self
            .as_node()
            .inclusive_descendants(TreeRelation::Selector)
            .filter(|node| {
                !node
                    .parent(TreeRelation::Selector)
                    .into_iter()
                    .filter_map(super::tree::Node::element)
                    .flat_map(|parent| {
                        std::iter::once(parent).chain(parent.ancestors(TreeRelation::Selector))
                    })
                    .take_while(|ancestor| *ancestor != self)
                    .any(|ancestor| {
                        (matches!(ancestor.namespace(), HTML_NAMESPACE | SVG_NAMESPACE)
                            && ancestor.name() == "script")
                            || html_element(ancestor, "img")
                    })
            })
            .filter_map(|node| node.value().as_text().map(|text| &**text))
            .collect();
        Some(
            text.split(is_html_whitespace)
                .filter(|part| !part.is_empty())
                .collect::<Vec<_>>()
                .join(" "),
        )
    }

    pub(crate) fn is_blank_control(self) -> bool {
        if self.namespace() != HTML_NAMESPACE {
            return false;
        }
        match self.name() {
            "textarea" => self.text().all(str::is_empty),
            "select" => self
                .initial_selected_options()
                .into_iter()
                .all(|option| option.option_value().is_none_or(|value| value.is_empty())),
            "input" => {
                let kind = self.input_type().expect("HTML input has an initial type");
                let value = self.attr("value").unwrap_or_default();
                match kind {
                    InputType::Hidden
                    | InputType::Range
                    | InputType::Color
                    | InputType::Submit
                    | InputType::Image
                    | InputType::Reset
                    | InputType::Button => false,
                    InputType::Checkbox | InputType::Radio => !self.initially_checked(),
                    InputType::File => true,
                    InputType::Number => !valid_number(value),
                    InputType::Date => !valid_date(value),
                    InputType::Month => !valid_month(value),
                    InputType::Week => !valid_week(value),
                    InputType::Time => !valid_time(value),
                    InputType::DateTimeLocal => !value
                        .split_once(['T', ' '])
                        .is_some_and(|(date, time)| valid_date(date) && valid_time(time)),
                    _ => kind
                        .auto_direction_value(self)
                        .is_some_and(|value| value.is_empty()),
                }
            }
            _ => false,
        }
    }
}

fn two_digits(value: &str) -> Option<u8> {
    (value.len() == DIGIT_PAIR_LENGTH && value.bytes().all(|ch| ch.is_ascii_digit()))
        .then(|| (value.as_bytes()[0] - b'0') * DECIMAL_RADIX + value.as_bytes()[1] - b'0')
}
fn year(value: &str) -> Option<u32> {
    (value.len() >= MIN_YEAR_DIGITS
        && value.bytes().all(|ch| ch.is_ascii_digit())
        && value.bytes().any(|ch| ch != b'0'))
    .then(|| {
        value.bytes().fold(0, |year, digit| {
            (year * u32::from(DECIMAL_RADIX) + u32::from(digit - b'0')) % GREGORIAN_CYCLE
        })
    })
}
fn month(value: &str) -> Option<(u32, u8)> {
    let (y, m) = value.split_once('-')?;
    let y = year(y)?;
    let m = two_digits(m)?;
    (m != 0 && m <= MONTHS_PER_YEAR).then_some((y, m))
}
fn leap(year: u32) -> bool {
    year.is_multiple_of(LEAP_PERIOD)
        && (!year.is_multiple_of(CENTURY) || year.is_multiple_of(GREGORIAN_CYCLE))
}
fn valid_month(value: &str) -> bool {
    month(value).is_some()
}
fn valid_date(value: &str) -> bool {
    let Some((ym, day)) = value.rsplit_once('-') else {
        return false;
    };
    let (Some((year, month)), Some(day)) = (month(ym), two_digits(day)) else {
        return false;
    };
    let days = match month {
        2 if leap(year) => 29,
        2 => 28,
        4 | 6 | 9 | 11 => 30,
        _ => 31,
    };
    day != 0 && day <= days
}
fn valid_week(value: &str) -> bool {
    let Some((y, week)) = value.split_once("-W") else {
        return false;
    };
    let (Some(y), Some(week)) = (year(y), two_digits(week)) else {
        return false;
    };
    let prior = (y + GREGORIAN_CYCLE - 1) % GREGORIAN_CYCLE;
    let jan_first = (prior + prior / LEAP_PERIOD - prior / CENTURY + 1) % DAYS_PER_WEEK;
    let last = if jan_first == THURSDAY || (jan_first == WEDNESDAY && leap(y)) {
        LONG_WEEKS
    } else {
        REGULAR_WEEKS
    };
    week != 0 && week <= last
}
fn valid_time(value: &str) -> bool {
    let mut parts = value.split(':');
    let (Some(hour), Some(minute)) = (
        parts.next().and_then(two_digits),
        parts.next().and_then(two_digits),
    ) else {
        return false;
    };
    if hour >= HOURS_PER_DAY || minute >= MINUTES_PER_HOUR {
        return false;
    }
    if let Some(seconds) = parts.next() {
        let (seconds, fraction) = seconds
            .split_once('.')
            .map_or((seconds, None), |(s, f)| (s, Some(f)));
        if two_digits(seconds).is_none_or(|s| s >= SECONDS_PER_MINUTE)
            || fraction.is_some_and(|f| f.is_empty() || !f.bytes().all(|ch| ch.is_ascii_digit()))
        {
            return false;
        }
    }
    parts.next().is_none()
}
fn valid_number(value: &str) -> bool {
    let value = value.strip_prefix('-').unwrap_or(value);
    let (significand, exponent) = value
        .split_once(['e', 'E'])
        .map_or((value, None), |(s, e)| (s, Some(e)));
    if exponent.is_some_and(|e| {
        let e = e.strip_prefix(['+', '-']).unwrap_or(e);
        e.is_empty() || !e.bytes().all(|ch| ch.is_ascii_digit())
    }) {
        return false;
    }
    let valid = if let Some((integer, fraction)) = significand.split_once('.') {
        integer.bytes().all(|ch| ch.is_ascii_digit())
            && !fraction.is_empty()
            && fraction.bytes().all(|ch| ch.is_ascii_digit())
    } else {
        !significand.is_empty() && significand.bytes().all(|ch| ch.is_ascii_digit())
    };
    valid && value.parse::<f64>().is_ok_and(f64::is_finite)
}