MatthewMueller/cheerio — pull request #231 by jugglinmike (2013-07-03 22:48:52+00:00)
Title: Implement $.parseHTML

[We could use `$.parseHTML` downstream](https://github.com/tbranyen/backbone.layoutmanager/issues/351), so I thought I'd try implementing it in Cheerio itself.

As I've noted in the included documentation, the `context` argument does not seem to be relevant. All the same, we'll want to keep it in order to maintain API consistency.

I've taken [the method's unit tests from jQuery](https://github.com/jquery/jquery/blob/6e630fbbb19306e1914712502a2b22872e94ddad/test/unit/core.js#L1274-L1312) and modified them for code style/test interface. I have omitted the following three assertions because I don't believe they are relevant for Cheerio:

```javascript
raises(function() {
jQuery.parseHTML( "

", document.getElementById("form") );
}, "Passing an element as the context raises an exception (context should be a document)");
// ...
equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
// ...
equal( jQuery.parseHTML("	")[ 1 ].parentNode.nodeType, 11,
"parentNode should be documentFragment for wrapMap (variable in manipulation module) elements too" );
```

Finally, the addition of a `script` argument in Cheerio's internal `parse` method may seem overly-specific at first glance ("Is it really necessary to modify a low-level shared function for this one special case method?"). I think this addition is justified because `parse` is roughly equivalent to [jQuery's internal `buildFragment` method, which is built to track scripts in this manner](https://github.com/jquery/jquery/blob/0dd88061a276675b468a2bacfef7e245e391f221/src/manipulation.js#L348-L433). That said, I'm open to suggestion about a better home for the script detection logic.

Commit message:

> From the [jQuery API documentation](http://api.jquery.com/jQuery.parseHTML/):
>
> > $.parseHTML( data [, context ] [, keepScripts ] )
> > Parses a string into an array of DOM nodes.
>
> The `context` argument has no meaning for Cheerio, but it is maintained
> for API compatability.