/// <summary>Create a valid, empty shell of a document, suitable for adding more elements to.</summary> /// <param name="baseUri">baseUri of document</param> /// <returns>document with html, head, and body elements.</returns> public static iText.StyledXmlParser.Jsoup.Nodes.Document CreateShell(String baseUri) { Validate.NotNull(baseUri); iText.StyledXmlParser.Jsoup.Nodes.Document doc = new iText.StyledXmlParser.Jsoup.Nodes.Document(baseUri); iText.StyledXmlParser.Jsoup.Nodes.Element html = doc.AppendElement("html"); html.AppendElement("head"); html.AppendElement("body"); return(doc); }
/// <summary>Normalise the document.</summary> /// <remarks> /// Normalise the document. This happens after the parse phase so generally does not need to be called. /// Moves any text content that is not in the body element into the body. /// </remarks> /// <returns>this document after normalisation</returns> public virtual iText.StyledXmlParser.Jsoup.Nodes.Document Normalise() { iText.StyledXmlParser.Jsoup.Nodes.Element htmlEl = FindFirstElementByTagName("html", this); if (htmlEl == null) { htmlEl = AppendElement("html"); } if (Head() == null) { htmlEl.PrependElement("head"); } if (Body() == null) { htmlEl.AppendElement("body"); } // pull text nodes out of root, html, and head els, and push into body. non-text nodes are already taken care // of. do in inverse order to maintain text order. NormaliseTextNodes(Head()); NormaliseTextNodes(htmlEl); NormaliseTextNodes(this); NormaliseStructure("head", htmlEl); NormaliseStructure("body", htmlEl); EnsureMetaCharsetElement(); return(this); }
/// <summary> /// Ensures a meta charset (html) or xml declaration (xml) with the current /// encoding used. /// </summary> /// <remarks> /// Ensures a meta charset (html) or xml declaration (xml) with the current /// encoding used. This only applies with /// <see cref="UpdateMetaCharsetElement(bool)">updateMetaCharset</see> /// set to /// <tt>true</tt>, otherwise this method does nothing. /// <ul> /// <li>An exsiting element gets updated with the current charset</li> /// <li>If there's no element yet it will be inserted</li> /// <li>Obsolete elements are removed</li> /// </ul> /// <p><b>Elements used:</b></p> /// <ul> /// <li><b>Html:</b> <i><meta charset="CHARSET"></i></li> /// <li><b>Xml:</b> <i><?xml version="1.0" encoding="CHARSET"></i></li> /// </ul> /// </remarks> private void EnsureMetaCharsetElement() { if (updateMetaCharset) { Syntax syntax = OutputSettings().Syntax(); if (syntax == Syntax.html) { iText.StyledXmlParser.Jsoup.Nodes.Element metaCharset = Select("meta[charset]").First(); if (metaCharset != null) { metaCharset.Attr("charset", Charset().DisplayName()); } else { iText.StyledXmlParser.Jsoup.Nodes.Element head = Head(); if (head != null) { head.AppendElement("meta").Attr("charset", Charset().DisplayName()); } } // Remove obsolete elements Select("meta[name=charset]").Remove(); } else { if (syntax == Syntax.xml) { iText.StyledXmlParser.Jsoup.Nodes.Node node = ChildNodes()[0]; if (node is XmlDeclaration) { XmlDeclaration decl = (XmlDeclaration)node; if (decl.Name().Equals("xml")) { decl.Attr("encoding", Charset().DisplayName()); String version = decl.Attr("version"); if (version != null) { decl.Attr("version", "1.0"); } } else { decl = new XmlDeclaration("xml", baseUri, false); decl.Attr("version", "1.0"); decl.Attr("encoding", Charset().DisplayName()); PrependChild(decl); } } else { XmlDeclaration decl = new XmlDeclaration("xml", baseUri, false); decl.Attr("version", "1.0"); decl.Attr("encoding", Charset().DisplayName()); PrependChild(decl); } } } } }