Skip to content

Commit

Permalink
Feature/options for tags (#12)
Browse files Browse the repository at this point in the history
* Add only_tags option
* Add including_tags option
* Add excluding_tags option
* Update docs
  • Loading branch information
ahangarha committed Jul 29, 2023
1 parent 916cfc5 commit f85592f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,24 @@ puts bidifier.apply('<div>input stringified html</div>')

Available options with their default values are as follows:

- `excluding_tags: []`

Removes tags from the list of bidifiable tags. This option affects the
provided tags in `including_tags` options.

- `including_tags: []`

Adds new tags to the list of bidifiable tags

- `greedy: false`

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

- `only_tags: []`

It sets the bidifiable tags to the given tags.

- `with_table_support: false`

Use `true` to add table tags support.
Expand Down
3 changes: 3 additions & 0 deletions lib/bidify/bidifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def apply
def configure
@bidifiable_tags = DEFAULT_BIDIFIABLE_TAGS.dup
@bidifiable_tags.concat(TABLE_TAGS) if @options[:with_table_support]
@bidifiable_tags.concat(@options[:including_tags]) if @options.key?(:including_tags)
@bidifiable_tags.delete_if { |tag| @options[:excluding_tags]&.include?(tag) }
@bidifiable_tags = @options[:only_tags] if @options.key?(:only_tags)
end

def bidify_recursively(html_node, options = {})
Expand Down
68 changes: 68 additions & 0 deletions spec/html_string_bidifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,72 @@

expect(actual_output).to eq expected_output
end

it 'only bidifies specified tags using `only_tags` option' do
input = <<~HTML
<p>راست left</p>
<div>left راست</div>
HTML

expected_output = <<~HTML
<p dir="auto">راست left</p>
<div>left راست</div>
HTML

bidifier = Bidify::HtmlStringBidifier.new(only_tags: ['p'])
actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end

it 'bidies specified additional tags using `including_tags` option' do
input = <<~HTML
<p>راست left</p>
<xyz>left راست</xyz>
HTML

expected_output = <<~HTML
<p dir="auto">راست left</p>
<xyz dir="auto">left راست</xyz>
HTML

bidifier = Bidify::HtmlStringBidifier.new(including_tags: ['xyz'])
actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end

it "doesn't bidify specified excluded tags using `excluding_tags` option" do
input = <<~HTML
<p>راست left</p>
<div>left راست</div>
HTML

expected_output = <<~HTML
<p dir="auto">راست left</p>
<div>left راست</div>
HTML

bidifier = Bidify::HtmlStringBidifier.new(excluding_tags: ['div'])
actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end

it '`excluding_tags` option overrides `including_tags`' do
input = <<~HTML
<p>راست left</p>
<xyz>left راست</xyz>
HTML

expected_output = <<~HTML
<p dir="auto">راست left</p>
<xyz>left راست</xyz>
HTML

bidifier = Bidify::HtmlStringBidifier.new(including_tags: ['xyz'], excluding_tags: ['xyz'])
actual_output = bidifier.apply(input)

expect(actual_output).to eq expected_output
end
end

0 comments on commit f85592f

Please sign in to comment.