Skip to content

Commit

Permalink
Add feature to stop budification if an element already has dir attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha committed Jul 29, 2023
1 parent 7175978 commit 797c8dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/bidify/bidifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def bidify_recursively(html_node, options = {})
seen_the_first_bidifiable_element = false

html_node.children.each do |child_node|
next if stop_recursion_at?(child_node)

bidify_recursively(child_node)

if (options[:root] || seen_the_first_bidifiable_element) && @bidifiable_tags.include?(child_node.name)
Expand All @@ -42,5 +44,9 @@ def bidify_recursively(html_node, options = {})
def actual_content?(node)
node.element? || (node.text? && !node.blank?)
end

def stop_recursion_at?(node)
node.has_attribute?('dir')
end
end
end
26 changes: 26 additions & 0 deletions spec/html_string_bidifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@

expect(actual_output).to eq expected_output
end

it 'stops recursive bidification on an element with explicit dir attribute' do
input = <<~HTML
<div>
<p>Item 1</p>
<div dir="ltr">
<p>Item 2</p>
<p>Item 3</p>
</div>
</div>
HTML

expected_output = <<~HTML
<div dir="auto">
<p>Item 1</p>
<div dir="ltr">
<p>Item 2</p>
<p>Item 3</p>
</div>
</div>
HTML

actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end
end

it 'bidifies a table with :with_table_support option' do
Expand Down

0 comments on commit 797c8dc

Please sign in to comment.