y-rb 0.3.2-aarch64-linux → 0.4.0-aarch64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/yrb/Cargo.toml +5 -5
- data/ext/yrb/src/awareness.rs +5 -5
- data/ext/yrb/src/lib.rs +81 -35
- data/ext/yrb/src/yarray.rs +68 -33
- data/ext/yrb/src/ydoc.rs +53 -10
- data/ext/yrb/src/ymap.rs +47 -24
- data/ext/yrb/src/ytext.rs +55 -60
- data/ext/yrb/src/ytransaction.rs +71 -23
- data/ext/yrb/src/yvalue.rs +26 -7
- data/ext/yrb/src/yxml_element.rs +133 -72
- data/ext/yrb/src/yxml_fragment.rs +16 -0
- data/ext/yrb/src/yxml_text.rs +96 -56
- data/lib/2.7/yrb.so +0 -0
- data/lib/3.0/yrb.so +0 -0
- data/lib/3.1/yrb.so +0 -0
- data/lib/y/array.rb +114 -100
- data/lib/y/awareness.rb +40 -14
- data/lib/y/doc.rb +77 -71
- data/lib/y/map.rb +39 -42
- data/lib/y/text.rb +92 -96
- data/lib/y/transaction.rb +31 -13
- data/lib/y/version.rb +1 -1
- data/lib/y/xml.rb +259 -227
- data/lib/y-rb.rb +1 -1
- metadata +5 -4
data/lib/y/xml.rb
CHANGED
@@ -21,7 +21,7 @@ module Y
|
|
21
21
|
|
22
22
|
# Create a new XMLElement instance
|
23
23
|
#
|
24
|
-
# @param [Y::Doc]
|
24
|
+
# @param doc [Y::Doc]
|
25
25
|
def initialize(doc = nil)
|
26
26
|
@document = doc || Y::Doc.new
|
27
27
|
|
@@ -30,22 +30,24 @@ module Y
|
|
30
30
|
|
31
31
|
# Retrieve node at index
|
32
32
|
#
|
33
|
-
# @param [Integer]
|
33
|
+
# @param index [Integer]
|
34
34
|
# @return [Y::XMLElement|nil]
|
35
35
|
def [](index)
|
36
|
-
node = yxml_element_get(index)
|
36
|
+
node = document.current_transaction { |tx| yxml_element_get(tx, index) }
|
37
37
|
node&.document = document
|
38
38
|
node
|
39
39
|
end
|
40
40
|
|
41
41
|
# Create a node at index
|
42
42
|
#
|
43
|
-
# @param [Integer]
|
44
|
-
# @param [String]
|
43
|
+
# @param index [Integer]
|
44
|
+
# @param name [String] Name of node, e.g. `<p />`
|
45
45
|
# @return [Y::XMLElement]
|
46
46
|
# rubocop:disable Lint/Void
|
47
47
|
def []=(index, name)
|
48
|
-
node =
|
48
|
+
node = document.current_transaction do |tx|
|
49
|
+
yxml_element_insert_element(tx, index, name)
|
50
|
+
end
|
49
51
|
node.document = document
|
50
52
|
node
|
51
53
|
end
|
@@ -55,7 +57,7 @@ module Y
|
|
55
57
|
#
|
56
58
|
# @return [Hash]
|
57
59
|
def attrs
|
58
|
-
yxml_element_attributes
|
60
|
+
document.current_transaction { |tx| yxml_element_attributes(tx) }
|
59
61
|
end
|
60
62
|
|
61
63
|
alias attributes attrs
|
@@ -64,7 +66,7 @@ module Y
|
|
64
66
|
#
|
65
67
|
# @return [Y::XMLElement]
|
66
68
|
def first_child
|
67
|
-
child = yxml_element_first_child
|
69
|
+
child = document.current_transaction { |tx| yxml_element_first_child(tx) }
|
68
70
|
child&.document = document
|
69
71
|
child
|
70
72
|
end
|
@@ -73,13 +75,14 @@ module Y
|
|
73
75
|
#
|
74
76
|
# Optional input is pushed to the text if provided
|
75
77
|
#
|
76
|
-
# @param [Integer]
|
77
|
-
# @param [String|nil]
|
78
|
+
# @param index [Integer]
|
79
|
+
# @param input [String|nil]
|
78
80
|
# @return [Y::XMLText]
|
79
|
-
def insert_text(index, input =
|
80
|
-
text =
|
81
|
+
def insert_text(index, input = "")
|
82
|
+
text = document.current_transaction do |tx|
|
83
|
+
yxml_element_insert_text(tx, index, input)
|
84
|
+
end
|
81
85
|
text.document = document
|
82
|
-
text << input unless input.nil?
|
83
86
|
text
|
84
87
|
end
|
85
88
|
|
@@ -87,7 +90,7 @@ module Y
|
|
87
90
|
#
|
88
91
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
89
92
|
def next_sibling
|
90
|
-
node = yxml_element_next_sibling
|
93
|
+
node = document.current_transaction { |tx| yxml_element_next_sibling(tx) }
|
91
94
|
node&.document = document
|
92
95
|
node
|
93
96
|
end
|
@@ -106,8 +109,8 @@ module Y
|
|
106
109
|
# xml_element = doc.get_xml_element("my xml element")
|
107
110
|
# xml_element.attach { |changes| … }
|
108
111
|
#
|
109
|
-
# @param [Proc]
|
110
|
-
# @param [Block]
|
112
|
+
# @param callback [Proc]
|
113
|
+
# @param block [Block]
|
111
114
|
# @return [Integer] The subscription ID
|
112
115
|
def attach(callback = nil, &block)
|
113
116
|
return yxml_element_observe(callback) unless callback.nil?
|
@@ -128,17 +131,19 @@ module Y
|
|
128
131
|
#
|
129
132
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
130
133
|
def prev_sibling
|
131
|
-
node = yxml_element_prev_sibling
|
134
|
+
node = document.current_transaction { |tx| yxml_element_prev_sibling(tx) }
|
132
135
|
node&.document = document
|
133
136
|
node
|
134
137
|
end
|
135
138
|
|
136
139
|
# Creates a new child an inserts at the end of the children list
|
137
140
|
#
|
138
|
-
# @param [String]
|
141
|
+
# @param name [String]
|
139
142
|
# @return [Y::XMLElement]
|
140
143
|
def <<(name)
|
141
|
-
xml_element =
|
144
|
+
xml_element = document.current_transaction do |tx|
|
145
|
+
yxml_element_push_element_back(tx, name)
|
146
|
+
end
|
142
147
|
xml_element.document = document
|
143
148
|
xml_element
|
144
149
|
end
|
@@ -149,12 +154,13 @@ module Y
|
|
149
154
|
#
|
150
155
|
# The optional str argument initializes the text node with its value
|
151
156
|
#
|
152
|
-
# @param [String]
|
157
|
+
# @param str [String]
|
153
158
|
# @return [Y::XMLText]
|
154
|
-
def push_text(str =
|
155
|
-
text =
|
159
|
+
def push_text(str = "")
|
160
|
+
text = document.current_transaction do |tx|
|
161
|
+
yxml_element_push_text_back(tx, str)
|
162
|
+
end
|
156
163
|
text.document = document
|
157
|
-
text << str unless str.nil?
|
158
164
|
text
|
159
165
|
end
|
160
166
|
|
@@ -162,7 +168,7 @@ module Y
|
|
162
168
|
#
|
163
169
|
# @return [Integer]
|
164
170
|
def size
|
165
|
-
yxml_element_size
|
171
|
+
document.current_transaction { |tx| yxml_element_size(tx) }
|
166
172
|
end
|
167
173
|
|
168
174
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
@@ -192,42 +198,44 @@ module Y
|
|
192
198
|
#
|
193
199
|
# @return [void]
|
194
200
|
def slice!(*args)
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
if args.size == 1
|
201
|
-
arg = args.first
|
201
|
+
document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength
|
202
|
+
if args.size.zero?
|
203
|
+
raise ArgumentError,
|
204
|
+
"Provide one of `index`, `range`, `start, length` as arguments"
|
205
|
+
end
|
202
206
|
|
203
|
-
if
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
+
if args.size == 1
|
208
|
+
arg = args.first
|
209
|
+
|
210
|
+
if arg.is_a?(Range)
|
211
|
+
if arg.exclude_end?
|
212
|
+
yxml_element_remove_range(tx, arg.first,
|
213
|
+
arg.last - arg.first)
|
214
|
+
end
|
215
|
+
unless arg.exclude_end?
|
216
|
+
yxml_element_remove_range(tx, arg.first,
|
217
|
+
arg.last + 1 - arg.first)
|
218
|
+
end
|
219
|
+
return nil
|
207
220
|
end
|
208
|
-
unless arg.exclude_end?
|
209
|
-
yxml_element_remove_range(transaction, arg.first,
|
210
|
-
arg.last + 1 - arg.first)
|
211
|
-
end
|
212
|
-
return nil
|
213
|
-
end
|
214
221
|
|
215
|
-
|
216
|
-
|
217
|
-
|
222
|
+
if arg.is_a?(Numeric)
|
223
|
+
yxml_element_remove_range(tx, arg.to_int, 1)
|
224
|
+
return nil
|
225
|
+
end
|
218
226
|
end
|
219
|
-
end
|
220
227
|
|
221
|
-
|
222
|
-
|
228
|
+
if args.size == 2
|
229
|
+
first, second = args
|
223
230
|
|
224
|
-
|
225
|
-
|
226
|
-
|
231
|
+
if first.is_a?(Numeric) && second.is_a?(Numeric)
|
232
|
+
yxml_element_remove_range(tx, first, second)
|
233
|
+
return nil
|
234
|
+
end
|
227
235
|
end
|
228
|
-
end
|
229
236
|
|
230
|
-
|
237
|
+
raise ArgumentError, "Please check your arguments, can't slice."
|
238
|
+
end
|
231
239
|
end
|
232
240
|
|
233
241
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
@@ -243,12 +251,12 @@ module Y
|
|
243
251
|
#
|
244
252
|
# @return [String]
|
245
253
|
def to_s
|
246
|
-
yxml_element_to_s
|
254
|
+
document.current_transaction { |tx| yxml_element_to_s(tx) }
|
247
255
|
end
|
248
256
|
|
249
257
|
# Detach a listener
|
250
258
|
#
|
251
|
-
# @param [Integer]
|
259
|
+
# @param subscription_id [Integer]
|
252
260
|
# @return [void]
|
253
261
|
def detach(subscription_id)
|
254
262
|
yxml_element_unobserve(subscription_id)
|
@@ -256,10 +264,12 @@ module Y
|
|
256
264
|
|
257
265
|
# Creates a new node and puts it in front of the child list
|
258
266
|
#
|
259
|
-
# @param [String]
|
267
|
+
# @param name [String]
|
260
268
|
# @return [Y::XMLElement]
|
261
269
|
def unshift_child(name)
|
262
|
-
xml_element =
|
270
|
+
xml_element = document.current_transaction do |tx|
|
271
|
+
yxml_element_push_element_front(tx, name)
|
272
|
+
end
|
263
273
|
xml_element.document = document
|
264
274
|
xml_element
|
265
275
|
end
|
@@ -268,12 +278,13 @@ module Y
|
|
268
278
|
#
|
269
279
|
# The optional str argument initializes the text node with its value
|
270
280
|
#
|
271
|
-
# @param [String]
|
281
|
+
# @param str [String]
|
272
282
|
# @return [Y::XMLText]
|
273
|
-
def unshift_text(str =
|
274
|
-
text =
|
283
|
+
def unshift_text(str = "")
|
284
|
+
text = document.current_transaction do |tx|
|
285
|
+
yxml_element_push_text_front(tx, str)
|
286
|
+
end
|
275
287
|
text.document = document
|
276
|
-
text << args.first unless str.nil?
|
277
288
|
text
|
278
289
|
end
|
279
290
|
|
@@ -299,15 +310,20 @@ module Y
|
|
299
310
|
getter = getter.to_s.slice(0...-1)&.to_sym if is_setter
|
300
311
|
|
301
312
|
define_singleton_method(setter.to_sym) do |new_val|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
313
|
+
document.current_transaction do |tx|
|
314
|
+
yxml_element_insert_attribute(tx,
|
315
|
+
method_name.to_s
|
316
|
+
.delete_suffix("=")
|
317
|
+
.delete_prefix("attr_"),
|
318
|
+
new_val)
|
319
|
+
end
|
307
320
|
end
|
308
321
|
|
309
322
|
define_singleton_method(getter) do
|
310
|
-
|
323
|
+
document.current_transaction do |tx|
|
324
|
+
yxml_element_get_attribute(tx,
|
325
|
+
method_name.to_s.delete_prefix("attr_"))
|
326
|
+
end
|
311
327
|
end
|
312
328
|
|
313
329
|
if is_setter
|
@@ -326,131 +342,131 @@ module Y
|
|
326
342
|
method_name.to_s.start_with?("attr_") || super
|
327
343
|
end
|
328
344
|
|
329
|
-
private
|
330
|
-
|
331
345
|
# @!method yxml_element_attributes
|
332
346
|
#
|
333
347
|
# @return [Hash]
|
334
348
|
|
335
|
-
# @!method yxml_element_first_child
|
349
|
+
# @!method yxml_element_first_child(tx)
|
336
350
|
#
|
351
|
+
# @param tx [Y::Transaction]
|
337
352
|
# @return [Y::XMLElement|Y::XMLText]
|
338
353
|
|
339
|
-
# @!method yxml_element_get_attribute(name)
|
354
|
+
# @!method yxml_element_get_attribute(tx, name)
|
340
355
|
#
|
341
|
-
# @param [
|
356
|
+
# @param tx [Y::Transaction]
|
357
|
+
# @param name [String]
|
342
358
|
# @return [String|nil]
|
343
359
|
|
344
|
-
# @!method yxml_element_get(index)
|
360
|
+
# @!method yxml_element_get(tx, index)
|
345
361
|
#
|
346
|
-
# @param [
|
362
|
+
# @param tx [Y::Transaction]
|
363
|
+
# @param index [Integer]
|
347
364
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
348
365
|
|
349
|
-
# @!method yxml_element_insert_attribute(
|
366
|
+
# @!method yxml_element_insert_attribute(tx, name, value)
|
350
367
|
#
|
351
|
-
# @param [Y::Transaction]
|
352
|
-
# @param [String]
|
353
|
-
# @param [String]
|
368
|
+
# @param tx [Y::Transaction]
|
369
|
+
# @param name [String]
|
370
|
+
# @param value [String]
|
354
371
|
# @return [String|nil]
|
355
372
|
|
356
|
-
# @!method yxml_element_insert_element(
|
373
|
+
# @!method yxml_element_insert_element(tx, index, name)
|
357
374
|
# Insert XML element into this XML element
|
375
|
+
#
|
358
376
|
# @!visibility private
|
359
|
-
# @param [Y::Transaction]
|
360
|
-
# @param [Integer]
|
361
|
-
# @param [String]
|
377
|
+
# @param tx [Y::Transaction]
|
378
|
+
# @param index [Integer]
|
379
|
+
# @param name [String]
|
362
380
|
# @return [Y::XMLElement]
|
363
381
|
|
364
|
-
# @!method yxml_element_insert_text(
|
382
|
+
# @!method yxml_element_insert_text(tx, index, text)
|
365
383
|
#
|
366
|
-
# @param [Y::Transaction]
|
367
|
-
# @param [Integer]
|
384
|
+
# @param tx [Y::Transaction]
|
385
|
+
# @param index [Integer]
|
386
|
+
# @param text [String]
|
368
387
|
# @return [Y::XMLText]
|
369
388
|
|
370
|
-
# @!method yxml_element_next_sibling()
|
389
|
+
# @!method yxml_element_next_sibling(tx)
|
371
390
|
#
|
391
|
+
# @param tx [Y::Transaction]
|
372
392
|
# @return [Y::XMLElement|XMLText|nil]
|
373
393
|
|
374
394
|
# @!method yxml_element_observe(callback)
|
375
395
|
#
|
376
|
-
# @param [Proc]
|
396
|
+
# @param callback [Proc]
|
377
397
|
# @return [Integer] The subscription ID
|
378
398
|
|
379
399
|
# @!method yxml_element_parent()
|
380
400
|
#
|
381
401
|
# @return [Y::XMLElement|nil]
|
382
402
|
|
383
|
-
# @!method yxml_element_prev_sibling()
|
403
|
+
# @!method yxml_element_prev_sibling(tx)
|
384
404
|
#
|
405
|
+
# @param tx [Y::Transaction]
|
385
406
|
# @return [Y::XMLElement|XMLText|nil]
|
386
407
|
|
387
|
-
# @!method
|
408
|
+
# @!method yxml_element_push_element_back(tx, name)
|
388
409
|
#
|
389
|
-
# @param [Y::Transaction]
|
390
|
-
# @param [String]
|
410
|
+
# @param tx [Y::Transaction]
|
411
|
+
# @param name [String]
|
391
412
|
# @return [Y::XMLElement]
|
392
413
|
|
393
|
-
# @!method
|
414
|
+
# @!method yxml_element_push_element_front(tx, name)
|
394
415
|
#
|
395
|
-
# @param [Y::Transaction]
|
396
|
-
# @param [String]
|
416
|
+
# @param tx [Y::Transaction]
|
417
|
+
# @param name [String]
|
397
418
|
# @return [Y::XMLElement]
|
398
419
|
|
399
|
-
# @!method yxml_element_push_text_back(
|
420
|
+
# @!method yxml_element_push_text_back(tx, text)
|
400
421
|
#
|
401
|
-
# @param [Y::Transaction]
|
422
|
+
# @param tx [Y::Transaction]
|
423
|
+
# @param text [string]
|
402
424
|
# @return [Y::XMLText]
|
403
425
|
|
404
|
-
# @!method yxml_element_push_text_front(
|
426
|
+
# @!method yxml_element_push_text_front(tx, text)
|
405
427
|
#
|
406
|
-
# @param [Y::Transaction]
|
428
|
+
# @param tx [Y::Transaction]
|
429
|
+
# @param text [string]
|
407
430
|
# @return [Y::XMLText]
|
408
431
|
|
409
|
-
# @!method yxml_element_remove_attribute(
|
410
|
-
#
|
411
|
-
# @param [Y::Transaction] transaction
|
412
|
-
# @param [String] name
|
432
|
+
# @!method yxml_element_remove_attribute(tx, name)
|
413
433
|
#
|
434
|
+
# @param tx [Y::Transaction]
|
435
|
+
# @param name [String] name
|
414
436
|
# @return [void]
|
415
437
|
|
416
|
-
# @!method yxml_element_remove_range(
|
438
|
+
# @!method yxml_element_remove_range(tx, index, length)
|
417
439
|
#
|
418
|
-
# @param [Y::Transaction]
|
419
|
-
# @param [Integer]
|
420
|
-
# @param [Integer]
|
440
|
+
# @param tx [Y::Transaction]
|
441
|
+
# @param index [Integer]
|
442
|
+
# @param length [Integer]
|
421
443
|
#
|
422
444
|
# @return [void]
|
423
445
|
|
424
|
-
# @!method yxml_element_size()
|
446
|
+
# @!method yxml_element_size(tx)
|
425
447
|
#
|
448
|
+
# @param tx [Y::Transaction]
|
426
449
|
# @return [Integer]
|
427
450
|
|
428
|
-
# @!method yxml_element_tag
|
451
|
+
# @!method yxml_element_tag
|
429
452
|
#
|
430
453
|
# @return [String]
|
431
454
|
|
432
|
-
# @!method yxml_element_to_s()
|
455
|
+
# @!method yxml_element_to_s(tx)
|
433
456
|
#
|
457
|
+
# @param tx [Y::Transaction]
|
434
458
|
# @return [String]
|
435
459
|
|
436
460
|
# @!method yxml_element_unobserve(subscription_id)
|
437
461
|
#
|
438
|
-
# @param [Integer]
|
462
|
+
# @param subscription_id [Integer]
|
439
463
|
# @return [void]
|
440
|
-
|
441
|
-
# A reference to the current active transaction of the document this element
|
442
|
-
# belongs to.
|
443
|
-
#
|
444
|
-
# @return [Y::Transaction] A transaction object
|
445
|
-
def transaction
|
446
|
-
document.current_transaction
|
447
|
-
end
|
448
464
|
end
|
449
465
|
|
450
466
|
# A XMLText
|
451
467
|
#
|
452
468
|
# Someone should not instantiate a text directly, but use
|
453
|
-
# {Y::Doc#
|
469
|
+
# {Y::Doc#get_xml_text}, {Y::XMLElement#insert_text},
|
454
470
|
# {Y::XMLElement#push_text}, {Y::XMLElement#unshift_text} instead.
|
455
471
|
#
|
456
472
|
# The XMLText API is similar to {Y::Text}, but adds a few methods to make it
|
@@ -469,7 +485,7 @@ module Y
|
|
469
485
|
|
470
486
|
# Create a new XMLText instance
|
471
487
|
#
|
472
|
-
# @param [Y::Doc]
|
488
|
+
# @param doc [Y::Doc]
|
473
489
|
def initialize(doc = nil)
|
474
490
|
@document = doc || Y::Doc.new
|
475
491
|
|
@@ -478,17 +494,17 @@ module Y
|
|
478
494
|
|
479
495
|
# Push a string to the end of the text node
|
480
496
|
#
|
481
|
-
# @param [String]
|
497
|
+
# @param str [String]
|
482
498
|
# @return {void}
|
483
499
|
def <<(str)
|
484
|
-
yxml_text_push(
|
500
|
+
document.current_transaction { |tx| yxml_text_push(tx, str) }
|
485
501
|
end
|
486
502
|
|
487
503
|
alias push <<
|
488
504
|
|
489
505
|
# Attach a listener to get notified about changes
|
490
506
|
#
|
491
|
-
# @param [Proc]
|
507
|
+
# @param callback [Proc]
|
492
508
|
# @return [Integer] subscription_id
|
493
509
|
def attach(callback = nil, &block)
|
494
510
|
yxml_text_observe(callback) unless callback.nil?
|
@@ -499,12 +515,12 @@ module Y
|
|
499
515
|
#
|
500
516
|
# @return [Hash]
|
501
517
|
def attrs
|
502
|
-
yxml_text_attributes
|
518
|
+
document.current_transaction { |tx| yxml_text_attributes(tx) }
|
503
519
|
end
|
504
520
|
|
505
521
|
# Detach a listener
|
506
522
|
#
|
507
|
-
# @param [Integer]
|
523
|
+
# @param subscription_id [Integer]
|
508
524
|
# @return [void]
|
509
525
|
def detach(subscription_id)
|
510
526
|
yxml_text_unobserve(subscription_id)
|
@@ -512,15 +528,17 @@ module Y
|
|
512
528
|
|
513
529
|
# Format text
|
514
530
|
#
|
515
|
-
# @param [Integer]
|
516
|
-
# @param [Integer]
|
517
|
-
# @param [Hash]
|
531
|
+
# @param index [Integer]
|
532
|
+
# @param length [Integer]
|
533
|
+
# @param attrs [Hash]
|
518
534
|
# @return [void]
|
519
535
|
def format(index, length, attrs)
|
520
|
-
|
536
|
+
document.current_transaction do |tx|
|
537
|
+
yxml_text_format(tx, index, length, attrs)
|
538
|
+
end
|
521
539
|
end
|
522
540
|
|
523
|
-
# rubocop:disable Metrics/
|
541
|
+
# rubocop:disable Metrics/MethodLength
|
524
542
|
|
525
543
|
# Insert a value at position and with optional attributes. This method is
|
526
544
|
# similar to [String#insert](https://ruby-doc.org/core-3.1.2/String.html),
|
@@ -542,42 +560,44 @@ module Y
|
|
542
560
|
# - Array (where element types must be supported)
|
543
561
|
# - Hash (where the the types of key and values must be supported)
|
544
562
|
#
|
545
|
-
# @param [Integer]
|
546
|
-
# @param [String, Float, Array, Hash, Boolean]
|
547
|
-
# @param [Hash|nil]
|
563
|
+
# @param index [Integer]
|
564
|
+
# @param value [String, Float, Integer, Array, Hash, Boolean]
|
565
|
+
# @param attrs [Hash|nil]
|
548
566
|
# @return [void]
|
549
567
|
def insert(index, value, attrs = nil)
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
568
|
+
document.current_transaction do |tx|
|
569
|
+
if value.is_a?(String)
|
570
|
+
yxml_text_insert(tx, index, value) if attrs.nil?
|
571
|
+
unless attrs.nil?
|
572
|
+
yxml_text_insert_with_attrs(tx, index, value,
|
573
|
+
attrs)
|
574
|
+
end
|
575
|
+
|
576
|
+
return nil
|
555
577
|
end
|
556
578
|
|
557
|
-
|
558
|
-
|
579
|
+
if can_insert?(value)
|
580
|
+
yxml_text_insert_embed(tx, index, value) if attrs.nil?
|
581
|
+
unless attrs.nil?
|
582
|
+
yxml_text_insert_embed_with_attrs(tx, index, value,
|
583
|
+
attrs)
|
584
|
+
end
|
559
585
|
|
560
|
-
|
561
|
-
yxml_text_insert_embed(transaction, index, value) if attrs.nil?
|
562
|
-
unless attrs.nil?
|
563
|
-
yxml_text_insert_embed_with_attrs(transaction, index, value,
|
564
|
-
attrs)
|
586
|
+
return nil
|
565
587
|
end
|
566
588
|
|
567
|
-
|
589
|
+
raise ArgumentError,
|
590
|
+
"Can't insert value. `#{value.class.name}` isn't supported."
|
568
591
|
end
|
569
|
-
|
570
|
-
raise ArgumentError,
|
571
|
-
"Can't insert value. `#{value.class.name}` isn't supported."
|
572
592
|
end
|
573
593
|
|
574
|
-
# rubocop:enable Metrics/
|
594
|
+
# rubocop:enable Metrics/MethodLength
|
575
595
|
|
576
596
|
# Return length of string
|
577
597
|
#
|
578
598
|
# @return [void]
|
579
599
|
def length
|
580
|
-
yxml_text_length
|
600
|
+
document.current_transaction { |tx| yxml_text_length(tx) }
|
581
601
|
end
|
582
602
|
|
583
603
|
alias size length
|
@@ -586,7 +606,7 @@ module Y
|
|
586
606
|
#
|
587
607
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
588
608
|
def next_sibling
|
589
|
-
node = yxml_text_next_sibling
|
609
|
+
node = document.current_transaction { |tx| yxml_text_next_sibling(tx) }
|
590
610
|
node.document = document
|
591
611
|
node
|
592
612
|
end
|
@@ -604,7 +624,7 @@ module Y
|
|
604
624
|
#
|
605
625
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
606
626
|
def prev_sibling
|
607
|
-
node = yxml_text_prev_sibling
|
627
|
+
node = document.current_transaction { |tx| yxml_text_prev_sibling(tx) }
|
608
628
|
node&.document = document
|
609
629
|
node
|
610
630
|
end
|
@@ -661,35 +681,37 @@ module Y
|
|
661
681
|
#
|
662
682
|
# @return [void]
|
663
683
|
def slice!(*args)
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
684
|
+
document.current_transaction do |tx|
|
685
|
+
if args.size.zero?
|
686
|
+
raise ArgumentError,
|
687
|
+
"Provide one of `index`, `range`, `start, length` as arguments"
|
688
|
+
end
|
668
689
|
|
669
|
-
|
670
|
-
|
690
|
+
if args.size == 1
|
691
|
+
arg = args.first
|
671
692
|
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
693
|
+
if arg.is_a?(Range)
|
694
|
+
yxml_text_remove_range(tx, arg.first, arg.last - arg.first)
|
695
|
+
return nil
|
696
|
+
end
|
676
697
|
|
677
|
-
|
678
|
-
|
679
|
-
|
698
|
+
if arg.is_a?(Numeric)
|
699
|
+
yxml_text_remove_range(tx, arg.to_int, 1)
|
700
|
+
return nil
|
701
|
+
end
|
680
702
|
end
|
681
|
-
end
|
682
703
|
|
683
|
-
|
684
|
-
|
704
|
+
if args.size == 2
|
705
|
+
first, second = args
|
685
706
|
|
686
|
-
|
687
|
-
|
688
|
-
|
707
|
+
if first.is_a?(Numeric) && second.is_a?(Numeric)
|
708
|
+
yxml_text_remove_range(tx, first, second)
|
709
|
+
return nil
|
710
|
+
end
|
689
711
|
end
|
690
|
-
end
|
691
712
|
|
692
|
-
|
713
|
+
raise ArgumentError, "Please check your arguments, can't slice."
|
714
|
+
end
|
693
715
|
end
|
694
716
|
|
695
717
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
@@ -698,7 +720,7 @@ module Y
|
|
698
720
|
#
|
699
721
|
# @return [String]
|
700
722
|
def to_s
|
701
|
-
yxml_text_to_s
|
723
|
+
document.current_transaction { |tx| yxml_text_to_s(tx) }
|
702
724
|
end
|
703
725
|
|
704
726
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
@@ -723,15 +745,19 @@ module Y
|
|
723
745
|
getter = getter.to_s.slice(0...-1).to_sym if is_setter
|
724
746
|
|
725
747
|
define_singleton_method(setter.to_sym) do |new_val|
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
748
|
+
document.current_transaction do |tx|
|
749
|
+
yxml_text_insert_attribute(tx,
|
750
|
+
method_name.to_s
|
751
|
+
.delete_suffix("=")
|
752
|
+
.delete_prefix("attr_"),
|
753
|
+
new_val)
|
754
|
+
end
|
731
755
|
end
|
732
756
|
|
733
757
|
define_singleton_method(getter) do
|
734
|
-
|
758
|
+
document.current_transaction do |tx|
|
759
|
+
yxml_text_get_attribute(tx, method_name.to_s.delete_prefix("attr_"))
|
760
|
+
end
|
735
761
|
end
|
736
762
|
|
737
763
|
if is_setter
|
@@ -765,105 +791,111 @@ module Y
|
|
765
791
|
#
|
766
792
|
# @return [Hash]
|
767
793
|
|
768
|
-
# @!method yxml_text_format(
|
794
|
+
# @!method yxml_text_format(tx, index, length, attrs)
|
769
795
|
#
|
770
|
-
# @param [
|
771
|
-
# @param [Integer]
|
772
|
-
# @param [
|
796
|
+
# @param tx [Y::Transaction]
|
797
|
+
# @param index [Integer]
|
798
|
+
# @param length [Integer]
|
799
|
+
# @param attrs [Hash]
|
773
800
|
# @return [void]
|
774
801
|
|
775
|
-
# @!method yxml_text_get_attribute(name)
|
802
|
+
# @!method yxml_text_get_attribute(tx, name)
|
776
803
|
#
|
777
|
-
# @param [
|
804
|
+
# @param tx [Y::Transaction]
|
805
|
+
# @param name [String]
|
778
806
|
# @return [String|nil]
|
779
807
|
|
780
|
-
# @!method yxml_text_insert(
|
808
|
+
# @!method yxml_text_insert(tx, index, str)
|
781
809
|
#
|
782
|
-
# @param [Y::Transaction]
|
783
|
-
# @param [Integer]
|
784
|
-
# @param [String]
|
810
|
+
# @param tx [Y::Transaction]
|
811
|
+
# @param index [Integer]
|
812
|
+
# @param str [String]
|
785
813
|
# @return [void]
|
786
814
|
|
787
|
-
# @!method yxml_text_insert_attribute(
|
815
|
+
# @!method yxml_text_insert_attribute(tx, name, value)
|
788
816
|
#
|
789
|
-
# @param [Y::Transaction]
|
790
|
-
# @param [String] name
|
791
|
-
# @param [String] value
|
817
|
+
# @param tx [Y::Transaction]
|
818
|
+
# @param name [String] name
|
819
|
+
# @param value [String] value
|
792
820
|
# @return [void]
|
793
821
|
|
794
|
-
# @!method yxml_text_insert_with_attrs(
|
822
|
+
# @!method yxml_text_insert_with_attrs(tx, index, value, attrs)
|
795
823
|
#
|
796
|
-
# @param [Y::Transaction]
|
797
|
-
# @param [Integer]
|
798
|
-
# @param [String]
|
799
|
-
# @param [Hash]
|
824
|
+
# @param tx [Y::Transaction]
|
825
|
+
# @param index [Integer]
|
826
|
+
# @param value [String]
|
827
|
+
# @param attrs [Hash]
|
800
828
|
# @return [void]
|
801
829
|
|
802
|
-
# @!method yxml_text_insert_embed(
|
830
|
+
# @!method yxml_text_insert_embed(tx, index, value)
|
803
831
|
#
|
804
|
-
# @param [Y::Transaction]
|
805
|
-
# @param [Integer]
|
806
|
-
# @param [String]
|
832
|
+
# @param tx [Y::Transaction]
|
833
|
+
# @param index [Integer]
|
834
|
+
# @param value [String]
|
807
835
|
# @return [void]
|
808
836
|
|
809
|
-
# @!method yxml_text_insert_embed_with_attrs(
|
837
|
+
# @!method yxml_text_insert_embed_with_attrs(tx, index, value, attrs)
|
810
838
|
#
|
811
|
-
# @param [Y::Transaction]
|
812
|
-
# @param [Integer]
|
813
|
-
# @param [true|false|Float|Integer|Array|Hash]
|
814
|
-
# @param [Hash]
|
839
|
+
# @param tx [Y::Transaction]
|
840
|
+
# @param index [Integer]
|
841
|
+
# @param value [true|false|Float|Integer|Array|Hash]
|
842
|
+
# @param attrs [Hash]
|
815
843
|
# @return [void]
|
816
844
|
|
817
|
-
# @!method yxml_text_length
|
845
|
+
# @!method yxml_text_length(tx)
|
818
846
|
#
|
847
|
+
# @param tx [Y::Transaction]
|
819
848
|
# @return [Integer]
|
820
849
|
|
821
|
-
# @!method yxml_text_next_sibling
|
850
|
+
# @!method yxml_text_next_sibling(tx)
|
822
851
|
#
|
852
|
+
# @param tx [Y::Transaction]
|
823
853
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
824
854
|
|
825
855
|
# @!method yxml_text_observe(callback)
|
826
856
|
#
|
827
|
-
# @param [Proc]
|
857
|
+
# @param callback [Proc]
|
828
858
|
# @return [Integer] A subscription ID
|
829
859
|
|
830
860
|
# @!method yxml_text_parent
|
831
861
|
#
|
832
862
|
# @return [Y::XMLElement|nil]
|
833
863
|
|
834
|
-
# @!method yxml_text_prev_sibling
|
864
|
+
# @!method yxml_text_prev_sibling(tx)
|
835
865
|
#
|
866
|
+
# @param tx [Y::Transaction]
|
836
867
|
# @return [Y::XMLElement|Y::XMLText|nil]
|
837
868
|
|
838
|
-
# @!method yxml_text_push(
|
869
|
+
# @!method yxml_text_push(tx, str)
|
839
870
|
#
|
840
|
-
# @param [Y::Transaction]
|
841
|
-
# @param [String]
|
871
|
+
# @param tx [Y::Transaction]
|
872
|
+
# @param str [String]
|
842
873
|
# @return [void]
|
843
874
|
|
844
|
-
# @!method yxml_text_remove_range(
|
875
|
+
# @!method yxml_text_remove_range(tx, index, length)
|
845
876
|
#
|
846
|
-
# @param [Y::Transaction]
|
847
|
-
# @param [Integer]
|
848
|
-
# @param [Integer]
|
877
|
+
# @param tx [Y::Transaction]
|
878
|
+
# @param index [Integer]
|
879
|
+
# @param length [Integer]
|
849
880
|
# @return [void]
|
850
881
|
|
851
|
-
# @!method yxml_text_to_s()
|
882
|
+
# @!method yxml_text_to_s(tx)
|
852
883
|
#
|
884
|
+
# @param tx [Y::Transaction]
|
853
885
|
# @return [void]
|
854
886
|
|
855
887
|
# @!method yxml_text_unobserve(subscription_id)
|
856
888
|
#
|
857
|
-
# @param [Integer]
|
889
|
+
# @param subscription_id [Integer]
|
858
890
|
# @return [void]
|
891
|
+
end
|
859
892
|
|
860
|
-
|
861
|
-
|
893
|
+
# @!visibility private
|
894
|
+
class XMLFragment
|
895
|
+
# @!attribute [r] document
|
862
896
|
#
|
863
|
-
# @return [Y::
|
864
|
-
|
865
|
-
document.current_transaction
|
866
|
-
end
|
897
|
+
# @return [Y::Doc] The document this array belongs to
|
898
|
+
attr_accessor :document
|
867
899
|
end
|
868
900
|
|
869
901
|
# rubocop:enable Metrics/ClassLength
|