yrby 0.8.1 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea4b274bb5d8928c48900e3fe08b1e2b5d93c7e9970fe8ee6d453366e2c2c9be
4
- data.tar.gz: 6df2e03ac56e522e1ba854266dd193124514d0fe5f756ad79f1038e4704103b9
3
+ metadata.gz: b872650c1daa2696dc90e8f209b3dba702d7bb796f5af330550343a7e7397e51
4
+ data.tar.gz: 9070c8d98f2323f80ea60e79d59c307a47486465093c384182252136faab69d0
5
5
  SHA512:
6
- metadata.gz: e45d38cf54bf43b63a72e0127c89aedd3f5c713a7677afddbdd5c2b4c702af1d755bc1c15fdf22d0f118d9611dc7dd06843efb7ec94cb89d6a898cf6f6d44b30
7
- data.tar.gz: 651268d3b107633e104e346840083bcf5b1ecda00ae3d4586ed89cdb235b8bda34df34c6d60fcde23fdd47b18d0439ff9fca5e36683322799fc66d86157923bb
6
+ metadata.gz: b3d3d9536640200254a1b66bba0e7a3090215ba8d25b614c9e7908d4ef3cec02f0a31115608c4f9b108636735a1eff42acd39b44519b6b03e0d362b66df73b29
7
+ data.tar.gz: f77e3b5693746f19a4707c5f2a3d31d2b45d83122fc6d5faf362c4a11b2b52b4f7b712f0433aee38bf5b8fee352d0bd9539012b5e74bc43fd81f6d73616a4974
data/CHANGELOG.md CHANGED
@@ -6,6 +6,123 @@ to follow [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.9.1] - 2026-11-08
10
+
11
+ ### Changed
12
+
13
+ - yrby builds against yrs 0.28.1. An insert into a text no longer recounts
14
+ the UTF-16 length of each string before it, so appending to a long text
15
+ costs about the same at any length: 80k one-character appends took 2.5 s
16
+ and now take 48 ms.
17
+
18
+ ### Experimental
19
+
20
+ - `Y::Doc#edit` returns the document's own writes and never a peer's. The
21
+ document keeps the update of every local transaction until an edit returns
22
+ it, merged into one, so a write made outside an edit, from another thread
23
+ or fiber, or in a block that raised comes with the next edit. Before this,
24
+ the edit returned everything the document gained while the block ran, so
25
+ an update applied from a peer meanwhile was sent again as the edit's own,
26
+ and a caller had to lock the document to keep them apart. Each edit takes
27
+ everything unsent at once, so the updates edits return have no gaps
28
+ between them. A peer's update doesn't wait for an edit.
29
+
30
+ - `Y::Map#set_text(key, content = "")` stores a new `Y.Text` at a key and
31
+ returns its live `Y::Text`. `Y::Array#push_text(content = "")` and
32
+ `#insert_text(index, content = "")` do the same in a list. Before this,
33
+ Ruby could edit a nested `Y.Text` a browser made but couldn't make one,
34
+ so a server couldn't set up a form whose fields merge typing.
35
+
36
+ ## yrby-client 0.7.1 - 2026-11-03
37
+
38
+ ### Fixed
39
+
40
+ - yrby-client: `connect()` restores the presence that `disconnect()` removed,
41
+ unless the app set or cleared presence in between. Before this, a `renew()`
42
+ (which document sessions call with a renewed grant) or a manual reconnect
43
+ left the local awareness state `null`, and y-protocols ignores
44
+ `setLocalStateField` while it is `null`, so the user stayed invisible to
45
+ peers until reload.
46
+
47
+ ## [0.9.0] - 2026-11-03
48
+
49
+ ### Experimental
50
+
51
+ These are new, and they may change in a minor release. See
52
+ [docs/experimental.md](docs/experimental.md).
53
+
54
+ - Live handles that write to a document from Ruby: `Doc#get_map`,
55
+ `get_array`, `get_text`, and `get_xml_text` return `Y::Map`, `Y::Array`,
56
+ `Y::Text`, and `Y::XmlText`. A write is a CRDT edit, so a person editing the
57
+ same document keeps their change. A handle stays on the shared type it was
58
+ made for while others edit around it, and releases the GVL like `Doc`.
59
+ Indexes are Ruby characters, and negative indexes count from the end as in
60
+ Ruby. `push`, `<<`, and `insert` return the receiver, and `each`
61
+ without a block an Enumerator, as Ruby's own do.
62
+ - `Doc#edit { }` runs a block that writes to the document and returns the
63
+ update its writes produced, or nil when it wrote nothing. It is the unit a
64
+ process saves and sends as it writes.
65
+ - `Y::Lexical` and `Y::Lexxy` write Lexical's node shape: `append_paragraph`,
66
+ `append_heading`, `append_list`, `append_quote`, `append_code`,
67
+ `append_markdown`, `insert_paragraph`, `append_text`, `replace_runs`, and
68
+ `delete_block`.
69
+ Text takes formatted runs (`bold:`, `italic:`, `code:`, `link:`, and so on).
70
+ What Ruby writes renders the same as what a person typed. Each helper
71
+ writes its block in one transaction. `XmlText#push_xml_text` and
72
+ `insert_xml_text` take the new block's content, and `append` and
73
+ `replace` write content into a block.
74
+ - `append_markdown` writes a link to a script as plain text.
75
+ - `Y::Anchor`, from `XmlText#anchor`, is a block's identity as a value to
76
+ store or send. `Doc#block_at` and `Doc#find` resolve it.
77
+ `relative_position(index)` builds the Yjs caret shape editors put in
78
+ awareness, and `Text#index_at(position)` resolves one.
79
+ - `Doc#apply_update_changes(update, root)` applies an update and returns the
80
+ ordinals of the top-level blocks it touched. It reads the blocks and
81
+ applies the update in one transaction, so another thread's write doesn't
82
+ show up in the result.
83
+ - `Y::Awareness` builds and reads presence frames, and `Doc#client_id` returns
84
+ the id a document writes under.
85
+
86
+ ### Changed
87
+
88
+ - yrby and the renderer crates are on yrs 0.28. yrs 0.28 replaced the number
89
+ variants of `Any` with `Any::Number(Number::Int | Number::Float)`. Because
90
+ the crates take yrs types, this is a breaking release for them:
91
+ `lexical-yjs-html` 0.2.0, `prosemirror-yjs-html` 0.2.0, and
92
+ `yjs-html-core` 0.2.0 need yrs 0.28.
93
+
94
+ ### Fixed
95
+
96
+ - The Lexical and ProseMirror renderers (and the `lexical-yjs-html` and
97
+ `prosemirror-yjs-html` 0.2.1 crates) refuse a link to a script: Lexical
98
+ sends it to `about:blank` and ProseMirror empties its `href`, as each
99
+ editor does.
100
+ - A `Timeout`, `Thread#raise`, or `Thread#kill` that arrives during a
101
+ native call raises once the call returns. Before, Ruby could jump over
102
+ the extension's Rust frames without running their cleanup.
103
+
104
+ ## yrby-client 0.7.0 - 2026-10-08
105
+
106
+ This release updates yrby-client only. The gems are unchanged.
107
+
108
+ ### Added
109
+
110
+ - yrby-client: `<yrby-document>` has a read-only `current` property that
111
+ returns the `yrby:synced` detail of the bound session, or undefined when
112
+ nothing is bound. A binding that loads after the event fired can bind from
113
+ it.
114
+ - yrby-client: `<yrby-document>` has a `retry()` method that acquires the
115
+ document again after its session blocked or was discarded, without changing
116
+ whether the page is live. It works from a lease abort handler or right after
117
+ `session.discard()`, so a binding can replace a broken document with a fresh
118
+ session. It replaces calling `activate()`, which is internal to the Turbo
119
+ adapter.
120
+ - yrby-client: `YrbyDocumentElement.consumer` accepts a function that returns
121
+ a consumer or a promise of one. The element calls it when it first needs a
122
+ consumer and reuses the result. A function that throws or rejects is called
123
+ again on the next attempt, and assigning a different value replaces the
124
+ result.
125
+
9
126
  ## [0.8.1] - 2026-10-03
10
127
 
11
128
  ### Added
data/Cargo.lock CHANGED
@@ -13,9 +13,9 @@ dependencies = [
13
13
 
14
14
  [[package]]
15
15
  name = "arc-swap"
16
- version = "1.8.0"
16
+ version = "1.9.3"
17
17
  source = "registry+https://github.com/rust-lang/crates.io-index"
18
- checksum = "51d03449bb8ca2cc2ef70869af31463d1ae5ccc8fa3e334b307203fbf815207e"
18
+ checksum = "380962b3d93b99642e50b9febf72c071e97e8b32d86eae9cf4ef582bb960ae6e"
19
19
  dependencies = [
20
20
  "rustversion",
21
21
  ]
@@ -44,16 +44,14 @@ dependencies = [
44
44
 
45
45
  [[package]]
46
46
  name = "bindgen"
47
- version = "0.69.5"
47
+ version = "0.72.1"
48
48
  source = "registry+https://github.com/rust-lang/crates.io-index"
49
- checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
49
+ checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895"
50
50
  dependencies = [
51
51
  "bitflags",
52
52
  "cexpr",
53
53
  "clang-sys",
54
54
  "itertools",
55
- "lazy_static",
56
- "lazycell",
57
55
  "proc-macro2",
58
56
  "quote",
59
57
  "regex",
@@ -117,9 +115,9 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
117
115
 
118
116
  [[package]]
119
117
  name = "dashmap"
120
- version = "6.1.0"
118
+ version = "6.2.1"
121
119
  source = "registry+https://github.com/rust-lang/crates.io-index"
122
- checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
120
+ checksum = "e6361d5c062261c78a176addb82d4c821ae42bed6089de0e12603cd25de2059c"
123
121
  dependencies = [
124
122
  "cfg-if",
125
123
  "crossbeam-utils",
@@ -221,15 +219,9 @@ version = "1.5.0"
221
219
  source = "registry+https://github.com/rust-lang/crates.io-index"
222
220
  checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
223
221
 
224
- [[package]]
225
- name = "lazycell"
226
- version = "1.3.0"
227
- source = "registry+https://github.com/rust-lang/crates.io-index"
228
- checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
229
-
230
222
  [[package]]
231
223
  name = "lexical-yjs-html"
232
- version = "0.1.4"
224
+ version = "0.2.1"
233
225
  dependencies = [
234
226
  "serde_json",
235
227
  "yjs-html-core",
@@ -308,9 +300,9 @@ dependencies = [
308
300
 
309
301
  [[package]]
310
302
  name = "once_cell"
311
- version = "1.21.3"
303
+ version = "1.21.4"
312
304
  source = "registry+https://github.com/rust-lang/crates.io-index"
313
- checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
305
+ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
314
306
 
315
307
  [[package]]
316
308
  name = "parking"
@@ -348,7 +340,7 @@ dependencies = [
348
340
 
349
341
  [[package]]
350
342
  name = "prosemirror-yjs-html"
351
- version = "0.1.5"
343
+ version = "0.2.1"
352
344
  dependencies = [
353
345
  "serde_json",
354
346
  "yjs-html-core",
@@ -366,18 +358,18 @@ dependencies = [
366
358
 
367
359
  [[package]]
368
360
  name = "rb-sys"
369
- version = "0.9.124"
361
+ version = "0.9.130"
370
362
  source = "registry+https://github.com/rust-lang/crates.io-index"
371
- checksum = "c85c4188462601e2aa1469def389c17228566f82ea72f137ed096f21591bc489"
363
+ checksum = "02faf625bb10ba893e3ae620f19c9fb1b5f8fcae0fe4eb86bb3f2230fad75edb"
372
364
  dependencies = [
373
365
  "rb-sys-build",
374
366
  ]
375
367
 
376
368
  [[package]]
377
369
  name = "rb-sys-build"
378
- version = "0.9.124"
370
+ version = "0.9.130"
379
371
  source = "registry+https://github.com/rust-lang/crates.io-index"
380
- checksum = "568068db4102230882e6d4ae8de6632e224ca75fe5970f6e026a04e91ed635d3"
372
+ checksum = "05be6f9c86fe5482808162826f6c047d093b3670582296c770de1d94f8066694"
381
373
  dependencies = [
382
374
  "bindgen",
383
375
  "lazy_static",
@@ -400,6 +392,27 @@ version = "0.2.3"
400
392
  source = "registry+https://github.com/rust-lang/crates.io-index"
401
393
  checksum = "cca7ad6a7e21e72151d56fe2495a259b5670e204c3adac41ee7ef676ea08117a"
402
394
 
395
+ [[package]]
396
+ name = "rb-sys-test-helpers"
397
+ version = "0.3.0"
398
+ source = "registry+https://github.com/rust-lang/crates.io-index"
399
+ checksum = "ccc637c53cad6a6b49ad51c5857c64e36bd70c955ffafe9dc9cc5bc9dd0b3b18"
400
+ dependencies = [
401
+ "rb-sys",
402
+ "rb-sys-env 0.2.3",
403
+ "rb-sys-test-helpers-macros",
404
+ ]
405
+
406
+ [[package]]
407
+ name = "rb-sys-test-helpers-macros"
408
+ version = "0.3.0"
409
+ source = "registry+https://github.com/rust-lang/crates.io-index"
410
+ checksum = "b1c1fce35d9ac77a2745e539232fb82a75856675a5daf4ef8b1ca994d89f8b3f"
411
+ dependencies = [
412
+ "quote",
413
+ "syn",
414
+ ]
415
+
403
416
  [[package]]
404
417
  name = "redox_syscall"
405
418
  version = "0.5.18"
@@ -440,9 +453,9 @@ checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
440
453
 
441
454
  [[package]]
442
455
  name = "rustc-hash"
443
- version = "1.1.0"
456
+ version = "2.1.3"
444
457
  source = "registry+https://github.com/rust-lang/crates.io-index"
445
- checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
458
+ checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
446
459
 
447
460
  [[package]]
448
461
  name = "rustversion"
@@ -528,9 +541,9 @@ dependencies = [
528
541
 
529
542
  [[package]]
530
543
  name = "smallvec"
531
- version = "1.15.1"
544
+ version = "1.16.4"
532
545
  source = "registry+https://github.com/rust-lang/crates.io-index"
533
- checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
546
+ checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8"
534
547
 
535
548
  [[package]]
536
549
  name = "syn"
@@ -628,7 +641,7 @@ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
628
641
 
629
642
  [[package]]
630
643
  name = "yjs-html-core"
631
- version = "0.1.3"
644
+ version = "0.2.1"
632
645
  dependencies = [
633
646
  "serde_json",
634
647
  "yrs",
@@ -643,6 +656,7 @@ dependencies = [
643
656
  "prosemirror-yjs-html",
644
657
  "rb-sys",
645
658
  "rb-sys-env 0.1.2",
659
+ "rb-sys-test-helpers",
646
660
  "serde_json",
647
661
  "yjs-html-core",
648
662
  "yrs",
@@ -650,9 +664,9 @@ dependencies = [
650
664
 
651
665
  [[package]]
652
666
  name = "yrs"
653
- version = "0.27.2"
667
+ version = "0.28.1"
654
668
  source = "registry+https://github.com/rust-lang/crates.io-index"
655
- checksum = "ccc6a0094c76c87b1b72ca0579bf7aa00f540957182264e6561cbde707c2ce51"
669
+ checksum = "9f7716b4846f9fab489af2ed7ab31c4554f5fe6965ede2392b9cdfed0e5087ff"
656
670
  dependencies = [
657
671
  "arc-swap",
658
672
  "async-lock",
data/README.md CHANGED
@@ -101,6 +101,7 @@ bin/rails generate yrby:install && bin/rails db:migrate
101
101
  - [Rendering to HTML](#rendering-to-html)
102
102
  - [Protocol codec (module functions)](#protocol-codec-module-functions)
103
103
  - [ActionCable Integration](#actioncable-integration)
104
+ - [Experimental: writing from Ruby](#experimental-writing-from-ruby)
104
105
  - [Thread Safety](#thread-safety)
105
106
  - [Parallelism (GVL release)](#parallelism-gvl-release)
106
107
  - [Message Type Constants](#message-type-constants)
@@ -117,7 +118,9 @@ primitives. By default the Ruby side treats a document as opaque CRDT state. It
117
118
  applies updates, answers sync handshakes, and records deltas without reading
118
119
  the contents, and the browser editor decides what shape the document has. When
119
120
  you do need to look inside, `Doc#read_text` and `Doc#read_map` rebuild it in
120
- Ruby.
121
+ Ruby, and live handles (`Doc#get_map`, `get_array`, `get_text`,
122
+ `get_xml_text`) write to it. The handles are
123
+ [experimental](docs/experimental.md).
121
124
 
122
125
  ## Durability and delivery
123
126
 
@@ -680,7 +683,7 @@ gets blocked at the next reconnect. To avoid that, pair `expires_in:` with
680
683
  # app/controllers/posts_controller.rb
681
684
  def grant
682
685
  @post = current_user.posts.find(params[:id]) # your own authorization, again
683
- render json: { grant: @post.collaborative_sgid(:body, expires_in: 10.minutes) }
686
+ render json: { grant: @post.collaborative_document(:body).grant(expires_in: 10.minutes) }
684
687
  end
685
688
  ```
686
689
 
@@ -744,7 +747,7 @@ up the record from it, so the page decides which document the client gets.
744
747
 
745
748
  ```erb
746
749
  <%# the view picks the document and signs it %>
747
- <%= tag.div data: { grant: post.collaborative_sgid(:body) } %>
750
+ <%= tag.div data: { grant: post.collaborative_document(:body).grant } %>
748
751
  ```
749
752
 
750
753
  ```ruby
@@ -1074,6 +1077,18 @@ The browser clients manage presence (cursors, selections). The server doesn't
1074
1077
  set or hold presence state, and it relays awareness frames without reading
1075
1078
  them. See `yrby-client` for the client-side awareness API.
1076
1079
 
1080
+ ## Experimental: writing from Ruby
1081
+
1082
+ These APIs are new. They may change in any minor release, and they aren't
1083
+ covered by yrby's usual compatibility promise.
1084
+ [docs/experimental.md](docs/experimental.md) documents them:
1085
+
1086
+ - Live `Y::Map`, `Y::Array`, and `Y::Text` handles that write to a document
1087
+ - Writing Lexical rich text from Ruby, with `Y::Lexical` and `Y::Lexxy`
1088
+ - Anchors: a block's identity as a value
1089
+ - Streaming into a document with `Doc#edit`
1090
+ - Editing a record's document from Ruby
1091
+
1077
1092
  ## Thread Safety
1078
1093
 
1079
1094
  You can share a `Doc` across Ruby threads. Puma workers, ActionCable connection
data/ext/yrby/Cargo.toml CHANGED
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
12
12
  [dependencies]
13
13
  magnus = "0.8"
14
14
  rb-sys = "0.9"
15
- yrs = { version = "0.27", features = ["sync"] }
15
+ yrs = { version = "0.28.1", features = ["sync"] }
16
16
  serde_json = "1.0"
17
17
  yjs-html-core = { path = "crates/html-core" }
18
18
  lexical-yjs-html = { path = "crates/lexical-html" }
@@ -20,3 +20,5 @@ prosemirror-yjs-html = { path = "crates/prosemirror-html" }
20
20
 
21
21
  [dev-dependencies]
22
22
  rb-sys-env = "0.1"
23
+ magnus = { version = "0.8", features = ["embed"] }
24
+ rb-sys-test-helpers = "0.3"
@@ -3,7 +3,7 @@ name = "yjs-html-core"
3
3
  description = "Internal core for the lexical-yjs-html and prosemirror-yjs-html renderer crates; depend on those instead"
4
4
  repository = "https://github.com/jpcamara/yrby"
5
5
  readme = "README.md"
6
- version = "0.1.3"
6
+ version = "0.2.1"
7
7
  edition = "2024"
8
8
  rust-version = "1.85"
9
9
  authors = ["JP Camara <johnpcamara@gmail.com>"]
@@ -12,5 +12,5 @@ keywords = ["yjs", "crdt", "yrs", "html", "rendering"]
12
12
  categories = ["text-processing", "web-programming"]
13
13
 
14
14
  [dependencies]
15
- yrs = { version = "0.27", features = ["sync"] }
15
+ yrs = { version = "0.28", features = ["sync"] }
16
16
  serde_json = "1.0"
@@ -18,7 +18,7 @@
18
18
  //! every binding and caller.
19
19
 
20
20
  use std::collections::{BTreeMap, BTreeSet, HashMap};
21
- use yrs::{Any, Out, ReadTxn, Xml};
21
+ use yrs::{Any, Number, Out, ReadTxn, Xml};
22
22
 
23
23
  /// One piece of renderer output. `Html` is finished markup; `Deferred` is a
24
24
  /// callback node whose markup the caller supplies after the render, carrying
@@ -216,12 +216,12 @@ pub fn xml_ref_attr<T: ReadTxn, N: Xml>(txn: &T, node: &N, name: &str) -> Option
216
216
  pub fn any_attr_string(any: &Any) -> Option<String> {
217
217
  match any {
218
218
  Any::String(s) => Some(s.to_string()),
219
- Any::Number(n) => Some(if n.fract() == 0.0 {
219
+ Any::Number(Number::Int(n)) => Some(format!("{n}")),
220
+ Any::Number(Number::Float(n)) => Some(if n.fract() == 0.0 {
220
221
  format!("{}", *n as i64)
221
222
  } else {
222
223
  format!("{n}")
223
224
  }),
224
- Any::BigInt(n) => Some(format!("{n}")),
225
225
  Any::Bool(b) => Some(if *b { "true" } else { "false" }.to_string()),
226
226
  _ => None,
227
227
  }
@@ -458,10 +458,41 @@ fn parse_parts(name: &str, parts: &[serde_json::Value]) -> Result<Vec<AttrPart>,
458
458
  .collect()
459
459
  }
460
460
 
461
+ /// The scheme a URL names, lowercased, or `None` for a URL without one (a
462
+ /// path). Browsers ignore whitespace and control characters in a scheme, so
463
+ /// they don't count here either: `java\tscript:` is `javascript`.
464
+ pub fn url_scheme(url: &str) -> Option<String> {
465
+ let url: String = url
466
+ .chars()
467
+ .filter(|c| !c.is_whitespace() && !c.is_control())
468
+ .collect();
469
+ let (scheme, _) = url.split_once(':')?;
470
+ let mut chars = scheme.chars();
471
+ let starts_with_letter = chars.next().is_some_and(|c| c.is_ascii_alphabetic());
472
+ let valid = chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '.' | '-'));
473
+ (starts_with_letter && valid).then(|| scheme.to_ascii_lowercase())
474
+ }
475
+
461
476
  #[cfg(test)]
462
477
  mod tests {
463
478
  use super::*;
464
479
 
480
+ #[test]
481
+ fn url_scheme_reads_a_scheme_as_a_browser_does() {
482
+ assert_eq!(url_scheme("https://example.com").as_deref(), Some("https"));
483
+ assert_eq!(
484
+ url_scheme("JavaScript:alert(1)").as_deref(),
485
+ Some("javascript")
486
+ );
487
+ assert_eq!(
488
+ url_scheme(" java\tscript:alert(1)").as_deref(),
489
+ Some("javascript")
490
+ );
491
+ assert_eq!(url_scheme("/docs/a:b"), None);
492
+ assert_eq!(url_scheme("#top"), None);
493
+ assert_eq!(url_scheme("example.com"), None);
494
+ }
495
+
465
496
  #[test]
466
497
  fn parses_the_compiled_rule_shape() {
467
498
  let rules = Rules::parse(
@@ -3,7 +3,7 @@ name = "lexical-yjs-html"
3
3
  description = "Render Lexical-shaped yrs documents to HTML, no browser or Node required"
4
4
  repository = "https://github.com/jpcamara/yrby"
5
5
  readme = "README.md"
6
- version = "0.1.4"
6
+ version = "0.2.1"
7
7
  edition = "2024"
8
8
  rust-version = "1.85"
9
9
  authors = ["JP Camara <johnpcamara@gmail.com>"]
@@ -12,6 +12,6 @@ keywords = ["yjs", "crdt", "lexical", "html", "yrs"]
12
12
  categories = ["text-processing", "web-programming"]
13
13
 
14
14
  [dependencies]
15
- yrs = { version = "0.27", features = ["sync"] }
15
+ yrs = { version = "0.28", features = ["sync"] }
16
16
  serde_json = "1.0"
17
- yjs-html-core = { path = "../html-core", version = "0.1.3" }
17
+ yjs-html-core = { path = "../html-core", version = "0.2.1" }
@@ -67,8 +67,8 @@ mod readme_examples {}
67
67
  pub use yjs_html_core::*;
68
68
  use yrs::types::text::YChange;
69
69
  use yrs::{
70
- Any, GetString, Map, Out, ReadTxn, Text, Xml, XmlElementRef, XmlFragment, XmlFragmentRef,
71
- XmlOut, XmlTextRef,
70
+ Any, GetString, Map, Number, Out, ReadTxn, Text, Xml, XmlElementRef, XmlFragment,
71
+ XmlFragmentRef, XmlOut, XmlTextRef,
72
72
  };
73
73
 
74
74
  // Lexical text format bitmask (lexical 0.44).
@@ -391,13 +391,11 @@ fn open_block<T: ReadTxn>(
391
391
  push_block_children(txn, t, depth, "</tr>", false, stack);
392
392
  }
393
393
  "tablecell" => {
394
- let header = matches!(
395
- t.get_attribute(txn, "__headerState"),
396
- Some(Out::Any(Any::Number(n))) if n > 0.0
397
- ) || matches!(
398
- t.get_attribute(txn, "__headerState"),
399
- Some(Out::Any(Any::BigInt(n))) if n > 0
400
- );
394
+ let header = match t.get_attribute(txn, "__headerState") {
395
+ Some(Out::Any(Any::Number(Number::Int(n)))) => n > 0,
396
+ Some(Out::Any(Any::Number(Number::Float(n)))) => n > 0.0,
397
+ _ => false,
398
+ };
401
399
  let close = if header {
402
400
  em.push_str("<th>");
403
401
  "</th>"
@@ -562,8 +560,8 @@ fn open_listitem<T: ReadTxn>(
562
560
  rules: &Rules,
563
561
  ) {
564
562
  let value = match t.get_attribute(txn, "__value") {
565
- Some(Out::Any(Any::Number(n))) => n as i64,
566
- Some(Out::Any(Any::BigInt(n))) => n,
563
+ Some(Out::Any(Any::Number(Number::Int(n)))) => n,
564
+ Some(Out::Any(Any::Number(Number::Float(n)))) => n as i64,
567
565
  _ => 1,
568
566
  };
569
567
  let checked = match t.get_attribute(txn, "__checked") {
@@ -669,8 +667,8 @@ fn render_inline<T: ReadTxn>(
669
667
  _ => {
670
668
  is_tab = false;
671
669
  format = match m.get(txn, "__format") {
672
- Some(Out::Any(Any::Number(n))) => n as u32,
673
- Some(Out::Any(Any::BigInt(n))) => n as u32,
670
+ Some(Out::Any(Any::Number(Number::Int(n)))) => n as u32,
671
+ Some(Out::Any(Any::Number(Number::Float(n)))) => n as u32,
674
672
  _ => 0,
675
673
  };
676
674
  style = match m.get(txn, "__style") {
@@ -796,13 +794,24 @@ fn lexxy_style(style: &str) -> Option<String> {
796
794
  if css.is_empty() { None } else { Some(css) }
797
795
  }
798
796
 
797
+ /// The schemes a link may use, and what an editor shows for any other, as
798
+ /// Lexical's `LinkNode` does: a `javascript:` link would run in the page.
799
+ const LINK_SCHEMES: [&str; 5] = ["http", "https", "mailto", "sms", "tel"];
800
+
801
+ fn safe_href(url: String) -> String {
802
+ match url_scheme(&url) {
803
+ Some(scheme) if !LINK_SCHEMES.contains(&scheme.as_str()) => "about:blank".to_string(),
804
+ _ => url,
805
+ }
806
+ }
807
+
799
808
  /// `link` / `autolink`: Lexxy's sanitize keeps only `href` and `title`
800
809
  /// (`target`/`rel` are stored in the doc but stripped from exported HTML).
801
810
  fn render_link<T: ReadTxn>(txn: &T, t: &XmlTextRef, depth: usize, em: &mut Emitter, rules: &Rules) {
802
811
  em.push_str("<a");
803
812
  if let Some(url) = str_attr(txn, t, "__url") {
804
813
  em.push_str(" href=\"");
805
- em.push_str(&escape_attr(&url));
814
+ em.push_str(&escape_attr(&safe_href(url)));
806
815
  em.push('"');
807
816
  }
808
817
  if let Some(title) = str_attr(txn, t, "__title").filter(|s| !s.is_empty()) {
@@ -981,6 +990,21 @@ pub fn escape_attr(s: &str) -> String {
981
990
  #[cfg(test)]
982
991
  mod tests {
983
992
  use super::*;
993
+
994
+ #[test]
995
+ fn a_link_to_an_unsafe_scheme_goes_nowhere() {
996
+ assert_eq!(
997
+ safe_href("https://example.com".into()),
998
+ "https://example.com"
999
+ );
1000
+ assert_eq!(safe_href("/docs".into()), "/docs");
1001
+ assert_eq!(
1002
+ safe_href("mailto:a@example.com".into()),
1003
+ "mailto:a@example.com"
1004
+ );
1005
+ assert_eq!(safe_href("javascript:alert(1)".into()), "about:blank");
1006
+ assert_eq!(safe_href(" JAVA\tSCRIPT:alert(1)".into()), "about:blank");
1007
+ }
984
1008
  use yrs::updates::decoder::Decode;
985
1009
  use yrs::{Doc, Transact, Update};
986
1010
 
@@ -1167,8 +1191,14 @@ mod tests {
1167
1191
  let frag = txn.get_xml_fragment("root").unwrap();
1168
1192
  let html = render(&txn, &frag).unwrap();
1169
1193
 
1170
- assert!(html.contains("rocks"), "marked text survives without a rule: {html}");
1171
- assert!(!html.contains("<mark"), "no wrapper is invented for it: {html}");
1194
+ assert!(
1195
+ html.contains("rocks"),
1196
+ "marked text survives without a rule: {html}"
1197
+ );
1198
+ assert!(
1199
+ !html.contains("<mark"),
1200
+ "no wrapper is invented for it: {html}"
1201
+ );
1172
1202
  assert!(html.contains("#ruby"), "hashtag text survives: {html}");
1173
1203
  }
1174
1204
 
@@ -3,7 +3,7 @@ name = "prosemirror-yjs-html"
3
3
  description = "Render ProseMirror-shaped yrs documents to HTML, no browser or Node required"
4
4
  repository = "https://github.com/jpcamara/yrby"
5
5
  readme = "README.md"
6
- version = "0.1.5"
6
+ version = "0.2.1"
7
7
  edition = "2024"
8
8
  rust-version = "1.85"
9
9
  authors = ["JP Camara <johnpcamara@gmail.com>"]
@@ -12,6 +12,6 @@ keywords = ["yjs", "crdt", "prosemirror", "tiptap", "html"]
12
12
  categories = ["text-processing", "web-programming"]
13
13
 
14
14
  [dependencies]
15
- yrs = { version = "0.27", features = ["sync"] }
15
+ yrs = { version = "0.28", features = ["sync"] }
16
16
  serde_json = "1.0"
17
- yjs-html-core = { path = "../html-core", version = "0.1.3" }
17
+ yjs-html-core = { path = "../html-core", version = "0.2.1" }