Skip to content

Wrapped <div> content gets cleared since #724 #789

@adam-h

Description

@adam-h

We have a number of records in our database with the original <div> wrapper which was used before #724

Now when running since that change, the editor input is blank when loading these prior records. New records, which don't include a wrapping <div> work just fine.

Test which seems to reproduce this:

# In test/system/load_html_test.rb
test "wrapped attachments are normalized" do
  visit new_post_path(attachment_tag_name: "bc-attachment")

  person = people(:james)

  find_editor.value = <<~HTML
  <div>Hello World <bc-attachment sgid="#{person.attachable_sgid}" content-type="#{person.content_type}" content="&quot;#{person.name}&quot;"></bc-attachment></div>
  HTML

  assert_editor_html do
    assert_selector "bc-attachment"
  end
end

Potential fix:

diff --git a/src/nodes/image_gallery_node.js b/src/nodes/image_gallery_node.js
index 5a26e1d..c7b28c8 100644
--- a/src/nodes/image_gallery_node.js
+++ b/src/nodes/image_gallery_node.js
@@ -22,8 +22,7 @@ export class ImageGalleryNode extends ElementNode {
   static importDOM() {
     return {
       div: (element) => {
-        const containsAttachment = element.querySelector(`:scope > :is(${this.#attachmentTags.join()})`)
-        if (!containsAttachment) return null
+        if (!this.#containsPreviewableAttachment(element)) return null
 
         return {
           conversion: () => {
@@ -50,6 +49,11 @@ export class ImageGalleryNode extends ElementNode {
     return Object.keys(ActionTextAttachmentNode.importDOM())
   }
 
+  static #containsPreviewableAttachment(element) {
+    return element.querySelector(`:scope > img`) ||
+      element.querySelector(`:scope > :is(${this.#attachmentTags.join()}):not([content])[content-type^="image/"]:not([content-type*="svg"])`)
+  }
+
   createDOM() {
     const div = document.createElement("div")
     div.className = this.#galleryClassNames

Although the :not rules here seem wonky but attempts to match the isPreviewableImage functionality from src/helpers/html_helper.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions