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 / PasteManager.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  8.0 KB  |  287 lines

  1. //=========================================================================================================
  2. //
  3. // Copyright 2002 Macromedia, Inc. All rights reserved.
  4. //
  5. // Feature: Paste Fix
  6. // Author:  JDH
  7. // Module:  PasteManager.js
  8. // Purpose:    Main Paste Manager class (and entry points).
  9. // Updates:
  10. //    5/17/02 - Started file control
  11. //    5/31/02 - Added more comments
  12. //
  13. //=========================================================================================================
  14.  
  15. //  The paste manager which handles the entire clipboard conversion process
  16.  
  17. function PasteManager()
  18. {
  19.     // Initialize the phases array
  20.  
  21.     this.phases = new Array();
  22.  
  23.     // Put together the initial list of content handlers
  24.  
  25.     var handlers = new Array();
  26.     handlers.push( new ParseMetaTags );
  27.     handlers.push( new IdentifyMSApplications );
  28.     handlers.push( new FixupMSGarbage );
  29.     handlers.push( new RetainStructure );
  30.     handlers.push( new DecomposeClasses );
  31.     handlers.push( new RemoveUnsupportedAttributes );
  32.     handlers.push( new RemoveParsingRequiredStructuralTags );
  33.     handlers.push( new RemoveCSSClasses );
  34.     handlers.push( new DemoteToParagraphs );
  35.     handlers.push( new SingleSpaceParagraphs );
  36.     handlers.push( new MergeRedundantFontTags );
  37.     handlers.push( new ChangeToStrongAndEm );
  38.  
  39.     // Iterate through the content handlers and put organize them by
  40.     // priority number into the phases member.
  41.  
  42.     for( index in handlers )
  43.     {
  44.         // Get the phase descriptor from the handler
  45.  
  46.         var phase_desc = handlers[ index ].getPhase();
  47.     
  48.         // Get the phase priority
  49.  
  50.         var phase = phase_desc.priority;
  51.  
  52.         // If the phase is currently empty then create the array to put the
  53.         // handlers into
  54.  
  55.         if ( this.phases[ phase ] == null )
  56.         {
  57.             this.phases[ phase ] = new Array();
  58.             this.phases[ phase ].name = phase_desc.name;
  59.             this.phases[ phase ].elements = new Array();
  60.         }
  61.  
  62.         // Push the handler into the handlers list for this phase number
  63.  
  64.         this.phases[ phase ].elements.push( handlers[ index ] );
  65.     }
  66. }
  67.  
  68. PasteManager.prototype.run = PasteManager_run;
  69. PasteManager.prototype.getClipHTML = PasteManager_getClipHTML;
  70. PasteManager.prototype.getDebugText = PasteManager_getDebugText;
  71. PasteManager.prototype.getDebugHTML = PasteManager_getDebugHTML;
  72.  
  73. function PasteManager_run( clipDOM, clipCSS, targetDOM, targetCSS, settings )
  74. {
  75.     // Put together the new context
  76.  
  77.     this.pasteContext = new PasteContext( clipDOM, clipCSS, targetDOM, targetCSS, settings );
  78.  
  79.     // Start the debug trace
  80.  
  81.     for( var setting in settings )
  82.         this.pasteContext.debugInformation( "PasteManager", "Setting : " + setting );
  83.     this.pasteContext.debugInformation( "PasteManager", "Start run" );
  84.  
  85.     // Iterate through the phases
  86.  
  87.     for ( var phase_index = 0; phase_index < PHASE_MAX; phase_index++ )
  88.     {
  89.         // If we have elements in this phase then iterate through them
  90.  
  91.         if ( this.phases[ phase_index ] )
  92.         {
  93.             // Put out the debugging information
  94.  
  95.             this.pasteContext.debugInformation( "PasteManager", "Phase: " + this.phases[ phase_index ].name );
  96.  
  97.             // Get the phase elements and interate through them and run each one
  98.  
  99.             var phase = this.phases[ phase_index ].elements;
  100.             for( handler_index in phase )
  101.             {
  102.                 var handler = phase[ handler_index ];
  103.                 handler.run( this.pasteContext );
  104.             }
  105.         }
  106.     }
  107.  
  108.     // Finish the debug trace
  109.  
  110.     this.pasteContext.debugInformation( "PasteManager", "End run" );
  111.     this.pasteContext.updateClipDOM();
  112.  
  113.     if ( this.pasteContext.settingDefined( SETTINGS_NO_FILTER ) )
  114.         return false;
  115.     return true;
  116. }
  117.  
  118. function PasteManager_getDebugText( ) { return this.pasteContext.getDebugText(); }
  119.  
  120. function PasteManager_getDebugHTML( ) { return this.pasteContext.getDebugHTML(); }
  121.  
  122. function PasteManager_getClipHTML( )
  123. {
  124.     // JDH: This is pretty brute force.  First we turn the whole document into text, then we 
  125.     // find the start and end of the fragment, suck it out, and then remove the markers from the
  126.     // finished piece.
  127.  
  128.     var full_html = this.pasteContext.getClipText();
  129.  
  130.     full_html = full_html.replace( /[\r\n]/g, " " );
  131.  
  132.     var out_html = full_html.match( /\<body\>(.*?)\<\/body\>/g );
  133.     out_html = out_html.toString();
  134.  
  135.     out_html = out_html.replace( /\<body\>/, "" );
  136.     out_html = out_html.replace( /\<\/body\>/, "" );
  137.     out_html = out_html.replace( /\<\!\-\-(\s*)StartFragment(\s*)\-\-\>/, "" );
  138.     out_html = out_html.replace( /\<\!\-\-(\s*)EndFragment(\s*)\-\-\>/, "" );
  139.  
  140.     return out_html;
  141. }
  142.  
  143.  
  144.  
  145. function smartPaste( bSilent, inputDOM, returnValue )
  146. {
  147.     // Check to make sure we aren't trying to paste in something huge
  148.  
  149. //    alert( inputDOM.documentElement.outerHTML );
  150. //    alert( "vesion: jdh_021203" );
  151. //    var deb = new DebugScanner();
  152. //    deb.scan( inputDOM.documentElement.outerHTML );
  153. //    return;
  154.     
  155.     if ( bSilent == false )
  156.     {
  157.         var text = inputDOM.documentElement.outerHTML;
  158.         if ( text.length > MM.od_MaxThreshold)
  159.         {
  160.             alert( MM.MSG_odStop );
  161.             returnValue[ 0 ] = true;
  162.             return;
  163.         }
  164.         if ( text.length > MM.od_WarnThreshold )
  165.         {
  166.             if ( ! confirm( MM.MSG_odWarn ) )
  167.             {
  168.                 returnValue[ 0 ] = true;
  169.                 return;
  170.             }
  171.         }
  172.     }
  173.  
  174.     try {
  175.  
  176.  
  177.         // Put together a new paste manager
  178.  
  179.         var mgr = new PasteManager();
  180.  
  181.         // Define the settings for the paste
  182.  
  183.         var settings = {};
  184.         settings[ SETTINGS_CONTRIBUTE ] = 1;
  185.  
  186.         // Look for Contribute specific settings
  187.         
  188.         if ( dreamweaver.appName == "Contribute" )
  189.         {
  190.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates() )
  191.                 settings[ SETTINGS_ETO ] = 1;
  192.  
  193.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_FontsEmitFontOrSpan() == "font" )
  194.                 settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] = 1;
  195.  
  196.             if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_FontsLetUserChange() )
  197.                 settings[ SETTINGS_NO_CSS ] = 1;
  198.  
  199.             if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleHTMLHeadings() )
  200.                 settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ] = 1;
  201.  
  202.             if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleCSS() )
  203.                 settings[ SETTINGS_NO_CSS ] = 1;
  204.  
  205.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_SingleSpaceParagraphsCSS() )
  206.                 settings[ SETTINGS_SINGLE_SPACE_P ] = 1;
  207.         }
  208.  
  209.         if ( dreamweaver.appName.match( /dreamweaver/i ) )
  210.         {
  211.             if ( dw.getPasteSettings().match( /high/ ) )
  212.             {
  213.                 settings[ SETTINGS_CREATE_CLASSES ] = 1;
  214.                 settings[ SETTINGS_NO_FONT_MAP ] = 1;
  215.             }
  216.             else
  217.             {
  218.                 settings[ SETTINGS_NO_CSS ] = 1;
  219.                 settings[ SETTINGS_LOW ] = 1;
  220.             }
  221.         }
  222.  
  223.         if ( dw.getPreferenceString("General Preferences", "Avoid Bold and Italic", 'TRUE') == 'TRUE')
  224.             settings[ SETTINGS_USE_EMPHASIS ] = 1;
  225.  
  226.         if ( dw.getPreferenceString("General", "Use <strong> and <em> in place of <b> and <i>", "") )
  227.             settings[ SETTINGS_USE_EMPHASIS ] = 1;
  228.  
  229.         // Get the names of the classes in the target
  230.  
  231.         var targetCSS = new CSSReferenceClassCollection();
  232.  
  233.         // Here we add referenced classes to the target CSS representative.  All that we
  234.         // use is the class name.
  235.         var styles = dreamweaver.cssStylePalette.getStyles();
  236.         for ( var index in styles )
  237.             targetCSS.add( styles[ index ] );
  238.  
  239.         // Get the CSS definitions from the clipboard
  240.  
  241.         var clipCSS = new CSSClassCollection( targetCSS );
  242.  
  243.         Utils_LoadCSSFromDOM( inputDOM, clipCSS );
  244.  
  245.         var filtered = mgr.run( inputDOM, clipCSS, null, targetCSS, settings );
  246.  
  247.         if ( filtered )
  248.         {
  249.             used_styles = clipCSS.create_used();
  250.     
  251.             out = mgr.getClipHTML();
  252.  
  253.             var htmlOut = "";
  254.             htmlOut += "<html>\n";
  255.             htmlOut += "<!--StartFragment-->\n";
  256.             htmlOut += "<head>\n";
  257.             if ( used_styles.length > 0 )
  258.             {
  259.                 htmlOut += "<style type=\"text/css\"><!--\n" + used_styles + "--></style>\n";
  260.             }
  261.             htmlOut += "</head>\n";
  262.             htmlOut += "<body>\n";
  263.             htmlOut += out;
  264.             htmlOut += "</body>\n";
  265.             htmlOut += "<!--EndFragment-->\n";
  266.             htmlOut += "</html>\n";
  267.  
  268.             inputDOM.documentElement.outerHTML = htmlOut;
  269.  
  270. //            alert( inputDOM.documentElement.outerHTML );
  271.         }
  272.  
  273.         dw.forceGarbageCollection();
  274.  
  275.         formatSource( inputDOM );
  276.  
  277.     }
  278.     catch( e )
  279.     {
  280.         alert( MM.MSG_odProblem );
  281.         dw.getDocumentDOM().clipPasteText();
  282.         returnValue[ 0 ] = true;
  283.     }
  284.  
  285.     return true;
  286. }
  287.