From 93d03c917282c1a17b894e15ad4a5d4db377c358 Mon Sep 17 00:00:00 2001 From: Dominic Watson Date: Mon, 19 May 2014 12:50:56 +0100 Subject: [PATCH] #106 New method, getIncludeUrl(), that allows you to retrieve the compiled URL of a given resource [skip ci] --- org/cfstatic/CfStatic.cfc | 35 ++++++++++++++++++- org/cfstatic/util/Base.cfc | 15 ++++++++ .../integration/org/cfstatic/CfStaticTest.cfc | 8 +++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/org/cfstatic/CfStatic.cfc b/org/cfstatic/CfStatic.cfc index 4fa5696..8550d17 100755 --- a/org/cfstatic/CfStatic.cfc +++ b/org/cfstatic/CfStatic.cfc @@ -115,6 +115,33 @@ + + + + + + + + var include = _appendFileTypesToSpecialIncludes( resource ); + + if ( arguments.throwOnMissing and not _resourceExists( arguments.resource ) ) { + $throw( type="cfstatic.missing.include", message="CfStatic include() error: The requested include, [#arguments.resource#], does not exist." ); + } + + renderCache = _getRenderedIncludeCache( type, debugMode )._urls; + + if ( StructKeyExists( renderCache, resource ) ) { + return renderCache[ resource ]; + } + + if ( throwOnMissing ) { + $throw( type="cfstatic.missing.include", message="CfStatic getIncludeUrl() error: The requested include, [#arguments.resource#], does not exist." ); + } + + return ""; + + + @@ -990,6 +1017,11 @@ _renderedIncludeCache.css['_ordered'] = ArrayNew(1); _renderedIncludeCache.debug.js['_ordered'] = ArrayNew(1); _renderedIncludeCache.debug.css['_ordered'] = ArrayNew(1); + + _renderedIncludeCache.js['_urls'] = StructNew(); + _renderedIncludeCache.css['_urls'] = StructNew(); + _renderedIncludeCache.debug.js['_urls'] = StructNew(); + _renderedIncludeCache.debug.css['_urls'] = StructNew(); @@ -1007,8 +1039,9 @@ node = _renderedIncludeCache[ type ]; } - ArrayAppend( node['_ordered'], rendered ); + ArrayAppend( node[ '_ordered' ], rendered ); node[ path ] = ArrayLen( node['_ordered'] ); + node[ '_urls' ][ path ] = $extractUrlFromRenderedInclude( rendered ); diff --git a/org/cfstatic/util/Base.cfc b/org/cfstatic/util/Base.cfc index f2269b7..e905994 100644 --- a/org/cfstatic/util/Base.cfc +++ b/org/cfstatic/util/Base.cfc @@ -182,6 +182,21 @@ ', ieConditional ) & $newline() /> + + + + + var regex = '^.*?(href|src)="(.*?)".*$'; + var replaced = ReReplaceNoCase( renderedInclude, regex, "\2" ); + + if ( replaced == renderedInclude ) { + return ""; + } + + return replaced; + + + diff --git a/tests/integration/org/cfstatic/CfStaticTest.cfc b/tests/integration/org/cfstatic/CfStaticTest.cfc index a18df80..9d627be 100644 --- a/tests/integration/org/cfstatic/CfStaticTest.cfc +++ b/tests/integration/org/cfstatic/CfStaticTest.cfc @@ -1067,8 +1067,9 @@ var expected = "/mystaticurl/min/someFolder.min.css"; - rootDir &= 'goodFiles/standardFolders/'; + var actual = ""; + rootDir &= 'goodFiles/standardFolders/'; cfstatic.init( staticDirectory = rootDir @@ -1076,7 +1077,10 @@ , debugKey = "doNotLetMxUnitDebugScrewTests" ); - super.assertEquals( expected, cfstatic.getIncludeUrl( "/css/someFolder/" ) ); + actual = cfstatic.getIncludeUrl( "css", "/someFolder/" ); + actual = _removeCheckSumFromFileNames( actual ); + + super.assertEquals( expected, actual );