[latex3-commits] [git/LaTeX3-latex3-latex2e] apdf161: Correct handling of hook names (730cf330)

PhelypeOleinik phelype.oleinik at latex-project.org
Mon Sep 7 21:37:22 CEST 2020


Repository : https://github.com/latex3/latex2e
On branch  : apdf161
Link       : https://github.com/latex3/latex2e/commit/730cf33091d78fa5f0360cd0cd4dd3c30195ee85

>---------------------------------------------------------------

commit 730cf33091d78fa5f0360cd0cd4dd3c30195ee85
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Mon Sep 7 16:37:22 2020 -0300

    Correct handling of hook names
    
    Expand (with \csname) and stringify (with \string)


>---------------------------------------------------------------

730cf33091d78fa5f0360cd0cd4dd3c30195ee85
 base/lthooks.dtx                       | 27 +++++++++++++++++++----
 base/testfiles-lthooks/lthooks-027.lvt | 31 +++++++++++++++++++++++++++
 base/testfiles-lthooks/lthooks-027.tlg | 39 ++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 4 deletions(-)

diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index d441cf2f..59abfa7e 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -1892,13 +1892,13 @@
       { \@@_parse_dot_label_aux:nw {#1} #3 \s_@@_mark }
       {
         \tl_if_empty:nTF {#3}
-          {#2}
+          { \@@_make_name:n {#2} }
           { \@@_parse_dot_label_cleanup:w #2 ./ #3 \s_@@_mark }
       }
   }
 \cs_new:Npn \@@_parse_dot_label_cleanup:w #1 ./ \s_@@_mark {#1}
 \cs_new:Npn \@@_parse_dot_label_aux:nw #1 #2 ./ \s_@@_mark
-  { \@@_currname_or_default:n {#1} / #2 }
+  { \@@_currname_or_default:n {#1} / \@@_make_name:n {#2} }
 %    \end{macrocode}
 % \end{macro}
 % \end{macro}
@@ -1913,7 +1913,7 @@
     \tl_if_empty:NTF \g_@@_hook_curr_name_tl
       {
         \tl_if_empty:NTF \@currname
-          {#1}
+          { \@@_make_name:n {#1} }
           { \@currname }
       }
       { \g_@@_hook_curr_name_tl }
@@ -1921,7 +1921,26 @@
 %    \end{macrocode}
 % \end{macro}
 %
-%
+% \begin{macro}[EXP]{\@@_make_name:n}
+%   Provides a standard sanitisation of a hook's name.
+%   It uses \cs{cs:w} to build a control sequence out of the hook name,
+%   then uses \cs{cs_to_str:N} to get the string representation of that,
+%   without the escape character.  \cs{cs:w}-based expansion is used
+%   instead of |e|-based because Unicode characters don't behave well
+%   inside \cs{expanded}.  The macro adds the \cs{@@-|} prefix to the
+%   hook name to avoid leaving \enquote{public} control sequences
+%   defined (as \cs{relax}) in TeX's memory.
+%    \begin{macrocode}
+\cs_new:Npn \@@_make_name:n #1
+  {
+    \exp_after:wN \@@_hook_name_clean:w
+      \exp:w \exp_end_continue_f:w
+      \exp_after:wN \cs_to_str:N \cs:w @@-| #1 \cs_end:
+  }
+\exp_last_unbraced:NNo
+\cs_new:Npn \@@_hook_name_clean:w \tl_to_str:n { @@-| } { }
+%    \end{macrocode}
+% \end{macro}
 %
 %
 % \begin{macro}{\hook_gput_code:nnn}
diff --git a/base/testfiles-lthooks/lthooks-027.lvt b/base/testfiles-lthooks/lthooks-027.lvt
index 2c0f1668..fb643d59 100644
--- a/base/testfiles-lthooks/lthooks-027.lvt
+++ b/base/testfiles-lthooks/lthooks-027.lvt
@@ -37,4 +37,35 @@
     \input{my-\foo{english}.xxx}
   }
 
+\TEST { Unicode hook names (expect 'aaa bbb'): }
+  {
+    \NewHook { grüße }
+    \AddToHook { grüße } {\message{aaa}}
+    \UseOneTimeHook { grüße }
+    \AddToHook { grüße } {\message{bbb}}
+    \ShowHook { grüße }
+  }
+
+\def\hookname{鉤}
+
+\TEST { Macros in hook names (expect 'aaa bbb'): }
+  {
+    \NewHook { \hookname }
+    \AddToHook { \hookname } {\message{aaa}}
+    \UseOneTimeHook { \hookname }
+    \AddToHook { \hookname } {\message{bbb}}
+    \ShowHook { \hookname }
+  }
+
+\def\hookname{ <-with spaces-> }
+
+\TEST { Spaces aren't trimmed if hidden in a macro (expect 'aaa bbb'): }
+  {
+    \NewHook { \hookname }
+    \AddToHook { \hookname } {\message{aaa}}
+    \UseOneTimeHook { \hookname }
+    \AddToHook { \hookname } {\message{bbb}}
+    \ShowHook { \hookname }
+  }
+
 \end{document}
diff --git a/base/testfiles-lthooks/lthooks-027.tlg b/base/testfiles-lthooks/lthooks-027.tlg
index a5e46d44..d6215b53 100644
--- a/base/testfiles-lthooks/lthooks-027.tlg
+++ b/base/testfiles-lthooks/lthooks-027.tlg
@@ -35,4 +35,43 @@ File: my-bar.xxx
 )
 File: after
 ============================================================
+============================================================
+TEST 4:  Unicode hook names (expect 'aaa bbb'): 
+============================================================
+The hook 'gr^^c3^^bc^^c3^^9fe':
+ Code chunks:
+    top-level -> \message {aaa}\message {bbb}
+ Extra code next invocation:
+    ---
+ Rules:
+    ---
+ Execution order:
+    top-level
+============================================================
+============================================================
+TEST 5:  Macros in hook names (expect 'aaa bbb'): 
+============================================================
+The hook '^^e9^^89^^a4':
+ Code chunks:
+    top-level -> \message {aaa}\message {bbb}
+ Extra code next invocation:
+    ---
+ Rules:
+    ---
+ Execution order:
+    top-level
+============================================================
+============================================================
+TEST 6:  Spaces aren't trimmed if hidden in a macro (expect 'aaa bbb'): 
+============================================================
+The hook ' <-with spaces-> ':
+ Code chunks:
+    top-level -> \message {aaa}\message {bbb}
+ Extra code next invocation:
+    ---
+ Rules:
+    ---
+ Execution order:
+    top-level
+============================================================
 (lthooks-027.aux)





More information about the latex3-commits mailing list.