Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #560 #563

Merged
merged 2 commits into from
Oct 6, 2012
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
10 changes: 6 additions & 4 deletions cli/tasks/usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = function(grunt) {

return asset;
}).reduce(function(a, b) {
b = b.split(',');
b = ( b ? b.split(',') : '');
return a.concat(b);
}, []);

Expand Down Expand Up @@ -308,7 +308,7 @@ module.exports = function(grunt) {

grunt.registerHelper('usemin:css', function(content, block, target) {
var indent = (block.split(linefeed)[0].match(/^\s*/) || [])[0];
return content.replace(block, indent + '<link rel="stylesheet" href="' + target + '"\/?>');
return content.replace(block, indent + '<link rel="stylesheet" href="' + target + '"\/>');
});

grunt.registerHelper('usemin:js', function(content, block, target) {
Expand Down Expand Up @@ -364,8 +364,10 @@ module.exports = function(grunt) {
var dirname = path.dirname(src);

// XXX files won't change, the filepath should filter the original list
// of cached files.
var filepath = grunt.file.expand(path.join('**/*') + basename)[0];
// of cached files (we need to treat the filename collision -- i.e. 2 files with same names
// in different subdirectories)
var filepaths = grunt.file.expand(path.join('**/*') + basename);
var filepath = filepaths.filter(function(f) { return dirname === path.dirname(f);})[0];

// not a file in temp, skip it
if ( !filepath ) {
Expand Down
79 changes: 79 additions & 0 deletions cli/test/fixtures/usemin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">

<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->


<link rel="stylesheet" href="styles/main.css">
<script src="scripts/vendor/modernizr.min.js"></script>
</head>
<body>
<div class="container" style="margin-top:50px">
<div class="hero-unit">
<h1>Wotcha!</h1>
<p>You now have</p>
<ul>
<li>HTML5 Boilerplate</li>
<li>Twitter Bootstrap</li>
<li>Twitter Bootstrap plugins</li>
<li>RequireJS</li>
<li>Support for ES6 Modules</li>
</ul>
<p>installed.</p>
<h3>Enjoy coding! - Yeoman</h3>
</div>
</div>

<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->

<!-- Add your site or application content here -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="scripts/vendor/jquery.min.js"><\/script>')</script>



<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>

<!-- build:js scripts/plugins.js -->
<script src="scripts/vendor/bootstrap/bootstrap-affix.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-alert.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-dropdown.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-tooltip.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-modal.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-transition.js"></script>

<script src="scripts/vendor/bootstrap/bootstrap-button.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-popover.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-typeahead.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-carousel.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-scrollspy.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-collapse.js"></script>
<script src="scripts/vendor/bootstrap/bootstrap-tab.js"></script>
<!-- endbuild -->

<!-- build:js scripts/amd-app.js -->
<script data-main="scripts/main" src="scripts/vendor/require.js"></script>
<!-- endbuild -->

<img src="images/test.png">
<img src="images/misc/test.png">
</body>
</html>
51 changes: 51 additions & 0 deletions cli/test/test-usemin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*global describe, before, after, beforeEach, afterEach, describe, it */
var fs = require('fs');
var path = require('path');
var grunt = require('grunt');
var assert = require('assert');
var helpers = require('./helpers');
var usemin = require('../tasks/usemin.js');

var opts = grunt.cli.options;
opts.redirect = !opts.silent;

// XXX Conform to coding guidelines, mostly literral spacing stuff
describe('usemin', function() {
before(helpers.directory('.test'));
describe('replace helper', function() {
it("should take into account path", function() {
usemin.call(grunt,grunt);

// Let's prepare our context: in index.html we do have references
// to images/test.png and images/misc/test.png.
// Usemin's replace is supposed to change this by files
// found on the filesystem matching the same path, but
// potentially prefixed
// e.g images/test.pmg -> images/misc/23012.test.png
grunt.file.mkdir('images');
grunt.file.mkdir('images/misc');
grunt.file.write('images/23012.test.png', "foo");
grunt.file.write('images/misc/2a436.test.png', "foo");

// Let's avoid cluttering the output
grunt.log.muted = true;
var content = grunt.file.read(path.join(__dirname,"fixtures/usemin.html"));
var changed = grunt.helper('replace',content, /<img[^\>]+src=['"]([^"']+)["']/gm);

// Check replace has performed its duty
assert.ok( changed.match(/img[^\>]+src=['"]images\/23012\.test\.png["']/) );
assert.ok( changed.match(/img[^\>]+src=['"]images\/misc\/2a436\.test\.png["']/) );
});
});
describe('usemin:css', function() {
it("should replace a block with link on furnished target", function() {
grunt.log.muted = true;
var block = " foo\nbar\nbaz";
var content = "before block\n" + block + "\nafter block";
var awaited = "before block\n <link rel=\"stylesheet\" href=\"foo\"/>\nafter block";
var changed = grunt.helper('usemin:css', content, block, 'foo');
assert.ok( changed == awaited );
});
});
});