source: trunk/modules/GetHtml/DOMwalk.js @ 999

Last change on this file since 999 was 999, checked in by ray, 11 years ago

#1195 Allow to specify an external url to load a plugin from

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 11.3 KB
Line 
1
2  /*--------------------------------------:noTabs=true:tabSize=2:indentSize=2:--
3    --  Xinha (is not htmlArea) - http://xinha.gogo.co.nz/
4    --
5    --  Use of Xinha is granted by the terms of the htmlArea License (based on
6    --  BSD license)  please read license.txt in this package for details.
7    --
8    --  Xinha was originally based on work by Mihai Bazon which is:
9    --      Copyright (c) 2003-2004 dynarch.com.
10    --      Copyright (c) 2002-2003 interactivetools.com, inc.
11    --      This copyright notice MUST stay intact for use.
12    --
13    --  This is the standard implementation of the method for rendering HTML code from the DOM
14    --
15    --  The file is loaded by the Xinha Core when no alternative method (plugin) is loaded.
16    --
17    --
18    --  $HeadURL:http://svn.xinha.webfactional.com/trunk/modules/GetHtml/DOMwalk.js $
19    --  $LastChangedDate:2008-02-02 19:30:42 +0100 (Sa, 02 Feb 2008) $
20    --  $LastChangedRevision:961 $
21    --  $LastChangedBy:ray $
22    --------------------------------------------------------------------------*/
23function GetHtmlImplementation(editor) {
24    this.editor = editor;
25}
26
27GetHtmlImplementation._pluginInfo = {
28  name          : "GetHtmlImplementation DOMwalk",
29  origin        : "Xinha Core",
30  version       : "$LastChangedRevision:961 $".replace(/^[^:]*: (.*) \$$/, '$1'),
31  developer     : "The Xinha Core Developer Team",
32  developer_url : "$HeadURL:http://svn.xinha.webfactional.com/trunk/modules/GetHtml/DOMwalk.js $".replace(/^[^:]*: (.*) \$$/, '$1'),
33  sponsor       : "",
34  sponsor_url   : "",
35  license       : "htmlArea"
36};
37
38// Retrieves the HTML code from the given node.  This is a replacement for
39// getting innerHTML, using standard DOM calls.
40// Wrapper legacy see #442
41Xinha.getHTML = function(root, outputRoot, editor)
42{
43  return Xinha.getHTMLWrapper(root,outputRoot,editor);
44};
45
46Xinha.emptyAttributes = " checked disabled ismap readonly nowrap compact declare selected defer multiple noresize noshade "
47
48Xinha.getHTMLWrapper = function(root, outputRoot, editor, indent)
49{
50  var html = "";
51  if ( !indent )
52  {
53    indent = '';
54  }
55
56  switch ( root.nodeType )
57  {
58    case 10:// Node.DOCUMENT_TYPE_NODE
59    case 6: // Node.ENTITY_NODE
60    case 12:// Node.NOTATION_NODE
61      // this all are for the document type, probably not necessary
62    break;
63
64    case 2: // Node.ATTRIBUTE_NODE
65      // Never get here, this has to be handled in the ELEMENT case because
66      // of IE crapness requring that some attributes are grabbed directly from
67      // the attribute (nodeValue doesn't return correct values), see
68      //http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=3porgu4mc4ofcoa1uqkf7u8kvv064kjjb4%404ax.com
69      // for information
70    break;
71
72    case 4: // Node.CDATA_SECTION_NODE
73      // Mozilla seems to convert CDATA into a comment when going into wysiwyg mode,
74      //  don't know about IE
75      html += (Xinha.is_ie ? ('\n' + indent) : '') + '<![CDATA[' + root.data + ']]>' ;
76    break;
77
78    case 5: // Node.ENTITY_REFERENCE_NODE
79      html += '&' + root.nodeValue + ';';
80    break;
81
82    case 7: // Node.PROCESSING_INSTRUCTION_NODE
83      // PI's don't seem to survive going into the wysiwyg mode, (at least in moz)
84      // so this is purely academic
85      html += (Xinha.is_ie ? ('\n' + indent) : '') + '<'+'?' + root.target + ' ' + root.data + ' ?>';
86    break;
87
88    case 1: // Node.ELEMENT_NODE
89    case 11: // Node.DOCUMENT_FRAGMENT_NODE
90    case 9: // Node.DOCUMENT_NODE
91      var closed;
92      var i;
93      var root_tag = (root.nodeType == 1) ? root.tagName.toLowerCase() : '';
94      if ( ( root_tag == "script" || root_tag == "noscript" ) && editor.config.stripScripts )
95      {
96        break;
97      }
98      if ( outputRoot )
99      {
100        outputRoot = !(editor.config.htmlRemoveTags && editor.config.htmlRemoveTags.test(root_tag));
101      }
102      if ( Xinha.is_ie && root_tag == "head" )
103      {
104        if ( outputRoot )
105        {
106          html += (Xinha.is_ie ? ('\n' + indent) : '') + "<head>";
107        }
108       
109        var save_multiline = RegExp.multiline;
110        RegExp.multiline = true;
111        var txt =
112        root.innerHTML.replace(Xinha.RE_tagName, function(str, p1, p2) { return p1 + p2.toLowerCase(); }) // lowercasize
113        .replace(/\s*=\s*(([^'"][^>\s]*)([>\s])|"([^"]+)"|'([^']+)')/g, '="$2$4$5"$3') //add attribute quotes
114        .replace(/<(link|meta)((\s*\S*="[^"]*")*)>/g, '<$1$2 />'); //terminate singlet tags
115        RegExp.multiline = save_multiline;
116        html += txt + '\n';
117        if ( outputRoot )
118        {
119          html += (Xinha.is_ie ? ('\n' + indent) : '') + "</head>";
120        }
121        break;
122      }
123      else if ( outputRoot )
124      {
125        closed = (!(root.hasChildNodes() || Xinha.needsClosingTag(root)));
126        html += ((Xinha.isBlockElement(root)) ? ('\n' + indent) : '') + "<" + root.tagName.toLowerCase();
127        var attrs = root.attributes;
128       
129        for ( i = 0; i < attrs.length; ++i )
130        {
131          var a = attrs.item(i);
132          if (typeof a.nodeValue == 'object' ) continue; // see #684
133          if (root.tagName.toLowerCase() == "input"
134              && root.type.toLowerCase() == "checkbox"
135              && a.nodeName.toLowerCase() == "value"
136              && a.nodeValue.toLowerCase() == "on")
137          {
138            continue;
139          }
140          if ( !a.specified
141            // IE claims these are !a.specified even though they are.  Perhaps others too?
142            && !(root.tagName.toLowerCase().match(/input|option/) && a.nodeName == 'value')
143            && !(root.tagName.toLowerCase().match(/area/) && a.nodeName.match(/shape|coords/i))
144          )
145          {
146            continue;
147          }
148          var name = a.nodeName.toLowerCase();
149          if ( /_moz_editor_bogus_node/.test(name) || ( name == 'class' && a.nodeValue == 'webkit-block-placeholder') )
150          {
151            html = "";
152            break;
153          }
154          if ( /(_moz)|(contenteditable)|(_msh)/.test(name) )
155          {
156            // avoid certain attributes
157            continue;
158          }
159          var value;
160          if ( Xinha.emptyAttributes.indexOf(" "+name+" ") != -1)
161          {
162            value = name;
163          }
164          else if ( name != "style" )
165          {
166            // IE5.5 reports 25 when cellSpacing is
167            // 1; other values might be doomed too.
168            // For this reason we extract the
169            // values directly from the root node.
170            // I'm starting to HATE JavaScript
171            // development.  Browser differences
172            // suck.
173            //
174            // Using Gecko the values of href and src are converted to absolute links
175            // unless we get them using nodeValue()
176            if ( typeof root[a.nodeName] != "undefined" && name != "href" && name != "src" && !(/^on/.test(name)) )
177            {
178              value = root[a.nodeName];
179            }
180            else
181            {
182              value = a.nodeValue;
183                          if (name == 'class')
184                          {
185                                value = value.replace(/Apple-style-span/,'');
186                                if (!value) continue;
187                          }
188              // IE seems not willing to return the original values - it converts to absolute
189              // links using a.nodeValue, a.value, a.stringValue, root.getAttribute("href")
190              // So we have to strip the baseurl manually :-/
191              if ( Xinha.is_ie && (name == "href" || name == "src") )
192              {
193                value = editor.stripBaseURL(value);
194              }
195
196              // High-ascii (8bit) characters in links seem to cause problems for some sites,
197              // while this seems to be consistent with RFC 3986 Section 2.4
198              // because these are not "reserved" characters, it does seem to
199              // cause links to international resources not to work.  See ticket:167
200
201              // IE always returns high-ascii characters un-encoded in links even if they
202              // were supplied as % codes (it unescapes them when we pul the value from the link).
203
204              // Hmmm, very strange if we use encodeURI here, or encodeURIComponent in place
205              // of escape below, then the encoding is wrong.  I mean, completely.
206              // Nothing like it should be at all.  Using escape seems to work though.
207              // It's in both browsers too, so either I'm doing something wrong, or
208              // something else is going on?
209
210              if ( editor.config.only7BitPrintablesInURLs && ( name == "href" || name == "src" ) )
211              {
212                value = value.replace(/([^!-~]+)/g, function(match) { return escape(match); });
213              }
214            }
215          }
216          else if ( !Xinha.is_ie )
217          {
218            value = root.style.cssText.replace(/rgb\(.*?\)/ig,function(rgb){ return Xinha._colorToRgb(rgb) });
219          }
220          if ( /^(_moz)?$/.test(value) )
221          {
222            // Mozilla reports some special tags
223            // here; we don't need them.
224            continue;
225          }
226          html += " " + name + '="' + Xinha.htmlEncode(value) + '"';
227        }
228        //IE fails to put style in attributes list & cssText is UPPERCASE
229        if ( Xinha.is_ie && root.style.cssText )
230        {
231          html += ' style="' + root.style.cssText.toLowerCase() + '"';
232        }
233        if ( Xinha.is_ie && root.tagName.toLowerCase() == "option" && root.selected )
234        {
235          html += ' selected="selected"';
236        }
237        if ( html !== "" )
238        {
239          if ( closed && root_tag=="p" )
240          {
241            //never use <p /> as empty paragraphs won't be visible
242            html += ">&nbsp;</p>";
243          }
244          else if ( closed )
245          {
246            html += " />";
247          }
248          else
249          {
250            html += ">";
251          }
252        }
253      }
254      var containsBlock = false;
255      if ( root_tag == "script" || root_tag == "noscript" )
256      {
257        if ( !editor.config.stripScripts )
258        {
259          if (Xinha.is_ie)
260          {
261            var innerText = "\n" + root.innerHTML.replace(/^[\n\r]*/,'').replace(/\s+$/,'') + '\n' + indent;
262          }
263          else
264          {
265            var innerText = (root.hasChildNodes()) ? root.firstChild.nodeValue : '';
266          }
267          html += innerText + '</'+root_tag+'>' + ((Xinha.is_ie) ? '\n' : '');
268        }
269      }
270      else if (root_tag == "pre")
271      {
272        html += ((Xinha.is_ie) ? '\n' : '') + root.innerHTML.replace(/<br>/g,'\n') + '</'+root_tag+'>';
273      }
274      else
275      {
276        for ( i = root.firstChild; i; i = i.nextSibling )
277        {
278          if ( !containsBlock && i.nodeType == 1 && Xinha.isBlockElement(i) )
279          {
280            containsBlock = true;
281          }
282          html += Xinha.getHTMLWrapper(i, true, editor, indent + '  ');
283        }
284        if ( outputRoot && !closed )
285        {
286          html += (((Xinha.isBlockElement(root) && containsBlock) || root_tag == 'head' || root_tag == 'html') ? ('\n' + indent) : '') + "</" + root.tagName.toLowerCase() + ">";
287        }
288      }
289    break;
290
291    case 3: // Node.TEXT_NODE
292      if ( /^script|noscript|style$/i.test(root.parentNode.tagName) )
293      {
294        html = root.data;
295      }
296      else if(root.data.trim() == '')
297      {
298        if(root.data)
299        {
300          html = ' ';
301        }
302        else
303        {
304          html = '';
305        }
306      }
307      else
308      {
309        html = Xinha.htmlEncode(root.data);
310      }
311    break;
312
313    case 8: // Node.COMMENT_NODE
314      html = "<!--" + root.data + "-->";
315    break;
316  }
317  return html;
318};
319
320
321
Note: See TracBrowser for help on using the repository browser.