Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to stop budification if an element already has dir attribute #10

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ padding-inline-end: 10px;
border-inline-start: 1px;
```

## Configuration

To use this gem with custom configuration, use the following syntax and pass
options while creating an instance of bidifier:

```rb
options = {
# ...
}

bidifier = Bidify::HtmlStringBidifier.new(options)

puts bidifier.apply('<div>input stringified html</div>')
```

### Options

The following is the list of options with their default values
- `greedy: false`

By default, bidification stops when it reaches an element that has `dir`
attribute. Use `true` to disregard any existing `dir` attributes.
- `with_table_support: false`

Use `true` to add table tags support.

## License

This project is a Free/Libre and Open Source software released under LGPLv3 license.
8 changes: 8 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,11 @@ def bidify_recursively(html_node, options = {})
def actual_content?(node)
node.element? || (node.text? && !node.blank?)
end

def stop_recursion_at?(node)
return false if @options[:greedy] == true

node.has_attribute?('dir')
end
end
end
53 changes: 53 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 All @@ -205,4 +231,31 @@

expect(actual_output).to eq expected_output
end

it 'with `greedy: true` option, it disregard any exisitng 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="auto">
<p>Item 2</p>
<p dir="auto">Item 3</p>
</div>
</div>
HTML

bidifier = Bidify::HtmlStringBidifier.new(greedy: true)
actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end
end
Loading