home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / schema.js < prev    next >
Encoding:
Text File  |  2004-07-12  |  7.2 KB  |  274 lines

  1. /*
  2.  * Copyright 1999-2004 The Apache Software Foundation
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *  Unless required by applicable law or agreed to in writing, software
  11.  *  distributed under the License is distributed on an "AS IS" BASIS,
  12.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *  See the License for the specific language governing permissions and
  14.  *  limitations under the License.
  15.  */
  16.  
  17. /*
  18.  * WARNING: this file is supposed to be cross-browser. All browser-specific features
  19.  * should be wrapped with a function call and factored out in the "browser_dependent.js"
  20.  * file. Please, keep it this way.
  21.  *
  22.  */
  23.  
  24. // ----------------------- Schema-related Functions -------------------------
  25.  
  26. Alternatives = {
  27.     blockquote: [ "none", "quote", "code", "note", "fixme" ],
  28.     img: [ "inline", "floating-left", "floating-right" ],
  29.     p: [ "normal", "figure" ]
  30. };
  31.  
  32. function serializeChildren(node) {
  33.     var str = "";
  34.     var children = node.childNodes;
  35.     for (var i = 0; i < children.length; i++) {
  36.         str += serialize(children.item(i));
  37.     }
  38.     return str;
  39. }
  40.  
  41. function serialize(node) {
  42.     var str = "";
  43.     var name = node.nodeName.toLowerCase();
  44.  
  45.     str += whitespaceBefore(node, getPreviousMeaningfulNode(node));
  46.  
  47.     if (node.nodeType == NodeType.TEXT_NODE) {
  48.         if (preserveWhitespace(node.parentNode) || isMeaningfulWhitespace(node)) {
  49.             str += escape(trimNewlines(node.nodeValue));
  50.         } else {
  51.             str += structurize(escape(trimWhitespace(node.nodeValue)));
  52.         }
  53.     } else if (!isTemplate(node)) {
  54.         str += "<" + name + escape(getAttributesFiltered(node));
  55.         var children = node.childNodes;
  56.         if ((children != null) && (children.length > 0)) {
  57.             str += ">";
  58.             for (var i = 0; i < children.length; i++) {
  59.                 str += serialize(children.item(i));
  60.             }
  61.             str += "</" + name + ">";
  62.         } else {
  63.             str += "/>";
  64.         }
  65.     }
  66.  
  67.     str += whitespaceAfter(node, getNextMeaningfulNode(node));
  68.  
  69.     return str;
  70. }
  71.  
  72. function isBlock(node) {
  73.     if (node && (node.nodeType == NodeType.ELEMENT_NODE)) {
  74.         switch (node.nodeName.toLowerCase()) {
  75.             case "p":
  76.             case "blockquote":
  77.             case "h1":
  78.             case "h2":
  79.             case "h3":
  80.             case "h4":
  81.             case "ol":
  82.             case "ul":
  83.             case "body":
  84.                 return true;
  85.         }
  86.     }
  87.     return false;
  88. }
  89.  
  90. function getBlockFormat(node) {
  91.     if (isBlock(node)) {
  92.         switch (node.nodeName.toLowerCase()) {
  93.             case "p": return 0;
  94.             case "h1": return 1;
  95.             case "h2": return 2;
  96.             case "h3": return 3;
  97.             case "h4": return 4;
  98.         }
  99.     }
  100.     return -1;
  101. }
  102.  
  103. function getParentBlock(node) {
  104.     var parent = node.parentNode;
  105.     if (isBlock(parent)) {
  106.         return parent;
  107.     } else {
  108.         return getParentBlock(parent);
  109.     }
  110. }
  111.  
  112. function isInline(node) {
  113.     if (node && (node.nodeType == NodeType.ELEMENT_NODE)) {
  114.         switch (node.nodeName.toLowerCase()) {
  115.             case "b":
  116.             case "i":
  117.             case "q":
  118.             case "strike":
  119.             case "img":
  120.             case "a":
  121.                 return true;
  122.         }
  123.     }
  124.     return false;
  125. }
  126.  
  127. function preserveWhitespace(node) {
  128.     if (node && (node.nodeType == NodeType.ELEMENT_NODE)) {
  129.         switch (node.nodeName.toLowerCase()) {
  130.             case "blockquote":
  131.                 return (getClass(node) == "code");
  132.         }
  133.     }
  134.     return false;
  135. }
  136.  
  137. function isMeaningfulWhitespace(node) {
  138.     if (node && (node.nodeType == NodeType.TEXT_NODE)) {
  139.         return (isInline(node.nextSibling) || isInline(node.previousSibling));
  140.     } else {
  141.         return false;
  142.     }
  143. }
  144.  
  145. function isTemplate(node) {
  146.     if (node && (node.nodeType == NodeType.ELEMENT_NODE)) {
  147.         if (node.getAttribute("template") == "yes") {
  148.             return true;
  149.         } else {
  150.             var name = node.nodeName.toLowerCase();
  151.             if (name == "br") {
  152.                 if (!node.nextSibling) {
  153.                     return true;
  154.                 } else {
  155.                     return isTemplate(node.nextSibling);
  156.                 }
  157.             }
  158.         }
  159.     }
  160.     return false;
  161. }
  162.  
  163. function setTemplate(node, status) {
  164.     var value = (status) ? "yes" : "no";
  165.     node.setAttribute("template",value,false);
  166. }
  167.  
  168. function getAlternatives(node) {
  169.     if (node) {
  170.         var name = node.nodeName.toLowerCase();
  171.         if (Alternatives[name]) {
  172.             return Alternatives[name];
  173.         } else {
  174.             return null;
  175.         }
  176.     }
  177. }
  178.  
  179. function getAttributesFiltered(node) {
  180.     var attr = node.attributes;
  181.     if (node && attr) {
  182.         var str = "";
  183.         for (var i = 0; i < attr.length; i++) {
  184.             str += getAttributeFiltered(node, attr.item(i));
  185.         }
  186.         return str += getHiddenAttributes(node);
  187.     } else {
  188.         return "";
  189.     }
  190. }
  191.  
  192. function getAttributeFiltered(node, at) {
  193.     var nodeName = node.nodeName.toLowerCase();
  194.     var atName = (at.name) ? at.name.toLowerCase() : "";
  195.  
  196.     if (((atName == "class") && (at.value != "")) || (atName == "href")) {
  197.         return " " + atName + '="' + at.value + '"';
  198.     } else {
  199.         return "";
  200.     }
  201. }
  202.  
  203. function getHiddenAttributes(node) {
  204.     var nodeName = node.nodeName.toLowerCase();
  205.     if (nodeName == "img") {
  206.         var href = node.getAttribute("ihref");
  207.         var src = href.substring(href.lastIndexOf('/') + 1);
  208.         return ' src="' + src + '" width="' + node.width + '" height="' + node.height + '"';
  209.     } else {
  210.         return '';
  211.     }
  212.     
  213. function whitespaceBefore(currentNode, precedingNode) {
  214.     var current = currentNode.nodeName.toLowerCase();
  215.     var preceding = (precedingNode) ? precedingNode.nodeName.toLowerCase() : "#none";
  216.  
  217.     if (current == "#text") {
  218.         if (!isWhitespace(currentNode.nodeValue) && isBlock(precedingNode)) {
  219.             return "\n";
  220.         }
  221.     } else if (isBlock(currentNode)) {
  222.         if (isBlock(precedingNode) || (preceding == "li") || (preceding == "br")) {
  223.             return "\n";
  224.         }
  225.     }
  226.  
  227.     return "";
  228. }
  229.  
  230. function whitespaceAfter(currentNode, followingNode) {
  231.     var current = currentNode.nodeName.toLowerCase();
  232.     var following = (followingNode) ? followingNode.nodeName.toLowerCase() : "#none";
  233.  
  234.     if (isBlock(currentNode)) {
  235.         if (isBlock(followingNode) || (following == "#text") || (following == "#none")) {
  236.             return "\n";
  237.         }
  238.     } else if (current == "br") {
  239.         if (following != "#none") {
  240.             return "\n";
  241.         }
  242.     } else if (current == "li") {
  243.         return "\n";
  244.     }
  245.  
  246.     return "";
  247. }
  248.  
  249. function structurize(str) {
  250.     str = str.replace(/\*((\w|\s)+)\*/g, "<b>$1</b>");
  251.     str = str.replace(/_((\w|\s)+)_/g,   "<i>$1</i>");
  252.     str = str.replace(/\/((\w|\s)+)\//g, "<i>$1</i>");
  253.     return str;
  254. }
  255.  
  256. function escape(str) {
  257.     str = str.replace(/&/g, "&");
  258.     str = str.replace(/</g, "<");
  259.     str = str.replace(/>/g, ">");
  260.     str = str.replace(/\u00A0/g, " ");
  261.     return str;
  262. }
  263.  
  264. function resume(str) {
  265.     str = str.replace(/&/g, "&");
  266.     str = str.replace(/</g, "<");
  267.     str = str.replace(/>/g, ">");
  268.     //str = str.replace(/ /g, \u00A0);
  269.     return str;
  270. }
  271.  
  272. // ------------------------------ end of file --------------------------
  273.