Skip to content

Commit

Permalink
#64: Get title from 'title' element.
Browse files Browse the repository at this point in the history
  • Loading branch information
RusKnyaz committed Oct 7, 2021
1 parent 7aba69d commit 727447e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions source/Knyaz.Optimus.JsTests/CommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
29 changes: 29 additions & 0 deletions source/Knyaz.Optimus.JsTests/Tests/DocumentTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
12 changes: 12 additions & 0 deletions source/Knyaz.Optimus/Dom/Elements/HtmlTitleElement.cs
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
}
21 changes: 20 additions & 1 deletion source/Knyaz.Optimus/Dom/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -465,10 +466,28 @@ internal void HandleFormSubmit(HtmlFormElement htmlFormElement, HtmlElement subm

public string CompatMode => ChildNodes.OfType<DocType>().Any() ? "CSS1Compat" : "BackCompat";


private HtmlTitleElement GetTitleElement() => (HtmlTitleElement)GetElementsByTagName(TagsNames.Title).FirstOrDefault();

/// <summary>
/// Sets or gets the title of the document.
/// </summary>
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;
}
}

/// <summary> Gets the currently focused element in the document. </summary>
public Element ActiveElement { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions source/Knyaz.Optimus/Dom/TagsNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public static class TagsNames
/// <summary>THEAD</summary>
/// <remarks>Table header.</remarks>
public const string THead = "THEAD";

public const string Title = "TITLE";

/// <summary>TR</summary>
/// <remarks>Table row.</remarks>
Expand Down

0 comments on commit 727447e

Please sign in to comment.