// 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-2024 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_alert.h" #include "swad_error.h" #include "swad_global.h" #include "swad_HTML.h" /*****************************************************************************/ /**************************** Private constants ******************************/ /*****************************************************************************/ static const char *ClassAlign[HTM_NUM_HEAD_ALIGN] = { [HTM_HEAD_LEFT ] = "LT", [HTM_HEAD_CENTER] = "CT", [HTM_HEAD_RIGHT ] = "RT", }; /*****************************************************************************/ /************************* Private global variables **************************/ /*****************************************************************************/ 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_FIELDSET_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_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_FIELDSET_BeginWithoutAttr (void); static void HTM_LABEL_BeginWithoutAttr (void); static void HTM_TEXTAREA_BeginWithoutAttr (void); static void HTM_SELECT_BeginWithoutAttr (void); /*****************************************************************************/ /******************************* Begin/end title *****************************/ /*****************************************************************************/ void HTM_TITLE_Begin (void) { HTM_Txt (""); } void HTM_TITLE_End (void) { HTM_Txt ("\n"); } /*****************************************************************************/ /******************************* 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 ("",Class); HTM_TABLE_NestingLevel++; free (Class); } else HTM_TABLE_BeginWithoutAttr (); } else HTM_TABLE_BeginWithoutAttr (); } void HTM_TABLE_BeginPadding (unsigned CellPadding) { if (CellPadding) { HTM_TxtF ("
", CellPadding); // CellPadding must be 0, 1, 2, 5 or 10 HTM_TABLE_NestingLevel++; } else HTM_TABLE_BeginWithoutAttr (); } static void HTM_TABLE_BeginWithoutAttr (void) { HTM_Txt ("
"); HTM_TABLE_NestingLevel++; } void HTM_TABLE_BeginCenterPadding (unsigned CellPadding) { if (CellPadding) { HTM_TxtF ("
", CellPadding); // CellPadding must be 0, 1, 2, 5 or 10 HTM_TABLE_NestingLevel++; } else HTM_TABLE_BeginCenter (); } void HTM_TABLE_BeginCenter (void) { HTM_Txt ("
"); HTM_TABLE_NestingLevel++; } void HTM_TABLE_BeginWidePadding (unsigned CellPadding) { if (CellPadding) { HTM_TxtF ("
", CellPadding); // CellPadding must be 0, 1, 2, 5 or 10 HTM_TABLE_NestingLevel++; } else HTM_TABLE_BeginWide (); } void HTM_TABLE_BeginWide (void) { HTM_Txt ("
"); HTM_TABLE_NestingLevel++; } void HTM_TABLE_BeginWideMarginPadding (unsigned CellPadding) { if (CellPadding) HTM_TxtF ("
", CellPadding); // CellPadding must be 0, 1, 2, 5, 10, 20 else HTM_Txt ("
"); 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 ("",Attr); free (Attr); } else HTM_TR_BeginWithoutAttr (); } else HTM_TR_BeginWithoutAttr (); HTM_TR_NestingLevel++; } static void HTM_TR_BeginWithoutAttr (void) { HTM_Txt (""); } void HTM_TR_End (void) { if (HTM_TR_NestingLevel == 0) // No TR open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR."); HTM_Txt (""); HTM_TR_NestingLevel--; } /*****************************************************************************/ /***************************** Table heading cells ***************************/ /*****************************************************************************/ void HTM_TH (const char *Title,HTM_HeadAlign HeadAlign) { HTM_TH_Span (Title,HeadAlign,1,1,NULL); } void HTM_TH_Begin (HTM_HeadAlign HeadAlign) { HTM_TH_Span_Begin (HeadAlign,1,1,NULL); } void HTM_TH_Span (const char *Title,HTM_HeadAlign HeadAlign, unsigned RowSpan,unsigned ColSpan, const char *ClassFmt,...) { va_list ap; int NumBytesPrinted; char *Attr = NULL; if (ClassFmt) if (ClassFmt[0]) { va_start (ap,ClassFmt); NumBytesPrinted = vasprintf (&Attr,ClassFmt,ap); va_end (ap); if (NumBytesPrinted < 0) // -1 if no memory or any other error Err_NotEnoughMemoryExit (); } if (RowSpan > 1 && ColSpan > 1) { if (Attr) HTM_TxtF ("", RowSpan,ColSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", RowSpan,ColSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else if (RowSpan > 1) { if (Attr) HTM_TxtF ("", RowSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", RowSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else if (ColSpan > 1) { if (Attr) HTM_TxtF ("", ColSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", ColSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else { if (Attr) HTM_TxtF ("", The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", The_GetSuffix (),ClassAlign[HeadAlign]); } if (ClassFmt) if (ClassFmt[0]) free (Attr); HTM_Txt (Title); HTM_Txt (""); } void HTM_TH_Span_Begin (HTM_HeadAlign HeadAlign, unsigned RowSpan,unsigned ColSpan, const char *ClassFmt,...) { va_list ap; int NumBytesPrinted; char *Attr = NULL; if (ClassFmt) if (ClassFmt[0]) { va_start (ap,ClassFmt); NumBytesPrinted = vasprintf (&Attr,ClassFmt,ap); va_end (ap); if (NumBytesPrinted < 0) // -1 if no memory or any other error Err_NotEnoughMemoryExit (); } if (RowSpan > 1 && ColSpan > 1) { if (Attr) HTM_TxtF ("", RowSpan,ColSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", RowSpan,ColSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else if (RowSpan > 1) { if (Attr) HTM_TxtF ("", RowSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", RowSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else if (ColSpan > 1) { if (Attr) HTM_TxtF ("", ColSpan, The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", ColSpan, The_GetSuffix (),ClassAlign[HeadAlign]); } else { if (Attr) HTM_TxtF ("", The_GetSuffix (),ClassAlign[HeadAlign],Attr); else HTM_TxtF ("", The_GetSuffix (),ClassAlign[HeadAlign]); } if (ClassFmt) if (ClassFmt[0]) free (Attr); HTM_TH_NestingLevel++; } void HTM_TH_End (void) { if (HTM_TH_NestingLevel == 0) // No TH open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR."); HTM_Txt (""); HTM_TH_NestingLevel--; } void HTM_TH_Empty (unsigned NumColumns) { unsigned NumCol; for (NumCol = 0; NumCol < NumColumns; NumCol++) HTM_Txt (""); } /*****************************************************************************/ /********************************* Table cells *******************************/ /*****************************************************************************/ void HTM_TD_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_TD_BeginWithoutAttr (); } else HTM_TD_BeginWithoutAttr (); HTM_TD_NestingLevel++; } static void HTM_TD_BeginWithoutAttr (void) { HTM_Txt (""); } void HTM_TD_End (void) { if (HTM_TD_NestingLevel == 0) // No TD open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TD."); HTM_Txt (""); HTM_TD_NestingLevel--; } void HTM_TD_Empty (unsigned NumColumns) { unsigned NumCol; for (NumCol = 0; NumCol < NumColumns; NumCol++) { HTM_TD_Begin (NULL); HTM_TD_End (); } } void HTM_TD_ColouredEmpty (unsigned NumColumns) { unsigned NumCol; for (NumCol = 0; NumCol < NumColumns; NumCol++) { HTM_TD_Begin ("class=\"%s\"",The_GetColorRows ()); HTM_TD_End (); } } /*****************************************************************************/ /************************************ Divs ***********************************/ /*****************************************************************************/ void HTM_DIV_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_DIV_BeginWithoutAttr (); } else HTM_DIV_BeginWithoutAttr (); HTM_DIV_NestingLevel++; } static void HTM_DIV_BeginWithoutAttr (void) { HTM_Txt ("
"); } void HTM_DIV_End (void) { if (HTM_DIV_NestingLevel == 0) // No DIV open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DIV."); HTM_Txt ("
"); HTM_DIV_NestingLevel--; } /*****************************************************************************/ /******************************** Main zone **********************************/ /*****************************************************************************/ void HTM_MAIN_Begin (const char *Class) { HTM_TxtF ("
",Class); } void HTM_MAIN_End (void) { HTM_Txt ("
"); } /*****************************************************************************/ /********************************* Articles **********************************/ /*****************************************************************************/ void HTM_ARTICLE_Begin (const char *ArticleId) { HTM_TxtF ("
",ArticleId); } void HTM_ARTICLE_End (void) { HTM_Txt ("
"); } /*****************************************************************************/ /********************************* Sections **********************************/ /*****************************************************************************/ void HTM_SECTION_Begin (const char *SectionId) { HTM_TxtF ("
",SectionId); } void HTM_SECTION_End (void) { HTM_Txt ("
"); } /*****************************************************************************/ /*********************************** Spans ***********************************/ /*****************************************************************************/ void HTM_SPAN_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_SPAN_BeginWithoutAttr (); } else HTM_SPAN_BeginWithoutAttr (); HTM_SPAN_NestingLevel++; } static void HTM_SPAN_BeginWithoutAttr (void) { HTM_Txt (""); } void HTM_SPAN_End (void) { if (HTM_SPAN_NestingLevel == 0) // No SPAN open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened SPAN."); HTM_Txt (""); HTM_SPAN_NestingLevel--; } /*****************************************************************************/ /*********************************** Lists ***********************************/ /*****************************************************************************/ void HTM_OL_Begin (void) { HTM_Txt ("
    "); HTM_OL_NestingLevel++; } void HTM_OL_End (void) { if (HTM_OL_NestingLevel == 0) // No OL open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened OL."); HTM_Txt ("
"); HTM_OL_NestingLevel--; } void HTM_UL_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_UL_BeginWithoutAttr (); } else HTM_UL_BeginWithoutAttr (); HTM_UL_NestingLevel++; } static void HTM_UL_BeginWithoutAttr (void) { HTM_Txt ("
      "); } void HTM_UL_End (void) { if (HTM_UL_NestingLevel == 0) // No UL open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened UL."); HTM_Txt ("
    "); HTM_UL_NestingLevel--; } /*****************************************************************************/ /******************************** List items *********************************/ /*****************************************************************************/ void HTM_LI_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_LI_BeginWithoutAttr (); } else HTM_LI_BeginWithoutAttr (); HTM_LI_NestingLevel++; } static void HTM_LI_BeginWithoutAttr (void) { HTM_Txt ("
  • "); } void HTM_LI_End (void) { if (HTM_LI_NestingLevel == 0) // No LI open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened LI."); HTM_Txt ("
  • "); HTM_LI_NestingLevel--; } /*****************************************************************************/ /****************************** Definition lists *****************************/ /*****************************************************************************/ void HTM_DL_Begin (void) { HTM_Txt ("
    "); 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_DL_NestingLevel--; } void HTM_DT_Begin (void) { 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_DT_NestingLevel--; } void HTM_DD_Begin (void) { 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); } } /*****************************************************************************/ /********************************* Fieldsets *********************************/ /*****************************************************************************/ void HTM_FIELDSET_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_FIELDSET_BeginWithoutAttr (); } else HTM_FIELDSET_BeginWithoutAttr (); HTM_FIELDSET_NestingLevel++; } static void HTM_FIELDSET_BeginWithoutAttr (void) { HTM_Txt ("
    "); } void HTM_FIELDSET_End (void) { if (HTM_FIELDSET_NestingLevel == 0) // No FIELDSET open Ale_ShowAlert (Ale_ERROR,"Trying to close unopened FIELDSET."); HTM_Txt ("
    "); HTM_FIELDSET_NestingLevel--; } void HTM_LEGEND (const char *Txt) { HTM_Txt (""); HTM_Txt (Txt); HTM_Txt (""); } /*****************************************************************************/ /*********************************** 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 ("