Append() public method

Insert content, specified by the parameter, to the end of each element in the set of matched elements.
public Append ( ) : CQ
return CQ
コード例 #1
0
ファイル: Page.cs プロジェクト: EusthEnoptEron/BakaPrince
        private string PrepareHtml(string html)
        {
            // Make title
            if (!Notitle)
            {
                html = "<h2>" + Title + "</h2>" + html;
            }
             html = "<span class=\"invisible chapterstart\">" + (Noheader ? "" : Title) + "</span>" + html;

            // Make sure page break is set
            if (Pagebreak)
            {
                html = "<span class=\"invisible pagebreak\"></span>" + html;
            }

            CQ dom = CQ.CreateFragment("<div class=\"content\">" + html + "</div>");

            // Remove next/prev table
            dom.Find("table:contains('Forward'):contains('Back')").Last().Remove();

            // Find images
            foreach(IDomElement aNode in dom.Find("a.image")) {
                var a = new CQ(aNode);
                var img = new CQ(a.Find("img"));

                var src = img.Attr("src").Replace("/thumb", "");
                src = Regex.Replace(src, @"[.](jpg|png|gif)\/.+$", @".$1", RegexOptions.IgnoreCase);

                var image = new Image(src, new Uri(Wiki)) {Sashie = true};

                CQ node = a.Closest(".thumb").Add(a).First();

                if (_images.Count == 0 && EntryPicture)
                {
                    // We can view it as a full-fledged image since we don't need to worry about text-flow
                    image.Sashie = false;
                    dom.Before(image.Html);
                }
                else
                {
                    node.Before(image.Html);
                    //node.After("<span class=\"image-stopper\"></span>");
                }

                node.Remove();

                _images.Add(image);
            }

            // Catch references
            foreach (IDomElement supNode in dom.Find("sup.reference"))
            {
                var sup = new CQ(supNode);
                CQ footnote = "<span class=\"fn\"></span>";
                CQ oldFootnote = dom.Find("#" + sup.Attr("id").Replace("_ref-", "_note-"));

                footnote.Html(oldFootnote.Find(".reference-text").Html());

                oldFootnote.Remove();
                sup.Before(footnote).Remove();
            }
            // Remove possible reference title
            dom.Find(".references").Prev(":header").Remove();

            // Remove edit links
            dom.Find(".editsection, #toc").Remove();

            // Make smart quotes
            dom.Find("p:contains('\"'), p:contains(\"'\"), li:contains('\"'), li:contains(\"'\")").Each((el) =>
            {
                CQ p = new CQ(el);
                string pHtml = p.Html();

                // Replace quotes
                if (Regex.Matches(pHtml, "&quot;").Count % 2 == 0)
                {

                    pHtml = Regex.Replace(pHtml, "&quot;(.+?)&quot;", "“$1”");
                }
                else
                {
                    Console.WriteLine("NOTICE: possible quotes problem ({0})", pHtml.Trim());
                }

                // Replace single quotes (\b doesn't work)
                pHtml = Regex.Replace(pHtml, "(?<!\\w)'(.+?)'(?!\\w)", "‘$1’");
                // Replace apostrophes
                pHtml = Regex.Replace(pHtml, "'", "’");

                p.Html(pHtml);
            });

            // Parse Ruby
            dom.Find("span > span > span").Each(el =>
            {
                var rubySpan = new CQ(el);
                if(rubySpan.Css("position") == "relative" && rubySpan.Css("left") == "-50%") {
                    var textSpan = rubySpan.Parent().Siblings("span");
                    var containerSpan = textSpan.Parent();
                    if (textSpan.Length == 1 && containerSpan.Css("white-space") == "nowrap")
                    {
                        // Okay, this is ruby.
                        var ruby = new CQ("<ruby>");
                        ruby.Html(textSpan.Html());
                        ruby.Append(new CQ("<rp>(</rp>"));
                        ruby.Append(new CQ("<rt>").Html(rubySpan.Html()));
                        ruby.Append(new CQ("<rp>)</rp>"));

                        containerSpan.ReplaceWith(
                            ruby
                        );

                    }
                }
            });

            // Hakomari specific
            foreach (IDomElement star in dom.Find("p:contains(✵)"))
            {
                star.InnerHTML = "<img src=\"" + (new Uri( Helper.GetAssetsPath() + "blackstar.jpg" )) + "\">";
            }

            return dom.Render();
        }
コード例 #2
0
ファイル: Traversing.cs プロジェクト: kaleb/CsQuery
        public void Contents()
        {
           
            Assert.AreEqual(jQuery("#ap").Contents().Length, 9, "Check element contents" );
            Assert.IsTrue( jQuery("#iframe").Length>0, "Check existence of IFrame document" );

            jQuery("#loadediframe").Append(@"<html>
              <head>
                <title>iframe</title>
              </head>
              <body>
                <div><span>span text</span></div>
              </body>
            </html>");
            
            var ibody = jQuery("#loadediframe").Contents().Find("body");
            Assert.IsTrue( ibody.Length>0, "Check existance of IFrame body" );
            var csq = new CQ("span", ibody);
            Assert.AreEqual(csq.Text(), "span text", "Find span in IFrame and check its text" );

            jQuery(ibody).Append("<div>init text</div>");
            csq = new CQ("div", ibody);
            Assert.AreEqual(csq.Length, 2, "Check the original div and the new div are in IFrame" );
            csq = new CQ("div:last", ibody);
            Assert.AreEqual(csq.Text(), "init text", "Add text to div in IFrame" );

            csq = new CQ("div:last", ibody);
            csq.Text("div text");
            Assert.AreEqual(new CQ("div:last", ibody).Text(), "div text", "Add text to div in IFrame");

            csq = new CQ("div:last", ibody).Remove();
            Assert.AreEqual(jQuery("div",ibody).Length, 1, "Delete the div and check only one div left in IFrame" );

            Assert.AreEqual(jQuery("div",ibody).Text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );

            csq = new CQ("<table/>", ibody);
            csq.Append("<tr><td>cell</td></tr>").AppendTo(ibody);
            csq = new CQ("table", ibody);
            csq.Remove();
            csq = new CQ("div", ibody);
            Assert.AreEqual(csq.Length, 1, "Check for JS error on add and delete of a table in IFrame" );

            // using contents will get comments regular, text, and comment nodes
            var c = jQuery("#nonnodes").Contents().Contents();
            Assert.AreEqual(c.Length, 1, "Check node,textnode,comment contents is just one" );
            Assert.AreEqual(c[0].NodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
        }