140 lines
3.0 KiB
C#
140 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Rokojori
|
|
{
|
|
public class HtmlElementNode:HtmlNode
|
|
{
|
|
public HtmlElementNode( HtmlDocument document, string nodeName ):base( document, HtmlNode.NodeType.Element )
|
|
{
|
|
_nodeName = nodeName;
|
|
}
|
|
|
|
string _nodeName;
|
|
public string nodeName => _nodeName;
|
|
|
|
List<HtmlNode> _children = new List<HtmlNode>();
|
|
public int numChildren => _children.Count;
|
|
|
|
public HtmlNode GetChildAt( int index )
|
|
{
|
|
return _children[ index ];
|
|
}
|
|
|
|
public bool HasOnlyTextNodes()
|
|
{
|
|
for ( int i = 0; i < _children.Count; i++ )
|
|
{
|
|
if ( _children[ i ].nodeType != NodeType.Text )
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return _children.Count > 0;
|
|
}
|
|
|
|
public void RemoveChild( HtmlNode node )
|
|
{
|
|
var childIndex = _children.IndexOf( node );
|
|
|
|
if ( childIndex == -1 )
|
|
{
|
|
return;
|
|
}
|
|
|
|
node._SetParent( null );
|
|
_children.RemoveAt( childIndex );
|
|
}
|
|
|
|
public void AppendChild( HtmlNode node )
|
|
{
|
|
if ( node.parentNode == this )
|
|
{
|
|
return;
|
|
}
|
|
|
|
if ( node.parentNode != null )
|
|
{
|
|
var element = node.parentNode as HtmlElementNode;
|
|
element.RemoveChild( node );
|
|
}
|
|
|
|
_children.Add( node );
|
|
|
|
node._SetParent( this );
|
|
|
|
}
|
|
|
|
public HtmlTextNode AddText( string text )
|
|
{
|
|
var textNode = document.CreateText( text );
|
|
AppendChild( textNode );
|
|
|
|
return textNode;
|
|
}
|
|
|
|
public HtmlElementNode AddScript( string script )
|
|
{
|
|
return AddElement( HtmlElementNodeName.script, script );
|
|
}
|
|
|
|
public HtmlElementNode AddStyle( string style )
|
|
{
|
|
return AddElement( HtmlElementNodeName.style, style );
|
|
}
|
|
|
|
public HtmlElementNode AddElement( HtmlElementNodeName name, string text = null )
|
|
{
|
|
var en = document.Create( name, text );
|
|
AppendChild( en );
|
|
return en;
|
|
}
|
|
|
|
public void SetAttribute( string name, string value )
|
|
{
|
|
var att = new HtmlAttributeNode( document, this, name, value );
|
|
_attributes.Add( att );
|
|
}
|
|
|
|
|
|
List<HtmlAttributeNode> _attributes = new List<HtmlAttributeNode>();
|
|
|
|
public int numAttributes => _attributes.Count;
|
|
|
|
public HtmlAttributeNode GetAttributeAt( int index )
|
|
{
|
|
return _attributes[ index ];
|
|
}
|
|
|
|
public HtmlElementNode querySelector( string selector )
|
|
{
|
|
var nodeName = HtmlElementNodeName.CreateNodeName( selector );
|
|
|
|
return querySelector( nodeName );
|
|
}
|
|
|
|
public HtmlElementNode querySelector( HtmlElementSelector selector )
|
|
{
|
|
|
|
var element = HtmlWalker.instance.Find( this,
|
|
n =>
|
|
{
|
|
var element = n as HtmlElementNode;
|
|
|
|
if ( element == null )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return selector.Selects( element );
|
|
},
|
|
|
|
false
|
|
);
|
|
|
|
return element == null ? null : ( element as HtmlElementNode );
|
|
}
|
|
}
|
|
} |