Skip to content

Commit

Permalink
Fix Issue #11 and Issue #12. Adjusment to Create methods for better b…
Browse files Browse the repository at this point in the history
…ackwards compatibility
  • Loading branch information
jamietre committed Jun 21, 2012
1 parent fe2f3c2 commit ee6e40a
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 84 deletions.
67 changes: 35 additions & 32 deletions CsQuery/CQ_Constructors.cs
Expand Up @@ -2,7 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsQuery.ExtensionMethods;
using CsQuery.ExtensionMethods;
using CsQuery.ExtensionMethods.Internal;
using CsQuery.Implementation;
using CsQuery.Engine;

Expand Down Expand Up @@ -119,16 +120,6 @@ public CQ(IEnumerable<IDomObject> elements, CQ context)
AddSelectionRange(elements);
}

/// <summary>
/// Creates a new DOM. This will DESTROY any existing DOM. This is not the same as Select.
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public void Load(string html)
{
LoadDocument((html ?? "").ToCharArray());
}

#endregion

#region implicit constructors
Expand Down Expand Up @@ -165,19 +156,42 @@ public void Load(string html)
protected void LoadDocument(char[] html)
{
Clear();
CreateNewDocument(html);
//CreateNewDocument(html);

if (html != null)
{
HtmlParser.HtmlElementFactory factory = new HtmlParser.HtmlElementFactory(Document);
factory.ParseToDocument();
AddSelectionRange(Document.ChildNodes);
//if (html != null)
//{
// HtmlParser.HtmlElementFactory factory = new HtmlParser.HtmlElementFactory(Document);
// factory.ParseToDocument();
// AddSelectionRange(Document.ChildNodes);


}
//}
CreateNewDocument(html);
ClearSelections();
HtmlParser.HtmlElementFactory factory = new HtmlParser.HtmlElementFactory(Document);
factory.ParseToDocument();
AddSelectionRange(Document.ChildNodes);

}

/// <summary>
/// Load as if content - tag generation (EXCEPT for html/body) is enabled
/// </summary>
/// <param name="html"></param>
protected void LoadContent(char[] html)
{
CreateNewFragment(html);
ClearSelections();
HtmlParser.HtmlElementFactory factory = new HtmlParser.HtmlElementFactory(Document);
factory.GenerateOptionalElements = true;
factory.IsDocument = false;
Document.AddChildrenAlways(factory.Parse());
AddSelectionRange(Document.ChildNodes);

}

/// <summary>
/// Creates a new DOM. This will DESTROY any existing DOM. This is not the same as Select.
/// Load as if a fragment - no tag generation whatsoever
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
Expand All @@ -186,14 +200,10 @@ protected void LoadFragment(IEnumerable<IDomObject> elements)
Clear();
CreateNewDocument();
ClearSelections();
Document.ChildNodes.AddRange(elements);
Document.AddChildrenAlways(elements);
AddSelectionRange(Document.ChildNodes);
}

protected void LoadFragment(string html)
{
LoadFragment(html.ToCharArray());
}
/// <summary>
/// Creates a new fragment, e.g. HTML and BODY are not generated
/// </summary>
Expand All @@ -204,13 +214,7 @@ protected void LoadFragment(char[] html)
Clear();
CreateNewFragment(html);
HtmlParser.HtmlElementFactory factory = new HtmlParser.HtmlElementFactory(Document);

// calling CreateObjects with the parameter html forces it into unbound mode so HTML/BODY tags will not be generated

foreach (IDomObject obj in factory.ParseAsFragment())
{
Document.ChildNodes.AddAlways(obj);
}
Document.AddChildrenAlways(factory.ParseAsFragment());
AddSelectionRange(Document.ChildNodes);

}
Expand All @@ -220,7 +224,6 @@ protected void LoadFragment(char[] html)
/// <param name="html"></param>
protected void CreateNewDocument(char[] html=null)
{

Document = new DomDocument(html);
FinishCreatingNewDocument();
}
Expand Down
118 changes: 102 additions & 16 deletions CsQuery/CQ_StaticMethods.cs
Expand Up @@ -56,20 +56,8 @@ public static CQ Create()
public static CQ Create(string html)
{
CQ csq = new CQ();
csq.Load(html);
csq.LoadContent(ConvertHtmlString(html));
return csq;
}

/// <summary>
/// Creeate a new DOM from HTML text
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateFragment(string html)
{
CQ csq = new CQ();
csq.LoadFragment(html);
return csq;
}

/// <summary>
Expand All @@ -85,7 +73,7 @@ public static CQ Create(char[] html)
}

/// <summary>
/// Create a new DOM object from html, and use quickSet to create attributes (and/or css)
/// Create a new object from html, and use quickSet to create attributes (and/or css)
/// </summary>
/// <param name="html">A string of HTML</param>
/// <param name="quickSet"></param>
Expand Down Expand Up @@ -139,8 +127,82 @@ public static CQ Create(IEnumerable<IDomObject> elements)
CQ csq = new CQ();
csq.LoadFragment(elements);
return csq;
}

}


/// <summary>
/// Creeate a new fragment from HTML text
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateFragment(string html)
{
CQ csq = new CQ();
csq.LoadFragment(ConvertHtmlString(html));
return csq;
}


/// <summary>
/// Creeate a new fragment from HTML text
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateFragment(char[] html)
{
CQ csq = new CQ();
csq.LoadFragment(html);
return csq;
}


/// <summary>
/// Creeate a new DOM from a squence of elements, or another CQ object
/// </summary>
/// <param name="elements">A sequence of elements</param>
/// <returns></returns>
public static CQ CreateFragment(IEnumerable<IDomObject> elements)
{
// this is synonymous with the Create method of the same sig because we definitely
// would never autogenerate elements from a sequence of elements

return Create(elements);
}

/// <summary>
/// Creeate a new DOM from HTML text using full HTML5 tag generation
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateDocument(string html)
{
CQ csq = new CQ();
csq.LoadDocument(ConvertHtmlString(html));
return csq;
}

/// <summary>
/// Creeate a new DOM from HTML text using full HTML5 tag generation
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateDocument(char[] html)
{
CQ csq = new CQ();
csq.LoadDocument(html);
return csq;
}


/// <summary>
/// UNTESTED!
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static CQ CreateDocument(Stream html)
{
return CreateDocument(StreamToCharArray(html));
}

/// <summary>
/// Creates a new DOM from an HTML file.
Expand Down Expand Up @@ -456,6 +518,30 @@ public static Browser Browser

#endregion

#region private methods


private static char[] StreamToCharArray(Stream stream)
{
StreamReader reader = new StreamReader(stream);

long len = stream.Length;

if (len>0 && len < int.MaxValue) {
char[] arr = new char[stream.Length];
reader.Read(arr,0,Convert.ToInt32(len));
return arr;
} else {
return reader.ReadToEnd().ToCharArray();
}
}

private static char[] ConvertHtmlString(string html)
{
return String.IsNullOrEmpty(html) ?
null :
html.ToCharArray();
}
#endregion
}
}
8 changes: 8 additions & 0 deletions CsQuery/ExtensionMethods/Internal/ExtensionMethods.cs
Expand Up @@ -564,6 +564,14 @@ public static string IfNullOrEmpty(this string value, string alternate)
public static string IfNull(this string value, string alternate)
{
return value==null ? alternate : value;
}

public static void AddChildrenAlways(this IDomDocument document, IEnumerable<IDomObject> list)
{
foreach (var item in list)
{
document.ChildNodes.AddAlways(item);
}
}

}
Expand Down
4 changes: 4 additions & 0 deletions CsQuery/HtmlParser/ExtensionMethods.cs
Expand Up @@ -38,7 +38,11 @@ public static string SubstringBetween(this char[] text, int startIndex, int endI
}
return result;
}
public static string AsString(this char[] text)
{
return String.Join("", text);

}


}
Expand Down
15 changes: 14 additions & 1 deletion CsQuery/HtmlParser/HtmlElementFactory.cs
Expand Up @@ -150,11 +150,24 @@ public IEnumerable<IDomObject> ParseAsDocument()
public IEnumerable<IDomObject> ParseAsFragment()
{
IsDocument = false;
GenerateOptionalElements = true;
GenerateOptionalElements = false;
WrapRootTextNodes = false;
return Parse();
}


/// <summary>
/// Parse with options for content (generate most optional elements but not document wrapping HTML/BODY)
/// </summary>
/// <returns></returns>
public IEnumerable<IDomObject> ParseAsContent()
{
IsDocument = false;
GenerateOptionalElements = true;
WrapRootTextNodes = true;
return Parse();
}

/// <summary>
/// Parse the HTML, and return it, based on options set.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion CsQuery/Web/AsyncPostbackData.cs
Expand Up @@ -57,7 +57,7 @@ public CQ Dom
{
if (_Dom == null)
{
_Dom = CQ.Create(Content);
_Dom = CQ.CreateFragment(Content);
}
return _Dom;
}
Expand Down
2 changes: 1 addition & 1 deletion Csquery.Tests/CsQueryTest.cs
Expand Up @@ -181,7 +181,7 @@ protected CQ TestDom(string name)


string html = Support.GetFile(TestDomPath(name));
return CQ.Create(html);
return CQ.CreateDocument(html);
}
public static string TestDomPath(string name)
{
Expand Down

0 comments on commit ee6e40a

Please sign in to comment.