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

Commit 19ebd20

Browse files
committed
Fixes #5
1 parent 63b75df commit 19ebd20

File tree

5 files changed

+90
-19
lines changed

5 files changed

+90
-19
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var path = require( "path" );
44
var phantomjs = require( "phantomjs-prebuilt" );
55
var childProcess = require( "child_process" );
66
var chalk = require( "chalk" );
7-
var binPath = phantomjs.path;
87
var argv = require( "minimist" )( process.argv.slice(2) );
98

109
var pluginName = "glyphhanger";
@@ -68,7 +67,7 @@ function phantomGlyphhanger( urls ) {
6867
console.log( prefix + urls.join( "\n" + prefix ) );
6968
}
7069

71-
childProcess.execFile( binPath, childArgs.concat( urls ), function( error, stdout, stderr ) {
70+
childProcess.execFile( phantomjs.path, childArgs.concat( urls ), function( error, stdout, stderr ) {
7271
if( error ) {
7372
throw error;
7473
}
@@ -80,7 +79,7 @@ function phantomGlyphhanger( urls ) {
8079
if( !argv.spider ) {
8180
phantomGlyphhanger( argv._ );
8281
} else {
83-
childProcess.execFile( binPath, urlsChildArgs, function( error, stdout, stderr ) {
82+
childProcess.execFile( phantomjs.path, urlsChildArgs, function( error, stdout, stderr ) {
8483
if( error ) {
8584
throw error;
8685
}

phantomjs-glyphhanger.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@ var args = require( "system" ).args;
77
var pluginName = "glyphhanger";
88

99
function requestUrl( url ) {
10-
var page = webpage.create();
10+
return new Rsvp.Promise(function( resolve, reject ) {
11+
var page = webpage.create();
1112

12-
page.onConsoleMessage = function( msg ) {
13-
console.log( pluginName + " phantom console:", msg );
14-
};
13+
page.onConsoleMessage = function( msg ) {
14+
console.log( pluginName + " phantom console:", msg );
15+
};
1516

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

22+
if( page.injectJs( "node_modules/characterset/lib/characterset.js" ) &&
23+
page.injectJs( "glyphhanger.js" ) ) {
24+
25+
resolve( page.evaluate( function() {
2126
var hanger = new GlyphHanger();
2227
hanger.init( document.body );
2328

2429
return hanger.getGlyphs();
2530
}) );
2631
} else {
32+
reject( "injectJs error" );
33+
}
34+
};
35+
36+
page.open( url, function( status ) {
37+
if( status !== "success" ) {
2738
reject( url, status );
2839
}
2940
});

test/test.html

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title></title>
7-
</head>
8-
<body>
9-
This is a another test of the glyph logger x. ¯\_(ツ)_/¯
10-
</body>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title></title>
7+
<script>
8+
window.addEventListener( "load", function() {
9+
var div = document.createElement( "div" );
10+
div.innerHTML = "def";
11+
document.body.appendChild( div );
12+
}, false );
13+
14+
window.addEventListener( "DOMContentLoaded", function() {
15+
var div = document.createElement( "div" );
16+
div.innerHTML = "ghi";
17+
document.body.appendChild( div );
18+
}, false );
19+
</script>
20+
</head>
21+
<body>
22+
abc
23+
</body>
1124
</html>

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ require( "jsdom-global" )();
33
var assert = require( "assert" );
44
var GlyphHanger = require( "../glyphhanger.js" );
55
var GlyphHangerSpider = require( "../glyphhanger-spider.js" );
6+
var path = require( "path" );
7+
var phantomjs = require( "phantomjs-prebuilt" );
8+
var childProcess = require( "child_process" );
69

710
describe( "glyphhanger", function() {
811
describe( "Simple node", function() {
@@ -77,6 +80,19 @@ describe( "glyphhanger", function() {
7780
assert.equal( "\\uD83D\\uDCA9\\uD83D\\uDE0E", gh.toString() );
7881
});
7982
});
83+
84+
describe( "integration test: onload and DOMContentLoaded content", function() {
85+
var args = [ path.join( __dirname, "..", "phantomjs-glyphhanger.js" ), false, false, "", path.join( __dirname, "test.html" ) ];
86+
87+
it( "should have 9 distinct glyphs", function( done ) {
88+
this.timeout( 6000 );
89+
childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) {
90+
91+
assert.equal( "abcdefghi", stdout.trim() );
92+
done();
93+
});
94+
})
95+
});
8096
});
8197

8298
describe( "glyphhanger-spider", function() {
@@ -103,4 +119,23 @@ describe( "glyphhanger-spider", function() {
103119
assert.deepEqual( [ "firstlink.html", "secondlink.html" ], urls );
104120
});
105121
});
122+
123+
describe( "Integration test: find links", function() {
124+
var args = [ path.join( __dirname, "..", "phantomjs-urls.js" ), path.join( __dirname, "urls.html" ) ];
125+
126+
it( "should have 3 links", function( done ) {
127+
this.timeout( 6000 );
128+
childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) {
129+
130+
var expecting = [
131+
"file://" + path.join( __dirname, "test.html" ),
132+
"file://" + path.join( __dirname, "test2.html" ),
133+
"file://" + path.join( __dirname, "test3.html" )
134+
];
135+
136+
assert.equal( expecting.join( "\n" ), stdout.trim() );
137+
done();
138+
});
139+
})
140+
});
106141
});

test/urls.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title></title>
7+
</head>
8+
<body>
9+
<a href="test.html">Test</a>
10+
<a href="test2.html">Test 2</a>
11+
<a href="test3.html">Test 3</a>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)