brakeman 4.10.0 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +46 -0
  3. data/README.md +11 -2
  4. data/bundle/load.rb +5 -3
  5. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/CHANGELOG.md +16 -0
  6. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/FAQ.md +0 -0
  7. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/Gemfile +1 -4
  8. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/MIT-LICENSE +0 -0
  9. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/README.md +2 -3
  10. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/REFERENCE.md +29 -7
  11. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/TODO +0 -0
  12. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/haml.gemspec +2 -1
  13. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml.rb +0 -0
  14. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_builder.rb +3 -3
  15. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_compiler.rb +42 -31
  16. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_parser.rb +0 -0
  17. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/buffer.rb +0 -0
  18. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/compiler.rb +0 -0
  19. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/engine.rb +0 -0
  20. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/error.rb +0 -0
  21. data/bundle/ruby/2.7.0/gems/haml-5.2.1/lib/haml/escapable.rb +77 -0
  22. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/exec.rb +0 -0
  23. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/filters.rb +0 -0
  24. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/generator.rb +0 -0
  25. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers.rb +7 -1
  26. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_extensions.rb +0 -0
  27. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_mods.rb +0 -0
  28. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_xss_mods.rb +0 -0
  29. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/safe_erubi_template.rb +0 -0
  30. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/safe_erubis_template.rb +0 -0
  31. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/xss_mods.rb +6 -3
  32. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/options.rb +0 -0
  33. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/parser.rb +32 -4
  34. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/plugin.rb +0 -0
  35. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/railtie.rb +0 -0
  36. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/sass_rails_filter.rb +0 -0
  37. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/template.rb +0 -0
  38. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/template/options.rb +0 -0
  39. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/temple_engine.rb +0 -0
  40. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/temple_line_counter.rb +0 -0
  41. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/util.rb +1 -1
  42. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/version.rb +1 -1
  43. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/yard/default/fulldoc/html/css/common.sass +0 -0
  44. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/yard/default/layout/html/footer.erb +0 -0
  45. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/MIT-LICENSE.txt +20 -0
  46. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb +523 -0
  47. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel/processor_count.rb +42 -0
  48. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel/version.rb +3 -0
  49. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/LICENSE.txt +22 -0
  50. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/NEWS.md +178 -0
  51. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/README.md +48 -0
  52. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml.rb +3 -0
  53. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/attlistdecl.rb +63 -0
  54. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/attribute.rb +205 -0
  55. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/cdata.rb +68 -0
  56. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/child.rb +97 -0
  57. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/comment.rb +80 -0
  58. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/doctype.rb +311 -0
  59. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/document.rb +451 -0
  60. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/attlistdecl.rb +11 -0
  61. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/dtd.rb +47 -0
  62. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/elementdecl.rb +18 -0
  63. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/entitydecl.rb +57 -0
  64. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/notationdecl.rb +40 -0
  65. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/element.rb +2599 -0
  66. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/encoding.rb +51 -0
  67. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/entity.rb +171 -0
  68. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/default.rb +116 -0
  69. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/pretty.rb +142 -0
  70. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/transitive.rb +58 -0
  71. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/functions.rb +447 -0
  72. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/instruction.rb +79 -0
  73. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/light/node.rb +188 -0
  74. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/namespace.rb +59 -0
  75. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/node.rb +76 -0
  76. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/output.rb +30 -0
  77. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parent.rb +166 -0
  78. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parseexception.rb +52 -0
  79. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/baseparser.rb +694 -0
  80. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/lightparser.rb +59 -0
  81. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/pullparser.rb +197 -0
  82. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/sax2parser.rb +273 -0
  83. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/streamparser.rb +61 -0
  84. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/treeparser.rb +101 -0
  85. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/ultralightparser.rb +57 -0
  86. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/xpathparser.rb +689 -0
  87. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/quickpath.rb +266 -0
  88. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/rexml.rb +37 -0
  89. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/sax2listener.rb +98 -0
  90. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/security.rb +28 -0
  91. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/source.rb +298 -0
  92. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/streamlistener.rb +93 -0
  93. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/text.rb +424 -0
  94. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/undefinednamespaceexception.rb +9 -0
  95. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/relaxng.rb +539 -0
  96. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/validation.rb +144 -0
  97. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/validationexception.rb +10 -0
  98. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xmldecl.rb +130 -0
  99. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xmltokens.rb +85 -0
  100. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xpath.rb +81 -0
  101. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xpath_parser.rb +974 -0
  102. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/History.rdoc +25 -0
  103. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/Manifest.txt +2 -0
  104. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/README.rdoc +0 -0
  105. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/compare/normalize.rb +2 -2
  106. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/debugging.md +190 -0
  107. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/rp_extensions.rb +0 -0
  108. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/rp_stringscanner.rb +0 -0
  109. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby20_parser.rb +2392 -2384
  110. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby20_parser.y +6 -1
  111. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby21_parser.rb +2553 -2550
  112. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby21_parser.y +6 -1
  113. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby22_parser.rb +2491 -2471
  114. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby22_parser.y +6 -1
  115. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby23_parser.rb +2422 -2403
  116. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby23_parser.y +6 -1
  117. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby24_parser.rb +2460 -2450
  118. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby24_parser.y +6 -1
  119. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby25_parser.rb +2450 -2441
  120. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby25_parser.y +6 -1
  121. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby26_parser.rb +2444 -2433
  122. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby26_parser.y +7 -1
  123. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby27_parser.rb +7310 -0
  124. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby27_parser.y +21 -1
  125. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby30_parser.rb +7310 -0
  126. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby30_parser.y +2677 -0
  127. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rb +19 -0
  128. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rex +1 -1
  129. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rex.rb +1 -1
  130. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser.rb +2 -0
  131. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser.yy +27 -1
  132. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser_extras.rb +2 -2
  133. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/tools/munge.rb +2 -2
  134. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/tools/ripper.rb +0 -0
  135. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/History.rdoc +12 -0
  136. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/Manifest.txt +0 -0
  137. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/README.rdoc +0 -0
  138. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/composite_sexp_processor.rb +0 -0
  139. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/pt_testcase.rb +2 -2
  140. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp.rb +0 -0
  141. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp_matcher.rb +0 -0
  142. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp_processor.rb +1 -1
  143. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/strict_sexp.rb +0 -0
  144. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/unique.rb +0 -0
  145. data/lib/brakeman.rb +21 -4
  146. data/lib/brakeman/app_tree.rb +36 -3
  147. data/lib/brakeman/checks/base_check.rb +7 -1
  148. data/lib/brakeman/checks/check_detailed_exceptions.rb +1 -1
  149. data/lib/brakeman/checks/check_evaluation.rb +1 -1
  150. data/lib/brakeman/checks/check_execute.rb +2 -1
  151. data/lib/brakeman/checks/check_mass_assignment.rb +4 -6
  152. data/lib/brakeman/checks/check_regex_dos.rb +1 -1
  153. data/lib/brakeman/checks/check_sanitize_methods.rb +2 -1
  154. data/lib/brakeman/checks/check_sql.rb +16 -3
  155. data/lib/brakeman/checks/check_unsafe_reflection_methods.rb +68 -0
  156. data/lib/brakeman/checks/check_verb_confusion.rb +75 -0
  157. data/lib/brakeman/file_parser.rb +50 -22
  158. data/lib/brakeman/options.rb +5 -1
  159. data/lib/brakeman/parsers/template_parser.rb +26 -3
  160. data/lib/brakeman/processors/alias_processor.rb +91 -19
  161. data/lib/brakeman/processors/base_processor.rb +4 -4
  162. data/lib/brakeman/processors/controller_alias_processor.rb +6 -43
  163. data/lib/brakeman/processors/controller_processor.rb +1 -1
  164. data/lib/brakeman/processors/haml_template_processor.rb +8 -1
  165. data/lib/brakeman/processors/lib/call_conversion_helper.rb +10 -0
  166. data/lib/brakeman/processors/lib/file_type_detector.rb +64 -0
  167. data/lib/brakeman/processors/lib/rails3_config_processor.rb +16 -16
  168. data/lib/brakeman/processors/lib/rails4_config_processor.rb +2 -1
  169. data/lib/brakeman/processors/library_processor.rb +9 -0
  170. data/lib/brakeman/processors/output_processor.rb +1 -1
  171. data/lib/brakeman/processors/template_alias_processor.rb +5 -0
  172. data/lib/brakeman/report.rb +12 -1
  173. data/lib/brakeman/report/ignore/interactive.rb +1 -1
  174. data/lib/brakeman/report/report_base.rb +0 -2
  175. data/lib/brakeman/report/report_csv.rb +37 -60
  176. data/lib/brakeman/report/report_github.rb +31 -0
  177. data/lib/brakeman/report/report_junit.rb +2 -2
  178. data/lib/brakeman/report/report_sarif.rb +1 -1
  179. data/lib/brakeman/report/report_sonar.rb +38 -0
  180. data/lib/brakeman/report/report_tabs.rb +1 -1
  181. data/lib/brakeman/report/report_text.rb +1 -1
  182. data/lib/brakeman/rescanner.rb +7 -5
  183. data/lib/brakeman/scanner.rb +47 -18
  184. data/lib/brakeman/tracker.rb +39 -4
  185. data/lib/brakeman/tracker/collection.rb +27 -5
  186. data/lib/brakeman/tracker/config.rb +73 -0
  187. data/lib/brakeman/tracker/controller.rb +1 -1
  188. data/lib/brakeman/tracker/method_info.rb +29 -0
  189. data/lib/brakeman/util.rb +17 -4
  190. data/lib/brakeman/version.rb +1 -1
  191. data/lib/brakeman/warning.rb +10 -2
  192. data/lib/brakeman/warning_codes.rb +2 -0
  193. data/lib/ruby_parser/bm_sexp.rb +9 -9
  194. metadata +149 -84
  195. data/bundle/ruby/2.7.0/gems/haml-5.1.2/lib/haml/escapable.rb +0 -50
  196. data/bundle/ruby/2.7.0/gems/ruby_parser-3.15.0/debugging.md +0 -57
  197. data/bundle/ruby/2.7.0/gems/ruby_parser-3.15.0/lib/ruby27_parser.rb +0 -7224
@@ -0,0 +1,974 @@
1
+ # frozen_string_literal: false
2
+
3
+ require "pp"
4
+
5
+ require_relative 'namespace'
6
+ require_relative 'xmltokens'
7
+ require_relative 'attribute'
8
+ require_relative 'parsers/xpathparser'
9
+
10
+ module REXML
11
+ module DClonable
12
+ refine Object do
13
+ # provides a unified +clone+ operation, for REXML::XPathParser
14
+ # to use across multiple Object types
15
+ def dclone
16
+ clone
17
+ end
18
+ end
19
+ refine Symbol do
20
+ # provides a unified +clone+ operation, for REXML::XPathParser
21
+ # to use across multiple Object types
22
+ def dclone ; self ; end
23
+ end
24
+ refine Integer do
25
+ # provides a unified +clone+ operation, for REXML::XPathParser
26
+ # to use across multiple Object types
27
+ def dclone ; self ; end
28
+ end
29
+ refine Float do
30
+ # provides a unified +clone+ operation, for REXML::XPathParser
31
+ # to use across multiple Object types
32
+ def dclone ; self ; end
33
+ end
34
+ refine Array do
35
+ # provides a unified +clone+ operation, for REXML::XPathParser
36
+ # to use across multiple Object+ types
37
+ def dclone
38
+ klone = self.clone
39
+ klone.clear
40
+ self.each{|v| klone << v.dclone}
41
+ klone
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ using REXML::DClonable
48
+
49
+ module REXML
50
+ # You don't want to use this class. Really. Use XPath, which is a wrapper
51
+ # for this class. Believe me. You don't want to poke around in here.
52
+ # There is strange, dark magic at work in this code. Beware. Go back! Go
53
+ # back while you still can!
54
+ class XPathParser
55
+ include XMLTokens
56
+ LITERAL = /^'([^']*)'|^"([^"]*)"/u
57
+
58
+ DEBUG = (ENV["REXML_XPATH_PARSER_DEBUG"] == "true")
59
+
60
+ def initialize(strict: false)
61
+ @debug = DEBUG
62
+ @parser = REXML::Parsers::XPathParser.new
63
+ @namespaces = nil
64
+ @variables = {}
65
+ @nest = 0
66
+ @strict = strict
67
+ end
68
+
69
+ def namespaces=( namespaces={} )
70
+ Functions::namespace_context = namespaces
71
+ @namespaces = namespaces
72
+ end
73
+
74
+ def variables=( vars={} )
75
+ Functions::variables = vars
76
+ @variables = vars
77
+ end
78
+
79
+ def parse path, nodeset
80
+ path_stack = @parser.parse( path )
81
+ match( path_stack, nodeset )
82
+ end
83
+
84
+ def get_first path, nodeset
85
+ path_stack = @parser.parse( path )
86
+ first( path_stack, nodeset )
87
+ end
88
+
89
+ def predicate path, nodeset
90
+ path_stack = @parser.parse( path )
91
+ match( path_stack, nodeset )
92
+ end
93
+
94
+ def []=( variable_name, value )
95
+ @variables[ variable_name ] = value
96
+ end
97
+
98
+
99
+ # Performs a depth-first (document order) XPath search, and returns the
100
+ # first match. This is the fastest, lightest way to return a single result.
101
+ #
102
+ # FIXME: This method is incomplete!
103
+ def first( path_stack, node )
104
+ return nil if path.size == 0
105
+
106
+ case path[0]
107
+ when :document
108
+ # do nothing
109
+ return first( path[1..-1], node )
110
+ when :child
111
+ for c in node.children
112
+ r = first( path[1..-1], c )
113
+ return r if r
114
+ end
115
+ when :qname
116
+ name = path[2]
117
+ if node.name == name
118
+ return node if path.size == 3
119
+ return first( path[3..-1], node )
120
+ else
121
+ return nil
122
+ end
123
+ when :descendant_or_self
124
+ r = first( path[1..-1], node )
125
+ return r if r
126
+ for c in node.children
127
+ r = first( path, c )
128
+ return r if r
129
+ end
130
+ when :node
131
+ return first( path[1..-1], node )
132
+ when :any
133
+ return first( path[1..-1], node )
134
+ end
135
+ return nil
136
+ end
137
+
138
+
139
+ def match(path_stack, nodeset)
140
+ nodeset = nodeset.collect.with_index do |node, i|
141
+ position = i + 1
142
+ XPathNode.new(node, position: position)
143
+ end
144
+ result = expr(path_stack, nodeset)
145
+ case result
146
+ when Array # nodeset
147
+ unnode(result)
148
+ else
149
+ [result]
150
+ end
151
+ end
152
+
153
+ private
154
+ def strict?
155
+ @strict
156
+ end
157
+
158
+ # Returns a String namespace for a node, given a prefix
159
+ # The rules are:
160
+ #
161
+ # 1. Use the supplied namespace mapping first.
162
+ # 2. If no mapping was supplied, use the context node to look up the namespace
163
+ def get_namespace( node, prefix )
164
+ if @namespaces
165
+ return @namespaces[prefix] || ''
166
+ else
167
+ return node.namespace( prefix ) if node.node_type == :element
168
+ return ''
169
+ end
170
+ end
171
+
172
+
173
+ # Expr takes a stack of path elements and a set of nodes (either a Parent
174
+ # or an Array and returns an Array of matching nodes
175
+ def expr( path_stack, nodeset, context=nil )
176
+ enter(:expr, path_stack, nodeset) if @debug
177
+ return nodeset if path_stack.length == 0 || nodeset.length == 0
178
+ while path_stack.length > 0
179
+ trace(:while, path_stack, nodeset) if @debug
180
+ if nodeset.length == 0
181
+ path_stack.clear
182
+ return []
183
+ end
184
+ op = path_stack.shift
185
+ case op
186
+ when :document
187
+ first_raw_node = nodeset.first.raw_node
188
+ nodeset = [XPathNode.new(first_raw_node.root_node, position: 1)]
189
+ when :self
190
+ nodeset = step(path_stack) do
191
+ [nodeset]
192
+ end
193
+ when :child
194
+ nodeset = step(path_stack) do
195
+ child(nodeset)
196
+ end
197
+ when :literal
198
+ trace(:literal, path_stack, nodeset) if @debug
199
+ return path_stack.shift
200
+ when :attribute
201
+ nodeset = step(path_stack, any_type: :attribute) do
202
+ nodesets = []
203
+ nodeset.each do |node|
204
+ raw_node = node.raw_node
205
+ next unless raw_node.node_type == :element
206
+ attributes = raw_node.attributes
207
+ next if attributes.empty?
208
+ nodesets << attributes.each_attribute.collect.with_index do |attribute, i|
209
+ XPathNode.new(attribute, position: i + 1)
210
+ end
211
+ end
212
+ nodesets
213
+ end
214
+ when :namespace
215
+ pre_defined_namespaces = {
216
+ "xml" => "http://www.w3.org/XML/1998/namespace",
217
+ }
218
+ nodeset = step(path_stack, any_type: :namespace) do
219
+ nodesets = []
220
+ nodeset.each do |node|
221
+ raw_node = node.raw_node
222
+ case raw_node.node_type
223
+ when :element
224
+ if @namespaces
225
+ nodesets << pre_defined_namespaces.merge(@namespaces)
226
+ else
227
+ nodesets << pre_defined_namespaces.merge(raw_node.namespaces)
228
+ end
229
+ when :attribute
230
+ if @namespaces
231
+ nodesets << pre_defined_namespaces.merge(@namespaces)
232
+ else
233
+ nodesets << pre_defined_namespaces.merge(raw_node.element.namespaces)
234
+ end
235
+ end
236
+ end
237
+ nodesets
238
+ end
239
+ when :parent
240
+ nodeset = step(path_stack) do
241
+ nodesets = []
242
+ nodeset.each do |node|
243
+ raw_node = node.raw_node
244
+ if raw_node.node_type == :attribute
245
+ parent = raw_node.element
246
+ else
247
+ parent = raw_node.parent
248
+ end
249
+ nodesets << [XPathNode.new(parent, position: 1)] if parent
250
+ end
251
+ nodesets
252
+ end
253
+ when :ancestor
254
+ nodeset = step(path_stack) do
255
+ nodesets = []
256
+ # new_nodes = {}
257
+ nodeset.each do |node|
258
+ raw_node = node.raw_node
259
+ new_nodeset = []
260
+ while raw_node.parent
261
+ raw_node = raw_node.parent
262
+ # next if new_nodes.key?(node)
263
+ new_nodeset << XPathNode.new(raw_node,
264
+ position: new_nodeset.size + 1)
265
+ # new_nodes[node] = true
266
+ end
267
+ nodesets << new_nodeset unless new_nodeset.empty?
268
+ end
269
+ nodesets
270
+ end
271
+ when :ancestor_or_self
272
+ nodeset = step(path_stack) do
273
+ nodesets = []
274
+ # new_nodes = {}
275
+ nodeset.each do |node|
276
+ raw_node = node.raw_node
277
+ next unless raw_node.node_type == :element
278
+ new_nodeset = [XPathNode.new(raw_node, position: 1)]
279
+ # new_nodes[node] = true
280
+ while raw_node.parent
281
+ raw_node = raw_node.parent
282
+ # next if new_nodes.key?(node)
283
+ new_nodeset << XPathNode.new(raw_node,
284
+ position: new_nodeset.size + 1)
285
+ # new_nodes[node] = true
286
+ end
287
+ nodesets << new_nodeset unless new_nodeset.empty?
288
+ end
289
+ nodesets
290
+ end
291
+ when :descendant_or_self
292
+ nodeset = step(path_stack) do
293
+ descendant(nodeset, true)
294
+ end
295
+ when :descendant
296
+ nodeset = step(path_stack) do
297
+ descendant(nodeset, false)
298
+ end
299
+ when :following_sibling
300
+ nodeset = step(path_stack) do
301
+ nodesets = []
302
+ nodeset.each do |node|
303
+ raw_node = node.raw_node
304
+ next unless raw_node.respond_to?(:parent)
305
+ next if raw_node.parent.nil?
306
+ all_siblings = raw_node.parent.children
307
+ current_index = all_siblings.index(raw_node)
308
+ following_siblings = all_siblings[(current_index + 1)..-1]
309
+ next if following_siblings.empty?
310
+ nodesets << following_siblings.collect.with_index do |sibling, i|
311
+ XPathNode.new(sibling, position: i + 1)
312
+ end
313
+ end
314
+ nodesets
315
+ end
316
+ when :preceding_sibling
317
+ nodeset = step(path_stack, order: :reverse) do
318
+ nodesets = []
319
+ nodeset.each do |node|
320
+ raw_node = node.raw_node
321
+ next unless raw_node.respond_to?(:parent)
322
+ next if raw_node.parent.nil?
323
+ all_siblings = raw_node.parent.children
324
+ current_index = all_siblings.index(raw_node)
325
+ preceding_siblings = all_siblings[0, current_index].reverse
326
+ next if preceding_siblings.empty?
327
+ nodesets << preceding_siblings.collect.with_index do |sibling, i|
328
+ XPathNode.new(sibling, position: i + 1)
329
+ end
330
+ end
331
+ nodesets
332
+ end
333
+ when :preceding
334
+ nodeset = step(path_stack, order: :reverse) do
335
+ unnode(nodeset) do |node|
336
+ preceding(node)
337
+ end
338
+ end
339
+ when :following
340
+ nodeset = step(path_stack) do
341
+ unnode(nodeset) do |node|
342
+ following(node)
343
+ end
344
+ end
345
+ when :variable
346
+ var_name = path_stack.shift
347
+ return [@variables[var_name]]
348
+
349
+ when :eq, :neq, :lt, :lteq, :gt, :gteq
350
+ left = expr( path_stack.shift, nodeset.dup, context )
351
+ right = expr( path_stack.shift, nodeset.dup, context )
352
+ res = equality_relational_compare( left, op, right )
353
+ trace(op, left, right, res) if @debug
354
+ return res
355
+
356
+ when :or
357
+ left = expr(path_stack.shift, nodeset.dup, context)
358
+ return true if Functions.boolean(left)
359
+ right = expr(path_stack.shift, nodeset.dup, context)
360
+ return Functions.boolean(right)
361
+
362
+ when :and
363
+ left = expr(path_stack.shift, nodeset.dup, context)
364
+ return false unless Functions.boolean(left)
365
+ right = expr(path_stack.shift, nodeset.dup, context)
366
+ return Functions.boolean(right)
367
+
368
+ when :div, :mod, :mult, :plus, :minus
369
+ left = expr(path_stack.shift, nodeset, context)
370
+ right = expr(path_stack.shift, nodeset, context)
371
+ left = unnode(left) if left.is_a?(Array)
372
+ right = unnode(right) if right.is_a?(Array)
373
+ left = Functions::number(left)
374
+ right = Functions::number(right)
375
+ case op
376
+ when :div
377
+ return left / right
378
+ when :mod
379
+ return left % right
380
+ when :mult
381
+ return left * right
382
+ when :plus
383
+ return left + right
384
+ when :minus
385
+ return left - right
386
+ else
387
+ raise "[BUG] Unexpected operator: <#{op.inspect}>"
388
+ end
389
+ when :union
390
+ left = expr( path_stack.shift, nodeset, context )
391
+ right = expr( path_stack.shift, nodeset, context )
392
+ left = unnode(left) if left.is_a?(Array)
393
+ right = unnode(right) if right.is_a?(Array)
394
+ return (left | right)
395
+ when :neg
396
+ res = expr( path_stack, nodeset, context )
397
+ res = unnode(res) if res.is_a?(Array)
398
+ return -Functions.number(res)
399
+ when :not
400
+ when :function
401
+ func_name = path_stack.shift.tr('-','_')
402
+ arguments = path_stack.shift
403
+
404
+ if nodeset.size != 1
405
+ message = "[BUG] Node set size must be 1 for function call: "
406
+ message += "<#{func_name}>: <#{nodeset.inspect}>: "
407
+ message += "<#{arguments.inspect}>"
408
+ raise message
409
+ end
410
+
411
+ node = nodeset.first
412
+ if context
413
+ target_context = context
414
+ else
415
+ target_context = {:size => nodeset.size}
416
+ if node.is_a?(XPathNode)
417
+ target_context[:node] = node.raw_node
418
+ target_context[:index] = node.position
419
+ else
420
+ target_context[:node] = node
421
+ target_context[:index] = 1
422
+ end
423
+ end
424
+ args = arguments.dclone.collect do |arg|
425
+ result = expr(arg, nodeset, target_context)
426
+ result = unnode(result) if result.is_a?(Array)
427
+ result
428
+ end
429
+ Functions.context = target_context
430
+ return Functions.send(func_name, *args)
431
+
432
+ else
433
+ raise "[BUG] Unexpected path: <#{op.inspect}>: <#{path_stack.inspect}>"
434
+ end
435
+ end # while
436
+ return nodeset
437
+ ensure
438
+ leave(:expr, path_stack, nodeset) if @debug
439
+ end
440
+
441
+ def step(path_stack, any_type: :element, order: :forward)
442
+ nodesets = yield
443
+ begin
444
+ enter(:step, path_stack, nodesets) if @debug
445
+ nodesets = node_test(path_stack, nodesets, any_type: any_type)
446
+ while path_stack[0] == :predicate
447
+ path_stack.shift # :predicate
448
+ predicate_expression = path_stack.shift.dclone
449
+ nodesets = evaluate_predicate(predicate_expression, nodesets)
450
+ end
451
+ if nodesets.size == 1
452
+ ordered_nodeset = nodesets[0]
453
+ else
454
+ raw_nodes = []
455
+ nodesets.each do |nodeset|
456
+ nodeset.each do |node|
457
+ if node.respond_to?(:raw_node)
458
+ raw_nodes << node.raw_node
459
+ else
460
+ raw_nodes << node
461
+ end
462
+ end
463
+ end
464
+ ordered_nodeset = sort(raw_nodes, order)
465
+ end
466
+ new_nodeset = []
467
+ ordered_nodeset.each do |node|
468
+ # TODO: Remove duplicated
469
+ new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
470
+ end
471
+ new_nodeset
472
+ ensure
473
+ leave(:step, path_stack, new_nodeset) if @debug
474
+ end
475
+ end
476
+
477
+ def node_test(path_stack, nodesets, any_type: :element)
478
+ enter(:node_test, path_stack, nodesets) if @debug
479
+ operator = path_stack.shift
480
+ case operator
481
+ when :qname
482
+ prefix = path_stack.shift
483
+ name = path_stack.shift
484
+ new_nodesets = nodesets.collect do |nodeset|
485
+ filter_nodeset(nodeset) do |node|
486
+ raw_node = node.raw_node
487
+ case raw_node.node_type
488
+ when :element
489
+ if prefix.nil?
490
+ raw_node.name == name
491
+ elsif prefix.empty?
492
+ if strict?
493
+ raw_node.name == name and raw_node.namespace == ""
494
+ else
495
+ # FIXME: This DOUBLES the time XPath searches take
496
+ ns = get_namespace(raw_node, prefix)
497
+ raw_node.name == name and raw_node.namespace == ns
498
+ end
499
+ else
500
+ # FIXME: This DOUBLES the time XPath searches take
501
+ ns = get_namespace(raw_node, prefix)
502
+ raw_node.name == name and raw_node.namespace == ns
503
+ end
504
+ when :attribute
505
+ if prefix.nil?
506
+ raw_node.name == name
507
+ elsif prefix.empty?
508
+ raw_node.name == name and raw_node.namespace == ""
509
+ else
510
+ # FIXME: This DOUBLES the time XPath searches take
511
+ ns = get_namespace(raw_node.element, prefix)
512
+ raw_node.name == name and raw_node.namespace == ns
513
+ end
514
+ else
515
+ false
516
+ end
517
+ end
518
+ end
519
+ when :namespace
520
+ prefix = path_stack.shift
521
+ new_nodesets = nodesets.collect do |nodeset|
522
+ filter_nodeset(nodeset) do |node|
523
+ raw_node = node.raw_node
524
+ case raw_node.node_type
525
+ when :element
526
+ namespaces = @namespaces || raw_node.namespaces
527
+ raw_node.namespace == namespaces[prefix]
528
+ when :attribute
529
+ namespaces = @namespaces || raw_node.element.namespaces
530
+ raw_node.namespace == namespaces[prefix]
531
+ else
532
+ false
533
+ end
534
+ end
535
+ end
536
+ when :any
537
+ new_nodesets = nodesets.collect do |nodeset|
538
+ filter_nodeset(nodeset) do |node|
539
+ raw_node = node.raw_node
540
+ raw_node.node_type == any_type
541
+ end
542
+ end
543
+ when :comment
544
+ new_nodesets = nodesets.collect do |nodeset|
545
+ filter_nodeset(nodeset) do |node|
546
+ raw_node = node.raw_node
547
+ raw_node.node_type == :comment
548
+ end
549
+ end
550
+ when :text
551
+ new_nodesets = nodesets.collect do |nodeset|
552
+ filter_nodeset(nodeset) do |node|
553
+ raw_node = node.raw_node
554
+ raw_node.node_type == :text
555
+ end
556
+ end
557
+ when :processing_instruction
558
+ target = path_stack.shift
559
+ new_nodesets = nodesets.collect do |nodeset|
560
+ filter_nodeset(nodeset) do |node|
561
+ raw_node = node.raw_node
562
+ (raw_node.node_type == :processing_instruction) and
563
+ (target.empty? or (raw_node.target == target))
564
+ end
565
+ end
566
+ when :node
567
+ new_nodesets = nodesets.collect do |nodeset|
568
+ filter_nodeset(nodeset) do |node|
569
+ true
570
+ end
571
+ end
572
+ else
573
+ message = "[BUG] Unexpected node test: " +
574
+ "<#{operator.inspect}>: <#{path_stack.inspect}>"
575
+ raise message
576
+ end
577
+ new_nodesets
578
+ ensure
579
+ leave(:node_test, path_stack, new_nodesets) if @debug
580
+ end
581
+
582
+ def filter_nodeset(nodeset)
583
+ new_nodeset = []
584
+ nodeset.each do |node|
585
+ next unless yield(node)
586
+ new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
587
+ end
588
+ new_nodeset
589
+ end
590
+
591
+ def evaluate_predicate(expression, nodesets)
592
+ enter(:predicate, expression, nodesets) if @debug
593
+ new_nodesets = nodesets.collect do |nodeset|
594
+ new_nodeset = []
595
+ subcontext = { :size => nodeset.size }
596
+ nodeset.each_with_index do |node, index|
597
+ if node.is_a?(XPathNode)
598
+ subcontext[:node] = node.raw_node
599
+ subcontext[:index] = node.position
600
+ else
601
+ subcontext[:node] = node
602
+ subcontext[:index] = index + 1
603
+ end
604
+ result = expr(expression.dclone, [node], subcontext)
605
+ trace(:predicate_evaluate, expression, node, subcontext, result) if @debug
606
+ result = result[0] if result.kind_of? Array and result.length == 1
607
+ if result.kind_of? Numeric
608
+ if result == node.position
609
+ new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
610
+ end
611
+ elsif result.instance_of? Array
612
+ if result.size > 0 and result.inject(false) {|k,s| s or k}
613
+ if result.size > 0
614
+ new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
615
+ end
616
+ end
617
+ else
618
+ if result
619
+ new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
620
+ end
621
+ end
622
+ end
623
+ new_nodeset
624
+ end
625
+ new_nodesets
626
+ ensure
627
+ leave(:predicate, new_nodesets) if @debug
628
+ end
629
+
630
+ def trace(*args)
631
+ indent = " " * @nest
632
+ PP.pp(args, "").each_line do |line|
633
+ puts("#{indent}#{line}")
634
+ end
635
+ end
636
+
637
+ def enter(tag, *args)
638
+ trace(:enter, tag, *args)
639
+ @nest += 1
640
+ end
641
+
642
+ def leave(tag, *args)
643
+ @nest -= 1
644
+ trace(:leave, tag, *args)
645
+ end
646
+
647
+ # Reorders an array of nodes so that they are in document order
648
+ # It tries to do this efficiently.
649
+ #
650
+ # FIXME: I need to get rid of this, but the issue is that most of the XPath
651
+ # interpreter functions as a filter, which means that we lose context going
652
+ # in and out of function calls. If I knew what the index of the nodes was,
653
+ # I wouldn't have to do this. Maybe add a document IDX for each node?
654
+ # Problems with mutable documents. Or, rewrite everything.
655
+ def sort(array_of_nodes, order)
656
+ new_arry = []
657
+ array_of_nodes.each { |node|
658
+ node_idx = []
659
+ np = node.node_type == :attribute ? node.element : node
660
+ while np.parent and np.parent.node_type == :element
661
+ node_idx << np.parent.index( np )
662
+ np = np.parent
663
+ end
664
+ new_arry << [ node_idx.reverse, node ]
665
+ }
666
+ ordered = new_arry.sort_by do |index, node|
667
+ if order == :forward
668
+ index
669
+ else
670
+ -index
671
+ end
672
+ end
673
+ ordered.collect do |_index, node|
674
+ node
675
+ end
676
+ end
677
+
678
+ def descendant(nodeset, include_self)
679
+ nodesets = []
680
+ nodeset.each do |node|
681
+ new_nodeset = []
682
+ new_nodes = {}
683
+ descendant_recursive(node.raw_node, new_nodeset, new_nodes, include_self)
684
+ nodesets << new_nodeset unless new_nodeset.empty?
685
+ end
686
+ nodesets
687
+ end
688
+
689
+ def descendant_recursive(raw_node, new_nodeset, new_nodes, include_self)
690
+ if include_self
691
+ return if new_nodes.key?(raw_node)
692
+ new_nodeset << XPathNode.new(raw_node, position: new_nodeset.size + 1)
693
+ new_nodes[raw_node] = true
694
+ end
695
+
696
+ node_type = raw_node.node_type
697
+ if node_type == :element or node_type == :document
698
+ raw_node.children.each do |child|
699
+ descendant_recursive(child, new_nodeset, new_nodes, true)
700
+ end
701
+ end
702
+ end
703
+
704
+ # Builds a nodeset of all of the preceding nodes of the supplied node,
705
+ # in reverse document order
706
+ # preceding:: includes every element in the document that precedes this node,
707
+ # except for ancestors
708
+ def preceding(node)
709
+ ancestors = []
710
+ parent = node.parent
711
+ while parent
712
+ ancestors << parent
713
+ parent = parent.parent
714
+ end
715
+
716
+ precedings = []
717
+ preceding_node = preceding_node_of(node)
718
+ while preceding_node
719
+ if ancestors.include?(preceding_node)
720
+ ancestors.delete(preceding_node)
721
+ else
722
+ precedings << XPathNode.new(preceding_node,
723
+ position: precedings.size + 1)
724
+ end
725
+ preceding_node = preceding_node_of(preceding_node)
726
+ end
727
+ precedings
728
+ end
729
+
730
+ def preceding_node_of( node )
731
+ psn = node.previous_sibling_node
732
+ if psn.nil?
733
+ if node.parent.nil? or node.parent.class == Document
734
+ return nil
735
+ end
736
+ return node.parent
737
+ #psn = preceding_node_of( node.parent )
738
+ end
739
+ while psn and psn.kind_of? Element and psn.children.size > 0
740
+ psn = psn.children[-1]
741
+ end
742
+ psn
743
+ end
744
+
745
+ def following(node)
746
+ followings = []
747
+ following_node = next_sibling_node(node)
748
+ while following_node
749
+ followings << XPathNode.new(following_node,
750
+ position: followings.size + 1)
751
+ following_node = following_node_of(following_node)
752
+ end
753
+ followings
754
+ end
755
+
756
+ def following_node_of( node )
757
+ if node.kind_of? Element and node.children.size > 0
758
+ return node.children[0]
759
+ end
760
+ return next_sibling_node(node)
761
+ end
762
+
763
+ def next_sibling_node(node)
764
+ psn = node.next_sibling_node
765
+ while psn.nil?
766
+ if node.parent.nil? or node.parent.class == Document
767
+ return nil
768
+ end
769
+ node = node.parent
770
+ psn = node.next_sibling_node
771
+ end
772
+ return psn
773
+ end
774
+
775
+ def child(nodeset)
776
+ nodesets = []
777
+ nodeset.each do |node|
778
+ raw_node = node.raw_node
779
+ node_type = raw_node.node_type
780
+ # trace(:child, node_type, node)
781
+ case node_type
782
+ when :element
783
+ nodesets << raw_node.children.collect.with_index do |child_node, i|
784
+ XPathNode.new(child_node, position: i + 1)
785
+ end
786
+ when :document
787
+ new_nodeset = []
788
+ raw_node.children.each do |child|
789
+ case child
790
+ when XMLDecl, Text
791
+ # Ignore
792
+ else
793
+ new_nodeset << XPathNode.new(child, position: new_nodeset.size + 1)
794
+ end
795
+ end
796
+ nodesets << new_nodeset unless new_nodeset.empty?
797
+ end
798
+ end
799
+ nodesets
800
+ end
801
+
802
+ def norm b
803
+ case b
804
+ when true, false
805
+ return b
806
+ when 'true', 'false'
807
+ return Functions::boolean( b )
808
+ when /^\d+(\.\d+)?$/, Numeric
809
+ return Functions::number( b )
810
+ else
811
+ return Functions::string( b )
812
+ end
813
+ end
814
+
815
+ def equality_relational_compare(set1, op, set2)
816
+ set1 = unnode(set1) if set1.is_a?(Array)
817
+ set2 = unnode(set2) if set2.is_a?(Array)
818
+
819
+ if set1.kind_of? Array and set2.kind_of? Array
820
+ # If both objects to be compared are node-sets, then the
821
+ # comparison will be true if and only if there is a node in the
822
+ # first node-set and a node in the second node-set such that the
823
+ # result of performing the comparison on the string-values of
824
+ # the two nodes is true.
825
+ set1.product(set2).any? do |node1, node2|
826
+ node_string1 = Functions.string(node1)
827
+ node_string2 = Functions.string(node2)
828
+ compare(node_string1, op, node_string2)
829
+ end
830
+ elsif set1.kind_of? Array or set2.kind_of? Array
831
+ # If one is nodeset and other is number, compare number to each item
832
+ # in nodeset s.t. number op number(string(item))
833
+ # If one is nodeset and other is string, compare string to each item
834
+ # in nodeset s.t. string op string(item)
835
+ # If one is nodeset and other is boolean, compare boolean to each item
836
+ # in nodeset s.t. boolean op boolean(item)
837
+ if set1.kind_of? Array
838
+ a = set1
839
+ b = set2
840
+ else
841
+ a = set2
842
+ b = set1
843
+ end
844
+
845
+ case b
846
+ when true, false
847
+ each_unnode(a).any? do |unnoded|
848
+ compare(Functions.boolean(unnoded), op, b)
849
+ end
850
+ when Numeric
851
+ each_unnode(a).any? do |unnoded|
852
+ compare(Functions.number(unnoded), op, b)
853
+ end
854
+ when /\A\d+(\.\d+)?\z/
855
+ b = Functions.number(b)
856
+ each_unnode(a).any? do |unnoded|
857
+ compare(Functions.number(unnoded), op, b)
858
+ end
859
+ else
860
+ b = Functions::string(b)
861
+ each_unnode(a).any? do |unnoded|
862
+ compare(Functions::string(unnoded), op, b)
863
+ end
864
+ end
865
+ else
866
+ # If neither is nodeset,
867
+ # If op is = or !=
868
+ # If either boolean, convert to boolean
869
+ # If either number, convert to number
870
+ # Else, convert to string
871
+ # Else
872
+ # Convert both to numbers and compare
873
+ compare(set1, op, set2)
874
+ end
875
+ end
876
+
877
+ def value_type(value)
878
+ case value
879
+ when true, false
880
+ :boolean
881
+ when Numeric
882
+ :number
883
+ when String
884
+ :string
885
+ else
886
+ raise "[BUG] Unexpected value type: <#{value.inspect}>"
887
+ end
888
+ end
889
+
890
+ def normalize_compare_values(a, operator, b)
891
+ a_type = value_type(a)
892
+ b_type = value_type(b)
893
+ case operator
894
+ when :eq, :neq
895
+ if a_type == :boolean or b_type == :boolean
896
+ a = Functions.boolean(a) unless a_type == :boolean
897
+ b = Functions.boolean(b) unless b_type == :boolean
898
+ elsif a_type == :number or b_type == :number
899
+ a = Functions.number(a) unless a_type == :number
900
+ b = Functions.number(b) unless b_type == :number
901
+ else
902
+ a = Functions.string(a) unless a_type == :string
903
+ b = Functions.string(b) unless b_type == :string
904
+ end
905
+ when :lt, :lteq, :gt, :gteq
906
+ a = Functions.number(a) unless a_type == :number
907
+ b = Functions.number(b) unless b_type == :number
908
+ else
909
+ message = "[BUG] Unexpected compare operator: " +
910
+ "<#{operator.inspect}>: <#{a.inspect}>: <#{b.inspect}>"
911
+ raise message
912
+ end
913
+ [a, b]
914
+ end
915
+
916
+ def compare(a, operator, b)
917
+ a, b = normalize_compare_values(a, operator, b)
918
+ case operator
919
+ when :eq
920
+ a == b
921
+ when :neq
922
+ a != b
923
+ when :lt
924
+ a < b
925
+ when :lteq
926
+ a <= b
927
+ when :gt
928
+ a > b
929
+ when :gteq
930
+ a >= b
931
+ else
932
+ message = "[BUG] Unexpected compare operator: " +
933
+ "<#{operator.inspect}>: <#{a.inspect}>: <#{b.inspect}>"
934
+ raise message
935
+ end
936
+ end
937
+
938
+ def each_unnode(nodeset)
939
+ return to_enum(__method__, nodeset) unless block_given?
940
+ nodeset.each do |node|
941
+ if node.is_a?(XPathNode)
942
+ unnoded = node.raw_node
943
+ else
944
+ unnoded = node
945
+ end
946
+ yield(unnoded)
947
+ end
948
+ end
949
+
950
+ def unnode(nodeset)
951
+ each_unnode(nodeset).collect do |unnoded|
952
+ unnoded = yield(unnoded) if block_given?
953
+ unnoded
954
+ end
955
+ end
956
+ end
957
+
958
+ # @private
959
+ class XPathNode
960
+ attr_reader :raw_node, :context
961
+ def initialize(node, context=nil)
962
+ if node.is_a?(XPathNode)
963
+ @raw_node = node.raw_node
964
+ else
965
+ @raw_node = node
966
+ end
967
+ @context = context || {}
968
+ end
969
+
970
+ def position
971
+ @context[:position]
972
+ end
973
+ end
974
+ end