Skip to content

Commit

Permalink
Merge pull request #311 from nicksay/resource-types
Browse files Browse the repository at this point in the history
Define internal types for pre-parsed response fragments.
  • Loading branch information
nicksay committed Mar 10, 2015
2 parents 123bdfc + 9250176 commit 608f0f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
53 changes: 50 additions & 3 deletions src/client/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,53 @@ spf.EventName = {
};


/** Type definition for a parsed script resource in a SPF response fragment.
*
* @typedef {{
* url: (string|undefined),
* text: (string|undefined),
* name: (string|undefined),
* async: (boolean|undefined)
* }}
*/
spf.ScriptResource;


/** Type definition for a parsed style resource in a SPF response fragment.
*
* @typedef {{
* url: (string|undefined),
* text: (string|undefined),
* name: (string|undefined)
* }}
*/
spf.StyleResource;


/** Type definition for a parsed link resource in a SPF response fragment.
*
* @typedef {{
* url: (string|undefined),
* rel: (string|undefined)
* }}
*/
spf.LinkResource;


/**
* Type definition for a fragment of a SPF response. Either a string of HTML or
* an object with the resources parsed out of the HTML.
*
* @typedef {string|{
* html: (string|undefined),
* scripts: (Array.<spf.ScriptResource>|undefined),
* styles: (Array.<spf.StyleResource>|undefined),
* links: (Array.<spf.LinkResource>|undefined)
* }}
*/
spf.ResponseFragment;


/**
* Type definition for a single SPF response object.
* - attr: Map of Element IDs to maps of attibute names to attribute values
Expand All @@ -162,11 +209,11 @@ spf.EventName = {
*
* @typedef {{
* attr: (Object.<string, Object.<string, string>>|undefined),
* body: (Object.<string, string>|undefined),
* body: (Object.<string, spf.ResponseFragment>|undefined),
* cacheKey: (string|undefined),
* cacheType: (string|undefined),
* foot: (string|undefined),
* head: (string|undefined),
* foot: (spf.ResponseFragment|undefined),
* head: (spf.ResponseFragment|undefined),
* redirect: (string|undefined),
* reload: (boolean|undefined),
* timing: (Object.<string, number>|undefined),
Expand Down
33 changes: 17 additions & 16 deletions src/client/nav/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,51 +524,52 @@ spf.nav.response.extract = function(response) {
* - JS: <script> and <script src>
* - CSS: <style> and <link rel=stylesheet>
*
* @param {string|Object} html The HTML string to parse, or a pre-parsed object.
* @param {spf.ResponseFragment} frag The response fragment, either a HTML
* string to parse, or a pre-parsed object.
* @return {!spf.nav.response.Extraction_}
* @private
*/
spf.nav.response.extract_ = function(html) {
spf.nav.response.extract_ = function(frag) {
var result = new spf.nav.response.Extraction_();
if (!html) {
if (!frag) {
return result;
}

// If the html isn't a string, it's a pre-parsed object. Use the provided
// values to populate the result instead.
if (!spf.string.isString(html)) {
// If the fragment isn't a string, it's a pre-parsed object. Use the
// provided values to populate the result instead.
if (!spf.string.isString(frag)) {
// Add the parsed scripts to the result.
if (html['scripts']) {
spf.array.each(html['scripts'], function(script) {
if (frag['scripts']) {
spf.array.each(frag['scripts'], function(script) {
result.scripts.push({url: script['url'] || '',
text: script['text'] || '',
name: script['name'] || '',
async: script['async'] || false});
});
}
// Add the parsed styles to the result.
if (html['styles']) {
spf.array.each(html['styles'], function(style) {
if (frag['styles']) {
spf.array.each(frag['styles'], function(style) {
result.styles.push({url: style['url'] || '',
text: style['text'] || '',
name: style['name'] || ''});
});
}
// Add the parsed links to the result.
if (html['links'] && spf.config.get('experimental-preconnect')) {
spf.array.each(html['links'], function(link) {
if (frag['links'] && spf.config.get('experimental-preconnect')) {
spf.array.each(frag['links'], function(link) {
if (link['rel'] == 'spf-preconnect') {
result.links.push({url: link['url'] || '',
rel: link['rel'] || ''});
}
});
}
result.html = html['html'] || '';
result.html = frag['html'] || '';
return result;
}

// Parse scripts and styles and add them to the result.
html = html.replace(spf.nav.response.ElementRegEx.SCRIPT_STYLE,
frag = frag.replace(spf.nav.response.ElementRegEx.SCRIPT_STYLE,
function(full, tag, attr, text) {
// A script tag can be either an inline or external style.
// Parse the name, src, and async attributes.
Expand All @@ -593,7 +594,7 @@ spf.nav.response.extract_ = function(html) {
});

// Parse links and add them to the result.
html = html.replace(spf.nav.response.ElementRegEx.LINK,
frag = frag.replace(spf.nav.response.ElementRegEx.LINK,
function(full, attr) {
var rel = attr.match(spf.nav.response.AttributeRegEx.REL);
rel = rel ? rel[1] : '';
Expand Down Expand Up @@ -621,7 +622,7 @@ spf.nav.response.extract_ = function(html) {
});

// The result html is what's left after parsing.
result.html = html;
result.html = frag;

return result;
};
Expand Down

0 comments on commit 608f0f9

Please sign in to comment.