示例#1
0
        /// <summary>
        /// Check if a character can be used as a tag name.
        /// </summary>
        /// <param name="c">A character to check</param>
        /// <returns>True if can be used, otherwise false.</returns>

        #endif

        #region Methods

        public static Element GetElement(TokenType type, string str)
        {
            string orig_str = str;
            Element elem = null;
            switch (type)
            {
                case TokenType.Tag:
                    MatchCollection res = null;
                    switch (str[1])
                    {
                        case '!':
                            // Remove <! and >
                            str = str.Substring(2, str.Length - 3);
                            res = MatchAttribute.Matches(str);
                            foreach (Match i in res)
                            {
                                if (i.Groups["tag"].Success)
                                {
                                    elem = new Element(i.Groups["tag"].Value, Element.ElementType.Special, source: orig_str);
                                    elem.Attributes[str.Substring(i.Index + i.Length)] = "";
                                    break;
                                }
                            }
                            break;
                        case '/':
                            // Remove </ and >
                            str = str.Substring(2, str.Length - 3);
                            res = MatchAttribute.Matches(str);
                            elem = new Element("", Element.ElementType.Unknown, false, orig_str);
                            foreach (Match i in res)
                            {
                                if (i.Groups["tag"].Success)
                                {
                                    elem.Name = i.Groups["tag"].Value;
                                    if (HtmlTag.TagMap.ContainsKey(i.Groups["tag"].Value))
                                    {
                                        elem.Type = HtmlTag.TagMap[elem.Name].Type;
                                    }
                                }
                                else if (i.Groups["attribute"].Success)
                                {
                                    if (i.Groups["name"].Success && elem.Type != Element.ElementType.Unknown && HtmlTag.TagMap[elem.Name][i.Groups["name"].Value] != null)
                                    {
                                        elem.Attributes[i.Groups["name"].Value] = i.Groups["value"].Success ? SubstituteSpecialChar(i.Groups["value"].Value) : "";
                                    }
                                }
                            }
                            if (elem.Name == "") elem = null;
                            break;
                        default:
                            // Remove < and >
                            str = str.Substring(1, str.Length - 2);
                            res = MatchAttribute.Matches(str);
                            elem = new Element("", Element.ElementType.Unknown, source: orig_str);
                            foreach (Match i in res)
                            {
                                if (i.Groups["tag"].Success)
                                {
                                    elem.Name = i.Groups["tag"].Value;
                                    if (HtmlTag.TagMap.ContainsKey(i.Groups["tag"].Value))
                                    {
                                        elem.Type = HtmlTag.TagMap[elem.Name].Type;
                                    }
                                }
                                else if (i.Groups["attribute"].Success)
                                {
                                    if (i.Groups["name"].Success && elem.Type != Element.ElementType.Unknown && HtmlTag.TagMap[elem.Name][i.Groups["name"].Value] != null)
                                    {
                                        elem.Attributes[i.Groups["name"].Value] = i.Groups["value"].Success ? SubstituteSpecialChar(i.Groups["value"].Value) : "";
                                    }
                                }
                            }
                            if (elem.Name == "") elem = null;
                            break;
                    }
                    break;
                case TokenType.Text:
                    str = SubstituteSpecialChar(str);
                    elem = new Element(str, Element.ElementType.Text, source: orig_str);
                    break;
                case TokenType.Comment:
                    str = str.Substring(4, str.Length - 7);
                    elem = new Element("--", Element.ElementType.Special, source: orig_str);
                    elem.Attributes[str] = "";
                    break;
            }
            // If it failed to get an element, assume that it is a text.
            return elem ?? GetElement(TokenType.Text, orig_str);
        }
示例#2
0
 public HyperlinkInformation(Element elem)
 {
     Href = elem["HREF"];
     Name = elem["NAME"];
     Title = elem["TITLE"];
 }
示例#3
0
 public Entity(Element element)
 {
     Name = element.Name;
     Type = element.Type;
     IsStartTag = element.IsStartTag;
     Source = element.Source;
 }
示例#4
0
 /// <summary>
 /// Check an element can be a child.
 /// </summary>
 /// <param name="list">Parent elements</param>
 /// <param name="elem">An element to check</param>
 /// <returns>Return true if can be a child, otherwise false.</returns>
 static bool CheckCanExist(List<HtmlTag.Entity> list, Element elem)
 {
     bool flag = true;
     foreach (var i in list)
     {
         switch (elem.Type)
         {
             case Element.ElementType.Markup:
             case Element.ElementType.Object:
             case Element.ElementType.Structure:
                 if (!i.Children.Contains(HtmlTag.TagMap[elem.Name]))
                 {
                     flag = false;
                     return false;
                 }
                 break;
             case Element.ElementType.Text:
                 if (!i.Children.Contains(HtmlTag.TagMap["CDATA"]))
                 {
                     flag = false;
                     return false;
                 }
                 break;
             case Element.ElementType.Unknown:
             case Element.ElementType.Special:
                 flag = false;
                 return false;
         }
     }
     return flag;
 }
 /// <summary>
 /// Formats the debug message and writes it to the build log.
 /// </summary>
 /// <param name="currentInstance">The current Task instance which is performing the log.</param>
 /// <param name="format">The format string.</param>
 /// <param name="arguments">The format string arguments.</param>
 public static void LogDebug(Element currentInstance, string format, params object[] arguments)
 {
   string logMessage = string.Format(CultureInfo.CurrentCulture, format, arguments);
   currentInstance.Log(Level.Debug, logMessage);
 }
    /// <summary>
    /// Formats the error message and writes it to the build log.
    /// If the build threads aren't executed at the moment, a <see cref="BuildException" /> will be thrown.
    /// </summary>
    /// <param name="currentInstance">The current Task instance which is performing the log.</param>
    /// <param name="innerException">The inner exception.</param>
    /// <param name="format">The format string.</param>
    /// <param name="arguments">The format string arguments.</param>
    public static void LogError(Element currentInstance, Exception innerException, string format, params object[] arguments)
    {
      string logMessage = string.Format(CultureInfo.CurrentCulture, format, arguments);
      currentInstance.Log(Level.Error, logMessage);
      lock (LastExceptionLock)
      {
        BuildException buildException = new BuildException(logMessage, innerException);
        if (buildThreadsActive == false)
        {
          throw buildException;
        }

        if (lastException == null)
        {
          lastException = buildException;
        }
      }
    }
示例#7
0
        public void TestCoreDataStructure()
        {
            /* Test with 3.4 Example HTML Document in RFC 1866(http://www.ietf.org/rfc/rfc1866.txt)
             *
             * <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
             * <HTML>
             * <!-- Here’s a good place to put a comment. -->
             * <HEAD>
             * <TITLE>Structural Example</TITLE>
             * </HEAD><BODY>
             * <H1>First Header</H1>
             * <P>This is a paragraph in the example HTML file. Keep in mind
             * that the title does not appear in the document text, but that
             * the header (defined by H1) does.</P>
             * <OL>
             * <LI>First item in an ordered list.
             * <LI>Second item in an ordered list.
             * <UL COMPACT>
             * <LI> Note that lists can be nested;
             * <LI> Whitespace may be used to assist in reading the
             * HTML source.
             * </UL>
             * <LI>Third item in an ordered list.
             * </OL>
             * <P>This is an additional paragraph. Technically, end tags are
             * not required for paragraphs, although they are allowed. You can
             * include character highlighting in a paragraph. <EM>This sentence
             * of the paragraph is emphasized.</EM> Note that the &lt;/P&gt;
             * end tag has been omitted.
             * <P>
             * <IMG SRC ="triangle.xbm" alt="Warning: ">
             * Be sure to read these <b>bold instructions</b>.
             * </BODY></HTML>
             */
            Element doctype = new Element("DOCTYPE", Element.ElementType.Special);
            doctype.Attributes["HTML"] = "";
            doctype.Attributes["PUBLIC"] = "";
            doctype.Attributes["\"-//IETF//DTD HTML 2.0//EN\""] = "";
            Element html_s = new Element("HTML");
            Element comment = new Element("--", Element.ElementType.Special);
            comment.Attributes["Here’s a good place to put a comment."] = "";
            comment.Attributes["--"] = "";
            Element head_s = new Element("HEAD");
            Element title_s = new Element("TITLE");
            Element txt1 = new Element("Structural Example", Element.ElementType.Text);
            Element title_e = new Element("TITLE", Element.ElementType.Structure, false);
            Element head_e = new Element("HEAD", Element.ElementType.Structure, false);
            Element body_s = new Element("BODY");
            Element h1_s = new Element("H1");
            Element txt2 = new Element("First Header", Element.ElementType.Text);
            Element h1_e = new Element("H1", Element.ElementType.Structure, false);
            Element p1_s = new Element("P");
            Element txt3 = new Element("This is a paragraph in the example HTML file. Keep in mind that the title does not appear in the document text, but that the header (defined by H1) does.", Element.ElementType.Text);
            Element p1_e = new Element("P", Element.ElementType.Structure, false);
            Element ol_s = new Element("OL");
            Element li1 = new Element("LI");
            Element txt4 = new Element("First item in an ordered list.", Element.ElementType.Text);
            Element txt5 = new Element("Second item in an ordered list.", Element.ElementType.Text);
            Element ul_s = new Element("UL");
            ul_s.Attributes["COMPACT"] = "";
            Element li3 = new Element("LI");
            Element txt6 = new Element("Note that lists can be nested;", Element.ElementType.Text);
            Element li4 = new Element("LI");
            Element txt7 = new Element("Whitespace may be used to assist in reading the HTML source.", Element.ElementType.Text);
            Element ul_e = new Element("UL", Element.ElementType.Structure, false);
            Element li5 = new Element("LI");
            Element txt8 = new Element("Third item in an ordered list.", Element.ElementType.Text);
            Element ol_e = new Element("OL", Element.ElementType.Structure, false);
            Element p2 = new Element("P");
            Element txt9 = new Element("This is an additional paragraph. Technically, end tags are not required for paragraphs, although they are allowed. You can include character highlighting in a paragraph.", Element.ElementType.Text);
            Element em_s = new Element("EM", Element.ElementType.Markup);
            Element txt10 = new Element("This sentence of the paragraph is emphasized.", Element.ElementType.Text);
            Element em_e = new Element("EM", Element.ElementType.Markup, false);
            Element txt11 = new Element("Note that the &lt;/P&gt; end tag has been omitted.", Element.ElementType.Text);
            Element p3 = new Element("P");
            Element img = new Element("IMG", Element.ElementType.Object);
            img.Attributes["SRC"] = "triangle.xbm";
            img.Attributes["alt"] = "Warning: ";
            Element txt12 = new Element("Be sure to read these ", Element.ElementType.Text);
            Element b_s = new Element("b", Element.ElementType.Markup);
            Element txt13 = new Element("bold instructions", Element.ElementType.Text);
            Element b_e = new Element("b", Element.ElementType.Markup, false);
            Element txt14 = new Element(".", Element.ElementType.Text);
            Element body_e = new Element("BODY", Element.ElementType.Structure, false);
            Element html_e = new Element("HTML", Element.ElementType.Structure, false);

            Document doc = new Document();
            doc.Items.Add(doctype);
            doc.Items.Add(html_s);
            doc.Items.Add(comment);
            doc.Items.Add(head_s);
            doc.Items.Add(title_s);
            doc.Items.Add(txt1);
            doc.Items.Add(title_e);
            doc.Items.Add(head_e);
            doc.Items.Add(body_s);
            doc.Items.Add(h1_s);
            doc.Items.Add(txt2);
            doc.Items.Add(h1_e);
            doc.Items.Add(p1_s);
            doc.Items.Add(txt3);
            doc.Items.Add(p1_e);
            doc.Items.Add(ol_s);
            doc.Items.Add(li1);
            doc.Items.Add(txt4);
            doc.Items.Add(txt5);
            doc.Items.Add(ul_s);
            doc.Items.Add(li3);
            doc.Items.Add(txt6);
            doc.Items.Add(li4);
            doc.Items.Add(txt7);
            doc.Items.Add(ul_e);
            doc.Items.Add(li5);
            doc.Items.Add(txt8);
            doc.Items.Add(ol_e);
            doc.Items.Add(p2);
            doc.Items.Add(txt9);
            doc.Items.Add(em_s);
            doc.Items.Add(txt10);
            doc.Items.Add(em_e);
            doc.Items.Add(txt11);
            doc.Items.Add(p3);
            doc.Items.Add(img);
            doc.Items.Add(txt12);
            doc.Items.Add(b_s);
            doc.Items.Add(txt13);
            doc.Items.Add(b_e);
            doc.Items.Add(txt14);
            doc.Items.Add(body_e);
            doc.Items.Add(html_e);

            string Result = Function.MakeHtml(doc);

            Assert.AreEqual(true, true);
        }