Skip to content

Commit

Permalink
Merge pull request jamietre#118 from joelverhagen/master
Browse files Browse the repository at this point in the history
Update Form properties on some elements. Added button interface and implementation.
  • Loading branch information
jamietre committed Aug 20, 2013
2 parents b84e967 + 25239c7 commit 8abc319
Show file tree
Hide file tree
Showing 19 changed files with 387 additions and 40 deletions.
2 changes: 2 additions & 0 deletions source/CsQuery.Tests/Csquery.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@
<Compile Include="Core\Selectors\ContentIsNotNumeric.cs" />
<Compile Include="Core\WebIO\BasicWeb.cs" />
<Compile Include="ExtensionMethods\Xml.cs" />
<Compile Include="HtmlParser\Button.cs" />
<Compile Include="HtmlParser\CharacterSetEncoding.cs" />
<Compile Include="HtmlParser\FormAssociatedElement.cs" />
<Compile Include="HtmlParser\HtmlEncoding.cs" />
<Compile Include="HtmlParser\PreProcessing.cs" />
<Compile Include="HtmlParser\FragmentContext.cs" />
Expand Down
63 changes: 63 additions & 0 deletions source/CsQuery.Tests/HtmlParser/Button.cs
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"));
}
}
}
79 changes: 79 additions & 0 deletions source/CsQuery.Tests/HtmlParser/FormAssociatedElement.cs
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);
}
}
}
14 changes: 14 additions & 0 deletions source/CsQuery.Tests/HtmlParser/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,19 @@ public void Parsing()
Assert.AreEqual(expected, actual);

}

/// <summary>
/// Ensure that the type of a TEXTAREA element is always the string "textarea"
/// </summary>
[Test, TestMethod]
public void Type()
{
CQ cq = CQ.Create("<textarea type=useless>Foo that bar</textarea>");
IHTMLTextAreaElement textArea = cq["textarea"].FirstElement() as IHTMLTextAreaElement;

Assert.IsNotNull(textArea);
Assert.AreEqual("textarea", textArea.Type);
Assert.AreEqual("useless", textArea.GetAttribute("type"));
}
}
}
5 changes: 5 additions & 0 deletions source/CsQuery/CsQuery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,18 @@
<Compile Include="CQ_jQuery\Not.cs" />
<Compile Include="CQ_jQuery\Parents.cs" />
<Compile Include="Dom\enumCSSRuleType.cs" />
<Compile Include="Dom\HtmlElements\IHTMLButtonElement.cs" />
<Compile Include="Dom\HtmlElements\IHTMLTextAreaElement.cs" />
<Compile Include="Dom\ICSSRule.cs" />
<Compile Include="Dom\ICSSStyleRule.cs" />
<Compile Include="Dom\ICSSStyleSheet.cs" />
<Compile Include="Dom\Implementation\CSSRule.cs" />
<Compile Include="Dom\Implementation\CSSStyleChangedArgs.cs" />
<Compile Include="Dom\Implementation\CSSStyleRule.cs" />
<Compile Include="Dom\Implementation\CSSStyleSheet.cs" />
<Compile Include="Dom\Implementation\HtmlElements\HTMLButtonElement.cs" />
<Compile Include="Dom\Implementation\HtmlElements\FormAssociatedElement.cs" />
<Compile Include="Dom\Implementation\HtmlElements\FormReassociateableElement.cs" />
<Compile Include="Dom\Implementation\HtmlElements\HTMLStyleElement.cs" />
<Compile Include="Dom\Implementation\HtmlElements\HTMLScriptElement.cs" />
<Compile Include="Dom\Implementation\HtmlElements\HTMLLabelElement.cs" />
Expand Down
24 changes: 24 additions & 0 deletions source/CsQuery/Dom/HtmlElements/IHTMLButtonElement.cs
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;}
}
}
2 changes: 1 addition & 1 deletion source/CsQuery/Dom/HtmlElements/IHTMLInputElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IHTMLInputElement : IDomElement
/// The form with which to associate the element.
/// </summary>

IDomElement Form {get;}
IHTMLFormElement Form {get;}

/// <summary>
/// A URL that provides the destination of the hyperlink. If the href attribute is not specified,
Expand Down
2 changes: 1 addition & 1 deletion source/CsQuery/Dom/HtmlElements/IHTMLOptionElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IHTMLOptionElement : IDomElement
/// The form with which the element is associated
/// </summary>

IDomElement Form {get;}
IHTMLFormElement Form {get;}

/// <summary>
/// Gets or sets the label attribute.
Expand Down
6 changes: 6 additions & 0 deletions source/CsQuery/Dom/HtmlElements/IHTMLSelectElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace CsQuery

public interface IHTMLSelectElement : IDomElement
{
/// <summary>
/// The form with which to associate the element.
/// </summary>

IHTMLFormElement Form { get; }

/// <summary>
/// A collection of HTML option elements (in document order)
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions source/CsQuery/Dom/HtmlElements/IHTMLTextAreaElement.cs
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; }
}
}
2 changes: 2 additions & 0 deletions source/CsQuery/Dom/Implementation/DomElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ internal static DomElement Create(ushort nodeNameId)
return new HtmlAnchorElement();
case HtmlData.tagFORM:
return new HtmlFormElement();
case HtmlData.tagBUTTON:
return new HTMLButtonElement();
case HtmlData.tagINPUT:
return new HTMLInputElement();
case HtmlData.tagLABEL:
Expand Down
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;
}
}
}
}
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;
}
}
}
}
Loading

0 comments on commit 8abc319

Please sign in to comment.