My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package net.tw.util.air {
import flash.html.HTMLLoader;
import com.adobe.htmlscout.DOMNode;
import mx.utils.StringUtil;
//
public class HTMLFixer {
public const ignoredTags:Vector.<String>=new Vector.<String>();
public const autoClosedTags:Vector.<String>=new Vector.<String>();
//
protected var _htmlLoader:HTMLLoader;
//
public function HTMLFixer(l:HTMLLoader=null) {
if (l) htmlLoader=l
else _htmlLoader=new HTMLLoader();
//
ignoredTags.push('script', 'style');
autoClosedTags.push('meta', 'img', 'br', 'hr', 'input', 'link', 'embed', 'param', 'wbr', 'base');
}
public function set htmlLoader(l:HTMLLoader):void {
_htmlLoader=l;
}
public function get htmlLoader():HTMLLoader {
return _htmlLoader;
}
public function getXML():XML {
/*var x:XML=new XML(<html />);
for (var i:int=0; i<namespaces.length; i++) {
x.addNamespace(namespaces[i]);
}
x.appendChild(new XMLList(getFixedHTML(false)));
return x;*/
return new XML(getFixedHTML());
}
public function getHTML(wrapWithHTMLTag:Boolean=true):String {
var s:String='';
if (wrapWithHTMLTag) s+='<html>';
s+=htmlLoader.window.document.documentElement.innerHTML;
if (wrapWithHTMLTag) s+='</html>';
return s;
}
public function getFixedHTML(wrapWithHTMLTag:Boolean=true):String {
return fixHTML(getHTML(wrapWithHTMLTag));
}
protected function fixHTML(s:String):String {
var i:int;
for (i=0; i<ignoredTags.length; i++) {
s=removeTag(s, ignoredTags[i]);
}
for (i=0; i<autoClosedTags.length; i++) {
s=autoCloseTag(s, autoClosedTags[i]);
}
s=s.replace(/&nbsp;/g, ' ');
s=s.replace(/&mdash;/g, '-');
s=s.replace(/&copy;/g, '(c)');
s=s.replace(/—/g, '-');
s=s.replace(/©/g, '(c)');
// NAMESPACES!
s=s.replace(/<(\/)?(\w+):(.*?)/gs, '&lt;$1$2:$3');
//
return s;
}
protected function removeTag(s:String, tag:String):String {
s=s.replace(new RegExp('<'+tag+'(.*?)<\\/'+tag+'>', 'gs'), '');
s=s.replace(new RegExp('<'+tag+'(.*?)>', 'gs'), '');
return s;
}
protected function autoCloseTag(s:String, tag:String):String {
s=s.replace(new RegExp('<'+tag+' (.*?)>', 'gs'), '<'+tag+' $1 />');
s=s.replace(new RegExp('<'+tag+'>', 'gs'), '<'+tag+'/>');
return s;
}
}
}

Change log

r60 by quentin.t on Apr 9, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r59 by quentin.t on Apr 8, 2010   Diff
New HTMLFixer.as class!
All revisions of this file

File info

Size: 2350 bytes, 73 lines
Powered by Google Project Hosting