Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/appHandlers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dom from './utils/dom';
import domify from 'domify';
import utils from './utils';

Expand All @@ -21,7 +22,7 @@ var _defaultMethods = {
appRender: function(appConfig, appHtml)
{
// if no app root is defined use the app's outer most node
if(!utils.isNativeDOMNode(appConfig.root))
if(!dom.isNativeNode(appConfig.root))
{
appConfig.root = domify(appHtml);
}
Expand Down Expand Up @@ -63,7 +64,7 @@ var _createHandler = function(token, sNamespace, func_or_element, bDomNodeApprop
var handler = {
func: (typeof(func_or_element)) ? func_or_element : null,
namespace: sNamespace,
domNode: (utils.isNativeDOMNode(func_or_element)) ? func_or_element : null
domNode: (dom.isNativeNode(func_or_element)) ? func_or_element : null
};

if(!handler.func && !handler.domNode)
Expand Down
2 changes: 1 addition & 1 deletion src/constants/appHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = {
            *       function(appConfig, appHtml)
            *       {
            *           // if no app root is defined use the app's outer most node
            *           if(!F2.isNativeDOMNode(appConfig.root))
            *           if(!appConfig.root)
            *           {
            *               appConfig.root = domify(appHtml);                               
            *           }
Expand Down
9 changes: 5 additions & 4 deletions src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import appClasses from './apps';
import classes from './classes';
import cloneDeep from 'lodash.clonedeep';
import constants from './constants';
import dom from './utils/dom';
import domify from 'domify';
import events from './events';
import utils from './utils';
Expand Down Expand Up @@ -175,7 +176,7 @@ var _initContainerEvents = function() {
*/
var _isPlaceholderElement = function(node) {
return (
utils.isNativeDOMNode(node) &&
dom.isNativeNode(node) &&
!_hasNonTextChildNodes(node) &&
!!node.getAttribute('data-f2-appid') &&
!!node.getAttribute('data-f2-manifesturl')
Expand Down Expand Up @@ -529,7 +530,7 @@ var _loadApps = function(appConfigs, appManifest) {
appConfigs[i] // the app config
);

if (!utils.isNativeDOMNode(root)) {
if (!dom.isNativeNode(root)) {
throw ('App root for ' + appId + ' must be a native DOM element. Check your AppHandler callbacks to ensure you have set app root to a native DOM element.');
}
}
Expand Down Expand Up @@ -698,7 +699,7 @@ export default {
}
};

if (!!parentNode && !utils.isNativeDOMNode(parentNode)) {
if (!!parentNode && !dom.isNativeNode(parentNode)) {
throw ('"parentNode" must be null or a DOM node');
}

Expand Down Expand Up @@ -886,7 +887,7 @@ export default {
// If the root property is defined then this app is considered to be preloaded and we will
// run it through that logic.
if (a.root && !_isPlaceholderElement(a.root)) {
if ((!a.root && typeof(a.root) != 'string') && !utils.isNativeDOMNode(a.root)) {
if ((!a.root && typeof(a.root) != 'string') && !dom.isNativeNode(a.root)) {
utils.log('AppConfig invalid for pre-load, not a valid string and not dom node');
utils.log('AppConfig instance:', a);
throw ('Preloaded appConfig.root property must be a native dom node or a string representing a sizzle selector. Please check your inputs and try again.');
Expand Down
23 changes: 23 additions & 0 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Utility method to determine whether or not the argument passed in is or is not a native dom node.
* @method isNativeNode
* @param {object} testObject The object you want to check as native dom node.
* @return {bool} Returns true if the object passed is a native dom node.
*/
function isNativeNode(testObject) {
var bIsNode = (
typeof Node === 'object' ? testObject instanceof Node :
testObject && typeof testObject === 'object' && typeof testObject.nodeType === 'number' && typeof testObject.nodeName === 'string'
);

var bIsElement = (
typeof HTMLElement === 'object' ? testObject instanceof HTMLElement : //DOM2
testObject && typeof testObject === 'object' && testObject.nodeType === 1 && typeof testObject.nodeName === 'string'
);

return (bIsNode || bIsElement);
}

export default {
isNativeNode
};
122 changes: 3 additions & 119 deletions src/utils.js → src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,4 @@
/**
* Abosolutizes a relative URL
* @method _absolutizeURI
* @private
* @param {e.g., location.href} base
* @param {URL to absolutize} href
* @return {string} URL
* Source: https://gist.github.com/Yaffle/1088850
* Tests: http://skew.org/uri/uri_tests.html
*/
var _absolutizeURI = function(base, href) {// RFC 3986

function removeDotSegments(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}

href = _parseURI(href || '');
base = _parseURI(base || '');

return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash;
};

/**
* Parses URI
* @method _parseURI
* @private
* @param {The URL to parse} url
* @return {Parsed URL} string
* Source: https://gist.github.com/Yaffle/1088850
* Tests: http://skew.org/uri/uri_tests.html
*/
var _parseURI = function(url) {
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
};
import uri from './uri';

export default {
/**
Expand Down Expand Up @@ -127,67 +67,11 @@ export default {
},
/**
* Tests a URL to see if it's on the same domain (local) or not
* @method isLocalRequest
* @method isLocal
* @param {URL to test} url
* @return {bool} Whether the URL is local or not
* Derived from: https://github.com/jquery/jquery/blob/master/src/ajax.js
*/
isLocalRequest: function(url){
var rurl = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,
urlLower = url.toLowerCase(),
parts = rurl.exec( urlLower ),
ajaxLocation,
ajaxLocParts;

try {
ajaxLocation = location.href;
} catch( e ) {
// Use the href attribute of an A element
// since IE will modify it given document.location
ajaxLocation = document.createElement('a');
ajaxLocation.href = '';
ajaxLocation = ajaxLocation.href;
}

ajaxLocation = ajaxLocation.toLowerCase();

// uh oh, the url must be relative
// make it fully qualified and re-regex url
if (!parts){
urlLower = _absolutizeURI(ajaxLocation,urlLower).toLowerCase();
parts = rurl.exec( urlLower );
}

// Segment location into parts
ajaxLocParts = rurl.exec( ajaxLocation ) || [];

// do hostname and protocol and port of manifest URL match location.href? (a "local" request on the same domain)
var matched = !(parts &&
(parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
(parts[ 3 ] || (parts[ 1 ] === 'http:' ? '80' : '443')) !==
(ajaxLocParts[ 3 ] || (ajaxLocParts[ 1 ] === 'http:' ? '80' : '443'))));

return matched;
},
/**
* Utility method to determine whether or not the argument passed in is or is not a native dom node.
* @method isNativeDOMNode
* @param {object} testObject The object you want to check as native dom node.
* @return {bool} Returns true if the object passed is a native dom node.
*/
isNativeDOMNode: function(testObject) {
var bIsNode = (
typeof Node === 'object' ? testObject instanceof Node :
testObject && typeof testObject === 'object' && typeof testObject.nodeType === 'number' && typeof testObject.nodeName === 'string'
);

var bIsElement = (
typeof HTMLElement === 'object' ? testObject instanceof HTMLElement : //DOM2
testObject && typeof testObject === 'object' && testObject.nodeType === 1 && typeof testObject.nodeName === 'string'
);

return (bIsNode || bIsElement);
},
isLocalRequest: uri.isLocal,
/**
* A utility logging function to write messages or objects to the browser console. This is a proxy for the [`console` API](https://developers.google.com/chrome-developer-tools/docs/console).
* @method log
Expand Down
110 changes: 110 additions & 0 deletions src/utils/uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Tests a URL to see if it's on the same domain (local) or not
* @method isLocal
* @param {URL to test} url
* @return {bool} Whether the URL is local or not
* Derived from: https://github.com/jquery/jquery/blob/master/src/ajax.js
*/
function isLocal(url){
var rurl = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,
urlLower = url.toLowerCase(),
parts = rurl.exec( urlLower ),
ajaxLocation,
ajaxLocParts;

try {
ajaxLocation = location.href;
} catch( e ) {
// Use the href attribute of an A element
// since IE will modify it given document.location
ajaxLocation = document.createElement('a');
ajaxLocation.href = '';
ajaxLocation = ajaxLocation.href;
}

ajaxLocation = ajaxLocation.toLowerCase();

// uh oh, the url must be relative
// make it fully qualified and re-regex url
if (!parts){
urlLower = toAbsolute(ajaxLocation,urlLower).toLowerCase();
parts = rurl.exec( urlLower );
}

// Segment location into parts
ajaxLocParts = rurl.exec( ajaxLocation ) || [];

// do hostname and protocol and port of manifest URL match location.href? (a "local" request on the same domain)
var matched = !(parts &&
(parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
(parts[ 3 ] || (parts[ 1 ] === 'http:' ? '80' : '443')) !==
(ajaxLocParts[ 3 ] || (ajaxLocParts[ 1 ] === 'http:' ? '80' : '443'))));

return matched;
}

/**
* Parses URI
* @method parse
* @param {The URL to parse} url
* @return {Parsed URL} string
* Source: https://gist.github.com/Yaffle/1088850
* Tests: http://skew.org/uri/uri_tests.html
*/
function parse(url) {
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
}

/**
* Abosolutizes a relative URL
* @method toAbsolute
* @param {e.g., location.href} base
* @param {URL to absolutize} href
* @return {string} URL
* Source: https://gist.github.com/Yaffle/1088850
* Tests: http://skew.org/uri/uri_tests.html
*/
function toAbsolute(base, href) {// RFC 3986

function removeDotSegments(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}

href = parse(href || '');
base = parse(base || '');

return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash;
}

export default {
isLocal,
parse,
toAbsolute
};