// Rendu d'un élément HTML public string renderHtmlElement(HtmlElement elem) { string res = ""; //on détermine le type de balise bool multiline = HtmlElement.multiLineTags.Exists(v => v == elem.getTagname()); bool monoline = HtmlElement.monoLineTags.Exists(v => v == elem.getTagname()); bool inline = (!multiline && !monoline); //chaine des attributs string resattr = ""; if (elem.attributes.Count > 0) { foreach (HtmlTagAttribute attr in elem.attributes) { resattr += " "; resattr += renderHtmlTagAttribute(attr); } } //OpenTag //if (multiline) res += '\n'; res += renderHtmlTag(elem.getOpenTag(), resattr); //retour à la ligne post-OpenTag multiline if (multiline) res += '\n'; //content if (elem.tagContent.Count > 0) { foreach (HtmlTagContent e in elem.tagContent) { if (e is HtmlElement) res += renderHtmlElement(e as HtmlElement); else if (e is HtmlText) res += renderHtmlText(e as HtmlText); } } //indentation avant les multilines (intent ='' pour les inlines) if (multiline) res += '\n'; //EndTag if (elem.isClosed()) res += renderHtmlTag(elem.getEndTag()); //retour à la ligne post-non-inline if (!inline) res += '\n'; return res; }
public HtmlTagContent(HtmlElement parent) : this() { _parent = parent; }
private string _title; //Titre #endregion Fields #region Constructors // Constructeurs ====================================================================================================== public HtmlPage() { //Page exemple /*_mainTag = new HtmlElement("html"); _mainTag.closeTag(); _doctype = defaultDoctype; _title = "Bonjour, bienvenue, Hello world ! C'est tout !"; _headTag = new HtmlElement("head"); _headTag.closeTag(); _headTag.addContent(titleTag()); _mainTag.addContent(_headTag); _bodyTag = new HtmlElement("body"); _bodyTag.attributes.Add(new HtmlTagAttribute("id", "Tamère")); _bodyTag.closeTag(); _mainTag.addContent(_bodyTag); _cssTag = new HtmlElement("link"); _cssTag.attributes.Add(new HtmlTagAttribute("type", "text/css")); _cssTag.attributes.Add(new HtmlTagAttribute("rel", "stylesheet")); _cssTag.attributes.Add(new HtmlTagAttribute("href", cssFile)); _headTag.addContent(_cssTag); _encodingTag = new HtmlElement("meta"); _encodingTag.attributes.Add(new HtmlTagAttribute("charset", "UTF-8")); _headTag.addContent(_encodingTag); HtmlElement _baliseH1 = new HtmlElement("h1"); _baliseH1.addContent(new HtmlText("Ceci est un magnifique titre !")); _baliseH1.attributes.Add(new HtmlTagAttribute("class", "montitre")); _baliseH1.closeTag(); _bodyTag.addContent(_baliseH1); HtmlElement _baliseH2 = new HtmlElement("h2"); _baliseH2.addContent(new HtmlText("Et ça, c'est un impressionnant sous-titre !")); _baliseH2.closeTag(); _bodyTag.addContent(_baliseH2); HtmlElement _baliseP = new HtmlElement("p"); _baliseP.addContent(new HtmlText("Lorem Ipsum et de toute façon je met le texte que je veux ce sera beautiful !")); HtmlElement _baliseBR = new HtmlElement("br"); _baliseP.addContent(_baliseBR); _baliseP.addContent(new HtmlText("N'est-ce pas ?")); _baliseP.closeTag(); _bodyTag.addContent(_baliseP); HtmlElement _baliseDIV1 = new HtmlElement("div"); _baliseDIV1.attributes.Add(new HtmlTagAttribute("id", "DTC")); _baliseDIV1.closeTag(); _bodyTag.addContent(_baliseDIV1); HtmlElement _baliseDIV2 = new HtmlElement("div"); _baliseDIV2.closeTag(); _baliseDIV1.addContent(_baliseDIV2); HtmlElement _baliseH3 = new HtmlElement("h3"); _baliseH3.addContent(new HtmlText("Et ça, c'est un scintillant sous-sous-titre !")); _baliseH3.closeTag(); _baliseDIV2.addContent(_baliseH3); HtmlElement _baliseP2 = new HtmlElement("p"); _baliseP2.addContent(new HtmlText("Barquette !! Barquette !!! J'aimerais mon niveau deux, s'il vous plait.")); HtmlElement _baliseBR2 = new HtmlElement("br"); _baliseP2.addContent(_baliseBR2); _baliseP2.addContent(new HtmlText("Je veux juste faire une longue phrase qui servira à tester le retour à la ligne non-automatique du TextBox.")); _baliseP2.closeTag(); _baliseDIV2.addContent(_baliseP2);*/ _mainTag = new HtmlElement("html"); _mainTag.closeTag(); _doctype = defaultDoctype; _title = "Vous jouez actuellement à cHTeMeLe !"; _headTag = new HtmlElement("head"); _headTag.closeTag(); _headTag.addContent(titleTag()); _mainTag.addContent(_headTag); _bodyTag = new HtmlElement("body"); //_bodyTag.attributes.Add(new HtmlTagAttribute("id", "Tamère")); _bodyTag.closeTag(); _mainTag.addContent(_bodyTag); _cssTag = new HtmlElement("link"); _cssTag.attributes.Add(new HtmlTagAttribute("type", "text/css")); _cssTag.attributes.Add(new HtmlTagAttribute("rel", "stylesheet")); _cssTag.attributes.Add(new HtmlTagAttribute("href", cssFile)); _headTag.addContent(_cssTag); _encodingTag = new HtmlElement("meta"); _encodingTag.attributes.Add(new HtmlTagAttribute("charset", "UTF-8")); _headTag.addContent(_encodingTag); }
// Rendu d'un élément HTML public List<StrTypePair> renderHtmlElement(HtmlElement elem) { List<StrTypePair> res = new List<StrTypePair>(); //on détermine le type de balise bool multiline = HtmlElement.multiLineTags.Exists(v => v == elem.getTagname()); bool monoline = HtmlElement.monoLineTags.Exists(v => v == elem.getTagname()); bool inline = (!multiline && !monoline); _isAutoTag = _htmlAutoTags.Exists(v => v == elem.getTagname()); //chaine des attributs List<StrTypePair> resattr = new List<StrTypePair>(); if (elem.attributes.Count > 0) { foreach (HtmlTagAttribute attr in elem.attributes) { resattr.Add(new StrTypePair(" ", (_isAutoTag) ? StrTypePair.StrType.AUTO_TAG : StrTypePair.StrType.USR_TAG)); resattr.AddRange(renderHtmlTagAttribute(attr)); } } //OpenTag //if (multiline) res += '\n'; res.AddRange(renderHtmlTag(elem.getOpenTag(), resattr)); //retour à la ligne post-OpenTag multiline if (multiline) res.Add(new StrTypePair("\n", StrTypePair.StrType.LINE_BREAK)); //content if (elem.tagContent.Count > 0) { foreach (HtmlTagContent e in elem.tagContent) { if(e is HtmlElement) res.AddRange(renderHtmlElement(e as HtmlElement)); else if(e is HtmlText) res.Add(renderHtmlText(e as HtmlText)); } } //indentation avant les multilines (intent ='' pour les inlines) if (multiline) res.Add(new StrTypePair("\n", StrTypePair.StrType.LINE_BREAK)); //EndTag if (elem.isClosed()) res.AddRange(renderHtmlTag(elem.getEndTag())); //retour à la ligne post-non-inline if (!inline) res.Add(new StrTypePair("\n", StrTypePair.StrType.LINE_BREAK)); return res; }
public HtmlElement titleTag() { HtmlElement ret = new HtmlElement("title"); ret.addContent(new HtmlText(_title)); ret.closeTag(); return ret; }