?? xml.js
字號:
?/*
By Hangring
#2007.11.02#
---
html到xhtml轉換
*/
var cXml = {XML:null, MainNode:null};
// 創建xmldom對象
cXml.CreateXmlObject = function() {
var xmlDom = null;
if (Browser.IsIE) {
var Doms = ['MSXML2.DOMDocument','Microsoft.XmlDom'];
for (var i = 0; i < Doms.length; i++) {
try {
xmlDom = new ActiveXObject(Doms[i]);
}
catch (e) {}
}
}
else {
xmlDom = document.implementation.createDocument('', '', null);
}
return xmlDom;
};
// 轉換為xhtml標準
cXml.HtmlToXHtml = function (html) {
var xmlDom = this.XML = this.CreateXmlObject();
this.MainNode = xmlDom.appendChild(xmlDom.createElement('xhtml'));
//var t1 = new Date().getTime();
this.AppendChild(this.MainNode, html);
//var t2 = new Date().getTime();
};
function getTime (t2, t1) {
var win = window.open('', 'test');
win.document.open();
win.document.write(t2 - t1);
win.document.close();
}
// 獲取xml串
cXml.GetXmlString = function () {
var xmlStr;
if (Browser.IsIE) {
xmlStr = this.MainNode.firstChild.xml;
}
else {
var oSerializer = new XMLSerializer();
xmlStr = oSerializer.serializeToString(this.MainNode.firstChild);
}
/// [safari error]
// 替換空格
xmlStr = xmlStr.replace(/\xA0/g, ' ');
// 替換rowspan="1"與colspan="1"
xmlStr = xmlStr.replace(/(<td[^>]*?)\s*rowspan="?1"?([^>]*?>)/gi, '$1$2');
xmlStr = xmlStr.replace(/(<td[^>]*?)\s*colspan="?1"?([^>]*?>)/gi, '$1$2');
//xmlStr = xmlStr.replace(/<([^>]+)> <\/\1>/g, '<$1></$1>');
xmlStr = xmlStr.replace(/^<body\s*[^>]*?>/g, '').replace(/<\/body>$/g, '');
if (xmlStr == '<br/>' || /^<p>( )?<\/p>$/i.test(xmlStr)) xmlStr = '';
return xmlStr;
};
// 添加一個子節點
cXml.AppendChild = function (xmlNode, htmlNode) {
if (! htmlNode) return;
switch (htmlNode.nodeType) {
// Node
case 1:
var oNodeName = htmlNode.tagName.toLowerCase();
// ie bug
if (/\//.test(oNodeName) || /^\s*$/.test(oNodeName)) return;
// <--- 效率低下
/*
// 排除不允許標簽,清除空內容標簽
var isNotAllowTag = Config.NotAllowTag().test(oNodeName);
if (isNotAllowTag) {
this.AppendChildNodes(xmlNode, htmlNode);
return;
}
*/
var isSpace = false;
if (!Config.AllowSingleTag.test(oNodeName) && htmlNode.innerHTML.Trim() == '') {
if (Config.NotInnerHTMLTag.test(oNodeName)) return;
isSpace = true;
htmlNode.innerHTML = oNodeName == 'body' ? '' : ' ';
}
// --->
// 添加屬性
var oNode = this.CreateNode(oNodeName);
this.AppendAttributes(oNode, htmlNode);
// 添加子節點
oNode = this.AppendChildNodes(oNode, htmlNode);
if (isSpace) htmlNode.innerHTML = '';
// 當前節點的xml dom樹
xmlNode.appendChild(oNode);
break;
// Text
case 3:
var oValue = htmlNode.nodeValue.Trim();
if (oValue) {
oValue = oValue.replace(/[ \t\n\r\xA0]/g, '\xA0');
xmlNode.appendChild(this.XML.createTextNode(oValue));
//xmlNode.appendChild(this.XML.createCDATASection(oValue));
}
break;
// Comment
//case 8:
// break;
}
};
// 添加所有子節點
cXml.AppendChildNodes = function (xmlNode, htmlNode) {
var oNode = htmlNode.firstChild;
while (oNode) {
this.AppendChild(xmlNode, oNode);
oNode = oNode.nextSibling;
}
return xmlNode;
};
// 添加屬性
cXml.AppendAttributes = function (xmlNode, htmlNode) {
var attributes = htmlNode.attributes;
var isImg = xmlNode.nodeName.toLowerCase() == 'img';
if (htmlNode.className) {
//alert(htmlNode.width + ' ' + htmlNode.height);
var oAttr = this.XML.createAttribute('class');
oAttr.value = htmlNode.className;
}
for (var i = 0, len = attributes.length; i < len; i++) {
var oAttribute = attributes[i];
var oAttrName = oAttribute.nodeName.toLowerCase();
if (!/^[a-z_]+[a-z0-9_]*$/i.test(oAttrName)) continue;
/*
if (isImg && Browser.IsIE && (oAttrName == 'width' || oAttrName == 'height')) {
continue;
}
*/
// 獲取屬性值
var oAttrValue = '';
// XHTML不支持類似"checked"最小化屬性,它必須是checked="checked"
if (oAttribute.nodeValue === true)
oAttrValue = oAttrName;
// 因為IE存在bug,style的nodeValue返回"null",所以需要設置style
else if (Browser.IsIE && oAttrName == 'style') {
var s = htmlNode.style.cssText.toLowerCase();
/*
if (htmlNode.getAttribute('width', 2) && s.indexOf('width') < 0) {
if (s != '') s += ';';
s += 'width: ' + htmlNode.getAttribute('width', 2) + 'px;'
+ 'height: ' + htmlNode.getAttribute('height', 2) + 'px;'
}
*/
oAttrValue = s;
}
// 獲取屬性值
else
// || oAttribute.nodeValue || '';
oAttrValue = htmlNode.getAttribute(oAttrName, 2);
// 創建屬性
var oAttr = this.XML.createAttribute(oAttrName);
if (oAttrName == 'hspace') continue;
if (oAttrName == 'alt' ||
oAttrValue && !Config.NotAllowAttrValue.test(oAttrValue)) {
///
oAttr.value = oAttrValue || '';
else continue;
// 排除非允許屬性
if (Config.NotAllowAttr.test(oAttrName)) continue;
xmlNode.attributes.setNamedItem(oAttr);
}
};
// 創建節點
cXml.CreateNode = function (nodeName) {
switch (nodeName) {
case 'strong':
nodeName = 'b';
break;
case 'em':
nodeName = 'i';
break;
}
return this.XML.createElement(nodeName);
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -