diff --git a/README.md b/README.md index 84c1aa6..5d011a2 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,24 @@ puts bidifier.apply('
input stringified html
') 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. diff --git a/lib/bidify/bidifier.rb b/lib/bidify/bidifier.rb index b32f7d9..aceaa55 100644 --- a/lib/bidify/bidifier.rb +++ b/lib/bidify/bidifier.rb @@ -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 = {}) diff --git a/spec/html_string_bidifier_spec.rb b/spec/html_string_bidifier_spec.rb index 804bdef..f45b3ec 100644 --- a/spec/html_string_bidifier_spec.rb +++ b/spec/html_string_bidifier_spec.rb @@ -258,4 +258,72 @@ expect(actual_output).to eq expected_output end + + it 'only bidifies specified tags using `only_tags` option' do + input = <<~HTML +

راست left

+
left راست
+ HTML + + expected_output = <<~HTML +

راست left

+
left راست
+ 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 +

راست left

+ left راست + HTML + + expected_output = <<~HTML +

راست left

+ left راست + 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 +

راست left

+
left راست
+ HTML + + expected_output = <<~HTML +

راست left

+
left راست
+ 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 +

راست left

+ left راست + HTML + + expected_output = <<~HTML +

راست left

+ left راست + 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