Enable frozen strings in remaining lib/banzai/filter/*.rb files
This also fixes a bug with the ImageLazyLoadFilter not doing the right thing when an existing class attribute is present. Part of #47424
This commit is contained in:
parent
dc7b4b7bb9
commit
954f307673
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Enable frozen strings in remaining lib/banzai/filter/*.rb files
|
||||||
|
merge_request: 20777
|
||||||
|
author:
|
||||||
|
type: performance
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Banzai
|
module Banzai
|
||||||
module Filter
|
module Filter
|
||||||
# HTML filter that moves the value of image `src` attributes to `data-src`
|
# HTML filter that moves the value of image `src` attributes to `data-src`
|
||||||
|
@ -5,7 +7,7 @@ module Banzai
|
||||||
class ImageLazyLoadFilter < HTML::Pipeline::Filter
|
class ImageLazyLoadFilter < HTML::Pipeline::Filter
|
||||||
def call
|
def call
|
||||||
doc.xpath('descendant-or-self::img').each do |img|
|
doc.xpath('descendant-or-self::img').each do |img|
|
||||||
img['class'] ||= '' << 'lazy'
|
img.add_class('lazy')
|
||||||
img['data-src'] = img['src']
|
img['data-src'] = img['src']
|
||||||
img['src'] = LazyImageTagHelper.placeholder_image
|
img['src'] = LazyImageTagHelper.placeholder_image
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rouge/plugins/common_mark'
|
require 'rouge/plugins/common_mark'
|
||||||
require 'rouge/plugins/redcarpet'
|
require 'rouge/plugins/redcarpet'
|
||||||
|
|
||||||
|
@ -15,7 +17,7 @@ module Banzai
|
||||||
end
|
end
|
||||||
|
|
||||||
def highlight_node(node)
|
def highlight_node(node)
|
||||||
css_classes = 'code highlight js-syntax-highlight'
|
css_classes = +'code highlight js-syntax-highlight'
|
||||||
lang = node.attr('lang')
|
lang = node.attr('lang')
|
||||||
retried = false
|
retried = false
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Banzai
|
module Banzai
|
||||||
module Filter
|
module Filter
|
||||||
# HTML filter that adds an anchor child element to all Headers in a
|
# HTML filter that adds an anchor child element to all Headers in a
|
||||||
|
@ -19,7 +21,7 @@ module Banzai
|
||||||
def call
|
def call
|
||||||
return doc if context[:no_header_anchors]
|
return doc if context[:no_header_anchors]
|
||||||
|
|
||||||
result[:toc] = ""
|
result[:toc] = +""
|
||||||
|
|
||||||
headers = Hash.new(0)
|
headers = Hash.new(0)
|
||||||
header_root = current_header = HeaderNode.new
|
header_root = current_header = HeaderNode.new
|
||||||
|
|
|
@ -7,6 +7,20 @@ describe Banzai::Filter::ImageLazyLoadFilter do
|
||||||
%(<img src="#{path}" />)
|
%(<img src="#{path}" />)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def image_with_class(path, class_attr = nil)
|
||||||
|
%(<img src="#{path}" class="#{class_attr}"/>)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'adds a class attribute' do
|
||||||
|
doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
||||||
|
expect(doc.at_css('img')['class']).to eq 'lazy'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'appends to the current class attribute' do
|
||||||
|
doc = filter(image_with_class('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg', 'test'))
|
||||||
|
expect(doc.at_css('img')['class']).to eq 'test lazy'
|
||||||
|
end
|
||||||
|
|
||||||
it 'transforms the image src to a data-src' do
|
it 'transforms the image src to a data-src' do
|
||||||
doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
||||||
expect(doc.at_css('img')['data-src']).to eq '/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'
|
expect(doc.at_css('img')['data-src']).to eq '/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'
|
||||||
|
|
Loading…
Reference in New Issue