kindling-mobi 0.14.5

Kindle MOBI/AZW3 builder for dictionaries, books, and comics. Drop-in kindlegen replacement.
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
// Section 6: CSS forbidden properties and parse rules (R6.13 through R6.17,
// plus R6.e1 and R6.e2).
//
// Cluster I ports epubcheck CSS rule subset that actually matters for KDP:
//   R6.13  CSS parse error                  (epubcheck CSS_005 / CSS_027)
//   R6.14  Forbidden position declaration   (epubcheck CSS_007 class)
//   R6.15  @import target unresolvable      (epubcheck CSS_015)
//   R6.16  url() target unresolvable        (epubcheck CSS_020)
//   R6.17  @font-face silently dropped      (epubcheck CSS_008 / CSS_017)
//   R6.e1  @namespace rule present          (epubcheck CSS_025)
//   R6.e2  Unsupported @media feature       (epubcheck CSS_019)
//
// Cluster B (parse_encoding.rs) already owns R6.6 through R6.12 and handles
// BOM / @charset checks, so this module intentionally skips those concerns.
//
// All text-scanning extraction lives on `ExtractedEpub::css_summary` so this
// module only consumes the cache and applies the rule-specific classification
// logic (manifest resolution, external-URL detection).

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use super::Check;
use crate::extracted::ExtractedEpub;
use crate::validate::ValidationReport;

pub struct CssForbiddenChecks;

impl Check for CssForbiddenChecks {
    fn ids(&self) -> &'static [&'static str] {
        &[
            "R6.13", "R6.14", "R6.15", "R6.16", "R6.17", "R6.e1", "R6.e2",
        ]
    }

    fn run(&self, epub: &ExtractedEpub, report: &mut ValidationReport) {
        let opf = &epub.opf;

        // Pre-compute the lowercase manifest href set so resource-resolution
        // checks (R6.15 / R6.16 / R6.17) are case-insensitive against the
        // authoring spelling.
        let manifest_hrefs: HashSet<String> = opf
            .manifest
            .values()
            .map(|(href, _)| normalize_manifest_href(href))
            .collect();

        let base_dir = opf.base_dir.clone();

        for (_id, (href, media_type)) in &opf.manifest {
            if !is_css_media_type(media_type) {
                continue;
            }
            let Some(summary) = epub.css_summary(href) else { continue };
            let file = Some(PathBuf::from(href));

            // R6.13 CSS parse error. lightningcss only returns an `Err` for
            // hard syntax errors; style-level mistakes survive with
            // `error_recovery` disabled, which is what we want for a
            // STEAL-grade check.
            if let Some(err) = &summary.parse_error {
                report.emit_at(
                    "R6.13",
                    format!("lightningcss error: {}", err),
                    file.clone(),
                    None,
                );
            }

            // R6.14 Forbidden position.
            for (line, value) in &summary.forbidden_positions {
                report.emit_at(
                    "R6.14",
                    format!("Found 'position: {}'.", value),
                    file.clone(),
                    Some(*line),
                );
            }

            // R6.15 @import unresolvable.
            for (line, target) in &summary.imports {
                if let Some(reason) =
                    classify_import(target, href, &manifest_hrefs, base_dir.as_path())
                {
                    report.emit_at(
                        "R6.15",
                        format!("@import \"{}\" {}.", target, reason),
                        file.clone(),
                        Some(*line),
                    );
                }
            }

            // R6.16 url() unresolvable.
            for (line, target) in &summary.url_refs {
                if let Some(reason) =
                    classify_url(target, href, &manifest_hrefs, base_dir.as_path())
                {
                    report.emit_at(
                        "R6.16",
                        format!("url(\"{}\") {}.", target, reason),
                        file.clone(),
                        Some(*line),
                    );
                }
            }

            // R6.17 @font-face that Kindle will drop.
            for face in &summary.font_faces {
                if face.missing_src {
                    report.emit_at(
                        "R6.17",
                        "@font-face block has no src descriptor; Kindle will drop it."
                            .to_string(),
                        file.clone(),
                        Some(face.line),
                    );
                    continue;
                }
                for (line, target) in &face.src_urls {
                    if target.starts_with('#') {
                        continue;
                    }
                    if is_external_url(target)
                        || !resolves_to_manifest(
                            target,
                            href,
                            &manifest_hrefs,
                            base_dir.as_path(),
                        )
                    {
                        report.emit_at(
                            "R6.17",
                            format!(
                                "@font-face src url(\"{}\") not in manifest; \
                                 Kindle will drop the font.",
                                target
                            ),
                            file.clone(),
                            Some(*line),
                        );
                    }
                }
            }

            // R6.e1 @namespace.
            for line in &summary.namespace_lines {
                report.emit_at("R6.e1", "", file.clone(), Some(*line));
            }

            // R6.e2 Unsupported @media feature.
            for (line, feat) in &summary.media_features {
                report.emit_at(
                    "R6.e2",
                    format!("Feature '{}' not supported by Kindle readers.", feat),
                    file.clone(),
                    Some(*line),
                );
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers: media type / manifest normalization
// ---------------------------------------------------------------------------

fn is_css_media_type(mt: &str) -> bool {
    mt.eq_ignore_ascii_case("text/css")
}

/// Normalize a manifest href to its lowercase path for case-insensitive
/// resource-resolution checks. Fragment and query are dropped.
fn normalize_manifest_href(href: &str) -> String {
    let base = href.split(['?', '#']).next().unwrap_or(href);
    base.to_ascii_lowercase()
}

// ---------------------------------------------------------------------------
// R6.15 classifier: `@import` targets
// ---------------------------------------------------------------------------

/// None if the import target is resolvable in the manifest. Otherwise return a
/// short human-readable reason.
fn classify_import(
    target: &str,
    css_href: &str,
    manifest_hrefs: &HashSet<String>,
    base_dir: &Path,
) -> Option<&'static str> {
    if is_external_url(target) {
        return Some("is an external URL");
    }
    if !resolves_to_manifest(target, css_href, manifest_hrefs, base_dir) {
        return Some("is not in the manifest");
    }
    None
}

// ---------------------------------------------------------------------------
// R6.16 classifier: `url()` references
// ---------------------------------------------------------------------------

/// None if the url target is resolvable. Otherwise a short reason. Fragment-
/// only targets like `url(#gradient)` are ignored (SVG internal refs).
fn classify_url(
    target: &str,
    css_href: &str,
    manifest_hrefs: &HashSet<String>,
    base_dir: &Path,
) -> Option<&'static str> {
    if target.starts_with('#') {
        return None;
    }
    if is_external_url(target) {
        return Some("is an external URL");
    }
    if !resolves_to_manifest(target, css_href, manifest_hrefs, base_dir) {
        return Some("is not in the manifest");
    }
    None
}

// ---------------------------------------------------------------------------
// Shared small helpers
// ---------------------------------------------------------------------------

/// True if `target` is an absolute http(s):// URL or a protocol-relative URL.
fn is_external_url(target: &str) -> bool {
    let lower = target.to_ascii_lowercase();
    lower.starts_with("http://")
        || lower.starts_with("https://")
        || lower.starts_with("//")
        || lower.starts_with("ftp://")
}

/// Resolve `target` against the css file's directory and look it up in the
/// manifest href set. Case-insensitive. Fragment and query are dropped.
fn resolves_to_manifest(
    target: &str,
    css_href: &str,
    manifest_hrefs: &HashSet<String>,
    base_dir: &Path,
) -> bool {
    let bare = target.split(['?', '#']).next().unwrap_or(target);
    if bare.is_empty() {
        return true;
    }
    // Resolve `target` relative to the css file's directory, then express the
    // result as a path relative to `base_dir` (= the manifest root).
    let css_path = Path::new(css_href);
    let css_dir = css_path.parent().unwrap_or_else(|| Path::new(""));
    let joined = css_dir.join(bare);
    let normalized = normalize_relative_path(&joined);
    let needle = normalized.to_ascii_lowercase();
    if manifest_hrefs.contains(&needle) {
        return true;
    }
    // Fall back: some authoring tools reference the href verbatim from the
    // manifest root, ignoring the stylesheet's directory.
    let raw = bare.to_ascii_lowercase();
    if manifest_hrefs.contains(&raw) {
        return true;
    }
    // Final fallback: check disk. A file that physically exists relative to
    // the content root (e.g. a resource added by a repacker after OPF write)
    // is treated as present to avoid false positives on round-tripped books.
    let disk_path = base_dir.join(&normalized);
    disk_path.exists()
}

/// Collapse `./` and `..` segments in a relative path into a clean string with
/// forward slashes. Leading `..` are preserved so the result still matches
/// literal `../` manifest hrefs rather than escaping the manifest root.
fn normalize_relative_path(path: &Path) -> String {
    let mut out: Vec<String> = Vec::new();
    for comp in path.components() {
        use std::path::Component;
        match comp {
            Component::CurDir => {}
            Component::ParentDir => {
                if out.last().is_some_and(|s| s != "..") {
                    out.pop();
                } else {
                    out.push("..".to_string());
                }
            }
            Component::Normal(os) => out.push(os.to_string_lossy().to_string()),
            Component::RootDir | Component::Prefix(_) => {}
        }
    }
    out.join("/")
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use lightningcss::stylesheet::{ParserOptions, StyleSheet};

    // ---- R6.13 parse error ----
    //
    // R6.13 detection lives in `CssSummary::parse_error`, which is built by
    // `ExtractedEpub::css_summary`. These tests pin lightningcss's behaviour
    // for the inputs we rely on so a future bump cannot silently turn a
    // hard error into a recoverable one.

    #[test]
    fn r6_13_valid_css_parses_clean() {
        let css = "body { color: red; } h1 { font-size: 2em; }";
        assert!(StyleSheet::parse(css, ParserOptions::default()).is_ok());
    }

    #[test]
    fn r6_13_garbage_returns_err() {
        // Truly malformed input with no recoverable structure.
        let css = "this is not valid css at all $#@!";
        assert!(StyleSheet::parse(css, ParserOptions::default()).is_err());
    }

    #[test]
    fn r6_13_stray_brace_returns_err() {
        let css = "{";
        assert!(StyleSheet::parse(css, ParserOptions::default()).is_err());
    }

    #[test]
    fn r6_13_declaration_without_block_returns_err() {
        // Lightningcss treats a bare `@media` with no prelude/block as a
        // parse error.
        let css = "@media";
        assert!(StyleSheet::parse(css, ParserOptions::default()).is_err());
    }

    // ---- R6.15 @import classifier ----

    #[test]
    fn r6_15_external_import_classified() {
        let base = std::env::temp_dir();
        let hrefs: HashSet<String> = HashSet::new();
        assert!(
            classify_import("https://cdn/reset.css", "a.css", &hrefs, &base).is_some()
        );
    }

    #[test]
    fn r6_15_manifested_import_clean() {
        let base = std::env::temp_dir();
        let mut hrefs = HashSet::new();
        hrefs.insert("reset.css".to_string());
        assert!(classify_import("reset.css", "a.css", &hrefs, &base).is_none());
    }

    // ---- R6.16 url() classifier ----

    #[test]
    fn r6_16_fragment_only_url_clean() {
        let base = std::env::temp_dir();
        let hrefs: HashSet<String> = HashSet::new();
        assert!(classify_url("#gradient", "a.css", &hrefs, &base).is_none());
    }

    #[test]
    fn r6_16_external_url_classified() {
        let base = std::env::temp_dir();
        let hrefs: HashSet<String> = HashSet::new();
        assert!(
            classify_url("https://cdn/bg.png", "a.css", &hrefs, &base).is_some()
        );
    }

    #[test]
    fn r6_16_relative_url_resolves_against_css_dir() {
        let base = std::env::temp_dir();
        let mut hrefs = HashSet::new();
        hrefs.insert("images/bg.png".to_string());
        assert!(
            classify_url("../images/bg.png", "css/style.css", &hrefs, &base).is_none()
        );
    }

    // ---- helpers ----

    #[test]
    fn normalize_relative_path_collapses_dotdot() {
        let p = Path::new("css/../images/bg.png");
        assert_eq!(normalize_relative_path(p), "images/bg.png");
    }

    #[test]
    fn normalize_relative_path_keeps_leading_dotdot() {
        let p = Path::new("../shared/bg.png");
        assert_eq!(normalize_relative_path(p), "../shared/bg.png");
    }

    #[test]
    fn is_external_url_detects_schemes() {
        assert!(is_external_url("http://a"));
        assert!(is_external_url("HTTPS://a"));
        assert!(is_external_url("//cdn/a.css"));
        assert!(!is_external_url("a.css"));
        assert!(!is_external_url("../a.css"));
    }
}