📄 xmlp.js
字号:
/*
* xmlp.js, version 1.0
* An XML parser in JavaScript
*
* Revision history:
* 1.0, 23 Nov 2001 : Initial version
*
* Copyright (C) 2001 David A. Lindquist (dave@gazingus.org)
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/// Node ///////////////////////////////////////////////////////////////////////
Node.ELEMENT_NODE = 1;
Node.ATTRIBUTE_NODE = 2;
Node.TEXT_NODE = 3;
Node.CDATA_SECTION_NODE = 4;
Node.ENTITY_REFERENCE_NODE = 5;
Node.ENTITY_NODE = 6; // not used
Node.PROCESSING_INSTRUCTION_NODE = 7;
Node.COMMENT_NODE = 8;
Node.DOCUMENT_NODE = 9;
Node.DOCUMENT_TYPE_NODE = 10;
Node.DOCUMENT_FRAGMENT_NODE = 11;
Node.NOTATION_NODE = 12; // not used
function Node() {
this.attributes = null;
this.childNodes = new NodeList();
this.firstChild = null
this.lastChild = null;
this.nextSibling = null;
this.nodeName = null;
this.nodeType = null;
this.nodeValue = null;
this.ownerDocument = null;
this.parentNode = null;
this.previousSibling = null;
}
Node.prototype.getAttributes = function() { return this.attributes; }
Node.prototype.getChildNodes = function() { return this.childNodes; }
Node.prototype.getFirstChild = function() { return this.firstChild; }
Node.prototype.getLastChild = function() { return this.lastChild; }
Node.prototype.getNextSibling = function() { return this.nextSibling; }
Node.prototype.getNodeName = function() { return this.nodeName; }
Node.prototype.getNodeType = function() { return this.nodeType; }
Node.prototype.getNodeValue = function() { return this.nodeValue; }
Node.prototype.getOwnerDocument = function() { return this.ownerDocument; }
Node.prototype.getParentNode = function() { return this.parentNode; }
Node.prototype.getPreviousSibling = function() { return this.previousSibling; }
Node.prototype.setNodeValue =
function() {
// Default behavior is to do nothing;
// overridden in some subclasses
}
Node.prototype.appendChild =
function( childNode ) {
if ( this.nodeType == Node.ELEMENT_NODE ||
this.nodeType == Node.ATTRIBUTE_NODE ||
this.nodeType == Node.DOCUMENT_NODE ||
this.nodeType == Node.DOCUMENT_FRAGMENT_NODE ) {
this.childNodes.add( childNode );
}
else {
// :REVISIT: change to DOMException
throw new ParseError( "Cannot append child node" );
}
if ( this.ownerDocument != childNode.ownerDocument ) {
// :REVISIT: change to DOMException
throw new ParseError( "Cannot append child to this document" );
}
if ( this.childNodes.length == 1 ) {
this.firstChild = childNode;
}
this.lastChild = childNode;
childNode.parentNode = this;
var prevSibling = this.childNodes.item( -2 );
childNode.previousSibling = prevSibling;
if ( prevSibling != null ) {
prevSibling.nextSibling = childNode;
}
}
Node.prototype.cloneNode =
function( deep ) {
// :REVISIT: change to DOMException
throw new ParseError( "Not implemented" );
}
Node.prototype.hasChildNodes =
function() {
return ( this.childNodes.length > 0 );
}
Node.prototype.insertBefore =
function( newChild, refChild ) {
var currentChildren = this.childNodes;
this.childNodes = new NodeList();
for ( var i = 0; i < currentChildren.length; ) {
var child = currentChildren.item(i);
if ( child == refChild && refChild != null ) {
this.appendChild( newChild );
refChild = null;
}
else {
this.appendChild( child );
i++;
}
}
}
Node.prototype.removeChild =
function( oldChild ) {
var currentChildren = this.childNodes;
this.childNodes = new NodeList();
for ( var i = 0; i < currentChildren.length; i++ ) {
var child = currentChildren.item(i)
if ( child != oldChild ) {
this.appendChild( child );
}
}
}
Node.prototype.replaceChild =
function( newChild, oldChild ) {
var oldChildren = this.childNodes;
this.childNodes = new NodeList();
for ( var i = 0; i < oldChildren.length; i++ ) {
if ( oldChildren.item(i) == oldChild ) {
this.appendChild( newChild );
}
else {
this.appendChild( oldChild );
}
}
}
/// Element ////////////////////////////////////////////////////////////////////
Element.prototype = new Node();
Element.prototype.constructor = Element;
// Element : Node
function Element( ownerDoc, name ) {
this.tagName = name;
// inherited from Node
this.attributes = new NamedNodeMap();
this.childNodes = new NodeList();
this.nodeType = Node.ELEMENT_NODE;
this.nodeName = name;
this.ownerDocument = ownerDoc;
}
Element.prototype.getAttribute =
function( name ) {
var attr = this.attributes.getNamedItem( name );
return ( attr == null ) ? "" : attr.getValue();
}
Element.prototype.getAttributeNode =
function( name ) {
return this.attributes.getNamedItem( name );
}
Element.prototype.getElementsByTagName =
function( tagName ) {
return new DeepNodeList( this, tagName );
}
Element.prototype.getTagName =
function() {
return this.tagName;
}
Element.prototype.normalize =
function() {
var child, next;
for ( child = this.getFirstChild(); child != null; child = next ) {
next = child.getNextSibling();
if ( child.getNodeType() == Node.TEXT_NODE ) {
if ( next != null && next.getNodeType() == Node.TEXT_NODE ) {
child.appendData( next.getNodeValue() );
this.removeChild( next );
next = child;
}
else {
if ( child.getNodeValue().length == 0 ) {
this.removeChild( child );
}
}
}
else if ( child.getNodeType() == Node.ELEMENT_NODE ) {
child.normalize();
}
}
}
Element.prototype.removeAttribute =
function( name ) {
this.attributes.removeNamedItem( name );
}
Element.prototype.removeAttributeNode =
function( attr ) {
return this.attributes.removeNamedItem( attr.nodeName );
}
Element.prototype.setAttribute =
function( name, value ) {
var attr = this.ownerDocument.createAttribute( name );
arrt.setValue( value );
this.attributes.setNamedItem( attr );
}
Element.prototype.setAttributeNode =
function( attr ) {
return this.attributes.setNamedItem( attr );
}
/// Attr ///////////////////////////////////////////////////////////////////////
Attr.prototype = new Node();
Attr.prototype.constructor = Attr;
// Attr : Node
function Attr( ownerDoc, name ) {
this.name = name;
this.specified = true;
this.value = null;
// inherited from Node
this.childNodes = new NodeList();
this.nodeName = name;
this.nodeType = Node.ATTRIBUTE_NODE;
this.nodeValue = null;
this.ownerDocument = ownerDoc;
}
Attr.prototype.getName =
function() {
return this.name;
}
Attr.prototype.getNodeValue =
function() {
return this.getValue();
}
Attr.prototype.getSpecified =
function() {
return this.specified;
}
Attr.prototype.getValue =
function() {
// :REVISIT:
var value = "";
for ( var i = 0; i < this.childNodes.length; i++ ) {
value += this.childNodes.item(i).getNodeValue();
}
return value;
}
Attr.prototype.setValue =
function( value ) {
// :REVISIT:
this.childNodes = new NodeList();
this.firstChild = null;
this.lastChild = null;
if ( value != null ) {
this.appendChild( this.ownerDocument.createTextNode( value ) );
}
}
/// CharacterData //////////////////////////////////////////////////////////////
CharacterData.prototype = new Node();
CharacterData.prototype.constructor = CharacterData;
// CharacterData : Node
function CharacterData( data ) {
this.data = data;
// inherited from Node
this.nodeValue = data;
}
CharacterData.prototype.appendData =
function( data ) {
this.setData( this.getData() + data );
}
CharacterData.prototype.deleteData =
function( offset, count ) {
var begin = this.getData().substring( 0, offset );
var end = this.getData().substring( offset + count );
this.setData( begin + end );
}
CharacterData.prototype.getData =
function() {
return this.data;
}
CharacterData.prototype.getLength =
function() {
return ( this.data ) ? this.data.length : 0;
}
CharacterData.prototype.insetData =
function( offset, data ) {
var begin = this.getData().substring( 0, offset );
var end = this.getData().substring( offset, this.getLength );
this.setData( begin + data + end );
}
CharacterData.prototype.replaceData =
function( offset, count, data ) {
this.deleteData( offset, count );
this.insertData( offset, data );
}
CharacterData.prototype.setData =
function( data ) {
this.setNodeValue( data );
}
CharacterData.prototype.setNodeValue =
function( value ) {
this.data = value;
this.nodeValue = value;
}
CharacterData.prototype.substringData =
function( offset, count ) {
return this.getData().substring( offset, offset + count );
}
/// Text ///////////////////////////////////////////////////////////////////////
Text.prototype = new CharacterData();
Text.prototype.constructor = Text;
// Text : CharacterData
function Text( ownerDoc, data ) {
// inherited from CharacterData
this.data = data;
// inherited from CharacterData : Node
this.nodeName = "#text";
this.nodeType = Node.TEXT_NODE;
this.nodeValue = data;
this.ownerDocument = ownerDoc;
}
Text.prototype.splitText =
function( offset ) {
// :REVISIT:
// check for index out of bounds condition
var newText =
this.getOwnerDocument().createTextNode( this.data.substring( offset ) );
var parentNode = this.getParentNode();
if ( parentNode != null ) {
parentNode.insetBefore( newText, this.nextSibling );
}
return newText;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -