Skip to content

Commit

Permalink
Merge pull request #308 from nicksay/cache-resource-extraction
Browse files Browse the repository at this point in the history
Extract resources before caching responses (experimental).
  • Loading branch information
nicksay committed Mar 4, 2015
2 parents 6c5133f + d128c9d commit 4e5c7d7
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
41 changes: 40 additions & 1 deletion src/client/nav/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ spf.nav.response.parse = function(text, opt_multipart, opt_lastDitch) {
extra = extra.substring(0, extra.length - lastDitchHalfToken.length);
}
}
if (spf.config.get('experimental-parse-extract')) {
for (var i = 0; i < parts.length; i++) {
parts[i] = spf.nav.response.extract(
/** @type {spf.SingleResponse} */(parts[i]));
}
}
return {
parts: /** @type {Array.<spf.SingleResponse>} */(parts),
extra: extra
Expand All @@ -99,6 +105,12 @@ spf.nav.response.parse = function(text, opt_multipart, opt_lastDitch) {
} else {
parts = [response];
}
if (spf.config.get('experimental-parse-extract')) {
for (var i = 0; i < parts.length; i++) {
parts[i] = spf.nav.response.extract(
/** @type {spf.SingleResponse} */(parts[i]));
}
}
return {
parts: /** @type {Array.<spf.SingleResponse>} */(parts),
extra: ''
Expand Down Expand Up @@ -483,7 +495,32 @@ spf.nav.response.completeAnimation_ = function(data) {


/**
* Parses and extracts resources from an HTML string:
* Extracts all resources from HTML in a SPF response.
*
* @param {spf.SingleResponse} response The SPF response object to extract.
* @return {spf.SingleResponse} The response, updated to have resources
* extracted from HTML strings. This does not create a new object and
* modifies the passed response in-place.
*/
spf.nav.response.extract = function(response) {
spf.debug.debug('spf.nav.response.extract', response);
if (response['head']) {
response['head'] = spf.nav.response.extract_(response['head']);
}
if (response['body']) {
for (var id in response['body']) {
response['body'][id] = spf.nav.response.extract_(response['body'][id]);
}
}
if (response['foot']) {
response['foot'] = spf.nav.response.extract_(response['foot']);
}
return response;
};


/**
* Extracts resources from an HTML string:
* - JS: <script> and <script src>
* - CSS: <style> and <link rel=stylesheet>
*
Expand Down Expand Up @@ -879,6 +916,8 @@ if (spf.tracing.ENABLED) {
spf.nav.response.preprocess = spf.tracing.instrument(
spf.nav.response.preprocess, 'spf.nav.response.preprocess');

spf.nav.response.extract = spf.tracing.instrument(
spf.nav.response.extract, 'spf.nav.response.extract');
spf.nav.response.extract_ = spf.tracing.instrument(
spf.nav.response.extract_, 'spf.nav.response.extract_');

Expand Down
92 changes: 88 additions & 4 deletions src/client/nav/response_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,90 @@ describe('spf.nav.response', function() {

describe('extract', function() {

it('handles strings', function() {
var response = {
head: '<script src=foo.js name=foo async></script>' +
'<link rel=stylesheet href=foo.css name=foo>',
body: {
id: '<script name="quatre">window.foo = 4</script>' +
'<style name="quatre">.foo { color: red }</style>'
},
foot: '<script src=bar.js name=bar></script>' +
'<link rel=stylesheet href=bar.css name=bar>'
};
var expected = {
head: {
scripts: [{url: 'foo.js', text: '', name: 'foo', async: true}],
styles: [{url: 'foo.css', text: '', name: 'foo'}],
links: [],
html: ''
},
body: {
id: {
scripts: [
{url: '', text: 'window.foo = 4', name: 'quatre', async: false}
],
styles: [{url: '', text: '.foo { color: red }', name: 'quatre'}],
links: [],
html: ''
}
},
foot: {
scripts: [{url: 'bar.js', text: '', name: 'bar', async: false}],
styles: [{url: 'bar.css', text: '', name: 'bar'}],
links: [],
html: ''
}
};
var result = spf.nav.response.extract(response);
expect(result).toEqual(expected);
});

it('handles objects', function() {
var response = {
head: {
scripts: [{url: 'foo.js', name: 'foo', async: true}],
styles: [{url: 'foo.css', name: 'foo'}]
},
body: {
id: {
scripts: [{text: 'window.foo = 4', name: 'quatre'}],
styles: [{text: '.foo { color: red }', name: 'quatre'}]
}
},
foot: {
scripts: [{url: 'bar.js', name: 'bar'}],
styles: [{url: 'bar.css', name: 'bar'}]
}
};
var expected = {
head: {
scripts: [{url: 'foo.js', text: '', name: 'foo', async: true}],
styles: [{url: 'foo.css', text: '', name: 'foo'}],
links: [],
html: ''
},
body: {
id: {
scripts: [
{url: '', text: 'window.foo = 4', name: 'quatre', async: false}
],
styles: [{url: '', text: '.foo { color: red }', name: 'quatre'}],
links: [],
html: ''
}
},
foot: {
scripts: [{url: 'bar.js', text: '', name: 'bar', async: false}],
styles: [{url: 'bar.css', text: '', name: 'bar'}],
links: [],
html: ''
}
};
var result = spf.nav.response.extract(response);
expect(result).toEqual(expected);
});

it('parses external scripts', function() {
var string = '<head>' +
// HTML4 style.
Expand Down Expand Up @@ -460,15 +544,15 @@ describe('spf.nav.response', function() {
// HTML4 style.
'<link rel="stylesheet" href="foo.css" name="foo">' +
// HTML4 style with spaces.
'<link rel="stylesheet" href = "foo.css" name = "foo">' +
'<link rel = "stylesheet" href = "foo.css" name = "foo">' +
// HTML5 style.
'<link rel="stylesheet" href=bar.css name=bar>' +
'<link rel=stylesheet href=bar.css name=bar>' +
// HTML5 style with spaces.
'<link rel="stylesheet" href = bar.css name = bar>' +
'<link rel = stylesheet href = bar.css name = bar>' +
// Single quotes.
"<link rel='stylesheet' href='baz.css' name='baz'>" +
// Single quotes with spaces.
"<link rel='stylesheet' href = 'baz.css' name = 'baz'>" +
"<link rel = 'stylesheet' href = 'baz.css' name = 'baz'>" +
// Non-matching HTML4 style.
'<link href="qux.css">' +
// Non-matching HTML5 style.
Expand Down

0 comments on commit 4e5c7d7

Please sign in to comment.