Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jan 12, 2017
1 parent 63b75df commit 19ebd20
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var path = require( "path" );
var phantomjs = require( "phantomjs-prebuilt" );
var childProcess = require( "child_process" );
var chalk = require( "chalk" );
var binPath = phantomjs.path;
var argv = require( "minimist" )( process.argv.slice(2) );

var pluginName = "glyphhanger";
Expand Down Expand Up @@ -68,7 +67,7 @@ function phantomGlyphhanger( urls ) {
console.log( prefix + urls.join( "\n" + prefix ) );
}

childProcess.execFile( binPath, childArgs.concat( urls ), function( error, stdout, stderr ) {
childProcess.execFile( phantomjs.path, childArgs.concat( urls ), function( error, stdout, stderr ) {
if( error ) {
throw error;
}
Expand All @@ -80,7 +79,7 @@ function phantomGlyphhanger( urls ) {
if( !argv.spider ) {
phantomGlyphhanger( argv._ );
} else {
childProcess.execFile( binPath, urlsChildArgs, function( error, stdout, stderr ) {
childProcess.execFile( phantomjs.path, urlsChildArgs, function( error, stdout, stderr ) {
if( error ) {
throw error;
}
Expand Down
27 changes: 19 additions & 8 deletions phantomjs-glyphhanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@ var args = require( "system" ).args;
var pluginName = "glyphhanger";

function requestUrl( url ) {
var page = webpage.create();
return new Rsvp.Promise(function( resolve, reject ) {
var page = webpage.create();

page.onConsoleMessage = function( msg ) {
console.log( pluginName + " phantom console:", msg );
};
page.onConsoleMessage = function( msg ) {
console.log( pluginName + " phantom console:", msg );
};

return new Rsvp.Promise(function( resolve, reject ) {
page.open( url, function( status ) {
if ( status === "success" && page.injectJs( "node_modules/characterset/lib/characterset.js" ) && page.injectJs( "glyphhanger.js" ) ) {
resolve( page.evaluate( function() {
page.onLoadFinished = function( status ) {
if( status !== "success" ) {
reject( "onLoadFinished error", status );
}

if( page.injectJs( "node_modules/characterset/lib/characterset.js" ) &&
page.injectJs( "glyphhanger.js" ) ) {

resolve( page.evaluate( function() {
var hanger = new GlyphHanger();
hanger.init( document.body );

return hanger.getGlyphs();
}) );
} else {
reject( "injectJs error" );
}
};

page.open( url, function( status ) {
if( status !== "success" ) {
reject( url, status );
}
});
Expand Down
29 changes: 21 additions & 8 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
This is a another test of the glyph logger x. ¯\_(ツ)_/¯
</body>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script>
window.addEventListener( "load", function() {
var div = document.createElement( "div" );
div.innerHTML = "def";
document.body.appendChild( div );
}, false );

window.addEventListener( "DOMContentLoaded", function() {
var div = document.createElement( "div" );
div.innerHTML = "ghi";
document.body.appendChild( div );
}, false );
</script>
</head>
<body>
abc
</body>
</html>
35 changes: 35 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ require( "jsdom-global" )();
var assert = require( "assert" );
var GlyphHanger = require( "../glyphhanger.js" );
var GlyphHangerSpider = require( "../glyphhanger-spider.js" );
var path = require( "path" );
var phantomjs = require( "phantomjs-prebuilt" );
var childProcess = require( "child_process" );

describe( "glyphhanger", function() {
describe( "Simple node", function() {
Expand Down Expand Up @@ -77,6 +80,19 @@ describe( "glyphhanger", function() {
assert.equal( "\\uD83D\\uDCA9\\uD83D\\uDE0E", gh.toString() );
});
});

describe( "integration test: onload and DOMContentLoaded content", function() {
var args = [ path.join( __dirname, "..", "phantomjs-glyphhanger.js" ), false, false, "", path.join( __dirname, "test.html" ) ];

it( "should have 9 distinct glyphs", function( done ) {
this.timeout( 6000 );
childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) {

assert.equal( "abcdefghi", stdout.trim() );
done();
});
})
});
});

describe( "glyphhanger-spider", function() {
Expand All @@ -103,4 +119,23 @@ describe( "glyphhanger-spider", function() {
assert.deepEqual( [ "firstlink.html", "secondlink.html" ], urls );
});
});

describe( "Integration test: find links", function() {
var args = [ path.join( __dirname, "..", "phantomjs-urls.js" ), path.join( __dirname, "urls.html" ) ];

it( "should have 3 links", function( done ) {
this.timeout( 6000 );
childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) {

var expecting = [
"file://" + path.join( __dirname, "test.html" ),
"file://" + path.join( __dirname, "test2.html" ),
"file://" + path.join( __dirname, "test3.html" )
];

assert.equal( expecting.join( "\n" ), stdout.trim() );
done();
});
})
});
});
13 changes: 13 additions & 0 deletions test/urls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<a href="test.html">Test</a>
<a href="test2.html">Test 2</a>
<a href="test3.html">Test 3</a>
</body>
</html>

0 comments on commit 19ebd20

Please sign in to comment.