Bloom.Book.XMatterHelper.InjectFlyleafIfNeeded C# (CSharp) Method

InjectFlyleafIfNeeded() private method

Some book layouts rely on the first page facing the second page. A Wall Calendar is one example. Here we check if the first content page has this requirement and, if so, we insert a blank "flyleaf" page.
private InjectFlyleafIfNeeded ( Layout layout ) : void
layout Layout
return void
        private void InjectFlyleafIfNeeded(Layout layout)
        {
            // the "inside" here means "not counting the cover"
            var numberOfFrontMatterPagesInside = XMatterDom.SafeSelectNodes("//div[contains(@class,'bloom-frontMatter')]").Count - 1;
            var firstPageWouldNotBePartOfASpread = numberOfFrontMatterPagesInside%2 != 0;

            if(firstPageWouldNotBePartOfASpread)
            {
                var lastFrontMatterPage = _bookDom.SelectSingleNode("//div[contains(@class,'bloom-frontMatter')][last()]");

                var firstContentPageAndAlsoStartsSpread = _bookDom.SelectSingleNode(
                    "//div[contains(@class,'bloom-frontMatter')][last()]" // last frontmatter page
                    + "/following-sibling::div[contains(@data-page, 'spread-start')]");
                    // page after that says it needs to be facing the next page
                if(firstContentPageAndAlsoStartsSpread != null)
                {
                    var flyDom = new XmlDocument();
                    flyDom.LoadXml(@"
                        <div class='bloom-flyleaf bloom-frontMatter bloom-page' data-page='required singleton'>
                            <div class='pageLabel'>Flyleaf</div>
                            <div style='height: 100px; width:100%'
                                data-hint='This page was automatically inserted because the following page is marked as part of a two page spread.'>
                            </div>
                        </div>");
                    var flyleaf = _bookDom.RawDom.ImportNode(flyDom.FirstChild, true) as XmlElement;
                    flyleaf.SetAttribute("id", Guid.NewGuid().ToString());
                    lastFrontMatterPage.ParentNode.InsertAfter(flyleaf, lastFrontMatterPage);
                    SizeAndOrientation.UpdatePageSizeAndOrientationClasses(flyleaf, layout);
                }
            }
        }