prensa 0.2.0

Optimize EPUBs for e-ink readers
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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! References between entries: resolving an `href` to a zip path, and
//! rewriting references to images that were renamed.
//!
//! CrossPoint picks an image decoder from the file extension, so an image
//! converted to another format must be renamed (`fig.webp` -> `fig.jpg`), and
//! every reference to it updated: `src`/`href` attributes in XHTML and SVG,
//! manifest items in the OPF, `url(...)` in CSS and `style` attributes.
//!
//! References to an image that was turned 90° lose their `width`/`height`
//! attributes and inline CSS sizes, which were set for the original
//! orientation.
//!
//! Edits replace or remove attributes (and `url(...)` arguments) in place;
//! every other byte of the document is kept, so the element tree and the text
//! never change.

use crate::passes::{Book, Outcome};
use anyhow::{Result, bail};
use quick_xml::events::{BytesStart, Event};
use quick_xml::{Reader, XmlVersion};
use regex::{Captures, Regex};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::sync::LazyLock;

/// `url(...)` in CSS, with the argument in group 1 (double quotes), 2 (single
/// quotes) or 3 (unquoted).
static CSS_URL: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?i)url\(\s*(?:"([^"]*)"|'([^']*)'|([^)'"\s]*))\s*\)"#).unwrap()
});

/// Zip path of an `href`, relative to the directory `base`. `None` for remote
/// resources. Fragments and queries are dropped and `%XX` escapes decoded.
pub(crate) fn resolve(base: &str, href: &str) -> Option<String> {
    let href = href.split(['#', '?']).next().unwrap_or_default();
    if href.contains("://") || href.starts_with("data:") {
        return None;
    }
    let href = percent_decode(href);
    let (start, href) = match href.strip_prefix('/') {
        Some(rest) => ("", rest),
        None => (base, href.as_str()),
    };
    let mut parts: Vec<&str> = start.split('/').filter(|p| !p.is_empty()).collect();
    for part in href.split('/') {
        match part {
            "" | "." => {}
            ".." => {
                parts.pop();
            }
            part => parts.push(part),
        }
    }
    Some(parts.join("/"))
}

/// Directory of a zip path: `OEBPS/text/ch1.xhtml` -> `OEBPS/text`.
pub(crate) fn dir_of(path: &str) -> &str {
    path.rsplit_once('/').map_or("", |(dir, _)| dir)
}

/// Decode `%XX` escapes. Invalid escapes or non-UTF-8 results are kept as is.
pub(crate) fn percent_decode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let hex = bytes
            .get(i + 1..i + 3)
            .and_then(|h| std::str::from_utf8(h).ok())
            .and_then(|h| u8::from_str_radix(h, 16).ok());
        match (bytes[i], hex) {
            (b'%', Some(byte)) => {
                out.push(byte);
                i += 3;
            }
            (b, _) => {
                out.push(b);
                i += 1;
            }
        }
    }
    String::from_utf8(out).unwrap_or_else(|_| s.to_owned())
}

/// Images renamed because their format changed, by old zip path.
#[derive(Debug, Default)]
pub(crate) struct Renames {
    by_old_path: HashMap<String, Renamed>,
}

#[derive(Debug)]
struct Renamed {
    /// New zip path, in the same directory.
    path: String,
    /// What replaces the old extension in the file name, e.g. `.jpg` or
    /// `-1.png`: the stem before it never changes.
    tail: String,
    media_type: &'static str,
}

impl Renames {
    pub(crate) fn is_empty(&self) -> bool {
        self.by_old_path.is_empty()
    }

    /// Rename the image at `old` to the extension `ext` (e.g. `jpg`), unless
    /// it already has it. Picks a name no entry in `taken` has, comparing
    /// without case (memory cards are case-insensitive), and adds it to
    /// `taken`. Returns the new path.
    pub(crate) fn add(
        &mut self,
        old: &str,
        ext: &str,
        media_type: &'static str,
        taken: &mut HashSet<String>,
    ) -> String {
        let (dir_and_stem, old_ext) = old.rsplit_once('.').unwrap_or((old, ""));
        if old_ext.eq_ignore_ascii_case(ext) {
            return old.to_owned();
        }
        let mut tail = format!(".{ext}");
        let mut n = 1;
        while taken.contains(&format!("{dir_and_stem}{tail}").to_lowercase()) {
            tail = format!("-{n}.{ext}");
            n += 1;
        }
        let path = format!("{dir_and_stem}{tail}");
        taken.insert(path.to_lowercase());
        self.by_old_path.insert(
            old.to_owned(),
            Renamed {
                path: path.clone(),
                tail,
                media_type,
            },
        );
        path
    }

    /// New zip path of the image at `old`, if it was renamed.
    pub(crate) fn new_path(&self, old: &str) -> Option<&str> {
        self.by_old_path.get(old).map(|r| r.path.as_str())
    }

    /// The new `href` for a reference from a document in `base` (a directory),
    /// or `None` if it does not point to a renamed image.
    ///
    /// Only the extension at the end of the path changes: directories,
    /// escapes, the fragment and the query are kept as written.
    fn new_href(&self, base: &str, href: &str) -> Option<String> {
        let renamed = self.by_old_path.get(&resolve(base, href)?)?;
        let path_end = href.find(['#', '?']).unwrap_or(href.len());
        let (path, rest) = href.split_at(path_end);
        // The extension is plain ASCII in every real book; if it is escaped
        // (`%2Ewebp`), leave the reference alone rather than guess.
        let dot = path.rfind('.').filter(|&i| !path[i..].contains('/'))?;
        Some(format!("{}{}{rest}", &path[..dot], renamed.tail))
    }

    /// New media type for a renamed manifest item.
    fn media_type(&self, base: &str, href: &str) -> Option<&'static str> {
        Some(self.by_old_path.get(&resolve(base, href)?)?.media_type)
    }

    /// Rewrite `url(...)` references in CSS from a document in `base`.
    /// `None` if nothing pointed to a renamed image.
    fn rewrite_css_urls(&self, base: &str, css: &str) -> Option<String> {
        let mut changed = false;
        let out = CSS_URL.replace_all(css, |caps: &Captures| {
            let arg = caps.get(1).or(caps.get(2)).or(caps.get(3));
            match arg.and_then(|arg| self.new_href(base, arg.as_str()).map(|new| (arg, new))) {
                Some((arg, new)) => {
                    changed = true;
                    let whole = caps.get(0).unwrap();
                    let (before, after) = (
                        &whole.as_str()[..arg.start() - whole.start()],
                        &whole.as_str()[arg.end() - whole.start()..],
                    );
                    format!("{before}{new}{after}")
                }
                None => caps[0].to_owned(),
            }
        });
        changed.then(|| out.into_owned())
    }
}

/// Rewrite references to renamed images in a CSS file at `entry`.
pub(crate) fn rewrite_css(css: &str, entry: &str, renames: &Renames) -> Result<Outcome> {
    Ok(match renames.rewrite_css_urls(dir_of(entry), css) {
        Some(css) => Outcome::Replace(css),
        None => Outcome::Keep,
    })
}

/// Rewrite references to changed images in an XHTML or SVG document at
/// `entry`: `src`, `href` and `xlink:href` attributes, and `url(...)` in
/// `style` attributes, follow renamed images; `data:` URIs that were extracted
/// point to their new file, wherever it is; an element showing a turned image loses its sizes.
pub(crate) fn rewrite_document(xhtml: &str, entry: &str, book: &Book) -> Result<Outcome> {
    let base = dir_of(entry);
    edit_attributes(xhtml, |_, attrs| {
        let shows_rotated = attrs.iter().any(|(name, value)| {
            matches!(name.as_str(), "src" | "href")
                && (resolve(base, value).is_some_and(|path| book.rotated.contains(&path))
                    || book.inline.get(value).is_some_and(|f| f.rotated))
        });
        let mut edits = Vec::new();
        for (i, (name, value)) in attrs.iter().enumerate() {
            let edit = match name.as_str() {
                "src" | "href" => match book.inline.get(value) {
                    Some(file) => Some(Edit::Set(relative_href(base, &file.path))),
                    None => book.renames.new_href(base, value).map(Edit::Set),
                },
                "width" | "height" if shows_rotated => Some(Edit::Remove),
                "style" => {
                    let css = book.renames.rewrite_css_urls(base, value);
                    let current = css.as_deref().unwrap_or(value);
                    match (shows_rotated.then(|| without_sizes(current)).flatten(), css) {
                        (Some(stripped), _) if stripped.trim().is_empty() => Some(Edit::Remove),
                        (Some(stripped), _) => Some(Edit::Set(stripped)),
                        (None, css) => css.map(Edit::Set),
                    }
                }
                _ => None,
            };
            edits.extend(edit.map(|edit| (i, edit)));
        }
        edits
    })
}

/// The `data:image/...` URIs in `src`, `href` and `xlink:href` attributes of
/// an XHTML or SVG document, in order.
pub(crate) fn data_uris(xhtml: &str) -> Result<Vec<String>> {
    let mut uris = Vec::new();
    edit_attributes(xhtml, |_, attrs| {
        uris.extend(
            attrs
                .iter()
                .filter(|(name, value)| {
                    matches!(name.as_str(), "src" | "href") && value.starts_with("data:image/")
                })
                .map(|(_, value)| value.clone()),
        );
        Vec::new()
    })?;
    Ok(uris)
}

/// `href` from a document in the directory `from_dir` to the zip path `to`,
/// with each path segment percent-encoded.
pub(crate) fn relative_href(from_dir: &str, to: &str) -> String {
    let from: Vec<&str> = from_dir.split('/').filter(|p| !p.is_empty()).collect();
    let to: Vec<&str> = to.split('/').filter(|p| !p.is_empty()).collect();
    let common = from
        .iter()
        .zip(&to)
        .take_while(|(a, b)| a == b)
        .count()
        .min(to.len().saturating_sub(1));
    let ups = std::iter::repeat_n("..".to_owned(), from.len() - common);
    let downs = to[common..].iter().map(|p| percent_encode(p));
    ups.chain(downs).collect::<Vec<_>>().join("/")
}

/// Percent-encode everything but unreserved URL characters.
fn percent_encode(segment: &str) -> String {
    let mut out = String::with_capacity(segment.len());
    for byte in segment.bytes() {
        if byte.is_ascii_alphanumeric() || b"-._~".contains(&byte) {
            out.push(byte as char);
        } else {
            out.push_str(&format!("%{byte:02X}"));
        }
    }
    out
}

/// `style` attribute text without its `width` and `height` declarations, or
/// `None` if it has none. `max-width` and the like stay.
fn without_sizes(style: &str) -> Option<String> {
    let is_size = |decl: &&str| {
        let property = decl.split(':').next().unwrap_or_default().trim();
        property.eq_ignore_ascii_case("width") || property.eq_ignore_ascii_case("height")
    };
    let decls: Vec<&str> = style.split(';').collect();
    if !decls.iter().any(|d| d.contains(':') && is_size(d)) {
        return None;
    }
    let kept: Vec<&str> = decls
        .into_iter()
        .filter(|d| !(d.contains(':') && is_size(d)))
        .collect();
    Some(kept.join(";"))
}

/// Point manifest items of renamed images in the OPF at `entry` to the new
/// files, with their new media type.
pub(crate) fn rewrite_opf(opf: &str, entry: &str, renames: &Renames) -> Result<Outcome> {
    let base = dir_of(entry);
    edit_attributes(opf, |element, attrs| {
        if element != "item" {
            return Vec::new();
        }
        let Some(href) = attrs.iter().find(|(n, _)| n == "href").map(|(_, v)| v) else {
            return Vec::new();
        };
        let (Some(new_href), Some(media_type)) =
            (renames.new_href(base, href), renames.media_type(base, href))
        else {
            return Vec::new();
        };
        attrs
            .iter()
            .enumerate()
            .filter_map(|(i, (name, _))| match name.as_str() {
                "href" => Some((i, Edit::Set(new_href.clone()))),
                "media-type" => Some((i, Edit::Set(media_type.to_owned()))),
                _ => None,
            })
            .collect()
    })
}

/// What to do with one attribute.
#[derive(Debug, PartialEq)]
enum Edit {
    /// Replace its value with this unescaped text.
    Set(String),
    /// Remove the attribute, with the whitespace before it.
    Remove,
}

/// Edit attributes in an XML document, keeping every other byte.
///
/// `edit` is called for each start or empty tag with the element's local name
/// and its attributes as (local name, unescaped value), and returns edits by
/// attribute index. [`Outcome::Keep`] if nothing changed.
///
/// End tags are not matched against start tags: only attributes are edited,
/// so HTML-style chapters (`<br>` never closed) are handled too.
fn edit_attributes(
    xml: &str,
    mut edit: impl FnMut(&str, &[(String, String)]) -> Vec<(usize, Edit)>,
) -> Result<Outcome> {
    let mut reader = Reader::from_str(xml);
    reader.config_mut().check_end_names = false;
    // Byte ranges to replace, in document order.
    let mut splices: Vec<(usize, usize, String)> = Vec::new();
    loop {
        match reader.read_event()? {
            Event::Eof => break,
            Event::Start(e) | Event::Empty(e) => {
                let (raws, attrs) = attributes(&e)?;
                for (i, edit) in edit(e.local_name().as_ref(), &attrs) {
                    let raw = &raws[i];
                    let (start, end) = span_in(xml, raw.value)?;
                    splices.push(match edit {
                        Edit::Set(new) => (start, end, quick_xml::escape::escape(new).into_owned()),
                        Edit::Remove => {
                            let (key_start, _) = span_in(xml, raw.key)?;
                            // Back over the whitespace before the name; on to
                            // the closing quote after the value.
                            let from = xml[..key_start].trim_end().len();
                            let to = end + xml[end..].chars().next().map_or(0, char::len_utf8);
                            (from, to, String::new())
                        }
                    });
                }
            }
            _ => {}
        }
    }
    if splices.is_empty() {
        return Ok(Outcome::Keep);
    }
    let mut out = String::with_capacity(xml.len());
    let mut copied = 0;
    for (start, end, value) in splices {
        out.push_str(&xml[copied..start]);
        out.push_str(&value);
        copied = end;
    }
    out.push_str(&xml[copied..]);
    Ok(Outcome::Replace(out))
}

/// An attribute as written in the document: its name and raw (escaped) value.
struct RawAttr<'t> {
    key: &'t str,
    value: &'t str,
}

/// The attributes of a tag: each one as written in the document, and
/// (local name, unescaped value) pairs, index for index.
///
/// An attribute whose value cannot be unescaped (an entity such as `&nbsp;`
/// with no DTD to define it) is left out: it is never edited, and the rest of
/// the document still is.
fn attributes<'t>(tag: &'t BytesStart) -> Result<(Vec<RawAttr<'t>>, Pairs)> {
    let mut raws = Vec::new();
    let mut attrs = Vec::new();
    for attr in tag.attributes() {
        let attr = attr?;
        let Ok(value) = attr.normalized_value(XmlVersion::Implicit1_0) else {
            continue;
        };
        let value = value.into_owned();
        let name = attr.key.local_name().as_ref().to_owned();
        // A reader over a `&str` borrows every raw name and value from it.
        let Cow::Borrowed(raw) = attr.value else {
            bail!("attribute value is not borrowed from the document");
        };
        raws.push(RawAttr {
            key: attr.key.0,
            value: raw,
        });
        attrs.push((name, value));
    }
    Ok((raws, attrs))
}

/// (local name, unescaped value) of each attribute of a tag.
type Pairs = Vec<(String, String)>;

/// Byte range of `part`, a slice borrowed from `whole`.
fn span_in(whole: &str, part: &str) -> Result<(usize, usize)> {
    let start = (part.as_ptr() as usize).wrapping_sub(whole.as_ptr() as usize);
    match whole.get(start..start.saturating_add(part.len())) {
        Some(slice) if std::ptr::eq(slice, part) => Ok((start, start + part.len())),
        _ => bail!("attribute value is not part of the document"),
    }
}

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

    fn renames(old: &[&str], ext: &str) -> Renames {
        let mut renames = Renames::default();
        let mut taken: HashSet<String> = old.iter().map(|p| p.to_lowercase()).collect();
        for path in old {
            renames.add(path, ext, "image/jpeg", &mut taken);
        }
        renames
    }

    fn book(renames: Renames, rotated: HashSet<String>) -> Book {
        Book {
            renames,
            rotated,
            ..Book::default()
        }
    }

    fn replaced(outcome: Outcome) -> String {
        match outcome {
            Outcome::Replace(text) => text,
            other => panic!("expected Replace, got {other:?}"),
        }
    }

    #[test]
    fn resolves_hrefs() {
        assert_eq!(resolve("OEBPS", "a.ttf").as_deref(), Some("OEBPS/a.ttf"));
        assert_eq!(
            resolve("OEBPS/text", "../fonts/a.ttf").as_deref(),
            Some("OEBPS/fonts/a.ttf")
        );
        assert_eq!(resolve("", "./a%20b.ttf#x").as_deref(), Some("a b.ttf"));
        assert_eq!(resolve("OEBPS", "/root.ttf").as_deref(), Some("root.ttf"));
        assert_eq!(resolve("OEBPS", "https://example.com/a.ttf"), None);
        assert_eq!(resolve("OEBPS", "data:image/png;base64,AAAA"), None);
        assert_eq!(percent_decode("100%"), "100%");
        assert_eq!(percent_decode("%zz"), "%zz");
    }

    #[test]
    fn relative_hrefs() {
        assert_eq!(relative_href("OEBPS/text", "OEBPS/text/a.png"), "a.png");
        assert_eq!(
            relative_href("OEBPS", "OEBPS/Text Files/a b.png"),
            "Text%20Files/a%20b.png"
        );
        assert_eq!(
            relative_href("OEBPS/text", "OEBPS/img/a.png"),
            "../img/a.png"
        );
        assert_eq!(relative_href("", "a.png"), "a.png");
        assert_eq!(relative_href("a/b", "c.png"), "../../c.png");
    }

    #[test]
    fn new_names_avoid_existing_entries() {
        let mut renames = Renames::default();
        let mut taken: HashSet<String> = ["a/fig.webp", "a/fig.bmp", "a/FIG.JPG", "a/x.jpg"]
            .iter()
            .map(|p| p.to_lowercase())
            .collect();
        assert_eq!(
            renames.add("a/fig.webp", "jpg", "image/jpeg", &mut taken),
            "a/fig-1.jpg"
        );
        assert_eq!(
            renames.add("a/fig.bmp", "jpg", "image/jpeg", &mut taken),
            "a/fig-2.jpg"
        );
        // Already the right extension: not a rename.
        assert_eq!(
            renames.add("a/x.jpg", "jpg", "image/jpeg", &mut taken),
            "a/x.jpg"
        );
        assert!(renames.new_href("a", "x.jpg").is_none());
    }

    #[test]
    fn new_href_keeps_directories_escapes_and_fragment() {
        let r = renames(&["OEBPS/images/my fig.webp"], "jpg");
        assert_eq!(
            r.new_href("OEBPS/text", "../images/my%20fig.webp#part")
                .as_deref(),
            Some("../images/my%20fig.jpg#part")
        );
        assert_eq!(r.new_href("OEBPS/text", "../images/other.webp"), None);
        assert_eq!(r.new_href("OEBPS/text", "my%20fig.webp"), None);
    }

    #[test]
    fn rewrites_only_the_attribute_values() {
        let r = renames(&["OEBPS/img/a.webp", "OEBPS/img/b.bmp"], "jpg");
        let xhtml = "<?xml version=\"1.0\"?>\n<!-- a.webp -->\n<html><body>\n  <p>See <img  src='../img/a.webp'\n alt=\"a.webp &amp; more\"/> and a.webp.</p>\n  <svg:image xlink:href=\"../img/b.bmp\"/><a href=\"ch2.xhtml\">next</a>\n  <div style=\"background: url(../img/a.webp)\"></div>\n</body></html>";
        let out = replaced(
            rewrite_document(xhtml, "OEBPS/text/ch1.xhtml", &book(r, HashSet::new())).unwrap(),
        );
        assert_eq!(
            out,
            "<?xml version=\"1.0\"?>\n<!-- a.webp -->\n<html><body>\n  <p>See <img  src='../img/a.jpg'\n alt=\"a.webp &amp; more\"/> and a.webp.</p>\n  <svg:image xlink:href=\"../img/b.jpg\"/><a href=\"ch2.xhtml\">next</a>\n  <div style=\"background: url(../img/a.jpg)\"></div>\n</body></html>"
        );
    }

    #[test]
    fn escaped_values_are_unescaped_then_escaped_again() {
        let r = renames(&["R&D.webp"], "jpg");
        let out = replaced(
            rewrite_document(
                r#"<img src="R&amp;D.webp"/>"#,
                "x.xhtml",
                &book(r, HashSet::new()),
            )
            .unwrap(),
        );
        assert_eq!(out, r#"<img src="R&amp;D.jpg"/>"#);
    }

    #[test]
    fn document_without_renamed_images_is_kept() {
        let r = renames(&["OEBPS/a.webp"], "jpg");
        let xhtml = r#"<html><body><img src="b.png"/></body></html>"#;
        assert_eq!(
            rewrite_document(xhtml, "OEBPS/ch.xhtml", &book(r, HashSet::new())).unwrap(),
            Outcome::Keep
        );
    }

    #[test]
    fn broken_document_is_an_error() {
        let r = renames(&["a.webp"], "jpg");
        assert!(
            rewrite_document(
                "<p><img src=\"a.webp\"</p>",
                "x.xhtml",
                &book(r, HashSet::new())
            )
            .is_err()
        );
    }

    #[test]
    fn html_style_void_tags_are_fine() {
        let r = renames(&["a.webp"], "jpg");
        let html = "<p>One<br>two <img src=\"a.webp\"></p>";
        let out = replaced(rewrite_document(html, "x.html", &book(r, HashSet::new())).unwrap());
        assert_eq!(out, "<p>One<br>two <img src=\"a.jpg\"></p>");
    }

    #[test]
    fn unknown_entity_skips_only_that_attribute() {
        let r = renames(&["a.webp"], "jpg");
        let xhtml = r#"<img alt="a&nbsp;b" src="a.webp"/>"#;
        let out = replaced(rewrite_document(xhtml, "x.xhtml", &book(r, HashSet::new())).unwrap());
        assert_eq!(out, r#"<img alt="a&nbsp;b" src="a.jpg"/>"#);
    }

    fn rotated(paths: &[&str]) -> HashSet<String> {
        paths.iter().map(|p| p.to_string()).collect()
    }

    #[test]
    fn turned_image_loses_its_sizes() {
        let xhtml = "<p><img class=\"f\" width=\"2391\" src=\"../img/wide.png\"\n   height='1000' style=\"width: 90%; max-width:100%;height:20em\" alt=\"\"/><img src=\"../img/tall.png\" width=\"300\"/></p>";
        let out = replaced(
            rewrite_document(
                xhtml,
                "OEBPS/text/ch.xhtml",
                &book(Renames::default(), rotated(&["OEBPS/img/wide.png"])),
            )
            .unwrap(),
        );
        assert_eq!(
            out,
            "<p><img class=\"f\" src=\"../img/wide.png\" style=\" max-width:100%\" alt=\"\"/><img src=\"../img/tall.png\" width=\"300\"/></p>"
        );
    }

    #[test]
    fn style_with_only_sizes_is_removed() {
        let out = replaced(
            rewrite_document(
                r#"<img src="w.webp" style="width:600px;height:300px"/>"#,
                "x.xhtml",
                &book(renames(&["w.webp"], "jpg"), rotated(&["w.webp"])),
            )
            .unwrap(),
        );
        assert_eq!(out, r#"<img src="w.jpg"/>"#);
    }

    #[test]
    fn sizes_are_kept_when_nothing_turned() {
        let xhtml = r#"<img src="a.png" width="900" style="width:50%"/>"#;
        assert_eq!(
            rewrite_document(
                xhtml,
                "x.xhtml",
                &book(Renames::default(), rotated(&["b.png"]))
            )
            .unwrap(),
            Outcome::Keep
        );
    }

    #[test]
    fn rewrites_manifest_href_and_media_type() {
        let r = renames(&["OEBPS/images/a.webp"], "jpg");
        let opf = r#"<package><manifest>
<item id="a" href="images/a.webp" media-type="image/webp" properties="cover-image"/>
<item id="b" href="images/b.png" media-type="image/png"/>
</manifest></package>"#;
        let out = replaced(rewrite_opf(opf, "OEBPS/content.opf", &r).unwrap());
        assert_eq!(
            out,
            r#"<package><manifest>
<item id="a" href="images/a.jpg" media-type="image/jpeg" properties="cover-image"/>
<item id="b" href="images/b.png" media-type="image/png"/>
</manifest></package>"#
        );
    }

    #[test]
    fn rewrites_css_urls_in_every_quoting() {
        let r = renames(&["OEBPS/img/a.webp"], "jpg");
        let css = "a { background: url(\"../img/a.webp\") } b { background: url( '../img/a.webp' ) } c { background: url(../img/a.webp) } d { background: url(../img/z.webp) }";
        let out = replaced(rewrite_css(css, "OEBPS/css/s.css", &r).unwrap());
        assert_eq!(
            out,
            "a { background: url(\"../img/a.jpg\") } b { background: url( '../img/a.jpg' ) } c { background: url(../img/a.jpg) } d { background: url(../img/z.webp) }"
        );
        assert_eq!(rewrite_css("p {}", "s.css", &r).unwrap(), Outcome::Keep);
    }
}