forked from jamietre/CsQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jamietre#118 from joelverhagen/master
Update Form properties on some elements. Added button interface and implementation.
- Loading branch information
Showing
19 changed files
with
387 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Text; | ||
using System.Reflection; | ||
using System.Diagnostics; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NUnit.Framework; | ||
using Assert = NUnit.Framework.Assert; | ||
using Description = NUnit.Framework.DescriptionAttribute; | ||
using TestContext = Microsoft.VisualStudio.TestTools.UnitTesting.TestContext; | ||
using CsQuery; | ||
using CsQuery.HtmlParser; | ||
using CsQuery.Utility; | ||
|
||
namespace CsQuery.Tests.HtmlParser | ||
{ | ||
|
||
[TestFixture, TestClass] | ||
public class Button : CsQueryTest | ||
{ | ||
/// <summary> | ||
/// Ensure a parsed button implements the button interface. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void ImplementsInterface() | ||
{ | ||
CQ cq = CQ.Create("<button>Boo!</button>"); | ||
IHTMLButtonElement buttonElement = cq["button"].FirstElement() as IHTMLButtonElement; | ||
|
||
Assert.IsNotNull(buttonElement); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure the button type is always lowercase. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void LowercaseType() | ||
{ | ||
CQ cq = CQ.Create("<button type=SEARCH>Boo!</button>"); | ||
IHTMLButtonElement buttonElement = cq["button"].FirstElement() as IHTMLButtonElement; | ||
|
||
Assert.IsNotNull(buttonElement); | ||
Assert.AreEqual("search", buttonElement.Type); | ||
Assert.AreEqual("SEARCH", buttonElement.GetAttribute("type")); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure the button type defaults to submit. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void DefaultSubmitType() | ||
{ | ||
CQ cq = CQ.Create("<button>Boo!</button>"); | ||
IHTMLButtonElement buttonElement = cq["button"].FirstElement() as IHTMLButtonElement; | ||
|
||
Assert.IsNotNull(buttonElement); | ||
Assert.AreEqual("submit", buttonElement.Type); | ||
Assert.IsFalse(buttonElement.HasAttribute("type")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Text; | ||
using System.Reflection; | ||
using System.Diagnostics; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NUnit.Framework; | ||
using Assert = NUnit.Framework.Assert; | ||
using Description = NUnit.Framework.DescriptionAttribute; | ||
using TestContext = Microsoft.VisualStudio.TestTools.UnitTesting.TestContext; | ||
using CsQuery; | ||
using CsQuery.HtmlParser; | ||
using CsQuery.Utility; | ||
|
||
namespace CsQuery.Tests.HtmlParser | ||
{ | ||
|
||
[TestFixture, TestClass] | ||
public class FormAssociatedElement : CsQueryTest | ||
{ | ||
/// <summary> | ||
/// Ensure the Form property returns the closest form. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void ClosestForm() | ||
{ | ||
CQ cq = CQ.Create("<form id=parent><div><input></div></form>"); | ||
IHTMLInputElement input = cq["input"].FirstElement() as IHTMLInputElement; | ||
|
||
Assert.IsNotNull(input); | ||
Assert.IsNotNull(input.Form); | ||
Assert.AreEqual("parent", input.Form.Id); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure that the form attribute is observed on a form-reassociateable element. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void ExplicitFormIdSpecified() | ||
{ | ||
CQ cq = CQ.Create("<form id=a><div><input form=b></div></form><form id=b></form>"); | ||
IHTMLInputElement input = cq["input"].FirstElement() as IHTMLInputElement; | ||
|
||
Assert.IsNotNull(input); | ||
Assert.IsNotNull(input.Form); | ||
Assert.AreEqual("b", input.Form.Id); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure that the form attribute is not observed on an a form associated element this is not form-reassociateable. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void ExplicitFormIdIgnoredOnNonFormReassociateable() | ||
{ | ||
CQ cq = CQ.Create("<form id=a><div><label form=b></div></form><form id=b></form>"); | ||
IHTMLLabelElement label = cq["label"].FirstElement() as IHTMLLabelElement; | ||
|
||
Assert.IsNotNull(label); | ||
Assert.IsNotNull(label.Form); | ||
Assert.AreEqual("a", label.Form.Id); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure that the options share the the form of their select elements. | ||
/// </summary> | ||
[Test, TestMethod] | ||
public void OptionSharesFormOfSelect() | ||
{ | ||
CQ cq = CQ.Create("<form id=a><select form=b><option></option></select></form><form id=b></form>"); | ||
IHTMLOptionElement option = cq["option"].FirstElement() as IHTMLOptionElement; | ||
|
||
Assert.IsNotNull(option); | ||
Assert.IsNotNull(option.Form); | ||
Assert.AreEqual("b", option.Form.Id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace CsQuery | ||
{ | ||
/// <summary> | ||
/// An HTML BUTTON element. | ||
/// </summary> | ||
/// | ||
/// <url> | ||
/// http://dev.w3.org/html5/markup/button.html | ||
/// </url> | ||
|
||
public interface IHTMLButtonElement : IDomElement | ||
{ | ||
/// <summary> | ||
/// The form with which to associate the element. | ||
/// </summary> | ||
|
||
IHTMLFormElement Form {get;} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace CsQuery | ||
{ | ||
/// <summary> | ||
/// An HTML TEXTAREA element. | ||
/// </summary> | ||
/// | ||
/// <url> | ||
/// http://dev.w3.org/html5/markup/textarea.html | ||
/// </url> | ||
|
||
public interface IHTMLTextAreaElement : IDomElement | ||
{ | ||
/// <summary> | ||
/// The form with which to associate the element. | ||
/// </summary> | ||
|
||
IHTMLFormElement Form { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
source/CsQuery/Dom/Implementation/HtmlElements/FormAssociatedElement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using CsQuery.HtmlParser; | ||
using CsQuery.Implementation; | ||
|
||
namespace CsQuery.Dom.Implementation.HtmlElements | ||
{ | ||
public abstract class FormAssociatedElement : DomElement | ||
{ | ||
/// <summary> | ||
/// Constructor to specify the element's token ID. | ||
/// </summary> | ||
/// <param name="tokenId">The token ID of the element.</param> | ||
protected FormAssociatedElement(ushort tokenId) | ||
: base(tokenId) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// The value of form element with which to associate the element. | ||
/// </summary> | ||
/// | ||
/// <remarks> | ||
/// The HTML5 spec says "The value of the id attribute on the form with which to associate the | ||
/// element." This is not what browsers currently return; they return the actual element. We'll | ||
/// keep that for now. | ||
/// </remarks> | ||
|
||
public IHTMLFormElement Form | ||
{ | ||
get | ||
{ | ||
return Closest(HtmlData.tagFORM) as IHTMLFormElement; | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
source/CsQuery/Dom/Implementation/HtmlElements/FormReassociateableElement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using CsQuery.HtmlParser; | ||
using CsQuery.Implementation; | ||
|
||
namespace CsQuery.Dom.Implementation.HtmlElements | ||
{ | ||
public abstract class FormReassociateableElement : DomElement | ||
{ | ||
/// <summary> | ||
/// Constructor to specify the element's token ID. | ||
/// </summary> | ||
/// <param name="tokenId">The token ID of the element.</param> | ||
protected FormReassociateableElement(ushort tokenId) | ||
: base(tokenId) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// The value of form element with which to associate the element. | ||
/// </summary> | ||
/// | ||
/// <remarks> | ||
/// The HTML5 spec says "The value of the id attribute on the form with which to associate the | ||
/// element." This is not what browsers currently return; they return the actual element. We'll | ||
/// keep that for now. If the "form" attribute is specified, the first form element with an ID | ||
/// matching the value will be returned instead. | ||
/// </remarks> | ||
|
||
public IHTMLFormElement Form | ||
{ | ||
get | ||
{ | ||
string formId = GetAttribute(HtmlData.tagFORM); | ||
IHTMLFormElement form = null; | ||
if (!string.IsNullOrEmpty(formId)) | ||
{ | ||
form = Document.GetElementById(formId) as IHTMLFormElement; | ||
} | ||
|
||
return form ?? Closest(HtmlData.tagFORM) as IHTMLFormElement; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.