EzDevInfo.com

jsdom

A JavaScript implementation of the WHATWG DOM and HTML standards, for use with io.js

Unmount / destroy Component in jsdom test

Is there a way to unmount and garbage collect a React Component that was mounted using TestUtils.renderIntoDocument inside a jsdom test?

I'm trying to test something that happens on componentWillUnmount and TestUtils.renderIntoDocument doesn't return any DOM node to call React. unmountComponentAtNode on.


Source: (StackOverflow)

node.js and jsdom - no way to detect that an http 500 error was returned?

I'm using jsdom with node.js and I'm trying to get it to provide me with some indication that an http error has occurred. I've set up a test server that simply returns an http 500 header for all requests, but when I attempt to load it with jsdom, jsdom doesn't throw any error and doesn't seem to provide me with any information that would identify that an http 500 error was returned. What's the best way to detect an http 500 error?


Source: (StackOverflow)

Advertisements

What are the use cases of jsdom

After reading this Micro templates are dead article. I've become curious:

  1. Whether Using the DOM on the server results in cleaner more maintainable code then templating.
  2. Whether it's more efficient to use jsdom instead of a templating engine.
  3. How to factor jsdom into the View of a standard MVC setup.

And generally in what situations would it be better to use a server-side DOM abstraction, like jsdom rather then a templating engine, like EJS or jade.

The question is specific to node.js and other SSJS


Source: (StackOverflow)

Building contextify under Windows 7 x64 (for NodeJS jQuery)

I try to get node-jquery working. contextify is one of the dependencies (required for jsdom).

contextify needs to be built somehow using Python. But this does not seem to work on Windows 7 x64. At least one my computer :)

After some readings (#10, #12, #17) and trying some binaries provided by this fork and this one I can get it work ...

I've got the same error from node-gyp configure or from node-gyp rebuild (npm install jquery) :

info it worked if it ends with ok
info downloading: http://nodejs.org/dist/v0.6.14/node-v0.6.14.tar.gz
info downloading: http://nodejs.org/dist/v0.6.14/x64/node.lib
info downloading: http://nodejs.org/dist/v0.6.14/node.lib
spawn python [ 'D:\\Users\\ngryman\\.node-gyp\\0.6.14\\tools\\gyp_addon',
  'binding.gyp',
  '-ID:\\Users\\ngryman\\build\\config.gypi',
  '-f',
  'msvs',
  '-G',
  'msvs_version=2010' ]
  File "D:\Users\ngryman\.node-gyp\0.6.14\tools\gyp_addon", line 40
    print 'Error running GYP'
                        ^
SyntaxError: invalid syntax
ERR! Error: `gyp_addon` failed with exit code: 1
  at ChildProcess.onCpExit (D:\Users\ngryman\AppData\Roaming\npm\node_modules
node-gyp\lib\configure.js:226:16)
  at ChildProcess.emit (events.js:70:17)
  at maybeExit (child_process.js:360:16)
  at Process.onexit (child_process.js:396:5)
ERR! not ok

It seems there is a syntax error ... I am not very comfortable with Python.

Here are my versions :

  • Python: 3.2.3
  • NodeJS: 0.6.14

Anyone have an idea?

Thanks!


Source: (StackOverflow)

Node.js jsdom error

I am trying to get jsdom to work :)

Here's the code:

var jsdom = require("jsdom");
var request = require("request");
var fs     = require('fs');
var jquery = fs.readFileSync("./jquery-1.7.2.js", 'utf-8');

request({ uri:'http://nodejs.org/dist/' }, function (error, response, body) {
    if (error && response.statusCode !== 200) {
        console.log('HTTP request error... '+error);
    }
    jsdom.env({
        html: body,
        scripts: [
            jquery
        ],
        done: function(errors, window) {
            console.log('done');
        }
    });
});

And here's the error:

jsdom.js:171
    features   = JSON.parse(JSON.stringify(window.document.implementation._fea
                                                          ^
TypeError: Cannot read property 'implementation' of undefined

I have checked if the page is fetched and if the jquery lib is parsed - they are.

We could look at the implementation of jsdom.js:

[snip]
exports.env = exports.jsdom.env = function() {
    [snip]
    window     = exports.html(html, null, options).createWindow(),
    features   = JSON.parse(JSON.stringify(window.document.implementation._features)),
    docsLoaded = 0,
    [snip]

It seems that the .createWindow() is not successful...

And I am running it on Cloud9.

Any help is welcome.


Source: (StackOverflow)

jsdom and node.js leaking memory

I found a few reference to people having a similar issue where the answer always was, make sure you call window.close() when done. However that does not seem to be working for me (node 0.8.14 and jsdom 0.3.1)

A simple repro

var util = require('util');
var jsdom=require('jsdom');

function doOne() {
  var htmlDoc = '<html><head></head><body id="' + i + '"></body></html>';
  jsdom.env(htmlDoc, null, null, function(errors, window) {
    window.close();
  });
}

for (var i=1;i< 100000;i++ )  {
  doOne();
  if(i % 500 == 0)  {
    console.log(i + ":" + util.inspect(process.memoryUsage()));
  }
}
console.log ("done");

Output I get is

500:{ rss: 108847104, heapTotal: 115979520, heapUsed: 102696768 }
1000:{ rss: 198250496, heapTotal: 194394624, heapUsed: 190892120 }
1500:{ rss: 267304960, heapTotal: 254246912, heapUsed: 223847712 }
...
11000:{ rss: 1565204480, heapTotal: 1593723904, heapUsed: 1466889432 }

At this point the fan goes wild and the test actually stops...or at leasts starts going very slowly

Does anyone have any other tips than window.close to get rid of the memory leak (or it sure looks like a memory leak)

Thanks!

Peter


Source: (StackOverflow)

npm not installing jsdom in Windows

Im building an app in node.js with jQuery, but when I try to execute, it says it couldnt find module jsdom so, even though I dont understand why installing jQuery didnt install this dependency, I run manually npm install jsdom and I get this error message:

MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of t he component to the system path if it is installed elsewhere. [C:\Sites\myapp\node_modules\jsdom\node_modules\contextify\build\binding.sln]

I tried the first option, but still didnt help. What am I (missing|doing wrong)?


Source: (StackOverflow)

Jsdom throwing error for some URLs

I am new to nodejs, what I'm trying to do is to scan all the url of my site (with javascript and jquery enabled) and check that the url contains a given string.

To do this I'm using jsdom, but when I launch the script extracts only some url and then crashes giving this error:

timers.js:110
    first._onTimeout();
          ^
TypeError: Property '_onTimeout' of object [object Object] is not a function
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Surely there is something wrong but I don't understand where..

This is my script:

var request = require('request');
var jsdom = require('jsdom');

request({ uri: 'http://www.example.com' }, function (error, response, html) {
  if (!error && response.statusCode == 200) {

     var doc = jsdom.jsdom(html, null, {
           features: {
              FetchExternalResources   : ['script'],
              ProcessExternalResources : ['script'],
              MutationEvents           : '2.0',
           }
     });

     var window = doc.createWindow();
     jsdom.jQueryify(window, "http://code.jquery.com/jquery-1.5.min.js", function() {
        var $ = window.jQuery;
        $('a').each(function(i, element){
             var a = $(this).attr('href');
             console.log(a);
             if (a.indexOf('string') != -1) {
               console.log('The winner: '+a);
               //return a;
             }
        });
        window.close();
    });
  }
});

Source: (StackOverflow)

Loading ajax app with jsdom

I'm looking for a solution to bootstrap a client side app (written in Backbone.js) on the server, so that I can serve correct content to crawlers and non-js consumers.

I've been playing around with jsdom & nodejs in an attempt to bootstrap the app, and can get it as far as loading in the basic template content, but the app appears to never start.

I've tried 2 different solutions to make sure that I have all the dependencies necessary and the app is loading correctly:

  1. I tried using jsdom.env() with all the scripts usually included inline.
  2. I tried using jsdom.jsdom() and making sure that FetchExternalResources, ProcessExternalResources, and MutationEvents were all correct and on.

Neither seem to fire the window.onload event though. I'm wondering if jsdom possibly doesn't fire this, or maybe calls it's callback after it would have normally been fired?

Is this a task that is possible in jsdom?


Source: (StackOverflow)

DOM-like APIs for HTML string content inside a web worker

Is there any library that could help with html string manipulation inside a web worker ?

What I'd like to be able to do is, inside a worker, have sizzle-type selector tool that would allow me to do things like:

hString = "<div><img src='foo'></img></div>"
imgSrc  = $(hString).find("img").attr("src")   // foo

, without the DOM interaction.


Source: (StackOverflow)

Generation of svg on server side using highcharts

I did require('jsdom') in node js but i constantly get jsdom module not found. More over i want to generate a svg document using highcharts on the server so that i can later on use that image in my pdf( I will convert that svg to image using batik). Is there a link that may help me with this. I have read through the http://blog.davidpadbury.com/2010/10/03/using-nodejs-to-render-js-charts-on-server/ but the prototype didnt make much sense. Is there a module which has been implemented rather than just a prototype.


Source: (StackOverflow)

Dual-Side Templating vs. Server-Side DOM Manipulation

I'm making an app that requires dynamic content be fully rendered on the page for search engine bots - a problem, potentially, should I use JS templating to control the content. Web spiders are supposedly getting better at indexing RIA sites, but I don't want to risk it. Also, as mobile internet is still spotty in most places, it seems like a good practice to maximize the server load initially to ensure that basic functionality/styles/dynamic content show up on your pages, even if the client hasn't downloaded any JS libraries.

That's how I stumbled upon dual-side templating:

Problem: How can you allow for dynamic, Ajax-style, rendering in the browser, but at the same time output it from the server upon initial page load?

c. 2010: Dual-Side Templating A single template is used on both browser and server, to render content wherever it’s appropriate – typically the server as the page loads and the browser as the app progresses. For example, blog comments. You output all existing comments from the server, using your server-side template. Then, when the user makes a new comment, you render a preview of it – and the final version – using browser-side templating.

I want to try dual-side templating with Node.js and Eco templates, but I don't know how to proceed. I'm new to JavaScript and all things Node.

Node-Lift is said to help, but I don't understand what it's doing or why.

Can someone provide a high level overview of how you might use dual-templating in the context of a mobile web app?

Where does server-side DOM manipulation with jQuery and JSDOM fit in to the equation?

TIA


Source: (StackOverflow)

Node Jsdom Scrape Google's Reverse Image Search

I want to programatically find a list of URLs for similar images given an image URL. I can't find any free image search APIs so I'm trying to do this by scraping Google's Search by Image.

If I have an image URL, say http://i.imgur.com/oLmwq.png, then navigating to https://www.google.com/searchbyimage?&image_url=http://i.imgur.com/oLmwq.png gives related images and info.

How do I get jsdom.env to produce the HTML your browser gets from the above URL?

Here's what I've tried (CoffeeScript):

jsdom = require 'jsdom'
url = 'https://www.google.com/searchbyimage?&image_url=http://i.imgur.com/oLmwq.png'
jsdom.env
    html: url
    scripts: [ "http://code.jquery.com/jquery.js" ]
    features:
        FetchExternalResources: ['script']
        ProcessExternalResources: ['script']
    done: (errors, window) ->
        console.log window.$('body').html()

You can see the HTML doesn't match what we want. Is this an issue with Jsdom's HTTP headers?


Source: (StackOverflow)

jsdom document.createWindow() returns empty for non-empty document

I'm trying to follow the very short, simple setup instructions at https://github.com/tmpvar/jsdom under section "Creating a browser-like BOM/DOM/Window". Unfortunately, after the 3rd line (the .createWindow step), I do console.log(window) and it prints an empty object. There should be at least window.document there, but there isn't. It seems as though document.createWindow is acting the same as jsdom.createWindow.

Here's my code so far:

var jsdom = require('jsdom').jsdom;

document = jsdom("<html><head></head><body>hello world</body></html>");
window = document.createWindow();

console.log(window); // output: {}

console.log(window.document.innerHTML); // error, cannot read innerHTML on undefined

So, what stupid thing am I doing wrong?

FYI document is created correctly. Printing it outputs a very large object.

I am using a Mac.


Source: (StackOverflow)

jsdom doesn't process script inside web page

I am new to nodejs. I want to use jsdom to parse some web pages which may contain script code inside. But I got error said the function or variable was not defined. Can anyone give some directions about this.

my code

var jsdom = require('jsdom');

jsdom.env({
  html: 'http://10.200.0.10:8080/test/a.html',
  scripts: [
    'http://code.jquery.com/jquery-1.5.min.js'
  ],
  done: function(errors, window) {
    var $ = window.$;
    window.onload();
    console.log(window.a);
  }
});

and the html page here

<html>
<head>
    <script>
    var a = 0;
    function loads(){
        a=1000;
    }
    </script>
</head>
<body onload='loads()'>
</body>
</html>

and got error message below

dfddddfdf
undefined:1: ReferenceError: loads is not defined
loads()
^
ReferenceError: loads is not defined
    at unknown source
    at /root/node_modules/jsdom/node_modules/contextify/lib/contextify.js:10:24
    at /root/node_modules/jsdom/lib/jsdom/level1/core.js:1024:50
    at /root/testnode.js:18:12
    at Array.0 (/root/node_modules/jsdom/lib/jsdom.js:199:39)
    at EventEmitter._tickCallback (node.js:192:40)

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
ReferenceError: loads is not defined
    at unknown source
    at /root/node_modules/jsdom/node_modules/contextify/lib/contextify.js:10:24
    at /root/node_modules/jsdom/lib/jsdom/level1/core.js:1024:50
    at /root/testnode.js:18:12
    at Array.0 (/root/node_modules/jsdom/lib/jsdom.js:199:39)
    at EventEmitter._tickCallback (node.js:192:40)

It reports the loads function was not defined, but it actually was declared in the web page.

Can anyone give some suggestions, or just simply tell me jsdom cannot process the scripts embedded in page.


Source: (StackOverflow)