home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / code / contr / setup.exe / Disk1 / data1.cab / Configuration_En / Commands / PMUtils.js < prev    next >
Encoding:
JavaScript  |  2003-07-18  |  9.5 KB  |  353 lines

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