How to create my jQuery API for C # so that it is not confusing?

I am making a jquery clone for C #. Now I have configured it so that each method is an extension method on IEnumerable<HtmlNode>, so it works well with existing projects that are already using HtmlAgilityPack. I thought I could leave without saving state ... however, I noticed that jQuery has two methods .andSelfand .endwhich "push" the most recent matched elements from the internal stack. I can imitate this function if I change my class so that it always works with SharpQuery objects, not enumerations, but the problem still remains.

With JavaScript, you are provided with an Html document automatically, but when working in C # you must explicitly load it, and you could use more than one document if you want. It seems that when $('xxx')you call, you essentially create a new jQuery object and start a new one from an empty stack. In C #, you would not want to do this because you do not want to reload / restore a document from the Internet. So instead, you load it once, either into the SharpQuery object or into the HtmlNodes list (you just need a DocumentNode to get started).

In jQuery docs they give this example

$('ul.first').find('.foo')
  .css('background-color', 'red')
.end().find('.bar')
  .css('background-color', 'green')
.end();

, (), sq.Find() , , . sq.Find() , sq.Find() - , ( ) , ... , .

... API? Init, , ( ?) Reset(), ? [] , ? " , , ?"

, jQuery #?

  • sq["ul.first"].Find(".foo") ...
    Downfalls: [].

  • sq.Init("ul.first").Find(".foo") ...
    Downfalls: Init, - "" ; .Find . , Init Find , , .

  • sq.Find("ul.first").Find(".foo") ... .ClearStack()
    : .

  • .
    end() .

  • .
    , HtmlDocument , , SharpQuery, . , HtmlDocument , SharpQuery . , , ( HtmlDocument, SharpQuery).

  • new SharpQuery(sq).Find("ul.first").Find(".foo") ...
    , .

+5
2

, , , , , SharpQuery . , jQuery; , jQuery . , (, find end add), , :

var theBody = $('body');
// $('body')[0] is the <body>
theBody.find('div').text('This is a div');
// $('body')[0] is still the <body>

( . end)

SharpQuery . SharpQuery , SharpQuery, . :

var sq = SharpQuery.Load(new Uri("http://api.jquery.com/category/selectors/"));
var header = sq.Find("h1"); // doesn't change sq
var allTheLinks = sq.Find(".title-link") // all .title-link in the whole document; also doesn't change sq
var someOfTheLinks = header.Find(".title-link"); // just the .title-link in the <h1>; again, doesn't change sq or header

. sq, header, allTheLinks .. - , . , node, SharpQuery (, allTheLinks.text("foo"), someOfTheLinks.text() == "foo".).

end . , SharpQuery , ( allTheLinks header, header sq). end , SharpQuery, , , :

public SharpQuery end()
{
    return new SharpQuery(this.parent.GetAllElements());
}

(, , .)

, , jQuery, . ; .

+4

2. jQuery $() - . # , . , , , ..

SharpQuery.Create("ul.first").Find(".foo")

SharpQuery sq, intellisense , ( resharper, SQ).

0

All Articles