| 1 | |||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Roland Mosler - Roland@Place.Ug | ||
| 5 | # | ||
| 6 | # Das ist eine uHTML-Bibliothek von Place.Ug | ||
| 7 | # Es ist erlaubt dieses Paket unter der aktuellen GNU LGPL zu nutzen | ||
| 8 | # Bei Weiterentwicklungen ist die Ursprungsbibliothek zu nennen | ||
| 9 | # | ||
| 10 | # Fehler und Verbesserungen bitte an uHTML@Place.Ug | ||
| 11 | # | ||
| 12 | # This is a uHTML library from Place.Ug | ||
| 13 | # It is allowed to use this library under the actual GNU LGPL | ||
| 14 | # The name of this library is to be named in all derivations | ||
| 15 | # | ||
| 16 | # Please report errors to uHTML@Place.Ug | ||
| 17 | # | ||
| 18 | # © Roland Mosler, Place.Ug | ||
| 19 | # | ||
| 20 | |||
| 21 | |||
| 22 | use strict ; | ||
| 23 | |||
| 24 | use version ; our $VERSION = "1.95" ; | ||
| 25 | |||
| 26 | package uHTMLnode; | ||
| 27 | |||
| 28 | |||
| 29 | # | ||
| 30 | # Node -> | ||
| 31 | # | ||
| 32 | # FirstChild: - Erstes Unterelement | ||
| 33 | # LastChild: - Letztes Unterelement | ||
| 34 | # Parent: - Darüberliegendes Element | ||
| 35 | # Name: - Elementname | ||
| 36 | # End: - Eins wenn das Element abgeschlossen ist | ||
| 37 | # Attributes: - Elmentattribute als Hash-Referenz | ||
| 38 | # Text: - Elementtext bis zum ersten Unterelement oder Nachfolger | ||
| 39 | # Trailer: - Text nach dem Abschluß | ||
| 40 | # XMLClose: - Eins wenn vor '>' ein '/' kommt | ||
| 41 | # tainted - Node->insert() wiederholen | ||
| 42 | # B<HTML> - Finaler HTML-Code | ||
| 43 | # ENV - Zeiger zur Umgebung | ||
| 44 | |||
| 45 | |||
| 46 | sub new($$$$) | ||
| 47 | { | ||
| 48 | my( $class,$Name,$Text,$Prev,$E ) = @_ ; | ||
| 49 | |||
| 50 | my $Element ; | ||
| 51 | |||
| 52 | $Element->{Name} = $Name ; | ||
| 53 | $Element->{Trailer} = $Text ; | ||
| 54 | $Element->{ENV} = $E ; | ||
| 55 | |||
| 56 | bless( $Element,$class ) ; | ||
| 57 | |||
| 58 | if( ref $Prev eq $class ) | ||
| 59 | { | ||
| 60 | if( ref $Prev->{Parent} eq $class ) | ||
| 61 | { | ||
| 62 | $Prev->{Parent}->addChild( $Element,$Prev ) ; | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { | ||
| 66 | $Element->{Prev} = $Prev ; | ||
| 67 | $Element->{Next} = $Prev->{Next} ; | ||
| 68 | $Prev->{Next} = $Element ; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return $Element ; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub name | ||
| 76 | { | ||
| 77 | my $Node = shift; | ||
| 78 | |||
| 79 | $Node->{Name} = shift if @_ ; | ||
| 80 | return $Node->{Name} ; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub text | ||
| 84 | { | ||
| 85 | my $Node = shift; | ||
| 86 | |||
| 87 | $Node->{Text} = join '',@_ if @_ ; | ||
| 88 | return $Node->{Text} ; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub parent | ||
| 92 | { | ||
| 93 | my $Node = shift ; | ||
| 94 | my $Parent = shift ; | ||
| 95 | |||
| 96 | if( ref $Parent eq 'uHTMLnode' and $Parent != $Node->{Parent} ) | ||
| 97 | { | ||
| 98 | $Node->detach(1) ; | ||
| 99 | $Parent->addChild( $Node ) ; | ||
| 100 | $Node->{Parent} = $Parent ; | ||
| 101 | } | ||
| 102 | return $Node->{Parent} ; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub prev | ||
| 106 | { | ||
| 107 | my $Node = shift ; | ||
| 108 | my $Prev = shift ; | ||
| 109 | |||
| 110 | if( ref $Prev eq 'uHTMLnode' ) | ||
| 111 | { | ||
| 112 | $Prev->detach(1) ; | ||
| 113 | my $Parent = $Node->{Parent} ; | ||
| 114 | if( ref $Parent eq 'uHTMLnode' ) | ||
| 115 | { | ||
| 116 | $Parent->{FirstChild} = $Prev if $Parent->{FirstChild} == $Node ; | ||
| 117 | $Prev->{Parent} = $Parent ; | ||
| 118 | } | ||
| 119 | $Prev->{Prev} = $Node->{Prev} ; | ||
| 120 | $Prev->{Next} = $Node ; | ||
| 121 | $Node->{Prev} = $Prev ; | ||
| 122 | } | ||
| 123 | return $Node->{Prev} ; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub next | ||
| 127 | { | ||
| 128 | my $Node = shift; | ||
| 129 | my $Next = shift ; | ||
| 130 | |||
| 131 | if( ref $Next eq 'uHTMLnode' ) | ||
| 132 | { | ||
| 133 | $Next->detach(1) ; | ||
| 134 | my $Parent = $Node->{Parent} ; | ||
| 135 | if( ref $Parent eq 'uHTMLnode' ) | ||
| 136 | { | ||
| 137 | $Parent->{LastChild} = $Next if $Parent->{LastChild} == $Node ; | ||
| 138 | $Next->{Parent} = $Parent ; | ||
| 139 | } | ||
| 140 | $Next->{Prev} = $Node ; | ||
| 141 | $Next->{Next} = $Node->{Next} ; | ||
| 142 | $Node->{Next} = $Next ; | ||
| 143 | } | ||
| 144 | return $Node->{Next} ; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub firstChild | ||
| 148 | { | ||
| 149 | my $Node = shift ; | ||
| 150 | my $FC = shift ; | ||
| 151 | |||
| 152 | if( ref $FC eq 'uHTMLnode' and $FC != $Node ) | ||
| 153 | { | ||
| 154 | $FC->detach(1) ; | ||
| 155 | $Node->addChild( $FC ) ; | ||
| 156 | } | ||
| 157 | return $Node->{FirstChild} ; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub lastChild | ||
| 161 | { | ||
| 162 | my $Node = shift ; | ||
| 163 | my $LC = shift ; | ||
| 164 | |||
| 165 | if( ref $LC eq 'uHTMLnode' and $LC != $Node ) | ||
| 166 | { | ||
| 167 | $LC->detach(1) ; | ||
| 168 | $Node->appendChild( $LC ) ; | ||
| 169 | } | ||
| 170 | return $Node->{LastChild} ; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub addChild | ||
| 174 | { | ||
| 175 | my $Node = shift; | ||
| 176 | my $new = shift; | ||
| 177 | my $prev = shift; | ||
| 178 | |||
| 179 | return undef unless ref $new eq 'uHTMLnode' and $new != $Node ; | ||
| 180 | return $new unless $new->{Parent} != $Node ; | ||
| 181 | $new->detach() if $new->{Parent} or $new->{Prev} or $new->{Next} ; | ||
| 182 | undef $prev unless ref $prev eq 'uHTMLnode' and $prev->{Parent} == $Node ; | ||
| 183 | |||
| 184 | if( $prev ) | ||
| 185 | { | ||
| 186 | ($new->{Next} = $prev->{Next}) ? ($prev->{Next}->{Prev}=$new) : ($Node->{LastChild}=$new) ; | ||
| 187 | $new->{Prev} = $prev ; | ||
| 188 | $prev->{Next} = $new ; | ||
| 189 | } | ||
| 190 | else | ||
| 191 | { | ||
| 192 | ($new->{Next} = $Node->{FirstChild}) ? ($new->{Next}->{Prev}=$new) : ($Node->{LastChild}=$new) ; | ||
| 193 | $new->{Prev} = undef ; | ||
| 194 | $Node->{FirstChild} = $new ; | ||
| 195 | } | ||
| 196 | $new->{Parent} = $Node ; | ||
| 197 | |||
| 198 | return $new ; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub appendChild( $ ) | ||
| 202 | { | ||
| 203 | my $Node = shift; | ||
| 204 | my $new = shift; | ||
| 205 | |||
| 206 | return $Node->addChild( $new, $Node->{LastChild} ) ; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub detach | ||
| 210 | { | ||
| 211 | my $Node = shift ; | ||
| 212 | my $KeepT = shift ; | ||
| 213 | my $Parent = $Node->{Parent} ; | ||
| 214 | my $Prev = $Node->{Prev} ; | ||
| 215 | my $Next = $Node->{Next} ; | ||
| 216 | my $Child ; | ||
| 217 | |||
| 218 | if( ref $Parent eq 'uHTMLnode' ) | ||
| 219 | { | ||
| 220 | $Parent->{FirstChild} = $Next if $Parent->{FirstChild} == $Node ; | ||
| 221 | $Parent->{LastChild} = $Prev if $Parent->{LastChild} == $Node ; | ||
| 222 | delete $Node->{Parent} ; | ||
| 223 | } | ||
| 224 | |||
| 225 | return $Node unless ref $Prev eq 'uHTMLnode' or ref $Next eq 'uHTMLnode' ; | ||
| 226 | |||
| 227 | $Prev->{Next} = $Next if ref $Prev eq 'uHTMLnode' ; | ||
| 228 | $Next->{Prev} = $Prev if ref $Next eq 'uHTMLnode' ; | ||
| 229 | |||
| 230 | if( not $KeepT and $Node->{Trailer} ) | ||
| 231 | { | ||
| 232 | if( $Prev ) | ||
| 233 | { | ||
| 234 | $Prev->{Trailer} .= $Node->{Trailer} ; | ||
| 235 | delete $Node->{Trailer} ; | ||
| 236 | } | ||
| 237 | elsif( $Parent ) | ||
| 238 | { | ||
| 239 | $Parent->{Text} .= $Node->{Trailer} ; | ||
| 240 | delete $Node->{Trailer} ; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | delete $Node->{Prev} ; | ||
| 245 | delete $Node->{Next} ; | ||
| 246 | |||
| 247 | return $Node ; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub delete | ||
| 251 | { | ||
| 252 | my $Node = shift; | ||
| 253 | |||
| 254 | $Node->detach() ; | ||
| 255 | $Node->{FirstChild}->delete() while $Node->{FirstChild} ; | ||
| 256 | undef $Node ; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub end | ||
| 260 | { | ||
| 261 | my $self = shift; | ||
| 262 | |||
| 263 | $self->{End} = shift if @_ ; | ||
| 264 | return $self->{End} ; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub taint | ||
| 268 | { | ||
| 269 | my $self = shift; | ||
| 270 | |||
| 271 | $self->{tainted} = 1 ; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub attributes | ||
| 275 | { | ||
| 276 | my $self = shift ; | ||
| 277 | |||
| 278 | $self->{Attributes} = shift if ref $_[0] eq 'HASH' ; | ||
| 279 | return $self->{Attributes} ; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub rawAttr | ||
| 283 | { | ||
| 284 | my $self = shift ; | ||
| 285 | my $Name = shift ; | ||
| 286 | |||
| 287 | return undef unless $Name and ((ref $self->{Attributes} eq 'HASH' and $self->{Attributes}->{$Name} ne '') or @_) ; | ||
| 288 | |||
| 289 | if( @_ ) | ||
| 290 | { | ||
| 291 | unless( ref $self->{Attributes} eq 'HASH' ) | ||
| 292 | { | ||
| 293 | my %P ; | ||
| 294 | $self->{Attributes} = \%P ; | ||
| 295 | } | ||
| 296 | $self->{Attributes}->{$Name} = join '',@_ ; | ||
| 297 | } | ||
| 298 | return $self->{Attributes}->{$Name} ; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub setAttr | ||
| 302 | { | ||
| 303 | my( $Node,$Name,$Value ) = @_ ; | ||
| 304 | return $Node->rawAttr( $Name,$Value ) ; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub testAttr | ||
| 308 | { | ||
| 309 | my $self = shift ; | ||
| 310 | my $Name = shift ; | ||
| 311 | |||
| 312 | return 1 if $Name and $self->{Attributes} and exists $self->{Attributes}->{$Name} ; | ||
| 313 | return 0 ; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub testAnyAttr | ||
| 317 | { | ||
| 318 | my $self = shift ; | ||
| 319 | my $Name ; | ||
| 320 | |||
| 321 | return 0 unless $self->{Attributes} ; | ||
| 322 | exists $self->{Attributes}->{$Name} and return 1 while $Name = shift ; | ||
| 323 | return 0 ; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub testAllAttr | ||
| 327 | { | ||
| 328 | my $self = shift ; | ||
| 329 | my $Name = shift ; | ||
| 330 | |||
| 331 | return 0 unless $Name and $self->{Attributes} and exists $self->{Attributes}->{$Name} ; | ||
| 332 | exists $self->{Attributes}->{$Name} or return 0 while $Name = shift ; | ||
| 333 | return 1 ; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub addAttr | ||
| 337 | { | ||
| 338 | my $self = shift ; | ||
| 339 | |||
| 340 | $_ and $self->{Attributes}->{$_} = undef foreach @_ ; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub deleteAttr | ||
| 344 | { | ||
| 345 | my $self = shift ; | ||
| 346 | my $Name = shift ; | ||
| 347 | |||
| 348 | return 0 unless $Name and defined $self->{Attributes} ; | ||
| 349 | |||
| 350 | my $ret = 0 ; | ||
| 351 | |||
| 352 | while( $Name ) | ||
| 353 | { | ||
| 354 | delete $self->{Attributes}->{$Name},$ret++ if exists $self->{Attributes}->{$Name} ; | ||
| 355 | $Name = shift ; | ||
| 356 | } | ||
| 357 | |||
| 358 | return $ret ; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub trailer | ||
| 362 | { | ||
| 363 | my $self = shift; | ||
| 364 | |||
| 365 | $self->{Trailer} = shift if @_ ; | ||
| 366 | return $self->{Trailer} ; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub XMLClose | ||
| 370 | { | ||
| 371 | my $self = shift; | ||
| 372 | |||
| 373 | $self->{XMLClose} = shift if @_ ; | ||
| 374 | return $self->{XMLClose} ; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub env | ||
| 378 | { | ||
| 379 | return $_[0]->{ENV} ; | ||
| 380 | } | ||
| 381 | |||
| 382 | sub HTML | ||
| 383 | { | ||
| 384 | my $self = shift; | ||
| 385 | |||
| 386 | @{$self->{HTML}} = @_ if @_ ; | ||
| 387 | return( wantarray ? @{$self->{HTML}} : join( '',@{$self->{HTML}} ) ) ; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub appendText | ||
| 391 | { | ||
| 392 | my $self = shift; | ||
| 393 | |||
| 394 | push @{$self->{HTML}},@_ if @_ ; | ||
| 395 | } | ||
| 396 | |||
| 397 | #------------------- | ||
| 398 | |||
| 399 | sub insert # InsertNode | ||
| 400 | { | ||
| 401 | my $Node = shift ; | ||
| 402 | my $Text = shift ; | ||
| 403 | my( $Child,$p,$a ) ; | ||
| 404 | |||
| 405 | if( $Node->{Name} ) | ||
| 406 | { | ||
| 407 | &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 408 | |||
| 409 | delete $Node->{HTML} ; # if $Text ; | ||
| 410 | |||
| 411 | if( ref $Node->{Attributes} eq 'HASH' ) | ||
| 412 | { | ||
| 413 | do | ||
| 414 | { | ||
| 415 | delete $Node->{tainted} ; | ||
| 416 | defined $Node->{Attributes}->{$_} and $Node->{Attributes}->{$_} = $Node->codeAttr($_) foreach keys %{$Node->{Attributes}} ; | ||
| 417 | } while( $Node->{tainted} ) ; | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | push @{$Node->{HTML}},$Node->{Attributes} ? | ||
| 422 | join( ' ',"<$Node->{Name}",map( defined $Node->{Attributes}->{$_} ? (($p=$Node->{Attributes}->{$_} and $p=~m/"/s)?"$_=\'$p\'":"$_=\"$p\"") : $_, | ||
| 423 | keys %{$Node->{Attributes}} ) ).'>' : | ||
| 424 | "<$Node->{Name}>" ; | ||
| 425 | |||
| 426 | |||
| 427 | |||
| 428 | if( $Text ) | ||
| 429 | { | ||
| 430 | push @{$Node->{HTML}},$Text ; | ||
| 431 | } | ||
| 432 | else | ||
| 433 | { | ||
| 434 | push @{$Node->{HTML}},$Node->{Text} if $Node->{Text} ne '' ; | ||
| 435 | for( $Child=$Node->{FirstChild}; $Child; $Child=$Child->{Next} ) | ||
| 436 | { | ||
| 437 | push @{$Node->{HTML}},$Child->process() ; | ||
| 438 | } | ||
| 439 | } | ||
| 440 | push @{$Node->{HTML}},"</$Node->{Name}>" if $Node->end() ; | ||
| 441 | } | ||
| 442 | else | ||
| 443 | { | ||
| 444 | push @{$Node->{HTML}},$Text,$Node->{Trailer} ; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | #Bearbeiten des Knotens | ||
| 449 | sub process() #ProcessNode | ||
| 450 | { | ||
| 451 | my $Node = shift ; | ||
| 452 | my $uhtml ; | ||
| 453 | |||
| 454 | if( $Node->{Name} ) | ||
| 455 | { | ||
| 456 | if( ref $uHTML::uTag{ $Node->{Name} } eq 'CODE' ) | ||
| 457 | { | ||
| 458 | ref eq 'CODE' and &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 459 | &{$uHTML::uTag{ $Node->{Name} }}( $Node ) ; | ||
| 460 | } | ||
| 461 | elsif( ref $uHTML::uSTag{ $Node->{Name} } eq 'CODE' ) | ||
| 462 | { | ||
| 463 | ref eq 'CODE' and &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 464 | $uhtml = &{$uHTML::uSTag{ $Node->{Name} }}( $Node,undef,undef ) ; | ||
| 465 | @{$Node->{HTML}} = uHTML::recode( $uhtml,$Node->{ENV} ) ; | ||
| 466 | } | ||
| 467 | else | ||
| 468 | { | ||
| 469 | $Node->insert() ; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | push @{$Node->{HTML}},$Node->{Trailer} if $Node->{Trailer} ne '' ; | ||
| 474 | return ref $Node->{HTML} eq 'ARRAY' ? @{$Node->{HTML}} : () ; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub map( $$ ) | ||
| 478 | { | ||
| 479 | my( $Node,$HeadText,$TailText ) = @_ ; | ||
| 480 | my( $T,$C,@HTML ) ; | ||
| 481 | |||
| 482 | if( $HeadText ne '' ) | ||
| 483 | { | ||
| 484 | for( $T = $C = uHTML::_struct( undef,$HeadText,$Node->{ENV} ) ; $C ; $C = $C->{Next} ) { push @HTML,$C->process() } | ||
| 485 | $C = $T, $T = $T->{Next}, $C->delete() while $T ; | ||
| 486 | } | ||
| 487 | |||
| 488 | push @HTML,$Node->{Text} ; | ||
| 489 | |||
| 490 | for( $C=$Node->{FirstChild}; $C; $C=$C->{Next} ) { push @HTML,$C->process() } ; | ||
| 491 | |||
| 492 | if( $TailText ne '' ) | ||
| 493 | { | ||
| 494 | for( $T = $C = uHTML::_struct( undef,$TailText,$Node->{ENV} ) ; $C ; $C = $C->{Next} ) { push @HTML,$C->process() } | ||
| 495 | $C = $T, $T = $T->{Next}, $C->delete() while $T ; | ||
| 496 | } | ||
| 497 | |||
| 498 | push @HTML,$Node->{Trailer} ; | ||
| 499 | |||
| 500 | return( $Node->{HTML} = \@HTML ) ; | ||
| 501 | } | ||
| 502 | |||
| 503 | sub copy() | ||
| 504 | { | ||
| 505 | my $Node = shift ; | ||
| 506 | my $CopyT = shift ; | ||
| 507 | my( $Copy,$Prev,$Child,$CC ) ; | ||
| 508 | |||
| 509 | $Copy = uHTMLnode->new( $Node->{Name},undef,undef,$Node->{ENV} ) ; | ||
| 510 | $Prev = undef ; | ||
| 511 | |||
| 512 | $Copy->{$_} = $Node->{$_} foreach qw( Text End XMLClose ) ; | ||
| 513 | $Copy->{Trailer} = $Node->{Trailer} if $CopyT ; | ||
| 514 | |||
| 515 | if( $Node->{Attributes} ) | ||
| 516 | { | ||
| 517 | my %Attributes = %{$Node->{Attributes}} ; | ||
| 518 | |||
| 519 | $Copy->{Attributes} = \%Attributes ; | ||
| 520 | } | ||
| 521 | |||
| 522 | for( $Child=$Node->{FirstChild}; $Child; $Child=$Child->{Next} ) | ||
| 523 | { | ||
| 524 | $Copy->appendChild( $Child->copy( 1 ) ) ; | ||
| 525 | } | ||
| 526 | |||
| 527 | return $Copy ; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub embed($) | ||
| 531 | { | ||
| 532 | my( $Node,$Name ) = @_ ; | ||
| 533 | |||
| 534 | my $ENode = uHTMLnode->new( $Name,'',undef,$Node->{ENV} ) ; | ||
| 535 | |||
| 536 | $ENode->{Parent} = $Node->{Parent} ; | ||
| 537 | $ENode->{Prev} = $Node->{Prev} ; | ||
| 538 | $ENode->{Next} = $Node->{Next} ; | ||
| 539 | $ENode->{FirstChild} = $Node ; | ||
| 540 | $ENode->{LastChild} = $Node ; | ||
| 541 | $ENode->{End} = 1 ; | ||
| 542 | $ENode->{Attributes} = undef ; | ||
| 543 | $ENode->{Trailer} = undef ; | ||
| 544 | |||
| 545 | $ENode->{Prev} ? ($ENode->{Prev}->{Next} = $ENode) : ($ENode->{Parent}->{FirstChild} = $ENode) ; | ||
| 546 | $ENode->{Next} ? ($ENode->{Next}->{Prev} = $ENode) : ($ENode->{Parent}->{LastChild} = $ENode) ; | ||
| 547 | |||
| 548 | $Node->{Parent} = $ENode ; | ||
| 549 | $Node->{Prev} = undef ; | ||
| 550 | $Node->{Next} = undef ; | ||
| 551 | |||
| 552 | return $ENode ; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub prepend($) | ||
| 556 | { | ||
| 557 | my $self = shift ; | ||
| 558 | my $Node = shift ; | ||
| 559 | |||
| 560 | $Node->{Parent}->{FirstChild} = $Node->{Next} if $Node->{Parent} and $Node->{Parent}->{FirstChild} == $Node ; | ||
| 561 | $Node->{Parent}->{LastChild} = $Node->{Prev} if $Node->{Parent} and $Node->{Parent}->{LastChild} == $Node ; | ||
| 562 | $Node->{Prev}->{Next} = $Node->{Next} if $Node->{Prev} ; | ||
| 563 | $Node->{Next}->{Prev} = $Node->{Prev} if $Node->{Next} ; | ||
| 564 | |||
| 565 | $Node->{Parent} = $self->{Parent} ; | ||
| 566 | $self->{Parent}->{FirstChild} = $Node if $self->{Parent} and not $self->{Prev} ; | ||
| 567 | $Node->{Prev} = $self->{Prev} ; | ||
| 568 | $Node->{Next} = $self ; | ||
| 569 | $self->{Prev} = $Node ; | ||
| 570 | } | ||
| 571 | |||
| 572 | sub append($) | ||
| 573 | { | ||
| 574 | my $self = shift ; | ||
| 575 | my $Node = shift ; | ||
| 576 | |||
| 577 | $Node->{Parent}->{FirstChild} = $Node->{Next} if $Node->{Parent} and $Node->{Parent}->{FirstChild} == $Node ; | ||
| 578 | $Node->{Parent}->{LastChild} = $Node->{Prev} if $Node->{Parent} and $Node->{Parent}->{LastChild} == $Node ; | ||
| 579 | $Node->{Prev}->{Next} = $Node->{Next} if $Node->{Prev} ; | ||
| 580 | $Node->{Next}->{Prev} = $Node->{Prev} if $Node->{Next} ; | ||
| 581 | |||
| 582 | $Node->{Parent} = $self->{Parent} ; | ||
| 583 | $self->{Parent}->{LastChild} = $Node if $self->{Parent} and not $self->{Next} ; | ||
| 584 | $Node->{Prev} = $self ; | ||
| 585 | $Node->{Next} = $self->{Next} ; | ||
| 586 | $self->{Next} = $Node ; | ||
| 587 | $Node->{Trailer} .= $self->{Trailer} ; | ||
| 588 | $self->{Trailer} = '' ; | ||
| 589 | } | ||
| 590 | |||
| 591 | |||
| 592 | sub _close( $$ ) | ||
| 593 | { | ||
| 594 | my( $Node,$Name,$Tail ) = @_ ; | ||
| 595 | |||
| 596 | my( $p, $e ) ; | ||
| 597 | |||
| 598 | for( $p = $Node; $p and $p->{Name} ne $Name or $p->{End} ; $p=$p->{Prev} ) {} ; | ||
| 599 | errorMsg( 0,"wrong close of $Name." ) and return undef unless $p ; | ||
| 600 | |||
| 601 | if( $p and $p->{Next} ) | ||
| 602 | { | ||
| 603 | $e = $p->{Next} ; | ||
| 604 | $p->{Next} = 0 ; | ||
| 605 | $p->{FirstChild} = $e ; | ||
| 606 | $p->{LastChild} = $Node ; | ||
| 607 | $e->{Prev} = 0 ; | ||
| 608 | while( $e ) | ||
| 609 | { | ||
| 610 | $e->{Parent} = $p ; | ||
| 611 | $e = $e->{Next} ; | ||
| 612 | } | ||
| 613 | } | ||
| 614 | else | ||
| 615 | { | ||
| 616 | $p = $Node ; | ||
| 617 | } | ||
| 618 | $p->{Text} = $p->{Trailer} ; | ||
| 619 | $p->{End} = 1 ; | ||
| 620 | $p->{Trailer} = $Tail ; | ||
| 621 | return $p ; | ||
| 622 | } | ||
| 623 | |||
| 624 | sub _callwrap | ||
| 625 | { | ||
| 626 | my( $Func,$Node,$AttrName,$Params ) = @_ ; | ||
| 627 | my @FParams = ($Params =~ m/([^,'"](?:\\,|[^,])*|'(?:\\'|[^'])*'|"(?:\\"|[^"])*"|)\s*,?/sg) ; | ||
| 628 | |||
| 629 | s/^\s*['"]?//s + s/['"]?\s*$//s foreach @FParams ; | ||
| 630 | return $uHTML::uAttr{$Func}( $Node,$AttrName,$Func,map((m/(?<!\\)\$(?=[a-zA-Z_])/s?codeAttr($Node,$AttrName,$_):$_),@FParams) ) if $uHTML::uAttr{$Func} ; | ||
| 631 | errorMsg( 0,"unknown variable $Func.") ; | ||
| 632 | return "\\\$$Func" ; | ||
| 633 | } | ||
| 634 | |||
| 635 | sub codeAttr | ||
| 636 | { | ||
| 637 | my $Node = shift ; | ||
| 638 | my $Attr = shift ; | ||
| 639 | my $Value = shift ; | ||
| 640 | |||
| 641 | return '' unless $Value or $Node->{Attributes} and ($Value = $Node->{Attributes}->{$Attr}) ne '' ; | ||
| 642 | |||
| 643 | my( $func,$par,$tail,$sub,$rsub ) ; | ||
| 644 | my @subs = split m/(?<!\\)\$(?=[a-zA-Z_\$])/s,$Value ; | ||
| 645 | while( $#subs > 0 ) | ||
| 646 | { | ||
| 647 | $rsub++ ; | ||
| 648 | next unless $sub = pop @subs ; | ||
| 649 | $sub =~ s/\\(?=\$)//s ; | ||
| 650 | |||
| 651 | ( $func,$par,$tail ) = ($sub =~ m/([a-zA-Z_][0-9a-zA-Z_]*)(?:\(\s*('(?:\\'|[^'])*'|"(?:\\"|[^"])*"|(?:\\.|[^()])*(?:\s*,(?:'(?:\\'|[^'])*'|"(?:\\"|[^"])*"|(?:\\.|[^()])))*)\s*\))?(.*)$/s) ; | ||
| 652 | $sub = _callwrap( $func,$Node,$Attr,$par ) . $tail ; | ||
| 653 | |||
| 654 | $subs[$#subs] .= $sub ; | ||
| 655 | } | ||
| 656 | $subs[0] =~ s/\\(?!\\)//sg ; | ||
| 657 | |||
| 658 | return $subs[0] ; | ||
| 659 | } | ||
| 660 | |||
| 661 | sub attr | ||
| 662 | { | ||
| 663 | my $Node = shift ; | ||
| 664 | my $Attr = shift ; | ||
| 665 | my $Value = shift ; | ||
| 666 | return( defined $Value ? rawAttr($Node,$Attr,$Value) : codeAttr($Node,$Attr) ) ; | ||
| 667 | } | ||
| 668 | |||
| 669 | sub errorMsg | ||
| 670 | { | ||
| 671 | print STDERR "uHTML Error in $uHTML::FileName: $_[1]\n" if $_[2] or $uHTML::FileName ; | ||
| 672 | } | ||
| 673 | |||
| 674 | ########################################################## | ||
| 675 | ########################################################## | ||
| 676 | ########################################################## | ||
| 677 | |||
| 678 | package uHTML; | ||
| 679 | |||
| 680 | local( $uHTML::Pos,@uHTML::Blocks,%uHTML::uHTML,%uHTML::uTag,%uHTML::uAttr,%uHTML::uSTag,$uHTML::FileName ) ; | ||
| 681 | |||
| 682 | sub _checkName | ||
| 683 | { | ||
| 684 | my $Name = shift ; | ||
| 685 | my $Code = shift ; | ||
| 686 | |||
| 687 | $_->{$Name} and $_->{$Name} != $Code and return 0 foreach @_ ; | ||
| 688 | return 1 ; | ||
| 689 | } | ||
| 690 | |||
| 691 | sub registerTagCode( $$ ) | ||
| 692 | { | ||
| 693 | my( $TagName,$Code ) = @_ ; | ||
| 694 | |||
| 695 | push @{$uHTML::uCode{$TagName}},$Code if _checkName( $TagName,$Code ) ; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub registerTag | ||
| 699 | { | ||
| 700 | my( $TagName,$Code,$nowarn ) = @_ ; | ||
| 701 | |||
| 702 | $uHTML::uTag{$TagName} = $Code if $nowarn or _checkName( $TagName,$Code,\%uHTML::uTag, \%uHTML::uSTag ) ; | ||
| 703 | } | ||
| 704 | |||
| 705 | sub registerAttrCode | ||
| 706 | { | ||
| 707 | my( $AttrName,$Code,$nowarn ) = @_ ; | ||
| 708 | |||
| 709 | registerVar( $AttrName,$Code ) if $nowarn or _checkName( $AttrName,$Code,\%uHTML::uAttr ) ; | ||
| 710 | } | ||
| 711 | |||
| 712 | sub registerVar | ||
| 713 | { | ||
| 714 | my( $AttrName,$Code,$nowarn ) = @_ ; | ||
| 715 | |||
| 716 | $uHTML::uAttr{$AttrName} = $Code if $nowarn or _checkName( $AttrName,$Code,\%uHTML::uAttr ) ; | ||
| 717 | } | ||
| 718 | |||
| 719 | sub register | ||
| 720 | { | ||
| 721 | my( $Name,$Code,$nowarn ) = @_ ; | ||
| 722 | |||
| 723 | $uHTML::uAttr{$Name} = $uHTML::uSTag{$Name} = $Code if $nowarn or _checkName( $Name,$Code,\%uHTML::uTag,\%uHTML::uAttr, \%uHTML::uSTag ) ; | ||
| 724 | } | ||
| 725 | |||
| 726 | sub tags() | ||
| 727 | { | ||
| 728 | return ( keys %uHTML::uTag ) ; | ||
| 729 | } | ||
| 730 | |||
| 731 | sub testTag | ||
| 732 | { | ||
| 733 | return( defined( $uHTML::uTag{$_[0]} ) or defined( $uHTML::uSTag{$_[0]} ) ? 1 : '' ) ; | ||
| 734 | } | ||
| 735 | |||
| 736 | sub vars() | ||
| 737 | { | ||
| 738 | return ( keys %uHTML::uAttr ) ; | ||
| 739 | } | ||
| 740 | |||
| 741 | sub testVar | ||
| 742 | { | ||
| 743 | return( defined( $uHTML::uAttr{$_[0]} ) || defined( $uHTML::uSTag{$_[0]} ) ? 1 : '' ) ; | ||
| 744 | } | ||
| 745 | |||
| 746 | sub fileStart | ||
| 747 | { | ||
| 748 | return unless $uHTML::FileName and $_[0] ; | ||
| 749 | push @uHTML::FNames,$uHTML::FileName ; | ||
| 750 | $uHTML::FileName = $_[0] ; | ||
| 751 | } | ||
| 752 | |||
| 753 | sub fileEnd | ||
| 754 | { | ||
| 755 | return $uHTML::FileName = pop @uHTML::FNames if $uHTML::FileName ; | ||
| 756 | } | ||
| 757 | |||
| 758 | sub _struct( $$$ ) | ||
| 759 | { | ||
| 760 | my( $Prev,$String,$env ) = @_ ; | ||
| 761 | my( $Struct,$New,$val,$par,$tag ) ; | ||
| 762 | |||
| 763 | if( ref $Prev eq 'uHTMLnode' ) | ||
| 764 | { | ||
| 765 | if( $Prev->{Trailer} and $Prev->{Trailer} =~ m/([^<]*)(<.*)/s ) | ||
| 766 | { | ||
| 767 | $String = $2 . $String ; | ||
| 768 | $Prev->{Trailer} = $1 ; | ||
| 769 | } | ||
| 770 | $Prev->{Trailer} .= $1 if $String =~ m/^([^<]*)/sgc ; | ||
| 771 | } | ||
| 772 | else | ||
| 773 | { | ||
| 774 | $Prev = uHTMLnode->new( '',($String =~ m/^([^<]*)/sgc ? $1 : ''),undef,$env ) ; | ||
| 775 | } | ||
| 776 | $Struct = $Prev ; | ||
| 777 | |||
| 778 | while( $tag = ($String =~ m/\G<(\/?[\w-]+|[!?*])/sgc)[0] ) | ||
| 779 | { | ||
| 780 | if( $tag =~ m/^[\w-]+$/s ) | ||
| 781 | { | ||
| 782 | my( %Attributes ) ; | ||
| 783 | |||
| 784 | while( $String =~ m/\G\s*([\w-]+)\s*(?:\=\s*("(?:[^"]|\\")*(?<!\\)"|'(?:[^']|\\')*(?<!\\)'|[^\s>]+))?/sgc ) | ||
| 785 | { | ||
| 786 | $par = $1 ; | ||
| 787 | $val = $2 ; | ||
| 788 | if( $par ) | ||
| 789 | { | ||
| 790 | if( $val ne '' ) | ||
| 791 | { | ||
| 792 | $val =~ s/^['"]|(?<!\\)\\|['"]$//sg ; | ||
| 793 | $Attributes{$par} .= $val ; | ||
| 794 | } | ||
| 795 | else | ||
| 796 | { | ||
| 797 | $Attributes{$par} = undef unless exists $Attributes{$par} ; | ||
| 798 | } | ||
| 799 | } | ||
| 800 | } | ||
| 801 | lc $tag eq 'style' || lc $tag eq 'script' && !exists $Attributes{'src'} ? | ||
| 802 | $String =~ m/\G[^>]*>(.*?)(?=<\/$tag>)/sgci : | ||
| 803 | $String =~ m/\G[^>]*>([^<]*)/sgc ; | ||
| 804 | $New = uHTMLnode->new( $tag,$1,$Prev,$env ) ; | ||
| 805 | $New->attributes( \%Attributes ) if %Attributes ; | ||
| 806 | } | ||
| 807 | elsif( $tag =~ m/^\/([\w-]+)$/s ) | ||
| 808 | { | ||
| 809 | $tag = $1 ; | ||
| 810 | $String =~ m/\G\s*\>([^<]*)/sgc ; | ||
| 811 | $Prev = $Prev->_close( $tag,$1,"wrong close of $tag." ) if $Prev ; | ||
| 812 | undef $New ; | ||
| 813 | } | ||
| 814 | elsif( $tag =~ m/^[!?]/ ) | ||
| 815 | { | ||
| 816 | if( $tag eq '!' and $String =~ m/\G--/sgc ) | ||
| 817 | { | ||
| 818 | $tag = '!--' ; | ||
| 819 | $String =~ m/\G(.*?-->[^<]*)/sgc ; | ||
| 820 | $val = $1 ; | ||
| 821 | } | ||
| 822 | else | ||
| 823 | { | ||
| 824 | $String =~ m/\G((?:[^'">]|"(?:[^"]|\\")*(?<!\\)"|'(?:[^']|\\')*(?<!\\)')*>[^<]*)/sgc ; | ||
| 825 | $val = $1 ; | ||
| 826 | } | ||
| 827 | $New = uHTMLnode->new( '',"<$tag$val",$Prev,$env ) ; | ||
| 828 | } | ||
| 829 | elsif( $tag eq '*' ) | ||
| 830 | { | ||
| 831 | $String =~ m/\G(.*?\*>[^<]*)/sgc ; | ||
| 832 | $New = uHTMLnode->new( '',"<*$1",$Prev,$env ) ; | ||
| 833 | } | ||
| 834 | else | ||
| 835 | { | ||
| 836 | $String =~ m/\G([^<]*)/sgc ; | ||
| 837 | $New = uHTMLnode->new( '',"$tag$1",$Prev,$env ) ; | ||
| 838 | } | ||
| 839 | if( $New ) | ||
| 840 | { | ||
| 841 | $Prev->{Next} = $New ; | ||
| 842 | $Prev = $Prev->{Next} ; | ||
| 843 | undef $New ; | ||
| 844 | } | ||
| 845 | } | ||
| 846 | |||
| 847 | $Prev->{Next} = uHTMLnode->new( '',$1,$Prev,$env ) if $String =~ m/\G(.+)/sg ; | ||
| 848 | return $Struct ; | ||
| 849 | |||
| 850 | } | ||
| 851 | |||
| 852 | sub recodedList( $$ ) | ||
| 853 | { | ||
| 854 | my( $uhtml,$env ) = @_ ; | ||
| 855 | my( @HTML,$node,$T ) ; | ||
| 856 | |||
| 857 | return undef if $uhtml eq '' ; | ||
| 858 | |||
| 859 | if( $uhtml =~ m/</ ) | ||
| 860 | { | ||
| 861 | loadModules( $env = \%ENV ) unless ref $env eq 'HASH' ; | ||
| 862 | for( $T = $node = _struct( undef,$uhtml,$env ) ; $node ; $node = $node->{Next} ) { push @HTML,$node->process() } | ||
| 863 | } | ||
| 864 | else | ||
| 865 | { | ||
| 866 | $HTML[0] = $uhtml ; | ||
| 867 | } | ||
| 868 | return \@HTML ; | ||
| 869 | } | ||
| 870 | |||
| 871 | sub recode( $$ ) | ||
| 872 | { | ||
| 873 | my( $uhtml,$env ) = @_ ; | ||
| 874 | return '' if $uhtml eq '' ; | ||
| 875 | my $HTML = recodedList( $uhtml,$env ) ; | ||
| 876 | return( wantarray ? @{$HTML} : join( '',@{$HTML} ) ) ; | ||
| 877 | } | ||
| 878 | |||
| 879 | ###################################### | ||
| 880 | ###################################### | ||
| 881 | |||
| 882 | sub loadModules( $ ) | ||
| 883 | { | ||
| 884 | my $env = shift ; | ||
| 885 | |||
| 886 | return unless ref $env eq 'HASH' ; | ||
| 887 | |||
| 888 | my $CPath ; | ||
| 889 | |||
| 890 | unless( $CPath = $env->{'SCRIPT_ROOT'} ) | ||
| 891 | { | ||
| 892 | $CPath = $0 =~ m%^/% ? $0 : $env->{'SCRIPT_FILENAME'} ; | ||
| 893 | $CPath =~ s%/[^/]*$%%s ; | ||
| 894 | $CPath = '.' unless $CPath ; | ||
| 895 | } | ||
| 896 | |||
| 897 | return unless opendir DH,$CPath ; | ||
| 898 | require "$CPath/$_" foreach sort grep -f "$CPath/$_", grep m/-uHTML\.pmc?$/, readdir DH ; | ||
| 899 | return unless opendir DH,"$CPath/uHTML" ; | ||
| 900 | require "$CPath/uHTML/$_" foreach sort grep -f "$CPath/uHTML/$_", grep m/\.pmc?$/, readdir DH ; | ||
| 901 | } | ||
| 902 | |||
| 903 | 1; | ||
| 904 | |||
| 905 | __END__ |