-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcompiler.rb
More file actions
1568 lines (1351 loc) · 46.9 KB
/
Copy pathcompiler.rb
File metadata and controls
1568 lines (1351 loc) · 46.9 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Oga
module XPath
# Compiling of XPath ASTs into Ruby code.
#
# The Compiler class can be used to turn an XPath AST into Ruby source code
# that can be executed to match XML nodes in a given input document/element.
# Compiled source code is cached per expression, removing the need for
# recompiling the same expression over and over again.
#
# @private
class Compiler
# @return [Oga::LRU]
CACHE = LRU.new
# Wildcard for node names/namespace prefixes.
STAR = '*'
# Node types that require a NodeSet to push nodes into.
RETURN_NODESET = [:path, :absolute_path, :axis, :predicate]
# Hash containing all operator callbacks, the conversion methods and the
# Ruby methods to use.
OPERATORS = {
:on_add => [:to_float, :+],
:on_sub => [:to_float, :-],
:on_div => [:to_float, :/],
:on_gt => [:to_float, :>],
:on_gte => [:to_float, :>=],
:on_lt => [:to_float, :<],
:on_lte => [:to_float, :<=],
:on_mul => [:to_float, :*],
:on_mod => [:to_float, :%],
:on_and => [:to_boolean, :and],
:on_or => [:to_boolean, :or]
}
# Compiles and caches an AST.
#
# @see [#compile]
def self.compile_with_cache(ast)
CACHE.get_or_set(ast) { new.compile(ast) }
end
def initialize
reset
end
# Resets the internal state.
def reset
@literal_id = 0
@predicate_nodesets = []
@predicate_indexes = []
end
# Compiles an XPath AST into a Ruby Proc.
#
# @param [AST::Node] ast
# @return [Proc]
def compile(ast)
document = literal(:node)
matched = matched_literal
if return_nodeset?(ast)
ruby_ast = process(ast, document) { |node| matched.push(node) }
else
ruby_ast = process(ast, document)
end
vars = variables_literal.assign(self.nil)
proc_ast = literal(:lambda).add_block(document, vars) do
input_assign = original_input_literal.assign(document)
if return_nodeset?(ast)
body = matched.assign(literal(XML::NodeSet).new)
.followed_by(ruby_ast)
.followed_by(matched)
else
body = ruby_ast
end
input_assign.followed_by(body)
end
generator = Ruby::Generator.new
source = generator.process(proc_ast)
eval(source)
ensure
reset
end
# Processes a single XPath AST node.
#
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def process(ast, input, &block)
send("on_#{ast.type}", ast, input, &block)
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_absolute_path(ast, input, &block)
if ast.children.empty?
matched_literal.push(input.root_node)
else
process(ast.children[0], input.root_node, &block)
end
end
# Dispatches the processing of axes to dedicated methods. This works
# similar to {#process} except the handler names are "on_axis_X" with "X"
# being the axis name.
#
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis(ast, input, &block)
name, test, following = *ast
handler = name.gsub('-', '_')
send(:"on_axis_#{handler}", test, input) do |matched|
process_following_or_yield(following, matched, &block)
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_child(ast, input)
child = unique_literal(:child)
document_or_node(input).if_true do
input.children.each.add_block(child) do
process(ast, child).if_true { yield child }
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_attribute(ast, input)
input.is_a?(XML::Element).if_true do
attribute = unique_literal(:attribute)
input.attributes.each.add_block(attribute) do
name_match = match_name_and_namespace(ast, attribute)
if name_match
name_match.if_true { yield attribute }
else
yield attribute
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_ancestor_or_self(ast, input)
parent = unique_literal(:parent)
process(ast, input).and(input.is_a?(XML::Node))
.if_true { yield input }
.followed_by do
attribute_or_node(input).if_true do
input.each_ancestor.add_block(parent) do
process(ast, parent).if_true { yield parent }
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_ancestor(ast, input)
parent = unique_literal(:parent)
attribute_or_node(input).if_true do
input.each_ancestor.add_block(parent) do
process(ast, parent).if_true { yield parent }
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_descendant_or_self(ast, input)
node = unique_literal(:descendant)
document_or_node(input).if_true do
process(ast, input)
.if_true { yield input }
.followed_by do
input.each_node.add_block(node) do
process(ast, node).if_true { yield node }
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_descendant(ast, input)
node = unique_literal(:descendant)
document_or_node(input).if_true do
input.each_node.add_block(node) do
process(ast, node).if_true { yield node }
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_parent(ast, input)
parent = unique_literal(:parent)
attribute_or_node(input).if_true do
parent.assign(input.parent).followed_by do
process(ast, parent).if_true { yield parent }
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_self(ast, input)
process(ast, input).if_true { yield input }
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_following_sibling(ast, input)
orig_input = original_input_literal
doc_node = literal(:doc_node)
check = literal(:check)
parent = literal(:parent)
root = literal(:root)
orig_input.is_a?(XML::Node)
.if_true { root.assign(orig_input.parent) }
.else { root.assign(orig_input) }
.followed_by do
input.is_a?(XML::Node).and(input.parent)
.if_true { parent.assign(input.parent) }
.else { parent.assign(self.nil) }
end
.followed_by(check.assign(self.false))
.followed_by do
document_or_node(root).if_true do
root.each_node.add_block(doc_node) do
doc_node.eq(input)
.if_true do
check.assign(self.true)
.followed_by(throw_message(:skip_children))
end
.followed_by do
check.not.or(parent != doc_node.parent).if_true do
send_message(:next)
end
end
.followed_by do
process(ast, doc_node).if_true { yield doc_node }
end
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_following(ast, input)
orig_input = original_input_literal
doc_node = literal(:doc_node)
check = literal(:check)
root = literal(:root)
orig_input.is_a?(XML::Node)
.if_true { root.assign(orig_input.root_node) }
.else { root.assign(orig_input) }
.followed_by(check.assign(self.false))
.followed_by do
document_or_node(root).if_true do
root.each_node.add_block(doc_node) do
doc_node.eq(input)
.if_true do
check.assign(self.true)
.followed_by(throw_message(:skip_children))
end
.followed_by do
check.if_false { send_message(:next) }
end
.followed_by do
process(ast, doc_node).if_true { yield doc_node }
end
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_namespace(ast, input)
underscore = literal(:_)
node = unique_literal(:namespace)
name = string(ast.children[1])
star = string(STAR)
input.is_a?(XML::Element).if_true do
input.available_namespaces.each.add_block(underscore, node) do
node.name.eq(name).or(name.eq(star)).if_true { yield node }
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_preceding(ast, input)
root = literal(:root)
doc_node = literal(:doc_node)
input.is_a?(XML::Node).if_true do
root.assign(input.root_node)
.followed_by do
document_or_node(root).if_true do
root.each_node.add_block(doc_node) do
doc_node.eq(input)
.if_true { self.break }
.followed_by do
process(ast, doc_node).if_true { yield doc_node }
end
end
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_axis_preceding_sibling(ast, input)
orig_input = original_input_literal
check = literal(:check)
root = literal(:root)
parent = literal(:parent)
doc_node = literal(:doc_node)
orig_input.is_a?(XML::Node)
.if_true { root.assign(orig_input.parent) }
.else { root.assign(orig_input) }
.followed_by(check.assign(self.false))
.followed_by do
input.is_a?(XML::Node).and(input.parent)
.if_true { parent.assign(input.parent) }
.else { parent.assign(self.nil) }
end
.followed_by do
document_or_node(root).if_true do
root.each_node.add_block(doc_node) do
doc_node.eq(input)
.if_true { self.break }
.followed_by do
doc_node.parent.eq(parent).if_true do
process(ast, doc_node).if_true { yield doc_node }
end
end
end
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_predicate(ast, input, &block)
test, predicate, following = *ast
index_var = unique_literal(:index)
if number?(predicate)
method = :on_predicate_index
elsif has_call_node?(predicate, 'last')
method = :on_predicate_temporary
else
method = :on_predicate_direct
end
@predicate_indexes << index_var
ast = index_var.assign(literal(1)).followed_by do
send(method, input, test, predicate) do |matched|
process_following_or_yield(following, matched, &block)
end
end
@predicate_indexes.pop
ast
end
# Processes a predicate that requires a temporary NodeSet.
#
# @param [Oga::Ruby::Node] input
# @param [AST::Node] test
# @param [AST::Node] predicate
# @return [Oga::Ruby::Node]
def on_predicate_temporary(input, test, predicate)
temp_set = unique_literal(:temp_set)
pred_node = unique_literal(:pred_node)
pred_var = unique_literal(:pred_var)
conversion = literal(Conversion)
index_var = predicate_index
index_step = literal(1)
@predicate_nodesets << temp_set
ast = temp_set.assign(literal(XML::NodeSet).new)
.followed_by do
process(test, input) { |node| temp_set << node }
end
.followed_by do
temp_set.each.add_block(pred_node) do
pred_ast = process(predicate, pred_node)
pred_var.assign(pred_ast)
.followed_by do
pred_var.is_a?(Numeric).if_true do
pred_var.assign(pred_var.to_i.eq(index_var))
end
end
.followed_by do
conversion.to_boolean(pred_var).if_true { yield pred_node }
end
.followed_by do
index_var.assign(index_var + index_step)
end
end
end
@predicate_nodesets.pop
ast
end
# Processes a predicate that doesn't require temporary NodeSet.
#
# @param [Oga::Ruby::Node] input
# @param [AST::Node] test
# @param [AST::Node] predicate
# @return [Oga::Ruby::Node]
def on_predicate_direct(input, test, predicate)
pred_var = unique_literal(:pred_var)
index_var = predicate_index
index_step = literal(1)
conversion = literal(Conversion)
process(test, input) do |matched_test_node|
if return_nodeset?(predicate)
pred_ast = catch_message(:predicate_matched) do
process(predicate, matched_test_node) do
throw_message(:predicate_matched, self.true)
end
end
else
pred_ast = process(predicate, matched_test_node)
end
pred_var.assign(pred_ast)
.followed_by do
pred_var.is_a?(Numeric).if_true do
pred_var.assign(pred_var.to_i.eq(index_var))
end
end
.followed_by do
conversion.to_boolean(pred_var).if_true do
yield matched_test_node
end
end
.followed_by do
index_var.assign(index_var + index_step)
end
end
end
# Processes a predicate that uses a literal index.
#
# @param [Oga::Ruby::Node] input
# @param [AST::Node] test
# @param [AST::Node] predicate
# @return [Oga::Ruby::Node]
def on_predicate_index(input, test, predicate)
index_var = predicate_index
index_step = literal(1)
index = process(predicate, input).to_i
process(test, input) do |matched_test_node|
index_var.eq(index)
.if_true do
yield matched_test_node
end
.followed_by do
index_var.assign(index_var + index_step)
end
end
end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_test(ast, input)
condition = element_or_attribute(input)
name_match = match_name_and_namespace(ast, input)
name_match ? condition.and(name_match) : condition
end
# Processes the `=` operator.
#
# @see [#operator]
def on_eq(ast, input, &block)
conv = literal(Conversion)
operator(ast, input) do |left, right|
mass_assign([left, right], conv.to_compatible_types(left, right))
.followed_by do
operation = left.eq(right)
block ? operation.if_true(&block) : operation
end
end
end
# Processes the `!=` operator.
#
# @see [#operator]
def on_neq(ast, input, &block)
conv = literal(Conversion)
operator(ast, input) do |left, right|
mass_assign([left, right], conv.to_compatible_types(left, right))
.followed_by do
operation = left != right
block ? operation.if_true(&block) : operation
end
end
end
OPERATORS.each do |callback, (conv_method, ruby_method)|
define_method(callback) do |ast, input, &block|
conversion = literal(XPath::Conversion)
operator(ast, input) do |left, right|
lval = conversion.__send__(conv_method, left)
rval = conversion.__send__(conv_method, right)
operation = lval.__send__(ruby_method, rval)
block ? conversion.to_boolean(operation).if_true(&block) : operation
end
end
end
# Processes the `|` operator.
#
# @see [#operator]
def on_pipe(ast, input, &block)
left, right = *ast
union = unique_literal(:union)
conversion = literal(Conversion)
union.assign(literal(XML::NodeSet).new)
.followed_by(process(left, input) { |node| union << node })
.followed_by(process(right, input) { |node| union << node })
.followed_by do
# block present means we're in a predicate
block ? conversion.to_boolean(union).if_true(&block) : union
end
end
# @param [AST::Node] ast
# @return [Oga::Ruby::Node]
def on_string(ast, *)
string(ast.children[0])
end
# @param [AST::Node] ast
# @return [Oga::Ruby::Node]
def on_int(ast, *)
literal(ast.children[0].to_f.to_s)
end
# @param [AST::Node] ast
# @return [Oga::Ruby::Node]
def on_float(ast, *)
literal(ast.children[0].to_s)
end
# @param [AST::Node] ast
# @return [Oga::Ruby::Node]
def on_var(ast, *)
name = ast.children[0]
variables_literal.and(variables_literal[string(name)])
.or(send_message(:raise, string("Undefined XPath variable: #{name}")))
end
# Delegates function calls to specific handlers.
#
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def on_call(ast, input, &block)
name, *args = *ast
handler = name.gsub('-', '_')
send(:"on_call_#{handler}", input, *args, &block)
end
# @return [Oga::Ruby::Node]
def on_call_true(*)
block_given? ? yield : self.true
end
# @return [Oga::Ruby::Node]
def on_call_false(*)
self.false
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_boolean(input, arg)
arg_ast = try_match_first_node(arg, input)
call_arg = unique_literal(:call_arg)
conversion = literal(Conversion)
call_arg.assign(arg_ast).followed_by do
converted = conversion.to_boolean(call_arg)
block_given? ? converted.if_true { yield } : converted
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_ceiling(input, arg)
arg_ast = try_match_first_node(arg, input)
call_arg = unique_literal(:call_arg)
conversion = literal(Conversion)
call_arg.assign(arg_ast)
.followed_by do
call_arg.assign(conversion.to_float(call_arg))
end
.followed_by do
call_arg.nan?
.if_true { call_arg }
.else { block_given? ? yield : call_arg.ceil.to_f }
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_floor(input, arg)
arg_ast = try_match_first_node(arg, input)
call_arg = unique_literal(:call_arg)
conversion = literal(Conversion)
call_arg.assign(arg_ast)
.followed_by do
call_arg.assign(conversion.to_float(call_arg))
end
.followed_by do
call_arg.nan?
.if_true { call_arg }
.else { block_given? ? yield : call_arg.floor.to_f }
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_round(input, arg)
arg_ast = try_match_first_node(arg, input)
call_arg = unique_literal(:call_arg)
conversion = literal(Conversion)
call_arg.assign(arg_ast)
.followed_by do
call_arg.assign(conversion.to_float(call_arg))
end
.followed_by do
call_arg.nan?
.if_true { call_arg }
.else { block_given? ? yield : call_arg.round.to_f }
end
end
# @param [Oga::Ruby::Node] input
# @param [Array<AST::Node>] args
# @return [Oga::Ruby::Node]
def on_call_concat(input, *args)
conversion = literal(Conversion)
assigns = []
conversions = []
args.each do |arg|
arg_var = unique_literal(:concat_arg)
arg_ast = try_match_first_node(arg, input)
assigns << arg_var.assign(arg_ast)
conversions << conversion.to_string(arg_var)
end
concatted = assigns.inject(:followed_by)
.followed_by(conversions.inject(:+))
block_given? ? concatted.empty?.if_false { yield } : concatted
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] haystack
# @param [AST::Node] needle
# @return [Oga::Ruby::Node]
def on_call_contains(input, haystack, needle)
haystack_lit = unique_literal(:haystack)
needle_lit = unique_literal(:needle)
conversion = literal(Conversion)
haystack_lit.assign(try_match_first_node(haystack, input))
.followed_by do
needle_lit.assign(try_match_first_node(needle, input))
end
.followed_by do
converted = conversion.to_string(haystack_lit)
.include?(conversion.to_string(needle_lit))
block_given? ? converted.if_true { yield } : converted
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_count(input, arg)
count = unique_literal(:count)
unless return_nodeset?(arg)
raise TypeError, 'count() can only operate on NodeSet instances'
end
count.assign(literal(0.0))
.followed_by do
process(arg, input) { count.assign(count + literal(1)) }
end
.followed_by do
block_given? ? count.zero?.if_false { yield } : count
end
end
# Processes the `id()` function call.
#
# The XPath specification states that this function's behaviour should be
# controlled by a DTD. If a DTD were to specify that the ID attribute for
# a certain element would be "foo" then this function should use said
# attribute.
#
# Oga does not support DTD parsing/evaluation and as such always uses the
# "id" attribute.
#
# This function searches the entire document for a matching node,
# regardless of the current position.
#
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_id(input, arg)
orig_input = original_input_literal
node = unique_literal(:node)
ids_var = unique_literal('ids')
matched = unique_literal('id_matched')
id_str_var = unique_literal('id_string')
attr_var = unique_literal('attr')
matched.assign(literal(XML::NodeSet).new)
.followed_by do
# When using some sort of path we'll want the text of all matched
# nodes.
if return_nodeset?(arg)
ids_var.assign(literal(:[])).followed_by do
process(arg, input) { |element| ids_var << element.text }
end
# For everything else we'll cast the value to a string and split it
# on every space.
else
conversion = literal(Conversion).to_string(ids_var)
.split(string(' '))
ids_var.assign(process(arg, input))
.followed_by(ids_var.assign(conversion))
end
end
.followed_by do
id_str_var.assign(string('id'))
end
.followed_by do
orig_input.each_node.add_block(node) do
node.is_a?(XML::Element).if_true do
attr_var.assign(node.attribute(id_str_var)).followed_by do
attr_var.and(ids_var.include?(attr_var.value))
.if_true { block_given? ? yield : matched << node }
end
end
end
end
.followed_by(matched)
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_lang(input, arg)
lang_var = unique_literal('lang')
node = unique_literal('node')
found = unique_literal('found')
xml_lang = unique_literal('xml_lang')
matched = unique_literal('matched')
conversion = literal(Conversion)
ast = lang_var.assign(try_match_first_node(arg, input))
.followed_by do
lang_var.assign(conversion.to_string(lang_var))
end
.followed_by do
matched.assign(self.false)
end
.followed_by do
node.assign(input)
end
.followed_by do
xml_lang.assign(string('xml:lang'))
end
.followed_by do
node.respond_to?(symbol(:attribute)).while_true do
found.assign(node.get(xml_lang))
.followed_by do
found.if_true do
found.eq(lang_var)
.if_true do
if block_given?
yield
else
matched.assign(self.true).followed_by(self.break)
end
end
.else { self.break }
end
end
.followed_by(node.assign(node.parent))
end
end
block_given? ? ast : ast.followed_by(matched)
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_local_name(input, arg = nil)
argument_or_first_node(input, arg) do |arg_var|
arg_var
.if_true do
ensure_element_or_attribute(arg_var)
.followed_by { block_given? ? yield : arg_var.name }
end
.else { string('') }
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_name(input, arg = nil)
argument_or_first_node(input, arg) do |arg_var|
arg_var
.if_true do
ensure_element_or_attribute(arg_var)
.followed_by { block_given? ? yield : arg_var.expanded_name }
end
.else { string('') }
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_namespace_uri(input, arg = nil)
default = string('')
argument_or_first_node(input, arg) do |arg_var|
arg_var
.if_true do
ensure_element_or_attribute(arg_var).followed_by do
arg_var.namespace
.if_true { block_given? ? yield : arg_var.namespace.uri }
.else { default } # no yield so predicates aren't matched
end
end
.else { default }
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_normalize_space(input, arg = nil)
conversion = literal(Conversion)
norm_var = unique_literal(:normalized)
find = literal('/\s+/')
replace = string(' ')
argument_or_first_node(input, arg) do |arg_var|
norm_var
.assign(conversion.to_string(arg_var).strip.gsub(find, replace))
.followed_by do
norm_var.empty?
.if_true { string('') }
.else { block_given? ? yield : norm_var }
end
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_not(input, arg)
arg_ast = try_match_first_node(arg, input)
call_arg = unique_literal(:call_arg)
conversion = literal(Conversion)
call_arg.assign(arg_ast).followed_by do
converted = conversion.to_boolean(call_arg).not
block_given? ? converted.if_true { yield } : converted
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] arg
# @return [Oga::Ruby::Node]
def on_call_number(input, arg = nil)
convert_var = unique_literal(:convert)
conversion = literal(Conversion)
argument_or_first_node(input, arg) do |arg_var|
convert_var.assign(conversion.to_float(arg_var)).followed_by do
if block_given?
convert_var.zero?.if_false { yield }
else
convert_var
end
end
end
end
# @param [Oga::Ruby::Node] input
# @param [AST::Node] haystack
# @param [AST::Node] needle
# @return [Oga::Ruby::Node]
def on_call_starts_with(input, haystack, needle)
haystack_var = unique_literal(:haystack)
needle_var = unique_literal(:needle)
conversion = literal(Conversion)