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
//! Transform ArenaDom to Chapter.
use super::arena::{ArenaDom, ArenaNodeData, ArenaNodeId};
use super::element_ref::ElementRef;
use super::role_map::element_to_role;
use crate::model::{Chapter, Node, NodeId, Role};
use crate::style::{ComputedStyle, Display, Origin, Stylesheet, WhiteSpace, compute_styles};
/// User agent stylesheet (browser defaults).
const UA_CSS: &str = include_str!("data/styles.css");
pub fn user_agent_stylesheet() -> Stylesheet {
Stylesheet::parse(UA_CSS)
}
/// Context for the transform operation.
struct TransformContext<'a> {
dom: &'a ArenaDom,
stylesheets: &'a [(Stylesheet, Origin)],
chapter: Chapter,
/// Map from ArenaNodeId to Chapter NodeId
node_map: std::collections::HashMap<ArenaNodeId, NodeId>,
}
impl<'a> TransformContext<'a> {
fn new(dom: &'a ArenaDom, stylesheets: &'a [(Stylesheet, Origin)]) -> Self {
Self {
dom,
stylesheets,
chapter: Chapter::new(),
node_map: std::collections::HashMap::new(),
}
}
/// Transform the DOM to IR.
fn transform(mut self) -> Chapter {
// Find the body element, or use document root
let body = self.dom.find_by_tag("body").unwrap_or(self.dom.document());
// Get language from html element (if present) to propagate to all content
let html_lang = self.dom.find_by_tag("html").and_then(|html_id| {
if let Some(node) = self.dom.get(html_id)
&& let ArenaNodeData::Element { attrs, .. } = &node.data
{
for attr in attrs {
if attr.name.local.as_ref() == "lang" && !attr.value.is_empty() {
return Some(attr.value.clone());
}
}
}
None
});
// Compute body's style so its properties (like hyphens: auto) are inherited
let mut body_style = {
let elem_ref = ElementRef::new(self.dom, body);
compute_styles(elem_ref, self.stylesheets, None, &mut self.chapter.styles)
};
// Add html lang to body style if present (so it's inherited by all content)
if let Some(lang) = html_lang
&& body_style.language.is_none()
{
body_style.language = Some(lang);
}
// Process body's children as children of IR root, inheriting body's style
self.process_children(body, NodeId::ROOT, Some(&body_style));
self.chapter
}
/// Process children of a DOM node.
fn process_children(
&mut self,
dom_parent: ArenaNodeId,
ir_parent: NodeId,
parent_style: Option<&ComputedStyle>,
) {
for child_id in self.dom.children(dom_parent).collect::<Vec<_>>() {
self.process_node(child_id, ir_parent, parent_style);
}
}
/// Process a single DOM node.
fn process_node(
&mut self,
dom_id: ArenaNodeId,
ir_parent: NodeId,
parent_style: Option<&ComputedStyle>,
) {
let node = match self.dom.get(dom_id) {
Some(n) => n,
None => return,
};
match &node.data {
ArenaNodeData::Text(text) => {
// Handle whitespace-only text nodes
if text.trim().is_empty() {
// Whitespace between inline elements should be preserved as a single space.
// We preserve whitespace unless:
// 1. We're at the root level (no parent style)
// 2. The whitespace contains newlines and we're in a block context
//
// This handles cases like: <cite><abbr>A</abbr> <abbr>B</abbr></cite>
// where the space between abbrs must be preserved even though cite is block.
let has_newlines = text.contains('\n');
let is_block_parent = parent_style
.map(|s| s.display != Display::Inline)
.unwrap_or(true);
// Skip pure-whitespace with newlines in block contexts (inter-element whitespace)
// But preserve spaces without newlines (intra-line whitespace between inline elements)
if has_newlines && is_block_parent {
return;
}
// No parent means we're at root level - skip whitespace
if parent_style.is_none() {
return;
}
// Preserve as a single space
let range = self.chapter.append_text(" ");
let text_node = Node::text(range);
let ir_id = self.chapter.alloc_node(text_node);
self.chapter.append_child(ir_parent, ir_id);
self.node_map.insert(dom_id, ir_id);
return;
}
// Check if whitespace should be preserved (pre, pre-wrap, pre-line)
let preserve_whitespace = parent_style
.map(|s| {
matches!(
s.white_space,
WhiteSpace::Pre | WhiteSpace::PreWrap | WhiteSpace::PreLine
)
})
.unwrap_or(false);
// Normalize whitespace unless we're in a pre-like context
let text_content = if preserve_whitespace {
text.to_string()
} else {
normalize_whitespace(text)
};
let range = self.chapter.append_text(&text_content);
// Text nodes don't have styles - they inherit from parent element
let text_node = Node::text(range);
let ir_id = self.chapter.alloc_node(text_node);
self.chapter.append_child(ir_parent, ir_id);
self.node_map.insert(dom_id, ir_id);
}
ArenaNodeData::Element { name, attrs, .. } => {
// Compute style for this element
let elem_ref = ElementRef::new(self.dom, dom_id);
let mut computed = compute_styles(
elem_ref,
self.stylesheets,
parent_style,
&mut self.chapter.styles,
);
// Merge lang attribute into style (for KFX language property)
// This must happen before interning so the style includes the language
for attr in attrs {
if attr.name.local.as_ref() == "lang" && !attr.value.is_empty() {
computed.language = Some(attr.value.to_string());
break;
}
}
// Map to role first (needed for Break check)
let role = element_to_role(&name.local);
// Skip hidden elements, but preserve Break nodes
// CSS may hide <br> (e.g., in verse: "span + br { display: none }") but
// we still need them for line breaks in text/markdown export
if computed.display == Display::None && role != Role::Break {
return;
}
// Create IR node
let mut ir_node = Node::new(role);
ir_node.style = self.chapter.styles.intern(computed.clone());
let ir_id = self.chapter.alloc_node(ir_node);
self.chapter.append_child(ir_parent, ir_id);
self.node_map.insert(dom_id, ir_id);
// Store semantic attributes
for attr in attrs {
let attr_name = attr.name.local.as_ref();
let attr_ns = attr.name.ns.as_ref();
match attr_name {
// Core layout attributes
"href" => {
self.chapter.semantics.set_href(ir_id, &attr.value);
}
"src" => self.chapter.semantics.set_src(ir_id, &attr.value),
"alt" => self.chapter.semantics.set_alt(ir_id, &attr.value),
"id" => self.chapter.semantics.set_id(ir_id, &attr.value),
"title" => self.chapter.semantics.set_title(ir_id, &attr.value),
// Language (both lang and xml:lang)
"lang" => self.chapter.semantics.set_lang(ir_id, &attr.value),
// List start attribute (ol@start)
"start" if name.local.as_ref() == "ol" => {
if let Ok(start) = attr.value.parse::<u32>() {
self.chapter.semantics.set_list_start(ir_id, start);
}
}
// Semantic fidelity attributes
// epub:type attribute - handle both namespaced and prefixed forms
// html5ever parses "epub:type" as literal name with empty namespace
"type" if attr_ns == "http://www.idpf.org/2007/ops" => {
self.chapter.semantics.set_epub_type(ir_id, &attr.value);
}
"epub:type" => {
self.chapter.semantics.set_epub_type(ir_id, &attr.value);
}
"role" => {
self.chapter.semantics.set_aria_role(ir_id, &attr.value);
}
"datetime" => {
self.chapter.semantics.set_datetime(ir_id, &attr.value);
}
// Table cell attributes
"rowspan" if matches!(name.local.as_ref(), "td" | "th") => {
if let Ok(span) = attr.value.parse::<u32>() {
self.chapter.semantics.set_row_span(ir_id, span);
}
}
"colspan" if matches!(name.local.as_ref(), "td" | "th") => {
if let Ok(span) = attr.value.parse::<u32>() {
self.chapter.semantics.set_col_span(ir_id, span);
}
}
// Extract language from class for code elements
"class" if matches!(name.local.as_ref(), "code" | "pre") => {
for class in attr.value.split_whitespace() {
if let Some(lang) = class.strip_prefix("language-") {
self.chapter.semantics.set_language(ir_id, lang);
break;
}
if let Some(lang) = class.strip_prefix("lang-") {
self.chapter.semantics.set_language(ir_id, lang);
break;
}
}
}
_ => {}
}
}
// Mark th elements as header cells
if name.local.as_ref() == "th" {
self.chapter.semantics.set_header_cell(ir_id, true);
}
// Process children
self.process_children(dom_id, ir_id, Some(&computed));
}
// Skip other node types
ArenaNodeData::Document | ArenaNodeData::Comment(_) | ArenaNodeData::Doctype { .. } => {
}
}
}
}
/// Transform an ArenaDom to Chapter.
pub fn transform(dom: &ArenaDom, stylesheets: &[(Stylesheet, Origin)]) -> Chapter {
let ctx = TransformContext::new(dom, stylesheets);
ctx.transform()
}
/// Normalize whitespace in text content according to HTML rules.
///
/// Collapses runs of whitespace (spaces, tabs, newlines) to single spaces.
/// This matches standard HTML text content normalization.
fn normalize_whitespace(text: &str) -> String {
let mut result = String::with_capacity(text.len());
let mut prev_was_whitespace = false;
for c in text.chars() {
if c.is_whitespace() {
if !prev_was_whitespace {
result.push(' ');
prev_was_whitespace = true;
}
// Skip consecutive whitespace
} else {
result.push(c);
prev_was_whitespace = false;
}
}
result
}
#[cfg(test)]
mod tests {
use html5ever::driver::ParseOpts;
use html5ever::parse_document;
use html5ever::tendril::TendrilSink;
use super::*;
use crate::dom::tree_sink::ArenaSink;
fn parse_html(html: &str) -> ArenaDom {
let sink = ArenaSink::new();
let result = parse_document(sink, ParseOpts::default())
.from_utf8()
.one(html.as_bytes());
result.into_dom()
}
#[test]
fn test_basic_transform() {
let dom = parse_html("<html><body><p>Hello, World!</p></body></html>");
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Should have root + paragraph (Text) + text content
assert!(chapter.node_count() >= 3);
// Find text nodes
let mut found_text = false;
for id in chapter.iter_dfs() {
let node = chapter.node(id).unwrap();
if node.role == Role::Text && !node.text.is_empty() {
found_text = true;
let text = chapter.text(node.text);
assert!(text.contains("Hello"));
}
}
assert!(found_text);
}
#[test]
fn test_heading_levels() {
let dom = parse_html("<html><body><h1>Title</h1><h2>Subtitle</h2></body></html>");
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
let mut h1_count = 0;
let mut h2_count = 0;
for id in chapter.iter_dfs() {
match chapter.node(id).unwrap().role {
Role::Heading(1) => h1_count += 1,
Role::Heading(2) => h2_count += 1,
_ => {}
}
}
assert_eq!(h1_count, 1);
assert_eq!(h2_count, 1);
}
#[test]
fn test_link_semantics() {
let dom = parse_html(r#"<a href="https://example.com">Link</a>"#);
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Find link node
for id in chapter.iter_dfs() {
if chapter.node(id).unwrap().role == Role::Link {
assert_eq!(chapter.semantics.href(id), Some("https://example.com"));
return;
}
}
panic!("Link not found");
}
#[test]
fn test_style_inheritance() {
let dom = parse_html(
r#"<html><body>
<div style="color: red;"><p>Inherited</p></div>
</body></html>"#,
);
let ua = user_agent_stylesheet();
let author = Stylesheet::parse("div { color: red; }");
let stylesheets = vec![(ua, Origin::UserAgent), (author, Origin::Author)];
let chapter = transform(&dom, &stylesheets);
// The paragraph should inherit the red color from div
// (This is implicit in the cascade since we pass parent_style)
assert!(chapter.node_count() > 1);
}
#[test]
fn test_hidden_elements() {
let dom = parse_html(
r#"<html><head><title>Test</title></head><body><p>Visible</p></body></html>"#,
);
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Should not contain title element (display: none)
for id in chapter.iter_dfs() {
let node = chapter.node(id).unwrap();
if node.role == Role::Text {
let text = chapter.text(node.text);
assert!(!text.contains("Test"));
}
}
}
#[test]
fn test_br_element() {
let dom = parse_html(r#"<html><body><p>Line one<br/>Line two</p></body></html>"#);
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Should have a Break node
let mut found_break = false;
for id in chapter.iter_dfs() {
if chapter.node(id).unwrap().role == Role::Break {
found_break = true;
break;
}
}
assert!(found_break, "Break node not found");
}
#[test]
fn test_br_element_xhtml_style() {
// Test with XHTML-style self-closing br with namespace
let dom = parse_html(
r#"<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body><p><span>Line one</span><br/><span>Line two</span></p></body></html>"#,
);
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Should have a Break node
let mut found_break = false;
for id in chapter.iter_dfs() {
if chapter.node(id).unwrap().role == Role::Break {
found_break = true;
break;
}
}
assert!(found_break, "Break node not found in XHTML-style input");
}
#[test]
fn test_br_in_blockquote_verse() {
// Exact structure from epictetus.epub endnote 30
let dom = parse_html(
r#"<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<blockquote>
<p lang="la">
<span>Cui non conveniet sua res, ut calceus olim,</span>
<br/>
<span>Si pede major erit, subvertet; si minor, uret.</span>
</p>
</blockquote>
</body></html>"#,
);
let ua = user_agent_stylesheet();
let stylesheets = vec![(ua, Origin::UserAgent)];
let chapter = transform(&dom, &stylesheets);
// Should have a Break node
let mut found_break = false;
for id in chapter.iter_dfs() {
if chapter.node(id).unwrap().role == Role::Break {
found_break = true;
break;
}
}
assert!(found_break, "Break node not found in blockquote verse");
}
}