Skip to content

Commit

Permalink
Improve JRuby support
Browse files Browse the repository at this point in the history
JRuby Nokogiri seems to strip `<th>`s without a proper `<table>` parent.
On MRI Nokogiri this does not happen.

A solution is to use a different tag in the transformation process
itself and then end by converting it back to `<th>`.
  • Loading branch information
orhantoy authored and marcandre committed Mar 29, 2019
1 parent 954fed7 commit 208ade8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,8 @@ Metrics/BlockLength:
- 'Rakefile'
- 'spec/**/*.rb'

Style/MutableConstant:
Enabled: false

Style/Encoding:
Enabled: false
1 change: 1 addition & 0 deletions lib/inky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def release_the_kraken(html_string)
html = Nokogiri::HTML.public_send(parse_cmd, str)
transform_doc(html)
string = html.to_html
string.gsub!(INTERIM_TH_TAG_REGEX, 'th')
Inky::Core.re_inject_raws(string, raws)
end

Expand Down
9 changes: 7 additions & 2 deletions lib/inky/component_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def component_factory(elem)
tags = tags.to_set if tags.respond_to? :to_set
IGNORED_ON_PASSTHROUGH = tags.freeze

# These constants are used to circumvent an issue with JRuby Nokogiri.
# For more details see https://github.com/zurb/inky-rb/pull/94
INTERIM_TH_TAG = 'inky-interim-th'.freeze
INTERIM_TH_TAG_REGEX = %r{(?<=\<|\<\/)#{Regexp.escape(INTERIM_TH_TAG)}}

def _pass_through_attributes(elem)
elem.attributes.reject { |e| IGNORED_ON_PASSTHROUGH.include?(e.downcase) }.map do |name, value|
%{#{name}="#{value}" }
Expand Down Expand Up @@ -60,7 +65,7 @@ def _transform_menu(component, inner)
def _transform_menu_item(component, inner)
target = _target_attribute(component)
attributes = _combine_attributes(component, 'menu-item')
%{<th #{attributes}><a href="#{component.attr('href')}"#{target}>#{inner}</a></th>}
%{<#{INTERIM_TH_TAG} #{attributes}><a href="#{component.attr('href')}"#{target}>#{inner}</a></#{INTERIM_TH_TAG}>}
end

def _transform_container(component, inner)
Expand Down Expand Up @@ -91,7 +96,7 @@ def _transform_columns(component, inner)
subrows = component.elements.css(".row").to_a.concat(component.elements.css("row").to_a)
expander = %{<th class="expander"></th>} if large_size.to_i == column_count && subrows.empty?

%{<th class="#{classes}" #{_pass_through_attributes(component)}><table><tr><th>#{inner}</th>#{expander}</tr></table></th>}
%{<#{INTERIM_TH_TAG} class="#{classes}" #{_pass_through_attributes(component)}><table><tr><th>#{inner}</th>#{expander}</tr></table></#{INTERIM_TH_TAG}>}
end

def _transform_block_grid(component, inner)
Expand Down

0 comments on commit 208ade8

Please sign in to comment.