nokodiff 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e12bce0257af1ebff2c6de595dce5d68cefc73feb5ae4a419a98a81d9dbac212
4
- data.tar.gz: 9479aa3c4ae7f3481a0a96baf002735fb02a4848530a9fc38565c72118e4a2eb
3
+ metadata.gz: 4804027f53e38322927f18d80b25a0c9cdfa659154e56090ba61e895dbde68f8
4
+ data.tar.gz: 855959815a1284325a50326d962127ff44feee8d16adfd74fd0e7af300b99ab9
5
5
  SHA512:
6
- metadata.gz: 864f165ec8332dbcc0a61cffda45f6649ed00d5483e969ad7d55dc9af2e4acdbe202f54686424ff387f6f36ecd160b3834e89f4d1633a8bf311534b6c09eb0b3
7
- data.tar.gz: f182809b76cf85e8ce23002575fb5c2708e527cd3659628d0ef248fdce79e1581ed6a551f99ca41a16dff7bacf9d67db87579185761052136e3f491d227f8bf6
6
+ metadata.gz: e9484c5b5682e6d8299d1c8d7ff9b38d6a9c53c85ffc15cbef6bdfd234d3bd81df05ba90423924511db20381e44fb70c078cbbef15fdfea631ad5d583647aede
7
+ data.tar.gz: 3563899d45760342ac2a70ad7977b35a8e7bfc5aeb058aba2a69ad567865a6a8de628a4240f687439e2f48e5ef3ddba1d82e0916d72910d89e2b149897fbe659
data/README.md CHANGED
@@ -4,7 +4,7 @@ A Ruby Gem to highlight additions, deletions and character level changes while p
4
4
 
5
5
  It includes functionality to:
6
6
  * Compare two HTML fragments and output diffs with semantic HTML
7
- * Inline character differences highlighting using `<strong>` tagging
7
+ * Inline character differences highlighting using `<span class="diff-marker">` tagging
8
8
  * Blocks of added or removed content wrapped in aria labelled `<ins>` and `<del>` tags
9
9
  * Optional CSS for styling the visual differences
10
10
 
@@ -48,7 +48,7 @@ In your application.scss file include:
48
48
  @import "nokodiff";
49
49
  ```
50
50
 
51
- This will include the styling for `<del>`, `<ins>` and `<strong>` tags to allow colour coding, highlighting and underlining of changes.
51
+ This will include the styling for `<del>`, `<ins>` and `<span>` tags to allow colour coding, highlighting and underlining of changes.
52
52
 
53
53
  ### More complex diffing with `data-diff-key`
54
54
 
@@ -139,10 +139,10 @@ Output:
139
139
  <div data-diff-key="ixn4"><p>First paragraph</p></div>
140
140
  <div data-diff-key="zm7q">
141
141
  <div class="diff">
142
- <del aria-label="removed content"><p><strong>S</strong>e<strong>cond</strong> paragraph</p></del>
142
+ <del aria-label="removed content"><p><span class="diff-marker">S</span>e<span class="diff-marker">cond</span> paragraph</p></del>
143
143
  </div>
144
144
  <div class="diff">
145
- <ins aria-label="added content"><p><strong>N</strong>e<strong>w</strong> paragraph</p></ins>
145
+ <ins aria-label="added content"><p><span class="diff-marker">N</span>e<span class="diff-marker">w</span> paragraph</p></ins>
146
146
  </div>
147
147
  </div>
148
148
  </div>
@@ -1,9 +1,9 @@
1
1
  // stylelint-disable selector-no-qualifying-type, max-nesting-depth
2
2
  // Diff of two editions
3
3
  $added-color: #e6fff3;
4
- $strong-added-color: #99ffcf;
4
+ $diff-marker-added-color: #99ffcf;
5
5
  $removed-color: #fadede;
6
- $strong-removed-color: #f3aeac;
6
+ $diff-marker-removed-color: #f3aeac;
7
7
  $text-colour: #0b0c0c;
8
8
  $light-grey: #f3f2f1;
9
9
  $black: #0b0c0c;
@@ -33,9 +33,9 @@ $black: #0b0c0c;
33
33
  background-color: $removed-color;
34
34
  padding-bottom: 2px;
35
35
 
36
- strong {
36
+ span.diff-marker {
37
37
  font-weight: normal;
38
- background-color: $strong-removed-color;
38
+ background-color: $diff-marker-removed-color;
39
39
  border-bottom: 2px dashed $black;
40
40
  }
41
41
  }
@@ -44,9 +44,9 @@ $black: #0b0c0c;
44
44
  background-color: $added-color;
45
45
  padding-bottom: 2px;
46
46
 
47
- strong {
47
+ span.diff-marker {
48
48
  font-weight: normal;
49
- background-color: $strong-added-color;
49
+ background-color: $diff-marker-added-color;
50
50
  border-bottom: 2px dashed $black;
51
51
  }
52
52
  }
@@ -63,13 +63,13 @@ $black: #0b0c0c;
63
63
 
64
64
  del::before {
65
65
  color: $text-colour;
66
- background-color: $strong-removed-color;
66
+ background-color: $diff-marker-removed-color;
67
67
  content: "−";
68
68
  }
69
69
 
70
70
  ins::before {
71
71
  color: $text-colour;
72
- background-color: $strong-added-color;
72
+ background-color: $diff-marker-added-color;
73
73
  content: "+";
74
74
  }
75
- }
75
+ }
@@ -43,18 +43,18 @@ module Nokodiff
43
43
  append_accumulated_text(before_fragment, accumulated_before_text)
44
44
  append_accumulated_text(after_fragment, accumulated_after_text)
45
45
 
46
- before_fragment.add_child(wrap_in_strong(change.old_element, before_fragment))
47
- after_fragment.add_child(wrap_in_strong(change.new_element, after_fragment))
46
+ before_fragment.add_child(highlight_changes(change.old_element, before_fragment))
47
+ after_fragment.add_child(highlight_changes(change.new_element, after_fragment))
48
48
  end
49
49
 
50
50
  def emphasise_deletion(change)
51
51
  append_accumulated_text(before_fragment, accumulated_before_text)
52
- before_fragment.add_child(wrap_in_strong(change.old_element, before_fragment))
52
+ before_fragment.add_child(highlight_changes(change.old_element, before_fragment))
53
53
  end
54
54
 
55
55
  def emphasise_addition(change)
56
56
  append_accumulated_text(after_fragment, accumulated_after_text)
57
- after_fragment.add_child(wrap_in_strong(change.new_element, after_fragment))
57
+ after_fragment.add_child(highlight_changes(change.new_element, after_fragment))
58
58
  end
59
59
 
60
60
  def append_accumulated_text(fragment, accumulated_text)
@@ -42,8 +42,8 @@ module Nokodiff
42
42
 
43
43
  before_fragment, after_fragment = Nokodiff::TextNodeDiffs.new(before_dup, after_dup).call
44
44
 
45
- merge_adjacent_strong_tags(before_fragment)
46
- merge_adjacent_strong_tags(after_fragment)
45
+ merge_adjacent_highlighted_changes(before_fragment)
46
+ merge_adjacent_highlighted_changes(after_fragment)
47
47
 
48
48
  [before_fragment.to_html, after_fragment.to_html]
49
49
  end
@@ -62,24 +62,28 @@ module Nokodiff
62
62
  end
63
63
  end
64
64
 
65
- def merge_adjacent_strong_tags(node)
65
+ def merge_adjacent_highlighted_changes(node)
66
66
  return unless node.element?
67
67
 
68
68
  node.children.each do |child|
69
- merge_adjacent_strong_tags(child) if child.element?
69
+ merge_adjacent_highlighted_changes(child) if child.element?
70
70
  end
71
71
 
72
72
  node.children.each_cons(2) do |left, right|
73
- next unless left.name == "strong" && right.name == "strong"
73
+ next unless node_is_a_change?(left) && node_is_a_change?(right)
74
74
 
75
75
  left.content = left.content + right.content
76
76
  right.remove
77
77
 
78
- merge_adjacent_strong_tags(node)
78
+ merge_adjacent_highlighted_changes(node)
79
79
  break
80
80
  end
81
81
  end
82
82
 
83
+ def node_is_a_change?(node)
84
+ node.name == "span" && node["class"] == "diff-marker"
85
+ end
86
+
83
87
  def unchanged_block(html)
84
88
  html.to_s
85
89
  end
@@ -1,11 +1,14 @@
1
1
  module Nokodiff
2
2
  module FormattingHelpers
3
- def wrap_in_strong(char, fragment)
4
- Nokogiri::XML::Node.new("strong", fragment.document).tap { |n| n.content = char }
3
+ def highlight_changes(char, fragment)
4
+ Nokogiri::XML::Node.new("span", fragment.document).tap do |n|
5
+ n.content = char
6
+ n["class"] = "diff-marker"
7
+ end
5
8
  end
6
9
 
7
- def strong(text_node)
8
- text_node.replace(wrap_in_strong(text_node.to_html, text_node.parent))
10
+ def highlighted_change(text_node)
11
+ text_node.replace(highlight_changes(text_node.to_html, text_node.parent))
9
12
  end
10
13
  end
11
14
  end
@@ -31,8 +31,8 @@ module Nokodiff
31
31
  end
32
32
 
33
33
  def diff_text_node_content(before_text_node, after_text_node)
34
- return strong(before_text_node) if text_removed?(before_text_node, after_text_node)
35
- return strong(after_text_node) if text_added?(before_text_node, after_text_node)
34
+ return highlighted_change(before_text_node) if text_removed?(before_text_node, after_text_node)
35
+ return highlighted_change(after_text_node) if text_added?(before_text_node, after_text_node)
36
36
 
37
37
  before_chars = before_text_node.text.chars
38
38
  after_chars = after_text_node.text.chars
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nokodiff
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokodiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev