home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / PMUtils.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  10.1 KB  |  405 lines

  1. //=========================================================================================================
  2. //
  3. // Copyright 2002 Macromedia, Inc. All rights reserved.
  4. //
  5. // Feature: Paste Fix
  6. // Author:  JDH
  7. // Module:  PMUtils.js
  8. // Purpose:    Utility functions for Paste Fix.
  9. // Updates:
  10. //    5/17/02 - Started file control
  11. //
  12. //=========================================================================================================
  13.  
  14. // The map of font names to Contribute font names
  15.  
  16. var FONT_MAP = {
  17.   "arial": "Arial",
  18.   "arial black": "Arial",
  19.   "arial narrow": "Arial",
  20.   "arial unicode ms": "Arial",
  21.   "batang": "Times New Roman",
  22.   "book antiqua": "Georgia",
  23.   "bookman old style": "Georgia",
  24.   "century": "Georgia",
  25.   "century gothic": "Times New Roman",
  26.   "comic sans ms": "Times New Roman",
  27.   "courier new": "Courier New",
  28.   "estrangelo edessa": "Times New Roman",
  29.   "franklin gothic medium": "Verdana",
  30.   "garamond": "Georgia",
  31.   "gautami": "Times New Roman",
  32.   "georgia": "Georgia",
  33.   "haettenschweiler": "Verdana",
  34.   "impact": "Verdana",
  35.   "latha": "Times New Roman",
  36.   "lucida console": "Courier New",
  37.   "lucida sans unicode": "Verdana",
  38.   "ms mincho": "Courier New",
  39.   "ms outlook": "Times New Roman",
  40.   "mt extra": "Times New Roman",
  41.   "mv boli": "Times New Roman",
  42.   "mangal": "Times New Roman",
  43.   "marlett": "Times New Roman",
  44.   "microsoft sans serif": "Verdana",
  45.   "monotype corsiva": "Times New Roman",
  46.   "palatino linotype": "Times New Roman",
  47.   "raavi": "Verdana",
  48.   "shruti": "Times New Roman",
  49.   "simsun": "Georgia",
  50.   "sylfaen": "Times New Roman",
  51.   "symbol": "Times New Roman",
  52.   "tahoma": "Verdana",
  53.   "times new roman": "Times New Roman",
  54.   "trebuchet ms": "Verdana",
  55.   "tunga": "Times New Roman",
  56.   "verdana": "Verdana",
  57.   "webdings": "Times New Roman",
  58.   "wingdings": "Times New Roman",
  59.   "wingdings 2": "Times New Roman",
  60.   "wingdings 3": "Times New Roman"
  61. };
  62.  
  63. // The point values for mapping into font size values
  64.  
  65. var FONT_VALUES = new Array(8,10,12,14,18,24,36);
  66.  
  67. // Maps a font name into a Contribute font
  68.  
  69. function Utils_MapFont( fontName )
  70. {
  71.     // Massage the font name
  72.  
  73.     fontNames = fontName.split( "," );
  74.     if ( fontNames )
  75.         fontName = fontNames[ 0 ];
  76.  
  77.     fontName = fontName.replace( /\"/g, "" );
  78.     fontName = Utils_StripWhitespace( fontName );
  79.     fontName = fontName.toLowerCase();
  80.  
  81.     // Find the font in the font map
  82.  
  83.     if( FONT_MAP[ fontName ] != null )
  84.         return FONT_MAP[ fontName ];
  85.     else 
  86.     {
  87.         // If no font is found in the font map, check Asian system font list
  88.         // This font list is UTF-8 so J, K, TC & SC fonts are in the one list so we don't need
  89.         // to check encoding or system script
  90.         for ( var i = 0; i < ASIAN_SYSTEM_FONTS_LIST.length; i++ ) {
  91.             if ( fontName == ASIAN_SYSTEM_FONTS_LIST[i].toLowerCase() )
  92.                 // If font found in the Asian system font list, use it
  93.                 return ASIAN_SYSTEM_FONTS_LIST[ i ];
  94.         }
  95.     }
  96.  
  97.     // Return the default value for each encoding and platform if no font is found
  98.  
  99.     var charSet = dw.getDocumentDOM().getCharSet().toLowerCase();
  100.  
  101.     if (charSet == "shift_jis" || charSet == "x-sjis" || charSet == "euc-jp" || charSet == "iso-2022-jp")
  102.     {
  103.         if ( navigator.platform.charAt( 0 ) == "M" ) 
  104.             return JAPANESE_SYSTEM_FONTS_MAC_LIST[ 0 ];
  105.         else
  106.             return JAPANESE_SYSTEM_FONTS_WIN_LIST[ 0 ];
  107.     }
  108.     else if ( charSet == "euc-kr" ) 
  109.     {
  110.         if ( navigator.platform.charAt( 0 ) == "M" ) 
  111.             return KOREAN_SYSTEM_FONTS_MAC_LIST[ 0 ];
  112.         else
  113.             return KOREAN_SYSTEM_FONTS_WIN_LIST[ 0 ];
  114.     }
  115.     else if ( charSet == "big5" ) 
  116.     {
  117.         if ( navigator.platform.charAt( 0 ) == "M" ) 
  118.             return TCHINESE_SYSTEM_FONTS_MAC_LIST[ 0 ];
  119.         else
  120.             return TCHINESE_SYSTEM_FONTS_WIN_LIST[ 0 ];
  121.     }
  122.     else if ( charSet == "gb2312" ) 
  123.     {
  124.         if ( navigator.platform.charAt( 0 ) == "M" ) 
  125.             return SCHINESE_SYSTEM_FONTS_MAC_LIST[ 0 ];
  126.         else
  127.             return SCHINESE_SYSTEM_FONTS_WIN_LIST[ 0 ];
  128.     }
  129.     else
  130.         return "Times New Roman";
  131.  
  132. }
  133.  
  134. // Convert a point size to a font size value
  135.  
  136. function Utils_ConvertPointsToFontSizes( pointSize )
  137. {
  138.     // Remove any point value and convert to a number
  139.  
  140.     pointSize = pointSize.replace( /pt/, "" );
  141.     pointSize = parseFloat( pointSize );
  142.  
  143.   if (isNaN(pointSize)) pointSize = 10; //If not a number, set default to 10 pt
  144.  
  145.     // Look through the font values 
  146.  
  147.     for ( var index = 0; index < FONT_VALUES.length; index++ )
  148.     {
  149.         if ( FONT_VALUES[ index ] >= pointSize )
  150.             return index + 1;
  151.     }
  152.  
  153.     return FONT_VALUES.length;
  154. }
  155.  
  156. function Utils_DeleteArrayItem( find_key, in_array )
  157. {
  158.     // Create the output array
  159.  
  160.     var out_array = {};
  161.  
  162.     // Iterate through the values and only add those items that
  163.     // don't match the find key
  164.     
  165.     for( var key in in_array )
  166.     {
  167.         if ( key.toString() != find_key.toString() )
  168.             out_array[ key ] = in_array[ key ];
  169.     }
  170.  
  171.     // Return the finished new array
  172.  
  173.     return out_array;
  174. }
  175.  
  176. // Strip the whitespace from a string
  177.  
  178. function Utils_StripWhitespace( str )
  179. {
  180.     str = str.replace( /^\s+/, "" );
  181.     str = str.replace( /\s+$/, "" );
  182.     return str;
  183. }
  184.  
  185. // Build a string for the style attribute
  186.  
  187. function Utils_BuildStyle( styleHash )
  188. {
  189.     var styleText = "";
  190.  
  191.     for( var styleName in styleHash )
  192.     {
  193.         var styleValue = styleHash[ styleName ];
  194.         styleText += styleName + ":" + styleValue + ";";
  195.     }
  196.  
  197.     return styleText;
  198. }
  199.  
  200. // Build a string for the interior of a class
  201.  
  202. function Utils_BuildClass( styleHash )
  203. {
  204.     var styleText = "";
  205.  
  206.     for( var styleName in styleHash )
  207.     {
  208.         var ignore = false;
  209.         if ( styleName.match( /mso/ ) )
  210.         {
  211.             ignore = true;
  212.         }
  213.         if ( !ignore )
  214.         {
  215.             var styleValue = styleHash[ styleName ];
  216.             styleText += styleName + ":" + styleValue + ";\n";
  217.         }
  218.     }
  219.  
  220.     return styleText;
  221. }
  222.  
  223. // Split up a style string into an associative array
  224.  
  225. function Utils_ParseStyle( styleText )
  226. {
  227.     var outItems = new Array();
  228.  
  229.     var items = [];
  230.  
  231.     var block = "";
  232.     var escaped = false;
  233.     var inQuotes = false;
  234.  
  235.     for( var charIndex = 0; charIndex < styleText.length; charIndex++ )
  236.     {
  237.         if ( escaped == false && inQuotes == false && styleText[ charIndex ] == ";" )
  238.         {
  239.             if ( block.length > 0 )
  240.                 items.push( Utils_StripWhitespace( block ) );
  241.             block = "";
  242.         }
  243.         else
  244.         {
  245.             if ( styleText[ charIndex ] == '\\' )
  246.                 escaped = true;
  247.             else
  248.             {
  249.                 escaped = false;
  250.                 if ( styleText[ charIndex ] == '\"' )
  251.                     inQuotes = inQuotes ? false : true;
  252.                 block += styleText[ charIndex ];
  253.             }
  254.         }
  255.     }
  256.  
  257.     if ( block.length > 0 )
  258.         items.push( Utils_StripWhitespace( block ) );
  259.  
  260.     // Iterate through the items
  261.  
  262.     for( var item_index in items )
  263.     {
  264.         var styleItem = items[ item_index ];
  265.  
  266.         // Split the name and value on the ':' delimiter
  267.  
  268.         var values = styleItem.split( ":" );
  269.  
  270.         if ( values[ 0 ] && values[ 0 ].length > 0 )
  271.         {
  272.             if ( values[ 1 ] == null )
  273.                 values[ 1 ] = "";
  274.  
  275.             // Turn those into the key and values by stripping the whitespace
  276.             var key = Utils_StripWhitespace( values[ 0 ] );
  277.             var value = Utils_StripWhitespace( values[ 1 ] );
  278.  
  279.             // Store the key/value pair
  280.  
  281.             outItems[ key.toLowerCase() ] = value;
  282.         }
  283.     }
  284.  
  285.     return outItems;
  286. }
  287.  
  288. // Parse the contents of a <style> tag into an associative array of
  289. // class name and their associated definitions
  290.  
  291. function Utils_ParseClasses( classText )
  292. {
  293.     // Remove any <!-- --> comments, or /* */ comments
  294.  
  295.     classText = classText.replace( /\<\!\-\-/g, "" );
  296.     classText = classText.replace( /\-\-\>/g, "" );
  297.     classText = classText.replace( /\/\*(.*?)\*\//g, "" );
  298.  
  299.     // Put together an associative array of clsases
  300.  
  301.     var classes = new Array();
  302.  
  303.     // Find the classes in the string
  304.  
  305.     class_matches = classText.match( /([^{]*?)\{([^}]*?)\}/g );
  306.  
  307.     // Iterate through the class names
  308.  
  309.     for( var cm_index in class_matches )
  310.     {
  311.         var cm = class_matches[ cm_index ];
  312.  
  313.         // Pull out the carraige returns
  314.  
  315.         cm = cm.replace( /[\n\r]/g, " " );
  316.  
  317.         // Split the string on the { } so that the name will be in the
  318.         // first index and the class definition in the second index
  319.  
  320.         var classDataArray = cm.split( /[{}]/ );
  321.  
  322.         // Formalize the name and data strings
  323.  
  324.         var classNamesStr = Utils_StripWhitespace( classDataArray[ 0 ] );
  325.         var classData = Utils_StripWhitespace( classDataArray[ 1 ] );
  326.  
  327.         // Parse up the data
  328.  
  329.         var stylesData = Utils_ParseStyle( classData );
  330.  
  331.         // Go through the class names and add each class to the
  332.         // class list
  333.  
  334.         var classNames = classNamesStr.split( /\s+/ );
  335.         for( var cn_index in classNames )
  336.         {
  337.             // Clean up the class name
  338.  
  339.             var className = Utils_StripWhitespace( classNames[ cn_index ] );
  340.             className = className.replace( /,/, "" );
  341.  
  342.             // If there is a class name after all that then add it to the list
  343.  
  344.             if ( className.length > 0 )
  345.                 classes[ className ] = stylesData;
  346.         }
  347.     }
  348.  
  349.     return classes;
  350. }
  351.  
  352. // Tag handler for the comment finder
  353.  
  354. function findCommentHelper( tag, findData )
  355. {
  356.     if ( tag.data == findData.comment )
  357.     {
  358.         findData.node = tag;
  359.         return false;
  360.     }
  361.     return true;
  362. }
  363.  
  364. // Finds a particular comment within the document.  Most likely either
  365. // <!--StartFragment--> or <!--EndFragment-->
  366.  
  367. function Utils_FindComment( root, comment )
  368. {
  369.     var findData = { comment: comment, node: null };
  370.     traverse( root, null, null, findCommentHelper, findData );
  371.     return findData.node;
  372. }
  373.  
  374. // This finds all of the <style> tags in the passed in DOM and calls the
  375. // appropriate add method in the passed in class collection object
  376.  
  377. function Utils_LoadCSSFromDOM( dom, classes )
  378. {
  379.     // Get the style tags
  380.  
  381.     var scanner = new GetContentScanner( [ "style" ] );
  382.     var styles = scanner.scan( dom.documentElement.outerHTML );
  383.     
  384.     if ( styles != null )
  385.     {
  386.         for( var st_index in styles )
  387.         {
  388.             var styleText = styles[ st_index ];
  389.  
  390.             styleText = styleText.replace( /\/\*([^*]*)\*\//g, "" );
  391.             styleText = styleText.replace( /\<\!\-\-/g, "" );
  392.             styleText = styleText.replace( /\-\-\>/g, "" );
  393.  
  394.             // Parse the classes and get the classes back from each style tag
  395.  
  396.             var classHash = Utils_ParseClasses( styleText );
  397.  
  398.             // Add each class to the class container
  399.  
  400.             for( var className in classHash )
  401.                 classes.add( className, classHash[ className ] );
  402.         }
  403.     }
  404. }
  405.