Skip to content

Commit

Permalink
Change some instances of Nokogiri HTML4 parsing to HTML5 (mastodon#31812
Browse files Browse the repository at this point in the history
)
  • Loading branch information
flavorjones authored Sep 8, 2024
1 parent b716248 commit 10143d0
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/helpers/admin/trends/statuses_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def one_line_preview(status)
text = if status.local?
status.text.split("\n").first
else
Nokogiri::HTML(status.text).css('html > body > *').first&.text
Nokogiri::HTML5(status.text).css('html > body > *').first&.text
end

return '' if text.blank?
Expand Down
8 changes: 4 additions & 4 deletions app/lib/emoji_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(html, custom_emojis, options = {})
def to_s
return html if custom_emojis.empty? || html.blank?

tree = Nokogiri::HTML.fragment(html)
tree = Nokogiri::HTML5.fragment(html)
tree.xpath('./text()|.//text()[not(ancestor[@class="invisible"])]').to_a.each do |node|
i = -1
inside_shortname = false
Expand All @@ -43,8 +43,8 @@ def to_s

next unless (char_after.nil? || !DISALLOWED_BOUNDING_REGEX.match?(char_after)) && (emoji = emoji_map[shortcode])

result << Nokogiri::XML::Text.new(text[last_index..shortname_start_index - 1], tree.document) if shortname_start_index.positive?
result << Nokogiri::HTML.fragment(tag_for_emoji(shortcode, emoji))
result << tree.document.create_text_node(text[last_index..shortname_start_index - 1]) if shortname_start_index.positive?
result << tree.document.fragment(tag_for_emoji(shortcode, emoji))

last_index = i + 1
elsif text[i] == ':' && (i.zero? || !DISALLOWED_BOUNDING_REGEX.match?(text[i - 1]))
Expand All @@ -53,7 +53,7 @@ def to_s
end
end

result << Nokogiri::XML::Text.new(text[last_index..], tree.document)
result << tree.document.create_text_node(text[last_index..])
node.replace(result)
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/plain_text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def to_s
if local?
text
else
node = Nokogiri::HTML.fragment(insert_newlines)
node = Nokogiri::HTML5.fragment(insert_newlines)
# Elements that are entirely removed with our Sanitize config
node.xpath('.//iframe|.//math|.//noembed|.//noframes|.//noscript|.//plaintext|.//script|.//style|.//svg|.//xmp').remove
node.text.chomp
Expand Down
2 changes: 1 addition & 1 deletion app/services/fetch_oembed_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def discover_endpoint!
return if html.nil?

@format = @options[:format]
page = Nokogiri::HTML(html)
page = Nokogiri::HTML5(html)

if @format.nil? || @format == :json
@endpoint_url ||= page.at_xpath('//link[@type="application/json+oembed"]|//link[@type="text/json+oembed"]')&.attribute('href')&.value
Expand Down
2 changes: 1 addition & 1 deletion app/services/fetch_resource_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def expected_type?(json)
end

def process_html(response)
page = Nokogiri::HTML(response.body_with_limit)
page = Nokogiri::HTML5(response.body_with_limit)
json_link = page.xpath('//link[@rel="alternate"]').find { |link| ACTIVITY_STREAM_LINK_TYPES.include?(link['type']) }

process(json_link['href'], terminal: true) unless json_link.nil?
Expand Down
2 changes: 1 addition & 1 deletion app/services/translate_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def wrap_emoji_shortcodes(text)
end

def unwrap_emoji_shortcodes(html)
fragment = Nokogiri::HTML.fragment(html)
fragment = Nokogiri::HTML5.fragment(html)
fragment.css('span[translate="no"]').each do |element|
element.remove_attribute('translate')
element.replace(element.children) if element.attributes.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize_ext/sanitize_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Config
:relative
end

current_node.replace(Nokogiri::XML::Text.new(current_node.text, current_node.document)) unless LINK_PROTOCOLS.include?(scheme)
current_node.replace(current_node.document.create_text_node(current_node.text)) unless LINK_PROTOCOLS.include?(scheme)
end

UNSUPPORTED_ELEMENTS_TRANSFORMER = lambda do |env|
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/emojis.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def gen_border(codepoint, color)
view_box[3] += 4
svg['viewBox'] = view_box.join(' ')
end
g = Nokogiri::XML::Node.new 'g', doc
g = doc.create_element('g')
doc.css('svg > *').each do |elem|
border_elem = elem.dup

Expand Down

0 comments on commit 10143d0

Please sign in to comment.