Class: Contrast::Utils::TagUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/tag_util.rb

Overview

Utility methods for working with tag ranges

Class Method Summary collapse

Class Method Details

.covered?(remaining_ranges, ranges) ⇒ Boolean

Determine if the given array of tags is covered by the other

Parameters:

  • remaining_ranges (Array<Contrast::Agent::Assess::Tag>)

    the tags left that haven’t been covered by those given

  • ranges

    Array<Contrast::Agent::Assess::Tag> the tags that are covering the first

Returns:

  • (Boolean)


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
# File 'lib/contrast/utils/tag_util.rb', line 14

def covered? remaining_ranges, ranges
  return true unless remaining_ranges&.any?

  tag = remaining_ranges[0]
  ranges.each do |range|
    # If we covered the tag before using all the ranges, break
    break if tag.length <= 0

    relationship = tag.compare_range(range.start_idx, range.end_idx)
    case relationship
      # since the tags are ordered, if we're below, nope out
    when Contrast::Agent::Assess::Tag::BELOW,
        # if we ever get a low span, that means a low part
        # won't be covered. there's no need to continue
        Contrast::Agent::Assess::Tag::LOW_SPAN,
        # if we ever get a without, that means a low part won't
        # be covered. there's no need to continue
        Contrast::Agent::Assess::Tag::WITHOUT

      return false
    when Contrast::Agent::Assess::Tag::WITHIN
      # if we're within, then 0 out this tag since it is
      # completely covered
      tag.update_start(0)
      tag.update_end(0)
    when Contrast::Agent::Assess::Tag::HIGH_SPAN
      tag.update_start(range.end_idx)
    when Contrast::Agent::Assess::Tag::ABOVE
      # The tag's above where we are, it doesn't cover us but a later
      # one may
    end
  end
  return false unless tag.length <= 0

  remaining_ranges.shift
  covered?(remaining_ranges, ranges)
end

.merge_tags(tags) ⇒ Hash{String => Array<Contrast::Agent::Assess::Tag>}, Array<Contrast::Agent::Assess::Tag>

Given a collection of tags, merge any tags that are continuous

If tags is a hash, it should be in the format label => [tags]. The array of tags will each be merged If tags is an array in the format [tags], the array will be merged

The original object is returned, although setters should not be necessary since tags is a collection in either case



87
88
89
90
91
92
93
# File 'lib/contrast/utils/tag_util.rb', line 87

def merge_tags tags
  if tags.is_a?(Hash)
    tags.each_value { |value| smallerize(value) }
  else
    smallerize(tags)
  end
end

.ordered_merge(arr, add_arr) ⇒ Object

Given an array of tags, add all new tags to that array

The addition is done such that the new entry(ies) are inserted so that the range they cover is in order Any overlapping ranges are merged before returning

arr: the array of tags to which we are adding add_arr: array of Tags or a single Tag to be added



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/contrast/utils/tag_util.rb', line 60

def ordered_merge arr, add_arr
  # [Contrast::Agent::Assess::Tag, ...]
  if add_arr.is_a?(Array)
    return arr unless add_arr.any?
    return add_arr unless arr&.any?

    add_arr.each { |new_element| single_ordered_merge(arr, new_element) }
  # Contrast::Agent::Assess::Tag
  else
    return arr unless add_arr
    return [add_arr] unless arr

    single_ordered_merge(arr, add_arr)
  end
  smallerize(arr)
end

.size_aware_merge(target_object, tags) ⇒ Hash{String => Array<Contrast::Agent::Assess::Tag>}, Array<Contrast::Agent::Assess::Tag>

Merge the given set of tags such that any overlap combines. For any tag which extends beyond the size of the target_object, the end will be updated to the target_object’s length.

Parameters:

Returns:



101
102
103
104
105
106
107
# File 'lib/contrast/utils/tag_util.rb', line 101

def size_aware_merge target_object, tags
  max_size = target_object.to_s.length
  tags = merge_tags(tags)
  tags.each do |tag|
    tag.update_end(max_size) if tag.extends_beyond_string_size?(max_size)
  end
end