📄 html.cxx
字号:
/* * html.cxx * * HTML classes. * * Portable Windows Library * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): ______________________________________. * * $Log: html.cxx,v $ * Revision 1.17 1998/11/30 04:51:51 robertj * New directory structure * * Revision 1.16 1998/09/23 06:22:04 robertj * Added open source copyright license. * * Revision 1.15 1998/01/26 02:49:15 robertj * GNU support. * * Revision 1.14 1997/06/16 13:18:03 robertj * Set Is() function to be const as it should have been. * * Revision 1.13 1996/08/19 13:40:31 robertj * Fixed incorrect formatting of HTML tags (cosmetic only). * * Revision 1.12 1996/06/28 13:08:55 robertj * Changed PHTML class so can create html fragments. * Fixed nesting problem in tables. * * Revision 1.11 1996/06/01 04:18:45 robertj * Fixed bug in RadioButton, having 2 VALUE fields * * Revision 1.10 1996/04/29 12:21:22 robertj * Fixed spelling error in assert. * Fixed check box HTML, should always have a value. * Added display of value of unclosed HTML element. * * Revision 1.9 1996/04/14 02:52:04 robertj * Added hidden fields to HTML. * * Revision 1.8 1996/03/31 09:03:07 robertj * Changed HTML token so doesn't have trailing CRLF. * * Revision 1.7 1996/03/16 04:54:06 robertj * Made the assert for unclosed HTML elements only on debug version. * * Revision 1.6 1996/03/12 11:30:33 robertj * Fixed resetting of HTML output using operator=. * * Revision 1.5 1996/03/10 13:14:55 robertj * Simplified some of the classes and added catch all string for attributes. * * Revision 1.4 1996/02/25 11:14:22 robertj * Radio button support for forms. * * Revision 1.3 1996/02/19 13:31:51 robertj * Removed MSC_VER test as now completely removed from WIN16 library. * * Revision 1.2 1996/02/08 12:24:30 robertj * Further implementation. * * Revision 1.1 1996/02/03 11:18:46 robertj * Initial revision * * Revision 1.3 1996/01/28 02:49:16 robertj * Further implementation. * * Revision 1.2 1996/01/26 02:24:30 robertj * Further implemetation. * * Revision 1.1 1996/01/23 13:04:32 robertj * Initial revision * */#ifdef __GNUC__#pragma implementation "html.h"#endif#include <ptlib.h>#include <ptclib/html.h>//////////////////////////////////////////////////////////////////////////////// PHTMLPHTML::PHTML(ElementInSet initialState){ memset(elementSet, 0, sizeof(elementSet)); tableNestLevel = 0; initialElement = initialState; switch (initialState) { case NumElementsInSet : break; case InBody : Set(InBody); break; case InForm : Set(InBody); Set(InForm); break; default : PAssertAlways(PInvalidParameter); }}PHTML::PHTML(const char * cstr){ memset(elementSet, 0, sizeof(elementSet)); tableNestLevel = 0; initialElement = NumElementsInSet; *this << Title(cstr) << Body() << Heading(1) << cstr << Heading(1);}PHTML::PHTML(const PString & str){ memset(elementSet, 0, sizeof(elementSet)); tableNestLevel = 0; initialElement = NumElementsInSet; *this << Title(str) << Body() << Heading(1) << str << Heading(1);}PHTML::~PHTML(){#ifndef NDEBUG if (initialElement != NumElementsInSet) { Clr(initialElement); Clr(InBody); } for (PINDEX i = 0; i < PARRAYSIZE(elementSet); i++) PAssert(elementSet[i] == 0, psprintf("Failed to close element %u", i));#endif}PHTML & PHTML::operator=(const char * cstr){ PStringStream::operator=(""); memset(elementSet, 0, sizeof(elementSet)); if (cstr != NULL && *cstr != '\0') *this << Title(cstr) << Body() << Heading(1) << cstr << Heading(1); return *this;}PHTML & PHTML::operator=(const PString & str){ return operator=((const char *)str);}PHTML & PHTML::operator=(const PHTML &){ PAssertAlways(PLogicError); return *this;}BOOL PHTML::Is(ElementInSet elmt) const{ return (elementSet[elmt>>3]&(1<<(elmt&7))) != 0;}void PHTML::Set(ElementInSet elmt){ elementSet[elmt>>3] |= (1<<(elmt&7));}void PHTML::Clr(ElementInSet elmt){ elementSet[elmt>>3] &= ~(1<<(elmt&7));}void PHTML::Toggle(ElementInSet elmt){ elementSet[elmt>>3] ^= (1<<(elmt&7));}void PHTML::Element::Output(PHTML & html) const{ PAssert(reqElement == NumElementsInSet || html.Is(reqElement), "HTML element out of context"); if (crlf == BothCRLF || (crlf == OpenCRLF && !html.Is(inElement))) html << "\r\n"; html << '<'; if (html.Is(inElement)) html << '/'; html << name; AddAttr(html); if (attr != NULL) html << ' ' << attr; html << '>'; if (crlf == BothCRLF || (crlf == CloseCRLF && html.Is(inElement))) html << "\r\n"; if (inElement != NumElementsInSet) html.Toggle(inElement);}void PHTML::Element::AddAttr(PHTML &) const{}PHTML::HTML::HTML(const char * attr) : Element("HTML", attr, InHTML, NumElementsInSet, BothCRLF){}PHTML::Head::Head() : Element("HEAD", NULL, InHead, NumElementsInSet, BothCRLF){}void PHTML::Head::Output(PHTML & html) const{ PAssert(!html.Is(InBody), "HTML element out of context"); if (!html.Is(InHTML)) html << HTML(); Element::Output(html);}PHTML::Body::Body(const char * attr) : Element("BODY", attr, InBody, NumElementsInSet, BothCRLF){}void PHTML::Body::Output(PHTML & html) const{ if (!html.Is(InHTML)) html << HTML(); if (html.Is(InTitle)) html << Title(); if (html.Is(InHead)) html << Head(); Element::Output(html); if (!html.Is(InBody)) html << HTML();}PHTML::Title::Title() : Element("TITLE", NULL, InTitle, InHead, CloseCRLF){ titleString = NULL;}PHTML::Title::Title(const char * titleCStr) : Element("TITLE", NULL, InTitle, InHead, CloseCRLF){ titleString = titleCStr;}PHTML::Title::Title(const PString & titleStr) : Element("TITLE", NULL, InTitle, InHead, CloseCRLF){ titleString = titleStr;}void PHTML::Title::Output(PHTML & html) const{ PAssert(!html.Is(InBody), "HTML element out of context"); if (!html.Is(InHead)) html << Head(); if (html.Is(InTitle)) { if (titleString != NULL) html << titleString; Element::Output(html); } else { Element::Output(html); if (titleString != NULL) { html << titleString; Element::Output(html); } }}PHTML::Banner::Banner(const char * attr) : Element("BANNER", attr, NumElementsInSet, InBody, BothCRLF){}PHTML::Division::Division(const char * attr) : Element("DIV", attr, InDivision, InBody, BothCRLF){}PHTML::Heading::Heading(int number, int sequence, int skip, const char * attr) : Element("H", attr, InHeading, InBody, CloseCRLF){ num = number; srcString = NULL; seqNum = sequence; skipSeq = skip;}PHTML::Heading::Heading(int number, const char * image, int sequence, int skip, const char * attr) : Element("H", attr, InHeading, InBody, CloseCRLF){ num = number; srcString = image; seqNum = sequence; skipSeq = skip;}PHTML::Heading::Heading(int number, const PString & imageStr, int sequence, int skip, const char * attr) : Element("H", attr, InHeading, InBody, CloseCRLF){ num = number; srcString = imageStr; seqNum = sequence; skipSeq = skip;}void PHTML::Heading::AddAttr(PHTML & html) const{ PAssert(num >= 1 && num <= 6, "Bad heading number"); html << num; if (srcString != NULL) html << " SRC=\"" << srcString << '"'; if (seqNum > 0) html << " SEQNUM=" << seqNum; if (skipSeq > 0) html << " SKIP=" << skipSeq;}PHTML::BreakLine::BreakLine(const char * attr) : Element("BR", attr, NumElementsInSet, InBody, CloseCRLF){}PHTML::Paragraph::Paragraph(const char * attr) : Element("P", attr, NumElementsInSet, InBody, OpenCRLF){}PHTML::PreFormat::PreFormat(int widthInChars, const char * attr) : Element("PRE", attr, InPreFormat, InBody, CloseCRLF){ width = widthInChars;}void PHTML::PreFormat::AddAttr(PHTML & html) const{ if (width > 0) html << " WIDTH=" << width;}PHTML::HotLink::HotLink(const char * href, const char * attr) : Element("A", attr, InAnchor, InBody, NoCRLF){ hrefString = href;}void PHTML::HotLink::AddAttr(PHTML & html) const{ if (hrefString != NULL && *hrefString != '\0') html << " HREF=\"" << hrefString << '"'; else PAssert(html.Is(InAnchor), PInvalidParameter);}PHTML::Target::Target(const char * name, const char * attr) : Element("A", attr, NumElementsInSet, InBody, NoCRLF){ nameString = name;}void PHTML::Target::AddAttr(PHTML & html) const{ if (nameString != NULL && *nameString != '\0') html << " NAME=\"" << nameString << '"';}PHTML::ImageElement::ImageElement(const char * n, const char * attr, ElementInSet elmt, ElementInSet req, OptionalCRLF c, const char * image) : Element(n, attr, elmt, req, c){ srcString = image;}void PHTML::ImageElement::AddAttr(PHTML & html) const{ if (srcString != NULL) html << " SRC=\"" << srcString << '"';}PHTML::Image::Image(const char * src, int w, int h, const char * attr) : ImageElement("IMG", attr, NumElementsInSet, InBody, NoCRLF, src){ altString = NULL; width = w; height = h;}PHTML::Image::Image(const char * src, const char * alt, int w, int h, const char * attr) : ImageElement("IMG", attr, NumElementsInSet, InBody, NoCRLF, src){ altString = alt; width = w; height = h;}void PHTML::Image::AddAttr(PHTML & html) const{ PAssert(srcString != NULL && *srcString != '\0', PInvalidParameter); if (altString != NULL) html << " ALT=\"" << altString << '"'; if (width != 0) html << " WIDTH=" << width; if (height != 0) html << " HEIGHT=" << height; ImageElement::AddAttr(html);}PHTML::HRule::HRule(const char * image, const char * attr) : ImageElement("HR", attr, NumElementsInSet, InBody, BothCRLF, image){}PHTML::Note::Note(const char * image, const char * attr) : ImageElement("NOTE", attr, InNote, InBody, BothCRLF, image){}PHTML::Address::Address(const char * attr) : Element("ADDRESS", attr, InAddress, InBody, BothCRLF){}PHTML::BlockQuote::BlockQuote(const char * attr) : Element("BQ", attr, InBlockQuote, InBody, BothCRLF){}PHTML::Credit::Credit(const char * attr) : Element("CREDIT", attr, NumElementsInSet, InBlockQuote, OpenCRLF){}PHTML::SetTab::SetTab(const char * id, const char * attr) : Element("TAB", attr, NumElementsInSet, InBody, NoCRLF){ ident = id;}void PHTML::SetTab::AddAttr(PHTML & html) const{ PAssert(ident != NULL && *ident != '\0', PInvalidParameter); html << " ID=" << ident;}PHTML::Tab::Tab(int indent, const char * attr) : Element("TAB", attr, NumElementsInSet, InBody, NoCRLF){ ident = NULL; indentSize = indent;}PHTML::Tab::Tab(const char * id, const char * attr) : Element("TAB", attr, NumElementsInSet, InBody, NoCRLF){ ident = id; indentSize = 0;}void PHTML::Tab::AddAttr(PHTML & html) const{ PAssert(indentSize!=0 || (ident!=NULL && *ident!='\0'), PInvalidParameter); if (indentSize > 0) html << " INDENT=" << indentSize; else html << " TO=" << ident;}PHTML::SimpleList::SimpleList(const char * attr) : Element("UL", attr, InList, InBody, BothCRLF){}void PHTML::SimpleList::AddAttr(PHTML & html) const{ html << " PLAIN";}PHTML::BulletList::BulletList(const char * attr) : Element("UL", attr, InList, InBody, BothCRLF){}PHTML::OrderedList::OrderedList(int seqNum, const char * attr) : Element("OL", attr, InList, InBody, BothCRLF){ sequenceNum = seqNum;}void PHTML::OrderedList::AddAttr(PHTML & html) const{ if (sequenceNum > 0) html << " SEQNUM=" << sequenceNum; if (sequenceNum < 0) html << " CONTINUE";}PHTML::DefinitionList::DefinitionList(const char * attr) : Element("DL", attr, InList, InBody, BothCRLF){}PHTML::ListHeading::ListHeading(const char * attr) : Element("LH", attr, InListHeading, InList, CloseCRLF){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -