rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
//! Bounded body-text clipboard adapters; the OS clipboard belongs to the host.
//!
//! HTML uses the core's existing formatting importer/exporter, not a browser or
//! a general HTML sanitizer. Objects/tables and scalar/logical inline affinity
//! are outside this surface. Empty HTML and empty text preserve selection/history.

use std::borrow::Cow;

use quick_xml::events::Event;
use quick_xml::Reader;
use serde::{Deserialize, Serialize};

use crate::document_core::native_interaction::{NativeTextPosition, NativeTextSelection};
use crate::DocumentCore;

use super::editor::{NativeEditOutcome, NativeEditor};
use super::NativeHostError;

pub const MAX_CLIPBOARD_BYTES: usize = 1024 * 1024;
/// Bound the number of core split/reflow operations in one plain-text paste.
pub const MAX_CLIPBOARD_PARAGRAPHS: usize = 256;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeClipboardPayload {
    pub plain_text: String,
    /// None for an empty selection or when the existing HTML exporter would
    /// omit an empty selected paragraph fragment. Plain text remains lossless.
    pub html: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct HtmlPasteResult {
    ok: bool,
    para_idx: Option<u32>,
    char_offset: Option<u32>,
    error: Option<String>,
}

impl NativeEditor {
    /// Read-only, including revision/history/selection. Active preedit is not a
    /// committed clipboard selection and must be resolved by the host first.
    pub fn copy_selection(
        &self,
        expected_revision: u64,
    ) -> Result<NativeClipboardPayload, NativeHostError> {
        self.check_edit_ready(expected_revision)?;
        let selection = self.selection();
        let (bytes, empty_fragment) =
            clipboard_selection_size(self.session().document(), selection)?;
        if bytes == 0 {
            return Ok(NativeClipboardPayload {
                plain_text: String::new(),
                html: None,
            });
        }
        Self::check_body_text_span(self.session().document(), selection)?;
        let plain_text = self.selected_text(selection);
        check_text_controls(&plain_text)?;
        let (start, end) = Self::ordered(selection);
        let html = if empty_fragment {
            None
        } else {
            let exported = self.session().document().export_selection_html_native(
                start.section as usize,
                start.paragraph as usize,
                start.scalar_offset as usize,
                end.paragraph as usize,
                end.scalar_offset as usize,
            )?;
            Some(compact_exported_html(&exported)?.into_owned())
        };
        if plain_text
            .len()
            .checked_add(html.as_ref().map_or(0, String::len))
            .is_none_or(|size| size > MAX_CLIPBOARD_BYTES)
        {
            return Err(clipboard_limit());
        }
        Ok(NativeClipboardPayload { plain_text, html })
    }

    /// Payload construction completes before deletion, so export/limit failures
    /// cannot cut content. The returned payload is for host clipboard ownership.
    pub fn cut_selection(
        &mut self,
        expected_revision: u64,
    ) -> Result<(NativeClipboardPayload, NativeEditOutcome), NativeHostError> {
        let payload = self.copy_selection(expected_revision)?;
        let outcome = self.replace_selection(expected_revision, "")?;
        Ok((payload, outcome))
    }

    pub fn paste_text(
        &mut self,
        expected_revision: u64,
        text: &str,
    ) -> Result<NativeEditOutcome, NativeHostError> {
        self.check_edit_ready(expected_revision)?;
        check_input_size(text)?;
        let text = text.replace("\r\n", "\n").replace('\r', "\n");
        check_text_controls(&text)?;
        if text.is_empty() {
            return self.set_selection(expected_revision, self.selection().clone());
        }
        let lines: Vec<&str> = text
            .split('\n')
            .take(MAX_CLIPBOARD_PARAGRAPHS + 1)
            .collect();
        if lines.len() > MAX_CLIPBOARD_PARAGRAPHS {
            return Err(NativeHostError::LimitExceeded(
                "clipboard paragraph limit exceeded".into(),
            ));
        }
        let selection = self.selection().clone();
        if lines.len() == 1 {
            return self.replace_range(expected_revision, &selection, &text);
        }
        let (start, end) = Self::ordered(&selection);
        // The comparison allocates selected text only after its byte bound is
        // known; replacing a larger selection with a small payload is allowed.
        if clipboard_selection_size(self.session().document(), &selection)
            .ok()
            .is_some_and(|(bytes, _)| bytes == text.len())
            && self.selected_text(&selection) == text
        {
            return self.set_selection(
                expected_revision,
                NativeTextSelection {
                    anchor: end.clone(),
                    focus: end.clone(),
                },
            );
        }
        self.edit_transaction(expected_revision, |core| {
            Self::apply_replacement(core, &selection, lines[0])?;
            let mut focus = start.clone();
            focus.scalar_offset = focus
                .scalar_offset
                .checked_add(lines[0].chars().count() as u32)
                .ok_or_else(clipboard_limit)?;
            for line in &lines[1..] {
                core.split_paragraph_native(
                    focus.section as usize,
                    focus.paragraph as usize,
                    focus.scalar_offset as usize,
                    None,
                )?;
                focus.paragraph = focus.paragraph.checked_add(1).ok_or_else(clipboard_limit)?;
                focus.scalar_offset = 0;
                if !line.is_empty() {
                    core.insert_text_native(
                        focus.section as usize,
                        focus.paragraph as usize,
                        0,
                        line,
                    )?;
                }
                focus.scalar_offset = line.chars().count() as u32;
            }
            Ok(NativeTextSelection {
                anchor: focus.clone(),
                focus,
            })
        })
    }

    pub fn paste_html(
        &mut self,
        expected_revision: u64,
        html: &str,
    ) -> Result<NativeEditOutcome, NativeHostError> {
        self.check_edit_ready(expected_revision)?;
        check_input_size(html)?;
        let html = prepare_body_html(html)?;
        let selection = self.selection().clone();
        if html.trim().is_empty() {
            return self.set_selection(expected_revision, selection);
        }
        let (start, end) = Self::ordered(&selection);
        check_imported_paragraphs(self.session().document(), start, end)?;
        let mut empty_html = false;
        let result = self.edit_transaction(expected_revision, |core| {
            if start != end {
                Self::apply_replacement(core, &selection, "")?;
            }
            let before_count = core.document().sections[start.section as usize]
                .paragraphs
                .len();
            let json = core.paste_html_native(
                start.section as usize,
                start.paragraph as usize,
                start.scalar_offset as usize,
                &html,
            )?;
            let imported: HtmlPasteResult = serde_json::from_str(&json).map_err(|_| {
                NativeHostError::InvalidArgument(
                    "HTML importer returned an invalid caret response".into(),
                )
            })?;
            if !imported.ok {
                if imported.error.as_deref() == Some("empty html")
                    && imported.para_idx.is_none()
                    && imported.char_offset.is_none()
                {
                    empty_html = true;
                }
                return Err(NativeHostError::InvalidArgument(
                    "HTML contained no supported content".into(),
                ));
            }
            if imported.error.is_some() {
                return Err(NativeHostError::InvalidArgument(
                    "HTML importer returned conflicting status".into(),
                ));
            }
            let mut focus = start.clone();
            focus.paragraph = imported.para_idx.ok_or_else(invalid_import_caret)?;
            focus.scalar_offset = imported.char_offset.ok_or_else(invalid_import_caret)?;
            let after_count = core.document().sections[start.section as usize]
                .paragraphs
                .len();
            let added = after_count
                .checked_sub(before_count)
                .ok_or_else(invalid_import_caret)?;
            if added >= MAX_CLIPBOARD_PARAGRAPHS {
                return Err(NativeHostError::LimitExceeded(
                    "clipboard paragraph limit exceeded".into(),
                ));
            }
            let last = (start.paragraph as usize)
                .checked_add(added)
                .ok_or_else(invalid_import_caret)?;
            if focus.paragraph as usize != last {
                return Err(invalid_import_caret());
            }
            // Validate every inserted paragraph, not only the final caret: the
            // importer may put a table between two otherwise valid body lines.
            check_imported_paragraphs(core, start, &focus)?;
            Self::validate_selection(
                core,
                &NativeTextSelection {
                    anchor: start.clone(),
                    focus: focus.clone(),
                },
            )?;
            if added == 0 && focus == *start {
                // <p></p> can return success after interning styles. Roll back
                // those changes and any prior selection deletion as a no-op.
                empty_html = true;
                return Err(NativeHostError::InvalidArgument(
                    "HTML inserted no content".into(),
                ));
            }
            Ok(NativeTextSelection {
                anchor: focus.clone(),
                focus,
            })
        });
        match result {
            // A failed snapshot restore is Core, not InvalidArgument, and is
            // propagated rather than misreported as a successful no-op.
            Err(NativeHostError::InvalidArgument(_)) if empty_html => {
                self.set_selection(expected_revision, selection)
            }
            result => result,
        }
    }
}

fn check_input_size(text: &str) -> Result<(), NativeHostError> {
    if text.len() > MAX_CLIPBOARD_BYTES {
        Err(clipboard_limit())
    } else {
        Ok(())
    }
}

fn clipboard_limit() -> NativeHostError {
    NativeHostError::LimitExceeded("clipboard payload exceeds 1 MiB".into())
}

fn invalid_import_caret() -> NativeHostError {
    NativeHostError::InvalidArgument("HTML importer returned an invalid scalar caret".into())
}

fn check_text_controls(text: &str) -> Result<(), NativeHostError> {
    if text.chars().any(|ch| ch.is_control() && ch != '\n') {
        return Err(NativeHostError::Unsupported(
            "clipboard tabs and control characters are unsupported".into(),
        ));
    }
    Ok(())
}

/// The selected paragraphs have no control characters. Line breaks in the
/// exporter's whitespace-only text tokens are therefore formatting, not content.
/// Preserve ordinary spaces and attribute values, and leave external HTML alone.
fn compact_exported_html(html: &str) -> Result<Cow<'_, str>, NativeHostError> {
    let mut reader = Reader::from_str(html);
    let mut normalized: Option<String> = None;
    let mut copy_start = 0;
    loop {
        let event_start = reader.buffer_position() as usize;
        match reader.read_event().map_err(|_| {
            NativeHostError::Core(crate::HwpError::RenderError(
                "clipboard exporter returned invalid markup".into(),
            ))
        })? {
            Event::Text(text)
                if text.as_ref().contains('\n')
                    && text.as_ref().bytes().all(|byte| byte.is_ascii_whitespace()) =>
            {
                normalized
                    .get_or_insert_with(|| String::with_capacity(html.len()))
                    .push_str(&html[copy_start..event_start]);
                copy_start = reader.buffer_position() as usize;
            }
            Event::Eof => break,
            _ => {}
        }
    }
    match normalized {
        Some(mut normalized) => {
            normalized.push_str(&html[copy_start..]);
            Ok(Cow::Owned(normalized))
        }
        None => Ok(Cow::Borrowed(html)),
    }
}

/// A lexical admission pass, not an HTML sanitizer or a second importer. The
/// core silently drops some unsupported source objects (for example non-data
/// images), so checking only the resulting IR cannot detect that content loss.
/// Existing XML tokenization distinguishes quoted attributes, comments and
/// escaped text; HTML void tags and optional/mixed-case end tags need no XML
/// nesting validation here. The caller has already bounded input to 1 MiB.
fn prepare_body_html(html: &str) -> Result<Cow<'_, str>, NativeHostError> {
    let mut reader = Reader::from_str(html);
    reader.config_mut().check_end_names = false;
    reader.config_mut().allow_unmatched_ends = true;
    reader.config_mut().allow_dangling_amp = true;
    let mut without_comments: Option<String> = None;
    let mut copy_start = 0;
    loop {
        let event_start = reader.buffer_position() as usize;
        match reader.read_event().map_err(|_| {
            NativeHostError::Unsupported("clipboard HTML contains unrecognized markup".into())
        })? {
            Event::Start(tag) | Event::Empty(tag) => {
                let name = tag.local_name().as_ref().to_ascii_lowercase();
                if matches!(
                    name.as_bytes(),
                    b"img"
                        | b"picture"
                        | b"source"
                        | b"table"
                        | b"svg"
                        | b"math"
                        | b"object"
                        | b"embed"
                        | b"iframe"
                        | b"canvas"
                        | b"video"
                        | b"audio"
                        | b"form"
                        | b"input"
                        | b"select"
                        | b"textarea"
                        | b"button"
                        | b"hr"
                ) {
                    return Err(NativeHostError::Unsupported(
                        "clipboard HTML object and table import is unsupported".into(),
                    ));
                }
            }
            Event::Comment(_) => {
                // The core's tag scanner can mistake '>' inside a comment for
                // its end and insert the remaining '-->' as document text.
                without_comments
                    .get_or_insert_with(|| String::with_capacity(html.len()))
                    .push_str(&html[copy_start..event_start]);
                copy_start = reader.buffer_position() as usize;
            }
            Event::Eof => break,
            _ => {}
        }
    }
    match without_comments {
        Some(mut normalized) => {
            normalized.push_str(&html[copy_start..]);
            Ok(Cow::Owned(normalized))
        }
        None => Ok(Cow::Borrowed(html)),
    }
}

fn clipboard_selection_size(
    core: &DocumentCore,
    selection: &NativeTextSelection,
) -> Result<(usize, bool), NativeHostError> {
    let (start, end) = NativeEditor::ordered(selection);
    let paragraphs = &core.document().sections[start.section as usize].paragraphs;
    let mut bytes = (end.paragraph - start.paragraph) as usize;
    let mut empty_fragment = false;
    if bytes >= MAX_CLIPBOARD_PARAGRAPHS {
        return Err(NativeHostError::LimitExceeded(
            "clipboard paragraph limit exceeded".into(),
        ));
    }
    for index in start.paragraph..=end.paragraph {
        let text = &paragraphs[index as usize].text;
        let from = if index == start.paragraph {
            start.scalar_offset as usize
        } else {
            0
        };
        let count = if index == end.paragraph {
            (end.scalar_offset as usize) - from
        } else {
            usize::MAX
        };
        let mut fragment_bytes = 0;
        for ch in text.chars().skip(from).take(count) {
            if ch.is_control() {
                return Err(NativeHostError::Unsupported(
                    "clipboard paragraph contains unsupported control characters".into(),
                ));
            }
            fragment_bytes += ch.len_utf8();
            bytes = bytes
                .checked_add(ch.len_utf8())
                .ok_or_else(clipboard_limit)?;
            if bytes > MAX_CLIPBOARD_BYTES {
                return Err(clipboard_limit());
            }
        }
        empty_fragment |= fragment_bytes == 0;
    }
    Ok((bytes, empty_fragment))
}

fn check_imported_paragraphs(
    core: &DocumentCore,
    start: &NativeTextPosition,
    end: &NativeTextPosition,
) -> Result<(), NativeHostError> {
    NativeEditor::check_body_text_span(
        core,
        &NativeTextSelection {
            anchor: start.clone(),
            focus: end.clone(),
        },
    )?;
    for paragraph in &core.document().sections[start.section as usize].paragraphs
        [start.paragraph as usize..=end.paragraph as usize]
    {
        if paragraph.text.chars().any(char::is_control) {
            return Err(NativeHostError::Unsupported(
                "clipboard paragraph contains unsupported control characters".into(),
            ));
        }
    }
    Ok(())
}