From 05bdc8e39928c520279234714e9f2f99ff79062a Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 3 Mar 2014 18:20:17 +0200 Subject: [PATCH 1/3] Build: Refine code that fixes the path for the combined CSS Fixes gh-6677 --- Gruntfile.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index bce0330e56f..a948a74e000 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -5,8 +5,40 @@ module.exports = function( grunt ) { cheerio = require( "cheerio" ), replaceCombinedCssReference = function( content, processedName ) { - return content.replace( /\.\.\/css\//, "css/" ) - .replace( /jquery\.mobile\.css/gi, processedName + ".min.css" ); + return content.replace( /([<]link[^>]*[>])/gi, function( match ) { + var index, tag, tagDelimiter, attributeDelimiter, pair; + + // Strip angle brackets + match = match.substring( 1, match.length - 1 ); + + // Split off the tag + tagDelimiter = match.indexOf( " " ); + tag = match.substring( 0, tagDelimiter ); + + // Split the rest into attribute definitions + match = match + .substring( tagDelimiter ) + + // Assumes no spaces in the attribute values + .split( " " ); + + // Establish attributes + for ( index in match ) { + attributeDelimiter = match[ index ].indexOf( "=" ); + pair = [ + match[ index ].substring( 0, attributeDelimiter ), + match[ index ].substring( attributeDelimiter + 1 ) ]; + + if ( pair[ 0 ] === "href" ) { + pair[ 1 ] = pair[ 1 ] + .replace( /\.\.\/css\//, "css/" ) + .replace( /jquery\.mobile\.css/, processedName + ".min.css" ); + match[ index ] = pair.join( "=" ); + } + } + + return "<" + tag + match.join( " " ) + ">"; + }); }, // Ensure that modules specified via the --modules option are in the same From 19326be1b7fe7bf0f3efdf196cf9e3cb75e6dca2 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 3 Mar 2014 18:21:26 +0200 Subject: [PATCH 2/3] Build: Update references to grunticon paths Closes gh-7202 Fixes gh-6941 --- Gruntfile.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Gruntfile.js b/Gruntfile.js index a948a74e000..2171b6e8a2d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -454,6 +454,26 @@ module.exports = function( grunt ) { // References to the icons CSS file need to be processed here content = content.replace( /css\/themes\/default\/jquery\.mobile\.icons\.css/gi, path.join( "..", "jquery.mobile.icons.min.css" ) ); + + // References to stylesheets via grunticon need to be updated + content = content.replace( /(grunticon\( \[([^\]]*))/, + function( match, group ) { + var index, + offset = group.indexOf( "[" ), + prefix = group.substring( 0, offset + 1 ); + + group = group.substring( offset + 1 ).split( "," ); + + for ( index in group ) { + group[ index ] = "\"" + group[ index ] + .trim() + .replace( /(^['"])|(['"]$)/g, "" ) + .replace( /\.\.\/css\//, "css/" ) + .replace( /\.css$/, ".min.css" ) + "\""; + } + + return prefix + " " + group.join( "," ) + " "; + }); return content; } }, From 06dc30cffb1d7565209ec99a3fe657740d756400 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Tue, 4 Mar 2014 15:05:29 +0200 Subject: [PATCH 3/3] Build: Process HTML files with cheerio --- Gruntfile.js | 162 ++++++++++++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 74 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 2171b6e8a2d..554df3e6d3b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,41 +4,10 @@ module.exports = function( grunt ) { var _ = require( "underscore" ), cheerio = require( "cheerio" ), - replaceCombinedCssReference = function( content, processedName ) { - return content.replace( /([<]link[^>]*[>])/gi, function( match ) { - var index, tag, tagDelimiter, attributeDelimiter, pair; - - // Strip angle brackets - match = match.substring( 1, match.length - 1 ); - - // Split off the tag - tagDelimiter = match.indexOf( " " ); - tag = match.substring( 0, tagDelimiter ); - - // Split the rest into attribute definitions - match = match - .substring( tagDelimiter ) - - // Assumes no spaces in the attribute values - .split( " " ); - - // Establish attributes - for ( index in match ) { - attributeDelimiter = match[ index ].indexOf( "=" ); - pair = [ - match[ index ].substring( 0, attributeDelimiter ), - match[ index ].substring( attributeDelimiter + 1 ) ]; - - if ( pair[ 0 ] === "href" ) { - pair[ 1 ] = pair[ 1 ] - .replace( /\.\.\/css\//, "css/" ) - .replace( /jquery\.mobile\.css/, processedName + ".min.css" ); - match[ index ] = pair.join( "=" ); - } - } - - return "<" + tag + match.join( " " ) + ">"; - }); + replaceCombinedCssReference = function( href, processedName ) { + return href + .replace( /\.\.\/css/, "css" ) + .replace( /jquery\.mobile\.css/, processedName + ".min.css" ); }, // Ensure that modules specified via the --modules option are in the same @@ -423,57 +392,96 @@ module.exports = function( grunt ) { "demos.processed": { options: { processContent: function( content, srcPath ) { - var processedName = grunt.config.process( name + "<%= versionSuffix %>" ); - content = content.replace( /_assets\/js\/">/gi, "_assets/js/index.js\">" ); - content = content.replace( /\.\.\/external\/jquery\//gi, "js/" ); - content = content.replace( /\.\.\/js\/\"/gi, "js/\"" ); - content = content.replace( /js\/"/gi, "js/" + processedName + ".min.js\"" ); - content = replaceCombinedCssReference( content, processedName ); + var processedName, $; + content = content.replace( /^\s*<\?php include\(\s*['"]([^'"]+)['"].*$/gmi, function( match, includePath /*, offset, string */ ) { - var fileToInclude, newSrcPath = srcPath; + var newSrcPath = srcPath; // If we've already handled the nested includes use the version // that was copied to the dist folder // TODO use the config from copy:demos.nested.files - if( includePath.match(/jqm\-contents.php|jqm\-navmenu.php|jqm\-search.php/) ) { + if( includePath.match(/jqm\-(contents|navmenu|search)\.php/) ) { newSrcPath = "dist/" + newSrcPath; } - fileToInclude = path.resolve( path.join(path.dirname(newSrcPath), includePath) ); - - return grunt.file.read( fileToInclude ); + return grunt.file.read( path.resolve( path.join( + path.dirname( newSrcPath ), includePath ) ) ); } ); - content = content.replace( /\.php/gi, ".html" ); - - // Demos that separately refer to the structure need to be processed here - content = content.replace( /css\/structure\/jquery\.mobile\.structure\.css/gi, - path.join( "css", "themes", "default", processedName + ".structure" + ".min.css" ) ); - - // References to the icons CSS file need to be processed here - content = content.replace( /css\/themes\/default\/jquery\.mobile\.icons\.css/gi, - path.join( "..", "jquery.mobile.icons.min.css" ) ); - - // References to stylesheets via grunticon need to be updated - content = content.replace( /(grunticon\( \[([^\]]*))/, - function( match, group ) { - var index, - offset = group.indexOf( "[" ), - prefix = group.substring( 0, offset + 1 ); - - group = group.substring( offset + 1 ).split( "," ); - - for ( index in group ) { - group[ index ] = "\"" + group[ index ] - .trim() - .replace( /(^['"])|(['"]$)/g, "" ) - .replace( /\.\.\/css\//, "css/" ) - .replace( /\.css$/, ".min.css" ) + "\""; + + if ( content.substring( 0, 15 ).toLowerCase() === "" || srcPath.match( /\.php$/ ) ) { + processedName = grunt.config.process( name + "<%= versionSuffix %>" ); + $ = cheerio.load( content ); + $( "script" ).each( function() { + var text, + element = $( this ), + src = element.attr( "src" ); + + if ( src ) { + element.attr( "src", src + .replace( /_assets\/js\/?$/, "_assets/js/index.js" ) + .replace( /\.\.\/external\/jquery\/jquery.js$/, + "js/jquery.js" ) + .replace( /\.\.\/js\/?$/, + "js/" + processedName + ".min.js" ) + .replace( /^js\/?$/, "demos/js/" + processedName + ".min.js" ) ); + } else { + text = element.text(); + + // References to stylesheets via grunticon need to be updated + text = text.replace( /(grunticon\( \[([^\]]*))/, + function( match, group ) { + var index, + offset = group.indexOf( "[" ), + prefix = group.substring( 0, offset + 1 ); + + group = group.substring( offset + 1 ).split( "," ); + + for ( index in group ) { + group[ index ] = "\"" + group[ index ] + .trim() + .replace( /(^['"])|(['"]$)/g, "" ) + .replace( /\.\.\/css\//, "css/" ) + .replace( /\.css$/, ".min.css" ) + "\""; + } + + return prefix + " " + group.join( "," ) + " "; + }); + + //element.html( text ); + element[ 0 ].children[ 0 ].data = text; } + }); + + $( "link[rel='stylesheet'][href]" ).each( function() { + var element = $( this ); + + element.attr( "href", + replaceCombinedCssReference( element.attr( "href" ), + processedName ) - return prefix + " " + group.join( "," ) + " "; + // Demos that separately refer to the structure need to be + // processed here + .replace( /css\/structure\/jquery\.mobile\.structure\.css/gi, + path.join( "css", "themes", "default", + processedName + ".structure" + ".min.css" ) ) + + // References to the icons CSS file need to be processed here + .replace( /css\/themes\/default\/jquery\.mobile\.icons\.css/, + path.join( "..", "jquery.mobile.icons.min.css" ) ) ); + + }); + + $( "a[href]" ).each( function() { + var element = $( this ); + + element.attr( "href", + element.attr( "href" ).replace( /\.php$/, ".html" ) ); }); + + content = $.html(); + } return content; } }, @@ -494,10 +502,16 @@ module.exports = function( grunt ) { if ( /\.html$/.test( srcPath ) ) { - content = replaceCombinedCssReference( content, processedName ); - $ = cheerio.load( content ); + $( "link[rel='stylesheet'][href]" ).each( function() { + var element = $( this ); + + element.attr( "href", + replaceCombinedCssReference( element.attr( "href" ), + processedName ) ); + }); + $( "script" ).each( function ( idx, element ) { var script = $( element ); if ( /requirejs\.config\.js$/.test( script.attr( "src" ) ) ) {