-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#169 Add <cbwire:script> and <cbwire:assets> functionality
- Loading branch information
1 parent
8072a7b
commit e4df642
Showing
7 changed files
with
220 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
component { | ||
/** | ||
* Returns content with scripts and assets parsed. | ||
* | ||
* @content string | The raw template contents | ||
* | ||
* @return string | ||
*/ | ||
function handle( content ) { | ||
// Handle <cbwire:script> tags | ||
content = parseScripts( content ); | ||
// Handle <cbwire:assets> tags | ||
content = parseAssets( content ); | ||
return content; | ||
} | ||
|
||
/** | ||
* Parses <cbwire:script> tags. | ||
* | ||
* @content | The raw template contents | ||
* @counter | Tracks the instances of cbwire:script | ||
* | ||
* @return string | ||
*/ | ||
function parseScripts( content, counter = 1 ) { | ||
if ( counter == 1 ) { | ||
content = replaceNoCase( content, "</cbwire:script>", "</cfsavecontent>", "all" ); | ||
} | ||
content = replaceNoCase( content, "<cbwire:script>", "<cfsavecontent variable=""attributes.returnValues.script#counter#"">" ); | ||
if ( findNoCase( "<cbwire:script>", content ) ) { | ||
content = parseScripts( content, counter + 1 ); | ||
} | ||
return content; | ||
} | ||
|
||
/** | ||
* Parses <cbwire:assets> tags. | ||
* | ||
* @content | The raw template contents | ||
* @counter | Tracks the instances of cbwire:assets | ||
* | ||
* @return string | ||
*/ | ||
function parseAssets( content, counter = 1 ) { | ||
if ( counter == 1 ) { | ||
content = replaceNoCase( content, "</cbwire:assets>", "</cfsavecontent>", "all" ); | ||
} | ||
content = replaceNoCase( content, "<cbwire:assets>", "<cfsavecontent variable=""attributes.returnValues.assets#counter#"">" ); | ||
if ( findNoCase( "<cbwire:assets>", content ) ) { | ||
content = parseAssets( content, counter + 1 ); | ||
} | ||
return content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,6 +287,33 @@ component extends="coldbox.system.testing.BaseTestCase" { | |
expect( parent.snapshot.memo.children[ keys[ 1 ] ][ 2 ] ).toBe( child.snapshot.memo.id ); | ||
} ); | ||
|
||
it( "should not render cbwire:script tags", function() { | ||
var result = CBWIREController.wire( "test.should_not_render_cbwire_script_tags" ); | ||
expect( result ).notToInclude( "<cbwire:script>" ); | ||
expect( result ).notToInclude( "</cbwire:script>" ); | ||
expect( result ).notToInclude( "This should not be rendered" ); | ||
} ); | ||
|
||
it( "should not render cbwire:assets tags", function() { | ||
var result = CBWIREController.wire( "test.should_not_render_cbwire_assets_tags" ); | ||
expect( result ).notToInclude( "<cbwire:assets>" ); | ||
expect( result ).notToInclude( "</cbwire:assets>" ); | ||
expect( result ).notToInclude( "tailwind.min.css" ); | ||
} ); | ||
|
||
fit( "should track scripts and assets in snapshot memo", function() { | ||
var result = CBWIREController.wire( "test.should_track_scripts_and_assets_in_snapshot_memo" ); | ||
var parsing = parseRendering( result ); | ||
writeDump( parsing.snapshot.memo ); | ||
abort; | ||
expect( parsing.snapshot.memo.scripts ).toBeArray(); | ||
expect( parsing.snapshot.memo.scripts.len() ).toBe( 1 ); | ||
expect( parsing.snapshot.memo.scripts[ 1 ] ).toBe( "https://cdn.jsdelivr.net/npm/[email protected]/dist/alpine.min.js" ); | ||
expect( parsing.snapshot.memo.assets ).toBeArray(); | ||
expect( parsing.snapshot.memo.assets.len() ).toBe( 1 ); | ||
expect( parsing.snapshot.memo.assets[ 1 ] ).toBe( "tailwind.min.css" ); | ||
} ); | ||
|
||
xit( "should provide original path to component when there is a template rendering error", function() { | ||
var result = CBWIREController.wire( "test.should_raise_error_for_template_rendering_error" ); | ||
expect( function() { | ||
|
15 changes: 15 additions & 0 deletions
15
test-harness/wires/test/should_not_render_cbwire_assets_tags.cfm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<cfoutput> | ||
<div> | ||
<h1>Should not render cbwire assets tags</h1> | ||
</div> | ||
</cfoutput> | ||
|
||
<cfscript> | ||
// @startWire | ||
// @endWire | ||
</cfscript> | ||
|
||
<cbwire:assets> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"> | ||
</cbwire:script> |
17 changes: 17 additions & 0 deletions
17
test-harness/wires/test/should_not_render_cbwire_script_tags.cfm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<cfoutput> | ||
<div> | ||
<h1>Should not render cbwire script tags</h1> | ||
</div> | ||
</cfoutput> | ||
|
||
<cfscript> | ||
// @startWire | ||
// @endWire | ||
</cfscript> | ||
|
||
<cbwire:script> | ||
<script> | ||
console.log('This should not be rendered'); | ||
</script> | ||
</cbwire:script> |
27 changes: 27 additions & 0 deletions
27
test-harness/wires/test/should_track_scripts_and_assets_in_snapshot_memo.cfm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<cfoutput> | ||
<div> | ||
<h1>Should track scripts and assets</h1> | ||
</div> | ||
</cfoutput> | ||
|
||
<cfscript> | ||
// @startWire | ||
// @endWire | ||
</cfscript> | ||
|
||
<cbwire:script> | ||
<script> | ||
console.log('This should be tracked'); | ||
</script> | ||
</cbwire:script> | ||
|
||
<cbwire:script> | ||
<script> | ||
console.log('This should be tracked also'); | ||
</script> | ||
</cbwire:script> | ||
|
||
<cbwire:assets> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"> | ||
</cbwire:script> |