diff --git a/source/Knyaz.Optimus.JsTests/CommonTests.cs b/source/Knyaz.Optimus.JsTests/CommonTests.cs index 9a4cebb..a79354c 100644 --- a/source/Knyaz.Optimus.JsTests/CommonTests.cs +++ b/source/Knyaz.Optimus.JsTests/CommonTests.cs @@ -52,6 +52,9 @@ public class CommonTests [TestCase("Images")] [TestCase("Links")] [TestCase("CreateAttribute")] + [TestCase("TitleFromElement")] + [TestCase("SetTitle")] + [TestCase("UpdateTitle")] public void DocumentTests(string testName) => JsTestsRunner.Run(_jsEngine, testName); [TestCase("CommentRemove")] diff --git a/source/Knyaz.Optimus.JsTests/Tests/DocumentTests.js b/source/Knyaz.Optimus.JsTests/Tests/DocumentTests.js index 3b4192d..ad9452b 100644 --- a/source/Knyaz.Optimus.JsTests/Tests/DocumentTests.js +++ b/source/Knyaz.Optimus.JsTests/Tests/DocumentTests.js @@ -320,5 +320,34 @@ Test("DocumentTests", { Assert.AreEqual("lay-submit", attr.name); Assert.AreEqual('', attr.value); } + }, + "TitleFromElement":{ + run:function(){ + var doc = document.implementation.createHTMLDocument(); + var title = doc.createElement('title'); + title.textContent = "Hello"; + doc.head.appendChild(title); + Assert.AreEqual("Hello", doc.title); + } + }, + "SetTitle": { + run:function(){ + var doc = document.implementation.createHTMLDocument(); + doc.title = "Hello" + Assert.AreEqual("Hello", doc.title); + var title = doc.getElementsByTagName("title")[0]; + Assert.AreEqual("Hello", title.textContent); + } + }, + "UpdateTitle":{ + run:function(){ + var doc = document.implementation.createHTMLDocument(); + var title = doc.createElement('title'); + title.textContent = "Hello"; + doc.head.appendChild(title); + Assert.AreEqual("Hello", doc.title); + doc.title = "world"; + Assert.AreEqual("world", title.textContent); + } } }); \ No newline at end of file diff --git a/source/Knyaz.Optimus/Dom/Elements/HtmlTitleElement.cs b/source/Knyaz.Optimus/Dom/Elements/HtmlTitleElement.cs new file mode 100644 index 0000000..73c626b --- /dev/null +++ b/source/Knyaz.Optimus/Dom/Elements/HtmlTitleElement.cs @@ -0,0 +1,12 @@ +using Knyaz.Optimus.ScriptExecuting; + +namespace Knyaz.Optimus.Dom.Elements +{ + [JsName("HTMLTitleElement")] + public class HtmlTitleElement : HtmlElement + { + internal HtmlTitleElement(HtmlDocument ownerDocument) : base(ownerDocument, TagsNames.Title) + { + } + } +} \ No newline at end of file diff --git a/source/Knyaz.Optimus/Dom/HtmlDocument.cs b/source/Knyaz.Optimus/Dom/HtmlDocument.cs index 0b98c34..47ff0eb 100644 --- a/source/Knyaz.Optimus/Dom/HtmlDocument.cs +++ b/source/Knyaz.Optimus/Dom/HtmlDocument.cs @@ -311,6 +311,7 @@ public Element CreateElement(string tagName) case TagsNames.Head:return new Head(this); case TagsNames.Body:return new HtmlBodyElement(this); case TagsNames.Textarea: return new HtmlTextAreaElement(this); + case TagsNames.Title: return new HtmlTitleElement(this); case TagsNames.Form:return new HtmlFormElement(this); case TagsNames.IFrame:return new HtmlIFrameElement(this); case TagsNames.Html:return new HtmlHtmlElement(this); @@ -465,10 +466,28 @@ internal void HandleFormSubmit(HtmlFormElement htmlFormElement, HtmlElement subm public string CompatMode => ChildNodes.OfType().Any() ? "CSS1Compat" : "BackCompat"; + + private HtmlTitleElement GetTitleElement() => (HtmlTitleElement)GetElementsByTagName(TagsNames.Title).FirstOrDefault(); + /// /// Sets or gets the title of the document. /// - public string Title { get; set; } + public string Title + { + get => GetTitleElement()?.TextContent ?? string.Empty; + set + { + var elt = GetTitleElement(); + if (elt == null && !string.IsNullOrEmpty(value)) + { + elt = (HtmlTitleElement)CreateElement("title"); + Head.AppendChild(elt); + } + + if(elt != null) + elt.TextContent = value; + } + } /// Gets the currently focused element in the document. public Element ActiveElement { get; set; } diff --git a/source/Knyaz.Optimus/Dom/TagsNames.cs b/source/Knyaz.Optimus/Dom/TagsNames.cs index 1b6f5d8..a53df6d 100644 --- a/source/Knyaz.Optimus/Dom/TagsNames.cs +++ b/source/Knyaz.Optimus/Dom/TagsNames.cs @@ -125,6 +125,8 @@ public static class TagsNames /// THEAD /// Table header. public const string THead = "THEAD"; + + public const string Title = "TITLE"; /// TR /// Table row.