private void EnsureDocumentHasTogglingScript(Element element) { Element rootElement = element.GetRootElement(); if (!RootElementsWithScript.Contains(rootElement)) { RootElementsWithScript.Add(rootElement); Element head = rootElement.GetFirstDescendantNamed("head"); if (head == null) { Console.WriteLine(rootElement.ToXml()); } Check.NotNull(head, "Document <head> section is missing"); Element script = new Element("script").AddAttribute("type", "text/javascript"); head.PrependChild(script); script.AppendText(HtmlFramework.TOGGLING_SCRIPT_RESOURCE); } }