Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit a9d37be

Browse files
author
Gabriel Schulhof
committed
Build: Use cheerio to update paths inside hrefs
Closes gh-7202 Fixes gh-6941 Fixes gh-6677
1 parent 68d2ccd commit a9d37be

File tree

1 file changed

+89
-23
lines changed

1 file changed

+89
-23
lines changed

Gruntfile.js

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ module.exports = function( grunt ) {
44
var _ = require( "underscore" ),
55
cheerio = require( "cheerio" ),
66

7-
replaceCombinedCssReference = function( content, processedName ) {
8-
return content.replace( /\.\.\/css\//, "css/" )
9-
.replace( /jquery\.mobile\.css/gi, processedName + ".min.css" );
7+
replaceCombinedCssReference = function( href, processedName ) {
8+
return href
9+
.replace( /\.\.\/css/, "css" )
10+
.replace( /jquery\.mobile\.css/, processedName + ".min.css" );
1011
},
1112

1213
// Ensure that modules specified via the --modules option are in the same
@@ -391,37 +392,96 @@ module.exports = function( grunt ) {
391392
"demos.processed": {
392393
options: {
393394
processContent: function( content, srcPath ) {
394-
var processedName = grunt.config.process( name + "<%= versionSuffix %>" );
395-
content = content.replace( /_assets\/js\/">/gi, "_assets/js/index.js\">" );
396-
content = content.replace( /\.\.\/external\/jquery\//gi, "js/" );
397-
content = content.replace( /\.\.\/js\/\"/gi, "js/\"" );
398-
content = content.replace( /js\/"/gi, "js/" + processedName + ".min.js\"" );
399-
content = replaceCombinedCssReference( content, processedName );
395+
var processedName, $;
396+
400397
content = content.replace( /^\s*<\?php include\(\s*['"]([^'"]+)['"].*$/gmi,
401398
function( match, includePath /*, offset, string */ ) {
402-
var fileToInclude, newSrcPath = srcPath;
399+
var newSrcPath = srcPath;
403400

404401
// If we've already handled the nested includes use the version
405402
// that was copied to the dist folder
406403
// TODO use the config from copy:demos.nested.files
407-
if( includePath.match(/jqm\-contents.php|jqm\-navmenu.php|jqm\-search.php/) ) {
404+
if( includePath.match(/jqm\-(contents|navmenu|search)\.php/) ) {
408405
newSrcPath = "dist/" + newSrcPath;
409406
}
410407

411-
fileToInclude = path.resolve( path.join(path.dirname(newSrcPath), includePath) );
412-
413-
return grunt.file.read( fileToInclude );
408+
return grunt.file.read( path.resolve( path.join(
409+
path.dirname( newSrcPath ), includePath ) ) );
414410
}
415411
);
416-
content = content.replace( /\.php/gi, ".html" );
417412

418-
// Demos that separately refer to the structure need to be processed here
419-
content = content.replace( /css\/structure\/jquery\.mobile\.structure\.css/gi,
420-
path.join( "css", "themes", "default", processedName + ".structure" + ".min.css" ) );
413+
if ( content.substring( 0, 15 ).toLowerCase() === "<!doctype html>" || srcPath.match( /\.php$/ ) ) {
414+
processedName = grunt.config.process( name + "<%= versionSuffix %>" );
415+
$ = cheerio.load( content );
416+
$( "script" ).each( function() {
417+
var text,
418+
element = $( this ),
419+
src = element.attr( "src" );
420+
421+
if ( src ) {
422+
element.attr( "src", src
423+
.replace( /_assets\/js\/?$/, "_assets/js/index.js" )
424+
.replace( /\.\.\/external\/jquery\/jquery.js$/,
425+
"js/jquery.js" )
426+
.replace( /\.\.\/js\/?$/,
427+
"js/" + processedName + ".min.js" )
428+
.replace( /^js\/?$/, "demos/js/" + processedName + ".min.js" ) );
429+
} else {
430+
text = element.text();
431+
432+
// References to stylesheets via grunticon need to be updated
433+
text = text.replace( /(grunticon\( \[([^\]]*))/,
434+
function( match, group ) {
435+
var index,
436+
offset = group.indexOf( "[" ),
437+
prefix = group.substring( 0, offset + 1 );
438+
439+
group = group.substring( offset + 1 ).split( "," );
440+
441+
for ( index in group ) {
442+
group[ index ] = "\"" + group[ index ]
443+
.trim()
444+
.replace( /(^['"])|(['"]$)/g, "" )
445+
.replace( /\.\.\/css\//, "css/" )
446+
.replace( /\.css$/, ".min.css" ) + "\"";
447+
}
448+
449+
return prefix + " " + group.join( "," ) + " ";
450+
});
451+
452+
//element.html( text );
453+
element[ 0 ].children[ 0 ].data = text;
454+
}
455+
});
456+
457+
$( "link[rel='stylesheet'][href]" ).each( function() {
458+
var element = $( this );
459+
460+
element.attr( "href",
461+
replaceCombinedCssReference( element.attr( "href" ),
462+
processedName )
463+
464+
// Demos that separately refer to the structure need to be
465+
// processed here
466+
.replace( /css\/structure\/jquery\.mobile\.structure\.css/gi,
467+
path.join( "css", "themes", "default",
468+
processedName + ".structure" + ".min.css" ) )
469+
470+
// References to the icons CSS file need to be processed here
471+
.replace( /css\/themes\/default\/jquery\.mobile\.icons\.css/,
472+
path.join( "..", "jquery.mobile.icons.min.css" ) ) );
421473

422-
// References to the icons CSS file need to be processed here
423-
content = content.replace( /css\/themes\/default\/jquery\.mobile\.icons\.css/gi,
424-
path.join( "..", "jquery.mobile.icons.min.css" ) );
474+
});
475+
476+
$( "a[href]" ).each( function() {
477+
var element = $( this );
478+
479+
element.attr( "href",
480+
element.attr( "href" ).replace( /\.php$/, ".html" ) );
481+
});
482+
483+
content = $.html();
484+
}
425485
return content;
426486
}
427487
},
@@ -442,10 +502,16 @@ module.exports = function( grunt ) {
442502

443503
if ( /\.html$/.test( srcPath ) ) {
444504

445-
content = replaceCombinedCssReference( content, processedName );
446-
447505
$ = cheerio.load( content );
448506

507+
$( "link[rel='stylesheet'][href]" ).each( function() {
508+
var element = $( this );
509+
510+
element.attr( "href",
511+
replaceCombinedCssReference( element.attr( "href" ),
512+
processedName ) );
513+
});
514+
449515
$( "script" ).each( function ( idx, element ) {
450516
var script = $( element );
451517
if ( /requirejs\.config\.js$/.test( script.attr( "src" ) ) ) {

0 commit comments

Comments
 (0)