FIX: Keep `alt` and `title` in lightbox when indexing for search.

FIX: Keep alt and title in lightbox when indexing for search.

Follow up to cfd507822f9330967f3ed9f970505e7f4896b523

diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rb
index f731e52..92194d1 100644
--- a/app/services/search_indexer.rb
+++ b/app/services/search_indexer.rb
@@ -190,9 +190,25 @@ class SearchIndexer
 
       document = Nokogiri::HTML("<div>#{html}</div>", nil, Encoding::UTF_8.to_s)
 
-      document.css(
+      nodes = document.css(
         "div.#{CookedPostProcessor::LIGHTBOX_WRAPPER_CSS_CLASS}"
-      ).remove
+      )
+
+      if nodes.present?
+        nodes.each do |node|
+          node.traverse do |child_node|
+            next if child_node == node
+
+            if %w{a img}.exclude?(child_node.name)
+              child_node.remove
+            elsif child_node.name == "a"
+              ATTRIBUTES.each do |attribute|
+                child_node.remove_attribute(attribute)
+              end
+            end
+          end
+        end
+      end
 
       document.css("a[href]").each do |node|
         node.remove_attribute("href") if node["href"] == node.text
diff --git a/spec/services/search_indexer_spec.rb b/spec/services/search_indexer_spec.rb
index 5aa9f21..9290b67 100644
--- a/spec/services/search_indexer_spec.rb
+++ b/spec/services/search_indexer_spec.rb
@@ -61,7 +61,7 @@ describe SearchIndexer do
 
     scrubbed = scrub(html)
 
-    expect(scrubbed).to eq("Discourse 51%20PM")
+    expect(scrubbed).to eq("Discourse 51%20PM Untitled%20design%20(21)")
   end
 
   it 'correctly indexes a post according to version' do
@@ -137,7 +137,7 @@ describe SearchIndexer do
 
       post = Fabricate(:post, raw: <<~RAW)
       Let me see how I can fix this image
-      <img src="#{src}" width="2" height="2">
+      <img src="#{src}" title="GOT" alt="white walkers" width="2" height="2">
       RAW
 
       post.rebake!
@@ -149,7 +149,7 @@ describe SearchIndexer do
       )
 
       expect(post.post_search_data.raw_data).to eq(
-        "#{topic.title} #{topic.category.name} Let me see how I can fix this image"
+        "#{topic.title} #{topic.category.name} Let me see how I can fix this image white walkers GOT"
       )
     end
   end

GitHub sha: 2a69ab4a

1 Like