From afc1667e644c1e95dc5643383c5227abb33d48e1 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Thu, 2 Mar 2023 09:39:14 -0500 Subject: [PATCH] Implement htmlescape() built-in function. --- man/remind.1.in | 4 ++++ src/funcs.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.cmp | 22 ++++++++++++++++++++++ tests/test.rem | 7 +++++++ 4 files changed, 82 insertions(+) diff --git a/man/remind.1.in b/man/remind.1.in index 72f1b463..e88ab7a0 100644 --- a/man/remind.1.in +++ b/man/remind.1.in @@ -3074,6 +3074,10 @@ Support for Hebrew dates - see the section "THE HEBREW CALENDAR" .B hour(tq_time) Returns the hour component of \fItime\fR. .TP +.B htmlescape(s_str) +Returns a modified copy of \fIstr\fR where "<" is replaced with +"<"; ">" is replaced with ">" and "&" is replaced with "&" +.TP .B iif(si_test1, x_arg1, [si_test2, x_arg2,...], x_default) If \fItest1\fR is not zero or the null string, returns \fIarg1\fR. Otherwise, if \fItest2\fR is not zero or the null string, returns diff --git a/src/funcs.c b/src/funcs.c index 7246a9ea..272e4d57 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -101,6 +101,7 @@ static int FHebday (func_info *); static int FHebmon (func_info *); static int FHebyear (func_info *); static int FHour (func_info *); +static int FHtmlEscape (func_info *); static int FIif (func_info *); static int FIndex (func_info *); static int FIsdst (func_info *); @@ -265,6 +266,7 @@ BuiltinFunc Func[] = { { "hebmon", 1, 1, 0, FHebmon }, { "hebyear", 1, 1, 0, FHebyear }, { "hour", 1, 1, 1, FHour }, + { "htmlescape", 1, 1, 1, FHtmlEscape }, { "iif", 1, NO_MAX, 1, FIif }, { "index", 2, 3, 1, FIndex }, { "isany", 1, NO_MAX, 1, FIsAny }, @@ -2248,6 +2250,53 @@ static int FHebyear(func_info *info) RETVAL = y; return OK; } + +/****************************************************************/ +/* */ +/* htmlescape - replace <. > and & by < > and & */ +/* */ +/****************************************************************/ + +static int FHtmlEscape(func_info *info) +{ + DynamicBuffer dbuf; + char const *s; + int r; + + ASSERT_TYPE(0, STR_TYPE); + + DBufInit(&dbuf); + + s = ARGSTR(0); + while(*s) { + switch(*s) { + case '<': + r = DBufPuts(&dbuf, "<"); + break; + + case '>': + r = DBufPuts(&dbuf, ">"); + break; + + case '&': + r = DBufPuts(&dbuf, "&"); + break; + + default: + r = DBufPutc(&dbuf, *s); + break; + } + if (r != OK) { + DBufFree(&dbuf); + return r; + } + s++; + } + r = RetStrVal(DBufValue(&dbuf), info); + DBufFree(&dbuf); + return r; +} + /****************************************************************/ /* */ /* FEasterdate - calc. easter Sunday from a year. */ diff --git a/tests/test.cmp b/tests/test.cmp index bf820e93..0f250295 100644 --- a/tests/test.cmp +++ b/tests/test.cmp @@ -4786,6 +4786,28 @@ FUNSET circle square rectangle SET a square(5) ../tests/test.rem(867): Undefined function: `square' +# htmlescape +set a htmlescape("foo") +htmlescape("foo") => "foo" +REM MSG [a] +../tests/test.rem(871): Trig = Saturday, 16 February, 1991 +a => "foo" +foo + +set a htmlescape("<&>") +htmlescape("<&>") => "<&>" +REM MSG [a] +../tests/test.rem(873): Trig = Saturday, 16 February, 1991 +a => "<&>" +<&> + +set a htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarquux") +htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarqu"...) => "@&^#*@&^##$*&@><>&l"... +REM MSG [a] +../tests/test.rem(875): Trig = Saturday, 16 February, 1991 +a => "@&^#*@&^##$*&@><>&l"... +@&^#*@&^##$*&@><><@#@#><@#>%_#$foobarquux + # Don't want Remind to queue reminders EXIT diff --git a/tests/test.rem b/tests/test.rem index ec06a43c..bb032559 100644 --- a/tests/test.rem +++ b/tests/test.rem @@ -866,6 +866,13 @@ FUNSET circle square rectangle # Should fail SET a square(5) +# htmlescape +set a htmlescape("foo") +REM MSG [a] +set a htmlescape("<&>") +REM MSG [a] +set a htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarquux") +REM MSG [a] # Don't want Remind to queue reminders EXIT