Skip to content

Commit

Permalink
Add support for multiple fallback CDN locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Johnston committed Oct 3, 2011
1 parent 7ccfe56 commit 762539a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
8 changes: 4 additions & 4 deletions build.xml
Expand Up @@ -3,8 +3,8 @@
<project name="PIE" default="build" basedir=".">

<property name="version" value="1.0beta6-SNAPSHOT" />
<!--<property name="default_base_url" value="http://cdnjs.cloudflare.com/ajax/libs/css3pie/${version}" />-->
<property name="default_base_url" value="http://lojjic.github.com/PIE/files/builds/${version}" />
<!-- 'http://cdnjs.cloudflare.com/ajax/libs/css3pie/${version}' -->
<property name="default_base_urls" value="['http://lojjic.github.com/PIE/files/builds/${version}']"/>
<property name="build_dir" value="./build" />
<property name="src_dir" value="./sources" />

Expand Down Expand Up @@ -94,7 +94,7 @@
<replace file="${build_dir}/PIE_uncompressed.htc">
<replacefilter token="$Version$" value="${version}" />
<replacefilter token="$JSVariant$" value="_uncompressed" />
<replacefilter token="$DefaultBaseUrl$" value="${default_base_url}" />
<replacefilter token="$DefaultBaseUrls$" value="${default_base_urls}" />
</replace>
<replace file="${build_dir}/PIE_IE678_uncompressed.js" token="$Version$" value="${version}"/>
<replace file="${build_dir}/PIE_IE9_uncompressed.js" token="$Version$" value="${version}"/>
Expand Down Expand Up @@ -125,7 +125,7 @@
<replace file="${build_dir}/PIE.htc">
<replacefilter token="$Version$" value="${version}" />
<replacefilter token="$JSVariant$" value="" />
<replacefilter token="$DefaultBaseUrl$" value="${default_base_url}" />
<replacefilter token="$DefaultBaseUrls$" value="${default_base_urls}" />
</replace>
<replace file="${build_dir}/PIE_IE678.js" token="$Version$" value="${version}"/>
<replace file="${build_dir}/PIE_IE9.js" token="$Version$" value="${version}"/>
Expand Down
45 changes: 28 additions & 17 deletions sources/htc_script.js
Expand Up @@ -6,9 +6,9 @@ var el = element,
if (!PIE && docMode < 10) {
(function() {
var queue = {},
defaultBaseUrl = '$DefaultBaseUrl$',
baseUrls = $DefaultBaseUrls$,
protocol = doc.location.protocol,
baseUrl, script, tester, isIE6;
baseUrl, tester, isIE6, i = 0;

// Create stub PIE object
PIE = window[ 'PIE' ] = {
Expand All @@ -30,27 +30,38 @@ if (!PIE && docMode < 10) {
baseUrl = doc.documentElement.currentStyle.getAttribute( ( isIE6 ? '' : '-' ) + 'pie-base-url' );
if( baseUrl ) {
baseUrl = baseUrl.replace(/^"|"$/g, '');
} else {
baseUrl = ( protocol === 'https:' ? protocol + defaultBaseUrl.replace( /^[^\/]*]/, '' ) : defaultBaseUrl );
baseUrls = [ baseUrl ];
}
baseUrl += '/PIE_IE' + ( docMode < 9 ? '678' : '9' ) + '$JSVariant$.js';

// Start loading JS file
script = doc.createElement( 'script' );
script.async = true;
script.onreadystatechange = function() {
var rs = script.readyState, id;
if ( queue && ( rs === 'complete' || rs === 'loaded' ) ) {
for( id in queue ) {
if ( queue.hasOwnProperty( id ) ) {
PIE[ 'attach' ]( queue[ id ] );
function tryLoading( baseUrl ) {
var script = doc.createElement( 'script' );
script.async = true;
script.onreadystatechange = function() {
var rs = script.readyState, id;
if ( queue && ( rs === 'complete' || rs === 'loaded' ) ) {
if ( 'version' in PIE ) {
for( id in queue ) {
if ( queue.hasOwnProperty( id ) ) {
PIE[ 'attach' ]( queue[ id ] );
}
}
queue = 0;
} else {
tryLoading( baseUrls[ ++i ] );
}
}
queue = 0;
};

if ( protocol === 'https:' ) {
baseUrl = baseUrl.replace( /^http:/, protocol );
}
};
script.src = baseUrl;
( doc.getElementsByTagName( 'head' )[0] || doc.body ).appendChild( script );
script.src = baseUrl + '/PIE_IE' + ( docMode < 9 ? '678' : '9' ) + '$JSVariant$.js';
( doc.getElementsByTagName( 'head' )[0] || doc.body ).appendChild( script );
}

tryLoading( baseUrls[ i ] );

})();
}

Expand Down

0 comments on commit 762539a

Please sign in to comment.