phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.83k stars 200 forks source link

Simulate/Emulate DOM? #8

Closed stutteringp0et closed 11 years ago

stutteringp0et commented 11 years ago

I'm trying to set up a server-side LESS compiler using V8Js, but I can't load less.js because it initially attempts to interact with the DOM.

Once less.js is initialized, I can easily compile less using its built-in functions - but getting past that initialization process isn't possible without a DOM for less to interact with.

I found jsdom, but I'm at a loss to figure out how to load it in the V8Js environment as it uses require to load pieces of itself.

satoshi75nakamoto commented 11 years ago

@stutteringp0et — hmm I'll need to think about what would be the best way to accomplish this. Anyone else have any ideas?

stutteringp0et commented 11 years ago

I also found envjs, but that's geared toward rhino, and again - I'm not sure how to load it other than stringing all of the files together and introducing it as a string.

I'll start with a overview of what I was trying to accomplish (done now), followed by what I did in V8Js to accomplish it.

The CSS interpreter less.js has only one official parser - their javascript interpreter. Unofficial interpreters exist for PHP and .net - but neither can fully realize the full feature set of less.js because they can't interpret JS directly - so if an input file uses JS in any of the settings - that file will fail in the unofficial interpreters.

So, my goal was to use V8Js to give less.js the environment it requires to execute serverside. The problem was that less.js, upon load, scans the DOM for scripts to process - halting operation in V8Js immediately - because there's no DOM.

So, in V8Js, I executed this string first: window = this;window.location={href:"file://",port:""};document={getElementsByTagName:function(){return []}};

less.js gets the responses it's expecting (even though there is still no DOM), allowing me to call it as a class to process strings instead of the content of dom elements.

Still, I'm sure it would be beneficial to have a full DOM in V8Js, I'm just not guru enough to figure out how to do it.

beest commented 11 years ago

If you only need LESS compilation then it should be possible without any DOM.

LESS can be used with node.js and without DOM support. However, it seems to "require" other files in order to initialise the compiler.

I've just submitted a pull request that will add CommonJS "require" support to V8Js. I think this will get you a step closer to having LESS work in V8Js.

stutteringp0et commented 11 years ago

I already have it running. I used the line of code I posted earler to fool less into thinking it had a DOM.

The LESS server side script has specific detection for nodejs, rhino and one other that I can't remember offhand. I spent some time looking up the objects that rhino and node make available and it was less trouble to go back to the client script and fake a DOM.

beest commented 11 years ago

Sorry, my bad - I didn't pick up that you had this working. Great to hear that you found a solution.

christiaan commented 11 years ago

@stutteringp0et You might also want to check out https://github.com/andreasgal/dom.js/ it emulates the DOM in plain JS.

cscott commented 10 years ago

@stutteringp0et, @christiaan https://github.com/fgnass/domino is a fork of dom.js that removes mozilla-specific proxy support, and is better maintained in general. I'll probably try to add this to v8js at some point (or else expose the PHP.dom package to JS, haven't decided yet).