From b5c926811c13dbe1cfb00d89e356ca2ee94a4c55 Mon Sep 17 00:00:00 2001
From: RobertDober <robert.dober@gmail.com>
Date: Tue, 9 Jul 2024 21:23:44 +0200
Subject: [PATCH] Fixes #154

---
 lib/earmark_parser/ast/inline.ex              |  2 +-
 lib/earmark_parser/helpers/ast_helpers.ex     |  9 ++++--
 .../i154_parser_crash_symbolic_input_test.exs | 32 +++++++++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)
 create mode 100644 test/acceptance/regressions/i154_parser_crash_symbolic_input_test.exs

diff --git a/lib/earmark_parser/ast/inline.ex b/lib/earmark_parser/ast/inline.ex
index ad92030..1a98e69 100644
--- a/lib/earmark_parser/ast/inline.ex
+++ b/lib/earmark_parser/ast/inline.ex
@@ -270,7 +270,7 @@ defmodule EarmarkParser.Ast.Inline do
     if match = Regex.run(@inline_ial, src) do
       [match, ial] = match
       {context1, ial_attrs} = parse_attrs(context, ial, lnb)
-      new_tags = augment_tag_with_ial(context.value, ial_attrs)
+      new_tags = augment_tag_with_ial(context.value, ial_attrs, match)
       {behead(src, match), lnb, set_value(context1, new_tags), use_linky?}
     end
   end
diff --git a/lib/earmark_parser/helpers/ast_helpers.ex b/lib/earmark_parser/helpers/ast_helpers.ex
index 74d6843..2f67953 100644
--- a/lib/earmark_parser/helpers/ast_helpers.ex
+++ b/lib/earmark_parser/helpers/ast_helpers.ex
@@ -25,13 +25,16 @@ defmodule EarmarkParser.Helpers.AstHelpers do
   end
 
   @doc false
-  def augment_tag_with_ial(tags, ial)
-  def augment_tag_with_ial([{t, a, c, m}|tags], atts) do
+  def augment_tag_with_ial(tags, ial, src)
+  def augment_tag_with_ial([{t, a, c, m}|tags], atts, _src) do
     [{t, merge_attrs(a, atts), c, m}|tags]
   end
-  def augment_tag_with_ial([], _atts) do
+  def augment_tag_with_ial([], _atts, _src) do
     []
   end
+  def augment_tag_with_ial([any], _atts, src) do
+    [any <> src]
+  end
 
   @doc false
   def code_classes(language, prefix) do
diff --git a/test/acceptance/regressions/i154_parser_crash_symbolic_input_test.exs b/test/acceptance/regressions/i154_parser_crash_symbolic_input_test.exs
new file mode 100644
index 0000000..2ef4937
--- /dev/null
+++ b/test/acceptance/regressions/i154_parser_crash_symbolic_input_test.exs
@@ -0,0 +1,32 @@
+defmodule Test.Acceptance.Regressions.I154pArserCrashSymbolicInputTest do
+  use ExUnit.Case
+
+  import Support.Helpers, only: [as_ast: 1]
+  import EarmarkAstDsl
+
+  test "this should not crash" do
+    ast = [ p("[{:}") ]
+    messages = []
+    assert as_ast("\\[{:}") == {:ok, ast, messages}
+  end
+
+  test "this does not crash" do
+    ast = [ p("[{:") ]
+    messages = []
+    assert as_ast("\\[{:") == {:ok, ast, messages}
+  end
+
+  test "nor does this" do
+    ast = [ p("[{:}") ]
+    messages = []
+    assert as_ast("[{:}") == {:ok, ast, messages}
+  end
+
+  test "nor this" do
+    ast = [{"p", [], [{"em", [], ["a"], %{}}], %{}}]
+    messages = []
+    assert as_ast("*a*{:}") == {:ok, ast, messages}
+    
+  end
+end
+# SPDX-License-Identifier: Apache-2.0