| 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.2" ; | ||
| 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 findChild | ||
| 210 | { | ||
| 211 | my $Node = shift; | ||
| 212 | my $name = shift; | ||
| 213 | my $child = shift; | ||
| 214 | my( $C,$R ) ; | ||
| 215 | |||
| 216 | return undef unless $name ne '' and $Node->{FirstChild} ; | ||
| 217 | undef $child unless ref $child eq 'uHTMLnode' and $child != $Node ; | ||
| 218 | |||
| 219 | for( $C = $Node->{FirstChild}; $C; $C=$C->{Next} ) | ||
| 220 | { | ||
| 221 | $C == $child and undef $child, next if $child ; | ||
| 222 | return $C if $name eq $C->{Name} ; | ||
| 223 | return $R if $R = $C->findChild( $name,$child ) ; | ||
| 224 | } | ||
| 225 | return undef ; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub adoptChildren | ||
| 229 | { | ||
| 230 | my $Node = shift; | ||
| 231 | my $from = shift; | ||
| 232 | my $child = shift; | ||
| 233 | my( $C,$F,$L ) ; | ||
| 234 | |||
| 235 | return undef unless ref $from eq 'uHTMLnode' and $from->{FirstChild} ; | ||
| 236 | for( $C=$from; $C; $C=$C->{Parent} ) | ||
| 237 | { | ||
| 238 | return undef if $C == $Node ; | ||
| 239 | } | ||
| 240 | undef $child unless ref $child eq 'uHTMLnode' and $child->{Parent} == $Node ; | ||
| 241 | |||
| 242 | for( $F=$C=$from->{FirstChild}; $C; $C=$C->{Next} ) | ||
| 243 | { | ||
| 244 | $C->{Parent} = $Node ; | ||
| 245 | $L = $C ; | ||
| 246 | } | ||
| 247 | delete $from->{FirstChild} ; | ||
| 248 | delete $from->{LastChild} ; | ||
| 249 | |||
| 250 | if( $child ) | ||
| 251 | { | ||
| 252 | if( $child->{Next} ) | ||
| 253 | { | ||
| 254 | $L->{Next} = $child->{Next} ; | ||
| 255 | $L->{Next}->{Prev} = $L ; | ||
| 256 | } | ||
| 257 | else | ||
| 258 | { | ||
| 259 | $Node->{LastChild} = $L ; | ||
| 260 | } | ||
| 261 | $child->{Next} = $F ; | ||
| 262 | } | ||
| 263 | else | ||
| 264 | { | ||
| 265 | if( $Node->{FirstChild} ) | ||
| 266 | { | ||
| 267 | $L->{Next} = $Node->{FirstChild} ; | ||
| 268 | $L->{Next}->{Prev} = $L ; | ||
| 269 | } | ||
| 270 | else | ||
| 271 | { | ||
| 272 | $Node->{LastChild} = $L ; | ||
| 273 | } | ||
| 274 | $Node->{FirstChild} = $F ; | ||
| 275 | } | ||
| 276 | |||
| 277 | return $F ; | ||
| 278 | } | ||
| 279 | |||
| 280 | sub replace | ||
| 281 | { | ||
| 282 | my $Node = shift; | ||
| 283 | my $new = shift; | ||
| 284 | my $KeepT = shift; | ||
| 285 | my $C ; | ||
| 286 | |||
| 287 | return undef unless ref $new eq 'uHTMLnode' ; | ||
| 288 | |||
| 289 | print STDERR "Replace node $Node->{Name} with $new->{Name}\n" ; | ||
| 290 | |||
| 291 | for( $C=$new; $C; $C=$C->{Parent} ) | ||
| 292 | { | ||
| 293 | return undef if $C == $Node ; | ||
| 294 | } | ||
| 295 | $new->detach() if $new->{Parent} or $new->{Prev} or $new->{Next} ; | ||
| 296 | |||
| 297 | $new->{Prev} = $Node->{Prev} ; | ||
| 298 | $new->{Next} = $Node->{Next} ; | ||
| 299 | $new->{Parent} = $Node->{Parent} ; | ||
| 300 | $new->{Trailer} = $Node->{Trailer} if $KeepT ; | ||
| 301 | |||
| 302 | return $Node->detach() ; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub detach | ||
| 306 | { | ||
| 307 | my $Node = shift ; | ||
| 308 | my $KeepT = shift ; | ||
| 309 | my $Parent = $Node->{Parent} ; | ||
| 310 | my $Prev = $Node->{Prev} ; | ||
| 311 | my $Next = $Node->{Next} ; | ||
| 312 | my $Child ; | ||
| 313 | |||
| 314 | if( ref $Parent eq 'uHTMLnode' ) | ||
| 315 | { | ||
| 316 | $Parent->{FirstChild} = $Next if $Parent->{FirstChild} == $Node ; | ||
| 317 | $Parent->{LastChild} = $Prev if $Parent->{LastChild} == $Node ; | ||
| 318 | delete $Node->{Parent} ; | ||
| 319 | } | ||
| 320 | |||
| 321 | return $Node unless ref $Prev eq 'uHTMLnode' or ref $Next eq 'uHTMLnode' ; | ||
| 322 | |||
| 323 | $Prev->{Next} = $Next if ref $Prev eq 'uHTMLnode' ; | ||
| 324 | $Next->{Prev} = $Prev if ref $Next eq 'uHTMLnode' ; | ||
| 325 | |||
| 326 | if( not $KeepT and $Node->{Trailer} ) | ||
| 327 | { | ||
| 328 | if( $Prev ) | ||
| 329 | { | ||
| 330 | $Prev->{Trailer} .= $Node->{Trailer} ; | ||
| 331 | delete $Node->{Trailer} ; | ||
| 332 | } | ||
| 333 | elsif( $Parent ) | ||
| 334 | { | ||
| 335 | $Parent->{Text} .= $Node->{Trailer} ; | ||
| 336 | delete $Node->{Trailer} ; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | delete $Node->{Prev} ; | ||
| 341 | delete $Node->{Next} ; | ||
| 342 | |||
| 343 | return $Node ; | ||
| 344 | } | ||
| 345 | |||
| 346 | sub delete | ||
| 347 | { | ||
| 348 | my $Node = shift; | ||
| 349 | |||
| 350 | $Node->detach() ; | ||
| 351 | $Node->{FirstChild}->delete() while $Node->{FirstChild} ; | ||
| 352 | undef $Node ; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub end | ||
| 356 | { | ||
| 357 | my $self = shift; | ||
| 358 | |||
| 359 | $self->{End} = shift if @_ ; | ||
| 360 | return $self->{End} ; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub taint | ||
| 364 | { | ||
| 365 | my $self = shift; | ||
| 366 | |||
| 367 | $self->{tainted} = 1 ; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub attributes | ||
| 371 | { | ||
| 372 | my $self = shift ; | ||
| 373 | |||
| 374 | $self->{Attributes} = shift if ref $_[0] eq 'HASH' ; | ||
| 375 | return $self->{Attributes} ; | ||
| 376 | } | ||
| 377 | |||
| 378 | sub rawAttr | ||
| 379 | { | ||
| 380 | my $self = shift ; | ||
| 381 | my $Name = shift ; | ||
| 382 | |||
| 383 | return undef unless $Name and ((ref $self->{Attributes} eq 'HASH' and $self->{Attributes}->{$Name} ne '') or @_) ; | ||
| 384 | |||
| 385 | if( @_ ) | ||
| 386 | { | ||
| 387 | unless( ref $self->{Attributes} eq 'HASH' ) | ||
| 388 | { | ||
| 389 | my %P ; | ||
| 390 | $self->{Attributes} = \%P ; | ||
| 391 | } | ||
| 392 | $self->{Attributes}->{$Name} = join '',@_ ; | ||
| 393 | } | ||
| 394 | return $self->{Attributes}->{$Name} ; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub setAttr | ||
| 398 | { | ||
| 399 | my( $Node,$Name,$Value ) = @_ ; | ||
| 400 | return $Node->rawAttr( $Name,$Value ) ; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub testAttr | ||
| 404 | { | ||
| 405 | my $self = shift ; | ||
| 406 | my $Name = shift ; | ||
| 407 | |||
| 408 | return 1 if $Name and $self->{Attributes} and exists $self->{Attributes}->{$Name} ; | ||
| 409 | return 0 ; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub testAnyAttr | ||
| 413 | { | ||
| 414 | my $self = shift ; | ||
| 415 | my $Name ; | ||
| 416 | |||
| 417 | return 0 unless $self->{Attributes} ; | ||
| 418 | exists $self->{Attributes}->{$Name} and return 1 while $Name = shift ; | ||
| 419 | return 0 ; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub testAllAttr | ||
| 423 | { | ||
| 424 | my $self = shift ; | ||
| 425 | my $Name = shift ; | ||
| 426 | |||
| 427 | return 0 unless $Name and $self->{Attributes} and exists $self->{Attributes}->{$Name} ; | ||
| 428 | exists $self->{Attributes}->{$Name} or return 0 while $Name = shift ; | ||
| 429 | return 1 ; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub addAttr | ||
| 433 | { | ||
| 434 | my $self = shift ; | ||
| 435 | |||
| 436 | $_ and $self->{Attributes}->{$_} = undef foreach @_ ; | ||
| 437 | } | ||
| 438 | |||
| 439 | sub deleteAttr | ||
| 440 | { | ||
| 441 | my $self = shift ; | ||
| 442 | my $Name = shift ; | ||
| 443 | |||
| 444 | return 0 unless $Name and defined $self->{Attributes} ; | ||
| 445 | |||
| 446 | my $ret = 0 ; | ||
| 447 | |||
| 448 | while( $Name ) | ||
| 449 | { | ||
| 450 | delete $self->{Attributes}->{$Name},$ret++ if exists $self->{Attributes}->{$Name} ; | ||
| 451 | $Name = shift ; | ||
| 452 | } | ||
| 453 | |||
| 454 | return $ret ; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub trailer | ||
| 458 | { | ||
| 459 | my $self = shift; | ||
| 460 | |||
| 461 | $self->{Trailer} = shift if @_ ; | ||
| 462 | return $self->{Trailer} ; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub XMLClose | ||
| 466 | { | ||
| 467 | my $self = shift; | ||
| 468 | |||
| 469 | $self->{XMLClose} = shift if @_ ; | ||
| 470 | return $self->{XMLClose} ; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub env | ||
| 474 | { | ||
| 475 | return $_[0]->{ENV} ; | ||
| 476 | } | ||
| 477 | |||
| 478 | sub HTML | ||
| 479 | { | ||
| 480 | my $self = shift; | ||
| 481 | |||
| 482 | @{$self->{HTML}} = @_ if @_ ; | ||
| 483 | return( wantarray ? @{$self->{HTML}} : join( '',@{$self->{HTML}} ) ) ; | ||
| 484 | } | ||
| 485 | |||
| 486 | sub appendText | ||
| 487 | { | ||
| 488 | my $self = shift; | ||
| 489 | |||
| 490 | push @{$self->{HTML}},@_ if @_ ; | ||
| 491 | } | ||
| 492 | |||
| 493 | #------------------- | ||
| 494 | |||
| 495 | sub insert # InsertNode | ||
| 496 | { | ||
| 497 | my $Node = shift ; | ||
| 498 | my $Text = shift ; | ||
| 499 | my( $Child,$p,$a ) ; | ||
| 500 | |||
| 501 | if( $Node->{Name} ) | ||
| 502 | { | ||
| 503 | &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 504 | |||
| 505 | delete $Node->{HTML} ; # if $Text ; | ||
| 506 | |||
| 507 | if( ref $Node->{Attributes} eq 'HASH' ) | ||
| 508 | { | ||
| 509 | do | ||
| 510 | { | ||
| 511 | delete $Node->{tainted} ; | ||
| 512 | defined $Node->{Attributes}->{$_} and $Node->{Attributes}->{$_} = $Node->codeAttr($_) foreach keys %{$Node->{Attributes}} ; | ||
| 513 | } while( $Node->{tainted} ) ; | ||
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | push @{$Node->{HTML}},$Node->{Attributes} ? | ||
| 518 | join( ' ',"<$Node->{Name}",map( defined $Node->{Attributes}->{$_} ? (($p=$Node->{Attributes}->{$_} and $p=~m/"/s)?"$_=\'$p\'":"$_=\"$p\"") : $_, | ||
| 519 | keys %{$Node->{Attributes}} ) ).'>' : | ||
| 520 | "<$Node->{Name}>" ; | ||
| 521 | |||
| 522 | |||
| 523 | |||
| 524 | if( $Text ) | ||
| 525 | { | ||
| 526 | push @{$Node->{HTML}},$Text ; | ||
| 527 | } | ||
| 528 | else | ||
| 529 | { | ||
| 530 | push @{$Node->{HTML}},$Node->{Text} if $Node->{Text} ne '' ; | ||
| 531 | for( $Child=$Node->{FirstChild}; $Child; $Child=$Child->{Next} ) | ||
| 532 | { | ||
| 533 | push @{$Node->{HTML}},$Child->process() ; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | push @{$Node->{HTML}},"</$Node->{Name}>" if $Node->end() ; | ||
| 537 | } | ||
| 538 | else | ||
| 539 | { | ||
| 540 | push @{$Node->{HTML}},$Text,$Node->{Trailer} ; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | #Bearbeiten des Knotens | ||
| 545 | sub process() #ProcessNode | ||
| 546 | { | ||
| 547 | my $Node = shift ; | ||
| 548 | my $uhtml ; | ||
| 549 | |||
| 550 | if( $Node->{Name} ) | ||
| 551 | { | ||
| 552 | if( ref $uHTML::uTag{ $Node->{Name} } eq 'CODE' ) | ||
| 553 | { | ||
| 554 | ref eq 'CODE' and &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 555 | &{$uHTML::uTag{ $Node->{Name} }}( $Node ) ; | ||
| 556 | } | ||
| 557 | elsif( ref $uHTML::uSTag{ $Node->{Name} } eq 'CODE' ) | ||
| 558 | { | ||
| 559 | ref eq 'CODE' and &{$_}( $Node ) foreach @{$uHTML::uCode{ $Node->{Name} }} ; | ||
| 560 | $uhtml = &{$uHTML::uSTag{ $Node->{Name} }}( $Node,undef,undef ) ; | ||
| 561 | @{$Node->{HTML}} = uHTML::recode( $uhtml,$Node->{ENV} ) ; | ||
| 562 | } | ||
| 563 | else | ||
| 564 | { | ||
| 565 | $Node->insert() ; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | |||
| 569 | push @{$Node->{HTML}},$Node->{Trailer} if $Node->{Trailer} ne '' ; | ||
| 570 | return ref $Node->{HTML} eq 'ARRAY' ? @{$Node->{HTML}} : () ; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub map( $$ ) | ||
| 574 | { | ||
| 575 | my( $Node,$HeadText,$TailText ) = @_ ; | ||
| 576 | my( $T,$C,@HTML ) ; | ||
| 577 | |||
| 578 | if( $HeadText ne '' ) | ||
| 579 | { | ||
| 580 | for( $T = $C = uHTML::_struct( undef,$HeadText,$Node->{ENV} ) ; $C ; $C = $C->{Next} ) { push @HTML,$C->process() } | ||
| 581 | $C = $T, $T = $T->{Next}, $C->delete() while $T ; | ||
| 582 | } | ||
| 583 | |||
| 584 | push @HTML,$Node->{Text} ; | ||
| 585 | |||
| 586 | for( $C=$Node->{FirstChild}; $C; $C=$C->{Next} ) { push @HTML,$C->process() } ; | ||
| 587 | |||
| 588 | if( $TailText ne '' ) | ||
| 589 | { | ||
| 590 | for( $T = $C = uHTML::_struct( undef,$TailText,$Node->{ENV} ) ; $C ; $C = $C->{Next} ) { push @HTML,$C->process() } | ||
| 591 | $C = $T, $T = $T->{Next}, $C->delete() while $T ; | ||
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | return( $Node->{HTML} = \@HTML ) ; | ||
| 596 | } | ||
| 597 | |||
| 598 | sub copy() | ||
| 599 | { | ||
| 600 | my $Node = shift ; | ||
| 601 | my $CopyT = shift ; | ||
| 602 | my( $Copy,$Prev,$Child,$CC ) ; | ||
| 603 | |||
| 604 | $Copy = uHTMLnode->new( $Node->{Name},undef,undef,$Node->{ENV} ) ; | ||
| 605 | $Prev = undef ; | ||
| 606 | |||
| 607 | $Copy->{$_} = $Node->{$_} foreach qw( Text End XMLClose ) ; | ||
| 608 | $Copy->{Trailer} = $Node->{Trailer} if $CopyT ; | ||
| 609 | |||
| 610 | if( $Node->{Attributes} ) | ||
| 611 | { | ||
| 612 | my %Attributes = %{$Node->{Attributes}} ; | ||
| 613 | |||
| 614 | $Copy->{Attributes} = \%Attributes ; | ||
| 615 | } | ||
| 616 | |||
| 617 | for( $Child=$Node->{FirstChild}; $Child; $Child=$Child->{Next} ) | ||
| 618 | { | ||
| 619 | $Copy->appendChild( $Child->copy( 1 ) ) ; | ||
| 620 | } | ||
| 621 | |||
| 622 | return $Copy ; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub embed($) | ||
| 626 | { | ||
| 627 | my( $Node,$Name ) = @_ ; | ||
| 628 | |||
| 629 | my $ENode = uHTMLnode->new( $Name,'',undef,$Node->{ENV} ) ; | ||
| 630 | |||
| 631 | $ENode->{Parent} = $Node->{Parent} ; | ||
| 632 | $ENode->{Prev} = $Node->{Prev} ; | ||
| 633 | $ENode->{Next} = $Node->{Next} ; | ||
| 634 | $ENode->{FirstChild} = $Node ; | ||
| 635 | $ENode->{LastChild} = $Node ; | ||
| 636 | $ENode->{End} = 1 ; | ||
| 637 | $ENode->{Attributes} = undef ; | ||
| 638 | $ENode->{Trailer} = undef ; | ||
| 639 | |||
| 640 | $ENode->{Prev} ? ($ENode->{Prev}->{Next} = $ENode) : ($ENode->{Parent}->{FirstChild} = $ENode) ; | ||
| 641 | $ENode->{Next} ? ($ENode->{Next}->{Prev} = $ENode) : ($ENode->{Parent}->{LastChild} = $ENode) ; | ||
| 642 | |||
| 643 | $Node->{Parent} = $ENode ; | ||
| 644 | $Node->{Prev} = undef ; | ||
| 645 | $Node->{Next} = undef ; | ||
| 646 | |||
| 647 | return $ENode ; | ||
| 648 | } | ||
| 649 | |||
| 650 | sub prepend($) | ||
| 651 | { | ||
| 652 | my $self = shift ; | ||
| 653 | my $Node = shift ; | ||
| 654 | |||
| 655 | $Node->{Parent}->{FirstChild} = $Node->{Next} if $Node->{Parent} and $Node->{Parent}->{FirstChild} == $Node ; | ||
| 656 | $Node->{Parent}->{LastChild} = $Node->{Prev} if $Node->{Parent} and $Node->{Parent}->{LastChild} == $Node ; | ||
| 657 | $Node->{Prev}->{Next} = $Node->{Next} if $Node->{Prev} ; | ||
| 658 | $Node->{Next}->{Prev} = $Node->{Prev} if $Node->{Next} ; | ||
| 659 | |||
| 660 | $Node->{Parent} = $self->{Parent} ; | ||
| 661 | $self->{Parent}->{FirstChild} = $Node if $self->{Parent} and not $self->{Prev} ; | ||
| 662 | $Node->{Prev} = $self->{Prev} ; | ||
| 663 | $Node->{Next} = $self ; | ||
| 664 | $self->{Prev} = $Node ; | ||
| 665 | } | ||
| 666 | |||
| 667 | sub append($) | ||
| 668 | { | ||
| 669 | my $self = shift ; | ||
| 670 | my $Node = shift ; | ||
| 671 | |||
| 672 | $Node->{Parent}->{FirstChild} = $Node->{Next} if $Node->{Parent} and $Node->{Parent}->{FirstChild} == $Node ; | ||
| 673 | $Node->{Parent}->{LastChild} = $Node->{Prev} if $Node->{Parent} and $Node->{Parent}->{LastChild} == $Node ; | ||
| 674 | $Node->{Prev}->{Next} = $Node->{Next} if $Node->{Prev} ; | ||
| 675 | $Node->{Next}->{Prev} = $Node->{Prev} if $Node->{Next} ; | ||
| 676 | |||
| 677 | $Node->{Parent} = $self->{Parent} ; | ||
| 678 | $self->{Parent}->{LastChild} = $Node if $self->{Parent} and not $self->{Next} ; | ||
| 679 | $Node->{Prev} = $self ; | ||
| 680 | $Node->{Next} = $self->{Next} ; | ||
| 681 | $self->{Next} = $Node ; | ||
| 682 | $Node->{Trailer} .= $self->{Trailer} ; | ||
| 683 | $self->{Trailer} = '' ; | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | sub _close( $$ ) | ||
| 688 | { | ||
| 689 | my( $Node,$Name,$Tail ) = @_ ; | ||
| 690 | |||
| 691 | my( $p, $e ) ; | ||
| 692 | |||
| 693 | for( $p = $Node; $p and $p->{Name} ne $Name or $p->{XMLClose} or $p->{End} ; $p=$p->{Prev} ) {} ; | ||
| 694 | errorMsg( 0,"wrong close of $Name." ) and return undef unless $p ; | ||
| 695 | |||
| 696 | if( $p and $p->{Next} ) | ||
| 697 | { | ||
| 698 | $e = $p->{Next} ; | ||
| 699 | $p->{Next} = 0 ; | ||
| 700 | $p->{FirstChild} = $e ; | ||
| 701 | $p->{LastChild} = $Node ; | ||
| 702 | $e->{Prev} = 0 ; | ||
| 703 | while( $e ) | ||
| 704 | { | ||
| 705 | $e->{Parent} = $p ; | ||
| 706 | $e = $e->{Next} ; | ||
| 707 | } | ||
| 708 | } | ||
| 709 | else | ||
| 710 | { | ||
| 711 | $p = $Node ; | ||
| 712 | } | ||
| 713 | $p->{Text} = $p->{Trailer} ; | ||
| 714 | $p->{End} = 1 ; | ||
| 715 | $p->{Trailer} = $Tail ; | ||
| 716 | return $p ; | ||
| 717 | } | ||
| 718 | |||
| 719 | sub ParamCount | ||
| 720 | { | ||
| 721 | my $self = shift ; | ||
| 722 | |||
| 723 | return( ref $self->{PCount} eq 'ARRAY' ? $self->{PCount}->[-1] : '' ) ; | ||
| 724 | } | ||
| 725 | |||
| 726 | sub _callwrap | ||
| 727 | { | ||
| 728 | my( $Func,$Node,$AttrName,$Params ) = @_ ; | ||
| 729 | my @FParams = ($Params =~ m/([^,'"](?:\\,|[^,])*|'(?:\\'|[^'])*'|"(?:\\"|[^"])*"|)\s*,?/sg) ; | ||
| 730 | |||
| 731 | s/^\s*['"]?//s + s/['"]?\s*$//s foreach @FParams ; | ||
| 732 | push @{$Node->{PCount}},scalar( @FParams ) ; | ||
| 733 | return $uHTML::uAttr{$Func}( $Node,$AttrName,$Func,map((m/(?<!\\)\$(?=[a-zA-Z_])/s?codeAttr($Node,$AttrName,$_):$_),@FParams) ) if $uHTML::uAttr{$Func} ; | ||
| 734 | errorMsg( 0,"unknown variable $Func.") ; | ||
| 735 | return "\\\$$Func" ; | ||
| 736 | } | ||
| 737 | |||
| 738 | sub codeAttr | ||
| 739 | { | ||
| 740 | my $Node = shift ; | ||
| 741 | my $Attr = shift ; | ||
| 742 | my $Value = shift ; | ||
| 743 | |||
| 744 | return '' unless $Value or $Node->{Attributes} and ($Value = $Node->{Attributes}->{$Attr}) ne '' ; | ||
| 745 | |||
| 746 | my( $func,$par,$tail,$sub,$rsub ) ; | ||
| 747 | my @subs = split m/(?<!\\)\$(?=[a-zA-Z_\$])/s,$Value ; | ||
| 748 | while( $#subs > 0 ) | ||
| 749 | { | ||
| 750 | $rsub++ ; | ||
| 751 | next unless $sub = pop @subs ; | ||
| 752 | $sub =~ s/\\(?=\$)//s ; | ||
| 753 | |||
| 754 | ( $func,$par,$tail ) = ($sub =~ m/([a-zA-Z_][0-9a-zA-Z_]*)(?:\(\s*('(?:\\'|[^'])*'|"(?:\\"|[^"])*"|(?:\\.|[^()])*(?:\s*,(?:'(?:\\'|[^'])*'|"(?:\\"|[^"])*"|(?:\\.|[^()])))*)\s*\))?(.*)$/s) ; | ||
| 755 | $sub = _callwrap( $func,$Node,$Attr,$par ) . $tail ; | ||
| 756 | pop @{$Node->{PCount}} ; | ||
| 757 | |||
| 758 | $subs[$#subs] .= $sub ; | ||
| 759 | } | ||
| 760 | $subs[0] =~ s/\\(?!\\)//sg ; | ||
| 761 | |||
| 762 | return $subs[0] ; | ||
| 763 | } | ||
| 764 | |||
| 765 | sub attr | ||
| 766 | { | ||
| 767 | my $Node = shift ; | ||
| 768 | my $Attr = shift ; | ||
| 769 | my $Value = shift ; | ||
| 770 | return( defined $Value ? rawAttr($Node,$Attr,$Value) : codeAttr($Node,$Attr) ) ; | ||
| 771 | } | ||
| 772 | |||
| 773 | sub errorMsg | ||
| 774 | { | ||
| 775 | print STDERR "uHTML Error in $uHTML::FileName: $_[1]\n" if $_[2] or $uHTML::FileName ; | ||
| 776 | } | ||
| 777 | |||
| 778 | ########################################################## | ||
| 779 | ########################################################## | ||
| 780 | ########################################################## | ||
| 781 | |||
| 782 | package uHTML; | ||
| 783 | |||
| 784 | local( $uHTML::Pos,@uHTML::Blocks,%uHTML::uHTML,%uHTML::uTag,%uHTML::uAttr,%uHTML::uSTag,$uHTML::FileName ) ; | ||
| 785 | |||
| 786 | sub _checkName | ||
| 787 | { | ||
| 788 | my $Name = shift ; | ||
| 789 | my $Code = shift ; | ||
| 790 | |||
| 791 | $_->{$Name} and $_->{$Name} != $Code and return 0 foreach @_ ; | ||
| 792 | return 1 ; | ||
| 793 | } | ||
| 794 | |||
| 795 | sub registerTagCode( $$ ) | ||
| 796 | { | ||
| 797 | my( $TagName,$Code ) = @_ ; | ||
| 798 | |||
| 799 | push @{$uHTML::uCode{$TagName}},$Code if _checkName( $TagName,$Code ) ; | ||
| 800 | } | ||
| 801 | |||
| 802 | sub registerTag | ||
| 803 | { | ||
| 804 | my( $TagName,$Code,$nowarn ) = @_ ; | ||
| 805 | |||
| 806 | $uHTML::uTag{$TagName} = $Code if $nowarn or _checkName( $TagName,$Code,\%uHTML::uTag, \%uHTML::uSTag ) ; | ||
| 807 | } | ||
| 808 | |||
| 809 | sub registerAttrCode | ||
| 810 | { | ||
| 811 | my( $AttrName,$Code,$nowarn ) = @_ ; | ||
| 812 | |||
| 813 | registerVar( $AttrName,$Code ) if $nowarn or _checkName( $AttrName,$Code,\%uHTML::uAttr ) ; | ||
| 814 | } | ||
| 815 | |||
| 816 | sub registerVar | ||
| 817 | { | ||
| 818 | my( $AttrName,$Code,$nowarn ) = @_ ; | ||
| 819 | |||
| 820 | $uHTML::uAttr{$AttrName} = $Code if $nowarn or _checkName( $AttrName,$Code,\%uHTML::uAttr ) ; | ||
| 821 | } | ||
| 822 | |||
| 823 | sub register | ||
| 824 | { | ||
| 825 | my( $Name,$Code,$nowarn ) = @_ ; | ||
| 826 | |||
| 827 | $uHTML::uAttr{$Name} = $uHTML::uSTag{$Name} = $Code if $nowarn or _checkName( $Name,$Code,\%uHTML::uTag,\%uHTML::uAttr, \%uHTML::uSTag ) ; | ||
| 828 | } | ||
| 829 | |||
| 830 | sub tags() | ||
| 831 | { | ||
| 832 | return ( keys %uHTML::uTag ) ; | ||
| 833 | } | ||
| 834 | |||
| 835 | sub testTag | ||
| 836 | { | ||
| 837 | return( defined( $uHTML::uTag{$_[0]} ) or defined( $uHTML::uSTag{$_[0]} ) ? 1 : '' ) ; | ||
| 838 | } | ||
| 839 | |||
| 840 | sub vars() | ||
| 841 | { | ||
| 842 | return ( keys %uHTML::uAttr ) ; | ||
| 843 | } | ||
| 844 | |||
| 845 | sub testVar | ||
| 846 | { | ||
| 847 | return( defined( $uHTML::uAttr{$_[0]} ) || defined( $uHTML::uSTag{$_[0]} ) ? 1 : '' ) ; | ||
| 848 | } | ||
| 849 | |||
| 850 | sub fileStart | ||
| 851 | { | ||
| 852 | return unless $uHTML::FileName and $_[0] ; | ||
| 853 | push @uHTML::FNames,$uHTML::FileName ; | ||
| 854 | $uHTML::FileName = $_[0] ; | ||
| 855 | } | ||
| 856 | |||
| 857 | sub fileEnd | ||
| 858 | { | ||
| 859 | return $uHTML::FileName = pop @uHTML::FNames if $uHTML::FileName ; | ||
| 860 | } | ||
| 861 | |||
| 862 | sub _struct( $$$ ) | ||
| 863 | { | ||
| 864 | my( $Prev,$String,$env ) = @_ ; | ||
| 865 | my( $Struct,$New,$val,$par,$tag ) ; | ||
| 866 | |||
| 867 | if( ref $Prev eq 'uHTMLnode' ) | ||
| 868 | { | ||
| 869 | if( $Prev->{Trailer} and $Prev->{Trailer} =~ m/([^<]*)(<.*)/s ) | ||
| 870 | { | ||
| 871 | $String = $2 . $String ; | ||
| 872 | $Prev->{Trailer} = $1 ; | ||
| 873 | } | ||
| 874 | $Prev->{Trailer} .= $1 if $String =~ m/^([^<]*)/sgc ; | ||
| 875 | } | ||
| 876 | else | ||
| 877 | { | ||
| 878 | $Prev = uHTMLnode->new( '',($String =~ m/^([^<]*)/sgc ? $1 : ''),undef,$env ) ; | ||
| 879 | } | ||
| 880 | $Struct = $Prev ; | ||
| 881 | |||
| 882 | while( $tag = ($String =~ m/\G<(\/?[\w:-]+|[!?*])/sgc)[0] ) | ||
| 883 | { | ||
| 884 | if( $tag =~ m/^\w[\w:-]*$/s ) | ||
| 885 | { | ||
| 886 | my( %Attributes ) ; | ||
| 887 | |||
| 888 | while( $String =~ m/\G\s*(\w[\w:-]*)\s*(?:\=\s*("(?:[^"]|\\")*(?<!\\)"|'(?:[^']|\\')*(?<!\\)'|[^\s>]+))?/sgc ) | ||
| 889 | { | ||
| 890 | $par = $1 ; | ||
| 891 | $val = $2 ; | ||
| 892 | if( $par ) | ||
| 893 | { | ||
| 894 | if( $val ne '' ) | ||
| 895 | { | ||
| 896 | $val =~ s/^['"]|(?<!\\)\\|['"]$//sg ; | ||
| 897 | $Attributes{$par} .= $val ; | ||
| 898 | } | ||
| 899 | else | ||
| 900 | { | ||
| 901 | $Attributes{$par} = undef unless exists $Attributes{$par} ; | ||
| 902 | } | ||
| 903 | } | ||
| 904 | } | ||
| 905 | lc $tag eq 'style' || lc $tag eq 'svg' || lc $tag eq 'script' && !exists $Attributes{'src'} ? | ||
| 906 | $String =~ m/\G[^>]*?(\/)?>(.*?)(?=<\/$tag>)/sgci : | ||
| 907 | $String =~ m/\G[^>]*?(\/)?>([^<]*)/sgc ; | ||
| 908 | $New = uHTMLnode->new( $tag,$2,$Prev,$env ) ; | ||
| 909 | $New->attributes( \%Attributes ) if %Attributes ; | ||
| 910 | $New->XMLClose( $1 ) ; | ||
| 911 | } | ||
| 912 | elsif( $tag =~ m/^\/(\w[\w:-]*)$/s ) | ||
| 913 | { | ||
| 914 | $tag = $1 ; | ||
| 915 | $String =~ m/\G\s*\>([^<]*)/sgc ; | ||
| 916 | $Prev = $Prev->_close( $tag,$1,"wrong close of $tag." ) if $Prev ; | ||
| 917 | undef $New ; | ||
| 918 | } | ||
| 919 | elsif( $tag =~ m/^[!?]/ ) | ||
| 920 | { | ||
| 921 | if( $tag eq '!' and $String =~ m/\G--/sgc ) | ||
| 922 | { | ||
| 923 | $tag = '!--' ; | ||
| 924 | $String =~ m/\G(.*?-->[^<]*)/sgc ; | ||
| 925 | $val = $1 ; | ||
| 926 | } | ||
| 927 | else | ||
| 928 | { | ||
| 929 | $String =~ m/\G((?:[^'">]|"(?:[^"]|\\")*(?<!\\)"|'(?:[^']|\\')*(?<!\\)')*>[^<]*)/sgc ; | ||
| 930 | $val = $1 ; | ||
| 931 | } | ||
| 932 | $New = uHTMLnode->new( '',"<$tag$val",$Prev,$env ) ; | ||
| 933 | } | ||
| 934 | elsif( $tag eq '*' ) | ||
| 935 | { | ||
| 936 | $String =~ m/\G(.*?\*>[^<]*)/sgc ; | ||
| 937 | $New = uHTMLnode->new( '',"<*$1",$Prev,$env ) ; | ||
| 938 | } | ||
| 939 | else | ||
| 940 | { | ||
| 941 | $String =~ m/\G([^<]*)/sgc ; | ||
| 942 | $New = uHTMLnode->new( '',"$tag$1",$Prev,$env ) ; | ||
| 943 | } | ||
| 944 | if( $New ) | ||
| 945 | { | ||
| 946 | $Prev->{Next} = $New ; | ||
| 947 | $Prev = $Prev->{Next} ; | ||
| 948 | undef $New ; | ||
| 949 | } | ||
| 950 | } | ||
| 951 | |||
| 952 | $Prev->{Next} = uHTMLnode->new( '',$1,$Prev,$env ) if $String =~ m/\G(.+)/sg ; | ||
| 953 | return $Struct ; | ||
| 954 | |||
| 955 | } | ||
| 956 | |||
| 957 | sub parse | ||
| 958 | { | ||
| 959 | my( $data,$env ) = @_ ; | ||
| 960 | |||
| 961 | $env = \%ENV unless ref $env eq 'HASH' ; | ||
| 962 | return _struct( undef,$data,$env ) ; | ||
| 963 | } | ||
| 964 | |||
| 965 | sub recodedList( $$ ) | ||
| 966 | { | ||
| 967 | my( $uhtml,$env ) = @_ ; | ||
| 968 | my( @HTML,$node,$T ) ; | ||
| 969 | |||
| 970 | return undef if $uhtml eq '' ; | ||
| 971 | |||
| 972 | if( $uhtml =~ m/</ ) | ||
| 973 | { | ||
| 974 | loadModules( $env = \%ENV ) unless ref $env eq 'HASH' ; | ||
| 975 | for( $T = $node = _struct( undef,$uhtml,$env ) ; $node ; $node = $node->{Next} ) { push @HTML,$node->process() } | ||
| 976 | } | ||
| 977 | else | ||
| 978 | { | ||
| 979 | $HTML[0] = $uhtml ; | ||
| 980 | } | ||
| 981 | return \@HTML ; | ||
| 982 | } | ||
| 983 | |||
| 984 | sub recode( $$ ) | ||
| 985 | { | ||
| 986 | my( $uhtml,$env ) = @_ ; | ||
| 987 | return '' if $uhtml eq '' ; | ||
| 988 | my $HTML = recodedList( $uhtml,$env ) ; | ||
| 989 | return( wantarray ? @{$HTML} : join( '',@{$HTML} ) ) ; | ||
| 990 | } | ||
| 991 | |||
| 992 | ###################################### | ||
| 993 | ###################################### | ||
| 994 | |||
| 995 | sub loadModules( $ ) | ||
| 996 | { | ||
| 997 | my $env = shift ; | ||
| 998 | |||
| 999 | return unless ref $env eq 'HASH' ; | ||
| 1000 | |||
| 1001 | my $CPath ; | ||
| 1002 | |||
| 1003 | unless( $CPath = $env->{'SCRIPT_ROOT'} ) | ||
| 1004 | { | ||
| 1005 | $CPath = $0 =~ m%^/% ? $0 : $env->{'SCRIPT_FILENAME'} ; | ||
| 1006 | $CPath =~ s%/[^/]*$%%s ; | ||
| 1007 | $CPath = '.' unless $CPath ; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | return unless opendir DH,$CPath ; | ||
| 1011 | require "$CPath/$_" foreach sort grep -f "$CPath/$_", grep m/-uHTML\.pmc?$/, readdir DH ; | ||
| 1012 | return unless opendir DH,"$CPath/uHTML" ; | ||
| 1013 | require "$CPath/uHTML/$_" foreach sort grep -f "$CPath/uHTML/$_", grep m/\.pmc?$/, readdir DH ; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | 1; | ||
| 1017 | |||
| 1018 | __END__ |