// swad_HTML.c: tables, divs
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2021 Antonio Caņas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For vasprintf
#include // For va_start, va_end
#include // For vasprintf
#include // For free
#include "swad_error.h"
#include "swad_global.h"
#include "swad_HTML.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private vatiables *****************************/
/*****************************************************************************/
static unsigned HTM_TABLE_NestingLevel = 0;
static unsigned HTM_TR_NestingLevel = 0;
static unsigned HTM_TH_NestingLevel = 0;
static unsigned HTM_TD_NestingLevel = 0;
static unsigned HTM_DIV_NestingLevel = 0;
static unsigned HTM_SPAN_NestingLevel = 0;
static unsigned HTM_OL_NestingLevel = 0;
static unsigned HTM_UL_NestingLevel = 0;
static unsigned HTM_LI_NestingLevel = 0;
static unsigned HTM_DL_NestingLevel = 0;
static unsigned HTM_DT_NestingLevel = 0;
static unsigned HTM_DD_NestingLevel = 0;
static unsigned HTM_A_NestingLevel = 0;
static unsigned HTM_SCRIPT_NestingLevel = 0;
static unsigned HTM_LABEL_NestingLevel = 0;
static unsigned HTM_BUTTON_NestingLevel = 0;
static unsigned HTM_TEXTAREA_NestingLevel = 0;
static unsigned HTM_SELECT_NestingLevel = 0;
static unsigned HTM_OPTGROUP_NestingLevel = 0;
static unsigned HTM_STRONG_NestingLevel = 0;
static unsigned HTM_EM_NestingLevel = 0;
static unsigned HTM_U_NestingLevel = 0;
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
static void HTM_TABLE_BeginWithoutAttr (void);
static void HTM_TBODY_BeginWithoutAttr (void);
static void HTM_TR_BeginWithoutAttr (void);
static void HTM_TH_BeginWithoutAttr (void);
static void HTM_TH_BeginAttr (const char *fmt,...);
static void HTM_TD_BeginWithoutAttr (void);
static void HTM_DIV_BeginWithoutAttr (void);
static void HTM_SPAN_BeginWithoutAttr (void);
static void HTM_UL_BeginWithoutAttr (void);
static void HTM_LI_BeginWithoutAttr (void);
static void HTM_A_BeginWithoutAttr (void);
static void HTM_LABEL_BeginWithoutAttr (void);
static void HTM_TEXTAREA_BeginWithoutAttr (void);
static void HTM_SELECT_BeginWithoutAttr (void);
static void HTM_SPTxt (const char *Txt);
/*****************************************************************************/
/******************************* Begin/end table *****************************/
/*****************************************************************************/
void HTM_TABLE_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Class;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Class,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("
");
HTM_TABLE_NestingLevel++;
}
void HTM_TABLE_End (void)
{
if (HTM_TABLE_NestingLevel == 0) // No TABLE open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TABLE.");
HTM_Txt ("
");
HTM_TABLE_NestingLevel--;
}
/*****************************************************************************/
/********************************* Table body ********************************/
/*****************************************************************************/
void HTM_TBODY_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("",Attr);
free (Attr);
}
else
HTM_TBODY_BeginWithoutAttr ();
}
else
HTM_TBODY_BeginWithoutAttr ();
}
static void HTM_TBODY_BeginWithoutAttr (void)
{
HTM_Txt ("");
}
void HTM_TBODY_End (void)
{
HTM_Txt ("");
}
/*****************************************************************************/
/**************************** Begin/end table row ****************************/
/*****************************************************************************/
void HTM_TR_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("
");
HTM_DL_NestingLevel++;
}
void HTM_DL_End (void)
{
if (HTM_DL_NestingLevel == 0) // No DL open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DL.");
HTM_Txt ("
");
HTM_DT_NestingLevel++;
}
void HTM_DT_End (void)
{
if (HTM_DL_NestingLevel == 0) // No DT open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DT.");
HTM_Txt ("
");
HTM_DD_NestingLevel++;
}
void HTM_DD_End (void)
{
if (HTM_DD_NestingLevel == 0) // No DD open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DD.");
HTM_Txt ("
");
HTM_DD_NestingLevel--;
}
/*****************************************************************************/
/********************************** Anchors **********************************/
/*****************************************************************************/
void HTM_A_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("",Attr);
free (Attr);
}
else
HTM_A_BeginWithoutAttr ();
}
else
HTM_A_BeginWithoutAttr ();
HTM_A_NestingLevel++;
}
static void HTM_A_BeginWithoutAttr (void)
{
HTM_Txt ("");
}
void HTM_A_End (void)
{
if (HTM_A_NestingLevel == 0) // No A open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened A.");
HTM_Txt ("");
HTM_A_NestingLevel--;
}
/*****************************************************************************/
/*********************************** Scripts *********************************/
/*****************************************************************************/
void HTM_SCRIPT_Begin (const char *URL,const char *CharSet)
{
HTM_Txt ("\n");
HTM_SCRIPT_NestingLevel--;
}
/*****************************************************************************/
/********************************* Parameters ********************************/
/*****************************************************************************/
void HTM_PARAM (const char *Name,
const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Value;
if (fmt)
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Value,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("\n",Name,Value);
free (Value);
}
}
/*****************************************************************************/
/*********************************** Labels **********************************/
/*****************************************************************************/
void HTM_LABEL_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Err_NotEnoughMemoryExit ();
/***** Print HTML *****/
HTM_TxtF ("