home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 August / PCpro_2005_08.ISO / files / firefox / ScrapBook.xpi / chrome / scrapbook.jar / content / linemarker.js < prev    next >
Encoding:
JavaScript  |  2005-01-17  |  4.5 KB  |  130 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Line Marker.
  15.  *
  16.  * The Initial Developer of the Original Code is SHIMODA Hiroshi.
  17.  * Portions created by the Initial Developer are Copyright (C) 2002-2004
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s): SHIMODA Hiroshi <piro@p.club.ne.jp>
  21.  *
  22.  * Alternatively, the contents of this file may be used under the terms of
  23.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  24.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25.  * in which case the provisions of the GPL or the LGPL are applicable instead
  26.  * of those above. If you wish to allow use of your version of this file only
  27.  * under the terms of either the GPL or the LGPL, and not to allow others to
  28.  * use your version of this file under the terms of the MPL, indicate your
  29.  * decision by deleting the provisions above and replace them with the notice
  30.  * and other provisions required by the GPL or the LGPL. If you do not delete
  31.  * the provisions above, a recipient may use your version of this file under
  32.  * the terms of any one of the MPL, the GPL or the LGPL.
  33.  *
  34.  * ***** END LICENSE BLOCK ***** */
  35.  
  36. // The original script can be found at: http://piro.sakura.ne.jp/xul/_linemarker.html
  37.  
  38. function lmSetMarker(selection, spanClass, spanStyle, spanTitle)
  39. {
  40.     var targetWindow   = document.getElementById("ScrapBookBrowser").contentWindow;
  41.     var targetDocument = document.getElementById("ScrapBookBrowser").contentDocument;
  42.  
  43.     var range = targetWindow.document.createRange();
  44.  
  45.     try {
  46.         range.setStart(selection.anchorNode, selection.anchorOffset);
  47.         range.setEnd(selection.focusNode, selection.focusOffset);
  48.     } catch(ex) {
  49.         range.setStart(selection.focusNode, selection.focusOffset);
  50.         range.setEnd(selection.anchorNode, selection.anchorOffset);
  51.     }
  52.  
  53.     var line = targetDocument.createElement('span');
  54.     if ( spanStyle ) line.setAttribute('style', spanStyle);
  55.     if ( spanClass ) line.setAttribute('class', spanClass);
  56.     if ( spanTitle ) line.setAttribute('title', spanTitle);
  57.  
  58.     if (range.startContainer == range.endContainer) {
  59.  
  60.         var containerNode = range.startContainer;
  61.         var startOffset = range.startOffset;
  62.         range.setStartBefore(range.startContainer);
  63.         var startEdge = range.extractContents();
  64.         range.insertNode(startEdge);
  65.  
  66.         range.selectNode(containerNode.previousSibling);
  67.         range.setStart(containerNode.previousSibling, startOffset);
  68.         var lineContents = range.extractContents();
  69.  
  70.         line.appendChild(lineContents.removeChild(lineContents.firstChild));
  71.         lineContents.appendChild(line);
  72.         range.insertNode(lineContents);
  73.     }
  74.     else {
  75.         var startRange = targetDocument.createRange();
  76.         startRange.setStart(range.startContainer, range.startOffset);
  77.         startRange.setEndAfter(range.startContainer);
  78.         startRange.surroundContents(line.cloneNode(true));
  79.         startRange.detach();
  80.  
  81.         var endRange = targetDocument.createRange();
  82.         endRange.setStartBefore(range.endContainer);
  83.         endRange.setEnd(range.endContainer, range.endOffset);
  84.         var endLine = line.cloneNode(true);
  85.         endLine.appendChild(endRange.extractContents());
  86.         endRange.insertNode(endLine);
  87.         endRange.detach();
  88.  
  89.         lmWrapUpTexts(range.startContainer, range.endContainer, line);
  90.     }
  91.     range.detach();
  92.  
  93. }
  94.  
  95. // Find text nodes from aStartNode to aEndNode, and wrap up them in elemen node (aParent).
  96. function lmWrapUpTexts(aStartNode, aEndNode, aParent)
  97. {
  98.     var node = aStartNode,
  99.         newNode;
  100.  
  101.     traceTree:
  102.     do
  103.     {
  104.         if (node.hasChildNodes()) {
  105.             node = node.firstChild;
  106.         }
  107.         else {
  108.             while (!node.nextSibling)
  109.             {
  110.                 node = node.parentNode;
  111.                 if (!node) break traceTree;
  112.             }
  113.             node = node.nextSibling;
  114.         }
  115.         if (node == aEndNode) break traceTree;
  116.  
  117.         if (node.nodeType == Node.TEXT_NODE) {
  118.             newNode = aParent.cloneNode(true);
  119.             newNode.appendChild(node.cloneNode(true));
  120.             node.parentNode.replaceChild(newNode, node);
  121.             node = newNode.lastChild;
  122.         }
  123.     }
  124.     while (node != aEndNode);
  125.  
  126.     return;
  127. }
  128.  
  129.  
  130.