Нема описа

css.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var url = require( "url" );
  3. var path = require( "path" );
  4. var inline = require( "./util" );
  5. module.exports = function( options, callback )
  6. {
  7. var settings = Object.assign( {}, inline.defaults, options );
  8. var replaceUrl = function( callback )
  9. {
  10. var args = this;
  11. if( inline.isBase64Path( args.src ) )
  12. {
  13. return callback( null ); // Skip
  14. }
  15. inline.getFileReplacement( args.src, settings, function( err, datauriContent )
  16. {
  17. if( err )
  18. {
  19. return inline.handleReplaceErr( err, args.src, settings.strict, callback );
  20. }
  21. if( typeof( args.limit ) === "number" && datauriContent.length > args.limit * 1000 )
  22. {
  23. return callback( null ); // Skip
  24. }
  25. var css = "url(\"" + datauriContent + "\")";
  26. var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( args.src ) + ")[\"']?\\s?\\)", "g" );
  27. result = result.replace( re, () => css );
  28. return callback( null );
  29. } );
  30. };
  31. var rebase = function( src )
  32. {
  33. var css = "url(\"" + ( inline.isRemotePath( src ) || inline.isRemotePath( settings.rebaseRelativeTo ) ? url.resolve( settings.rebaseRelativeTo, src ) : path.join( settings.rebaseRelativeTo, src ).replace( /\\/g, "/" ) ) + "\")";
  34. var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( src ) + ")[\"']?\\s?\\)", "g" );
  35. result = result.replace( re, () => css );
  36. };
  37. var result = settings.fileContent;
  38. var tasks = [];
  39. var found = null;
  40. var urlRegex = /url\(\s?["']?([^)'"]+)["']?\s?\).*/i;
  41. var index = 0;
  42. if( settings.rebaseRelativeTo )
  43. {
  44. var matches = {};
  45. var src;
  46. while( ( found = urlRegex.exec( result.substring( index ) ) ) !== null )
  47. {
  48. src = found[ 1 ];
  49. matches[ src ] = true;
  50. index = found.index + index + 1;
  51. }
  52. for( src in matches )
  53. {
  54. if( !inline.isRemotePath( src ) && !inline.isBase64Path( src ) )
  55. {
  56. rebase( src );
  57. }
  58. }
  59. }
  60. var inlineAttributeCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "\\s?\\*\\/", "i" );
  61. var inlineAttributeIgnoreCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "-ignore\\s?\\*\\/", "i" );
  62. index = 0;
  63. while( ( found = urlRegex.exec( result.substring( index ) ) ) !== null )
  64. {
  65. if( !inlineAttributeIgnoreCommentRegex.test( found[ 0 ] ) &&
  66. ( settings.images || inlineAttributeCommentRegex.test( found[ 0 ] ) ) )
  67. {
  68. tasks.push( replaceUrl.bind(
  69. {
  70. src: found[ 1 ],
  71. limit: settings.images
  72. } ) );
  73. }
  74. index = found.index + index + 1;
  75. }
  76. var promises = tasks.map( function( fn )
  77. {
  78. return new Promise( function( resolve, reject )
  79. {
  80. fn( function( error )
  81. {
  82. if ( error ) {
  83. reject ( error );
  84. } else {
  85. resolve();
  86. }
  87. } );
  88. } );
  89. } );
  90. Promise.all( promises )
  91. .then( function()
  92. {
  93. callback( null, result );
  94. }, function( error )
  95. {
  96. callback( error, result );
  97. } );
  98. };