aredridel / html5

Event-driven HTML5 Parser in Javascript
http://dinhe.net/~aredridel/projects/js/html5/
MIT License
590 stars 168 forks source link

Integrate jsdom feature flags #38

Open ghost opened 13 years ago

ghost commented 13 years ago

This not an issue for your project. I just wanted to pass along a comment I just posted on the jsdom github site: ... I reported on this issue, see #290. Inspired by this post (#328), I also tried the html5 parser. Code snippet:

                request(url, function (error, response, body) {
                    if (error) {
                        onError(message, error, socket);
                    } else {
                        var window = jsdom.jsdom(null, null, { parser : html5 }).createWindow();
                        var parser = new html5.Parser({ document: window.document });
                        parser.parse(body);
                        jsdom.jQueryify(window, jQueryLib, function(window, jquery) {
                            var $ = window.$;
                            // Do work...
                        });
                    }
                });

This pattern can be found on the htlm5 github site. While I was able to pass in 'features' options (as detailed above), the jsdom.jQueryify function appears to overwrite the feature's settings. See jsdom.js (lines 122-4):

122 window.document.implementation.addFeature('FetchExternalResources', ['script']); 123 window.document.implementation.addFeature('ProcessExternalResources', ['script']); 124 window.document.implementation.addFeature('MutationEvents', ["1.0"]);

Naturally, I wanted to set these to false to avoid script processing. My only option was to edited the code, setting all 3 features to false. It would be nice if the global defaultDocumentFeatures function worked as expected or the jQueryify function signature provided for options/features. ...

So using the html5 parser allows me to circumvent the jsdom 'hierarchy request' error. But the integration between html5 and jsdom does not allow me to disable target features. Unless, I'm missing something.

aredridel commented 13 years ago

Oh fun! I wasn't aware of @tmpvar adding those. I'll integrate those, unless you have ideas as to how it should work!

ghost commented 13 years ago

Given the above use case, the jsdom.jQueryify function should (1) provide an options parameter or (2) honor jsdom.jsdom options and/or global options. Doesn't that sound about right?

\ Options to include features.

aredridel commented 13 years ago

Yes!