diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex
index 034f18bd..5a5b722e 100644
--- a/lib/floki/raw_html.ex
+++ b/lib/floki/raw_html.ex
@@ -133,14 +133,6 @@ defmodule Floki.RawHTML do
          self_closing_tags,
          line_ending
        ) do
-    encoder =
-      case type do
-        "script" -> @no_encoder
-        "style" -> @no_encoder
-        "title" -> @no_encoder
-        _ -> encoder
-      end
-
     open_tag_content = [
       tag_with_attrs(type, attrs, children, pad, encoder, self_closing_tags),
       line_ending
@@ -156,10 +148,19 @@ defmodule Floki.RawHTML do
         _ ->
           children = List.wrap(children)
 
+          curr_encoder =
+            case type do
+              "script" -> @no_encoder
+              "style" -> @no_encoder
+              "title" -> @no_encoder
+              _ -> encoder
+            end
+
           build_raw_html(
             children,
             acc,
-            encoder,
+            # Need to make sure to pass the encoder for the current node
+            curr_encoder,
             pad_increase(pad),
             self_closing_tags,
             line_ending
@@ -168,6 +169,7 @@ defmodule Floki.RawHTML do
 
     close_tag_content = close_end_tag(type, children, pad, self_closing_tags, line_ending)
     acc = [close_tag_content | acc]
+    # Return the original encoder here, we don't want to propagate that
     build_raw_html(tail, acc, encoder, pad, self_closing_tags, line_ending)
   end
 
diff --git a/test/floki_test.exs b/test/floki_test.exs
index 04696b73..6acda66f 100644
--- a/test/floki_test.exs
+++ b/test/floki_test.exs
@@ -443,6 +443,37 @@ defmodule FlokiTest do
 
     tree = document!(html_body("<span data-stuff=\"&quot;'\"></span>"))
     assert Floki.raw_html(tree) == expected_html
+
+    expected_html = ~S"""
+    <html>
+      <head>
+      </head>
+      <body>
+        <div>
+          <style data-attrs-test="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
+          </style>
+          <a data-attrs-event="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
+            Next
+          </a>
+        </div>
+      </body>
+    </html>
+    """
+
+    tree =
+      document!(
+        html_body(~S"""
+        <div>
+          <style data-attrs-test="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
+          </style>
+          <a data-attrs-event="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
+            Next
+          </a>
+        </div>
+        """)
+      )
+
+    assert Floki.raw_html(tree, pretty: true) == expected_html
   end
 
   test "raw_html (with >)" do