Implement htmlescape() built-in function.

This commit is contained in:
Dianne Skoll
2023-03-02 09:39:14 -05:00
parent 8d25270c43
commit afc1667e64
4 changed files with 82 additions and 0 deletions

View File

@@ -3074,6 +3074,10 @@ Support for Hebrew dates - see the section "THE HEBREW CALENDAR"
.B hour(tq_time) .B hour(tq_time)
Returns the hour component of \fItime\fR. Returns the hour component of \fItime\fR.
.TP .TP
.B htmlescape(s_str)
Returns a modified copy of \fIstr\fR where "<" is replaced with
"&lt;"; ">" is replaced with "&gt;" and "&" is replaced with "&amp;"
.TP
.B iif(si_test1, x_arg1, [si_test2, x_arg2,...], x_default) .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. 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 Otherwise, if \fItest2\fR is not zero or the null string, returns

View File

@@ -101,6 +101,7 @@ static int FHebday (func_info *);
static int FHebmon (func_info *); static int FHebmon (func_info *);
static int FHebyear (func_info *); static int FHebyear (func_info *);
static int FHour (func_info *); static int FHour (func_info *);
static int FHtmlEscape (func_info *);
static int FIif (func_info *); static int FIif (func_info *);
static int FIndex (func_info *); static int FIndex (func_info *);
static int FIsdst (func_info *); static int FIsdst (func_info *);
@@ -265,6 +266,7 @@ BuiltinFunc Func[] = {
{ "hebmon", 1, 1, 0, FHebmon }, { "hebmon", 1, 1, 0, FHebmon },
{ "hebyear", 1, 1, 0, FHebyear }, { "hebyear", 1, 1, 0, FHebyear },
{ "hour", 1, 1, 1, FHour }, { "hour", 1, 1, 1, FHour },
{ "htmlescape", 1, 1, 1, FHtmlEscape },
{ "iif", 1, NO_MAX, 1, FIif }, { "iif", 1, NO_MAX, 1, FIif },
{ "index", 2, 3, 1, FIndex }, { "index", 2, 3, 1, FIndex },
{ "isany", 1, NO_MAX, 1, FIsAny }, { "isany", 1, NO_MAX, 1, FIsAny },
@@ -2248,6 +2250,53 @@ static int FHebyear(func_info *info)
RETVAL = y; RETVAL = y;
return OK; return OK;
} }
/****************************************************************/
/* */
/* htmlescape - replace <. > and & by &lt; &gt; and &amp; */
/* */
/****************************************************************/
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, "&lt;");
break;
case '>':
r = DBufPuts(&dbuf, "&gt;");
break;
case '&':
r = DBufPuts(&dbuf, "&amp;");
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. */ /* FEasterdate - calc. easter Sunday from a year. */

View File

@@ -4786,6 +4786,28 @@ FUNSET circle square rectangle
SET a square(5) SET a square(5)
../tests/test.rem(867): Undefined function: `square' ../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("<&>") => "&lt;&amp;&gt;"
REM MSG [a]
../tests/test.rem(873): Trig = Saturday, 16 February, 1991
a => "&lt;&amp;&gt;"
&lt;&amp;&gt;
set a htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarquux")
htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarqu"...) => "@&amp;^#*@&amp;^##$*&amp;@&gt;&lt;&gt;&l"...
REM MSG [a]
../tests/test.rem(875): Trig = Saturday, 16 February, 1991
a => "@&amp;^#*@&amp;^##$*&amp;@&gt;&lt;&gt;&l"...
@&amp;^#*@&amp;^##$*&amp;@&gt;&lt;&gt;&lt;@#@#&gt;&lt;@#&gt;%_#$foobarquux
# Don't want Remind to queue reminders # Don't want Remind to queue reminders
EXIT EXIT

View File

@@ -866,6 +866,13 @@ FUNSET circle square rectangle
# Should fail # Should fail
SET a square(5) 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 # Don't want Remind to queue reminders
EXIT EXIT