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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
//
// This script was created
// by Mircho Mirev
// mo /mo@momche.net/
//
// :: feel free to use it BUT
// :: if you want to use this code PLEASE send me a note
// :: and please keep this disclaimer intact
//
// This in fact is a simple dom iterator
// requires: mobrowser.js
function cDomExtension( hParent, aSelectors, hInitFunction )
{
this.hParent = hParent
this.aSelectors = aSelectors
this.hInitFunction = hInitFunction
}
cDomExtensionManager =
{
aExtensions : new Array()
}
cDomExtensionManager.register = function( hDomExtension )
{
cDomExtensionManager.aExtensions.push( hDomExtension )
}
cDomExtensionManager.initSelector = function( hParent, sSelector, hInitFunction )
{
var hSelectorRegEx
var hAttributeRegEx
var aSelectorData
var aAttributeData
var sAttribute
hSelectorRegEx = /([a-z0-9_]*)\[?([^\]]*)\]?/i
hAttributeRegEx = /([a-z0-9_]*)([\*\^\$]?)(=?)(([a-z0-9_=]*))/i
if( hSelectorRegEx.test( sSelector ) && !/[@#\.]/.test( sSelector ) )
{
aSelectorData = hSelectorRegEx.exec( sSelector )
if( aSelectorData[ 1 ] != '' )
{
hGroup = hParent.getElementsByTagName( aSelectorData[ 1 ].toLowerCase() )
for( nI = 0; nI < hGroup.length; nI ++ )
{
hGroup[ nI ].markExt = true
}
for( nI = 0; nI < hGroup.length; nI ++ )
{
if( !hGroup[ nI ].markExt )
{
continue
}
else
{
hGroup[ nI ].markExt = false
}
if( aSelectorData[ 2 ] == '' )
{
if( hGroup[ nI ].tagName.toLowerCase() == aSelectorData[ 1 ].toLowerCase() )
{
hInitFunction( hGroup[ nI ] )
}
}
else
{
aAttributeData = hAttributeRegEx.exec( aSelectorData[ 2 ] )
if( aAttributeData[ 1 ] == 'class' )
{
sAttribute = hGroup[ nI ].className
}
else
{
sAttribute = hGroup[ nI ].getAttribute( aAttributeData[ 1 ] )
}
if( sAttribute != null && sAttribute.length > 0 )
{
if( aAttributeData[ 3 ] == '=' )
{
if( aAttributeData[ 2 ] == '' )
{
if( sAttribute == aAttributeData[4] )
{
hInitFunction( hGroup[ nI ] )
}
}
else
{
switch( aAttributeData[ 2 ] )
{
case '^' : if( sAttribute.indexOf( aAttributeData[ 4 ] ) == 0 )
{
hInitFunction( hGroup[ nI ] )
}
break
case '$' : if( sAttribute.lastIndexOf( aAttributeData[ 4 ] ) == sAttribute.length - aAttributeData[ 4 ].length )
{
hInitFunction( hGroup[ nI ] )
}
break
case '*' : if( sAttribute.indexOf( aAttributeData[ 4 ] ) >= 0 )
{
hInitFunction( hGroup[ nI ] )
}
break
}
}
}
else
{
hInitFunction( hGroup[ nI ] )
}
}
}
}
//we have the new implementation - css3 style selectors, so return
return
}
}
hSelectorRegEx = /([a-z0-9_]*)([\.#@]?)([a-z0-9_=~]*)/i
hAttributeRegEx = /([a-z0-9_]*)([=~])?([a-z0-9_]*)/i
aSelectorData = hSelectorRegEx.exec( sSelector )
if( aSelectorData[ 1 ] != '' )
{
var hGroup = hParent.getElementsByTagName( aSelectorData[ 1 ] )
for( nI = 0; nI < hGroup.length; nI ++ )
{
hGroup[ nI ].markExt = true
}
for( nI = 0; nI < hGroup.length; nI ++ )
{
if( !hGroup[ nI ].markExt )
{
continue
}
else
{
hGroup[ nI ].markExt = false
}
if( aSelectorData[ 2 ] != '' )
{
switch( aSelectorData[ 2 ] )
{
case '.' : if( hGroup[ nI ].className == aSelectorData[ 3 ] )
{
hInitFunction( hGroup[ nI ] )
}
break
case '#' : if( hGroup[ nI ].id == aSelectorData[ 3 ] )
{
hInitFunction( hGroup[ nI ] )
}
break
case '@' : aAttributeData = hAttributeRegEx.exec( aSelectorData[ 3 ] )
sAttribute = hGroup[ nI ].getAttribute( aAttributeData[ 1 ] )
if( sAttribute != null && sAttribute.length > 0 )
{
if( aAttributeData[ 3 ] != '' )
{
if( aAttributeData[ 2 ] == '=' )
{
if( sAttribute == aAttributeData[ 3 ] )
{
hInitFunction( hGroup[ nI ] )
}
}
else /* the case is like ~ */
{
if( sAttribute.indexOf( aAttributeData[ 3 ] ) >= 0 )
{
hInitFunction( hGroup[ nI ] )
}
}
}
else
{
hInitFunction( hGroup[ nI ] )
}
}
break
}
}
}
}
}
cDomExtensionManager.initialize = function()
{
var hDomExtension = null
var aSelectors
for( var nKey in cDomExtensionManager.aExtensions )
{
aSelectors = cDomExtensionManager.aExtensions[ nKey ].aSelectors
for( var nKey2 in aSelectors )
{
cDomExtensionManager.initSelector( cDomExtensionManager.aExtensions[ nKey ].hParent, aSelectors[ nKey2 ], cDomExtensionManager.aExtensions[ nKey ].hInitFunction )
}
}
}
if( window.addEventListener )
{
window.addEventListener( 'load', cDomExtensionManager.initialize, false )
}
else if( window.attachEvent )
{
window.attachEvent( 'onload', cDomExtensionManager.initialize )
}
|