Changeset 4931
- Timestamp:
- 09/15/09 06:36:40 (2 years ago)
- Location:
- branches/Release01x00/WysiwygPlugin
- Files:
-
- 3 edited
-
lib/Foswiki/Plugins/WysiwygPlugin.pm (modified) (1 diff)
-
test/unit/WysiwygPlugin/ExtendedTranslatorTests.pm (modified) (12 diffs)
-
test/unit/WysiwygPlugin/TranslatorTests.pm (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/Release01x00/WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin.pm
r4315 r4931 510 510 || ''; 511 511 } 512 return 0 unless $exclusions; 513 514 my $calls_ok = Foswiki::Func::getPreferencesValue('WYSIWYG_EDITABLE_CALLS') 515 || '---'; 516 $calls_ok =~ s/\s//g; 517 518 my $ok = 1; 519 if ( $exclusions =~ /calls/ 520 && $_[0] =~ /%((?!($calls_ok){)[A-Z_]+{.*?})%/s ) 521 { 522 print STDERR "WYSIWYG_DEBUG: has calls $1 (not in $calls_ok)\n" 523 if (WHY); 524 return "Text contains calls"; 525 } 526 if ( $exclusions =~ /(macros|variables)/ && $_[0] =~ /%([A-Z_]+)%/s ) { 527 print STDERR "$exclusions WYSIWYG_DEBUG: has macros $1\n" 528 if (WHY); 529 return "Text contains macros"; 530 } 531 if ( $exclusions =~ /html/ 532 && $_[0] =~ /<\/?((?!literal|verbatim|noautolink|nop|br)\w+)/ ) 533 { 534 print STDERR "WYSIWYG_DEBUG: has html: $1\n" 535 if (WHY); 536 return "Text contains HTML"; 537 } 538 if ( $exclusions =~ /comments/ && $_[0] =~ /<[!]--/ ) { 539 print STDERR "WYSIWYG_DEBUG: has comments\n" 540 if (WHY); 541 return "Text contains comments"; 542 } 543 if ( $exclusions =~ /pre/ && $_[0] =~ /<pre\w/ ) { 544 print STDERR "WYSIWYG_DEBUG: has pre\n" 545 if (WHY); 546 return "Text contains PRE"; 547 } 512 513 # Check for explicit exclusions before generic, non-configurable 514 # purely content-related reasons for exclusion 515 if ($exclusions) { 516 my $calls_ok = Foswiki::Func::getPreferencesValue('WYSIWYG_EDITABLE_CALLS') 517 || '---'; 518 $calls_ok =~ s/\s//g; 519 520 my $ok = 1; 521 if ( $exclusions =~ /calls/ 522 && $_[0] =~ /%((?!($calls_ok){)[A-Z_]+{.*?})%/s ) 523 { 524 print STDERR "WYSIWYG_DEBUG: has calls $1 (not in $calls_ok)\n" 525 if (WHY); 526 return "Text contains calls"; 527 } 528 if ( $exclusions =~ /(macros|variables)/ && $_[0] =~ /%([A-Z_]+)%/s ) { 529 print STDERR "$exclusions WYSIWYG_DEBUG: has macros $1\n" 530 if (WHY); 531 return "Text contains macros"; 532 } 533 if ( $exclusions =~ /html/ 534 && $_[0] =~ /<\/?((?!literal|verbatim|noautolink|nop|br)\w+)/ ) 535 { 536 print STDERR "WYSIWYG_DEBUG: has html: $1\n" 537 if (WHY); 538 return "Text contains HTML"; 539 } 540 if ( $exclusions =~ /comments/ && $_[0] =~ /<[!]--/ ) { 541 print STDERR "WYSIWYG_DEBUG: has comments\n" 542 if (WHY); 543 return "Text contains comments"; 544 } 545 if ( $exclusions =~ /pre/ && $_[0] =~ /<pre\w/ ) { 546 print STDERR "WYSIWYG_DEBUG: has pre\n" 547 if (WHY); 548 return "Text contains PRE"; 549 } 550 } 551 552 # Copy the content. 553 # Then crunch verbatim blocks, because verbatim blocks may contain *anything*. 554 my $text = $_[0]; 555 556 # Look for combinations of sticky and other markup that cause problems together 557 for my $tag ('verbatim', 'literal', keys %xmltag) { 558 while ($text =~ /<$tag\b[^>]*>(.*?)<\/$tag>/gsi) { 559 my $inner = $1; 560 if ($inner =~ /<sticky\b[^>]*>/i) { 561 print STDERR "WYSIWYG_DEBUG: <sticky> inside <$tag>\n" 562 if (WHY); 563 return "<sticky> inside <$tag>"; 564 } 565 } 566 } 567 568 my $wasAVerbatimTag = "\000verbatim\001"; 569 while ($text =~ s/<verbatim\b[^>]*>(.*?)<\/verbatim>/$wasAVerbatimTag/i) { 570 #my $content = $1; 571 # If there is any content that breaks conversion if it is inside a verbatim block, 572 # check for it here: 573 } 574 575 # Look for combinations of verbatim and other markup that cause problems together 576 for my $tag ('literal', keys %xmltag) { 577 while ($text =~ /<$tag\b[^>]*>(.*?)<\/$tag>/gsi) { 578 my $inner = $1; 579 if ($inner =~ /$wasAVerbatimTag/i) { 580 print STDERR "WYSIWYG_DEBUG: <verbatim> inside <$tag>\n" 581 if (WHY); 582 return "<verbatim> inside <$tag>"; 583 } 584 } 585 } 586 587 # Look for combinations of literal and other markup that cause problems together 588 for my $tag (keys %xmltag) { 589 while ($text =~ /<$tag\b[^>]*>(.*?)<\/$tag>/gsi) { 590 my $inner = $1; 591 if ($inner =~ /<literal\b[^>]*>/i) { 592 print STDERR "WYSIWYG_DEBUG: <literal> inside <$tag>\n" 593 if (WHY); 594 return "<literal> inside <$tag>"; 595 } 596 } 597 } 598 548 599 return 0; 549 600 } -
branches/Release01x00/WysiwygPlugin/test/unit/WysiwygPlugin/ExtendedTranslatorTests.pm
r4638 r4931 26 26 # 27 27 package ExtendedTranslatorTests; 28 use base qw( FoswikiTestCase);28 use base qw(TranslatorTests); 29 29 30 30 use strict; … … 36 36 # Bits for test type 37 37 # Fields in test records: 38 my $TML2HTML = 1 << 0; # test tml => html 39 my $HTML2TML = 1 << 1; # test html => finaltml (default tml) 40 my $ROUNDTRIP = 1 << 2; # test tml => => finaltml 38 my $TML2HTML = 1 << 0; # test tml => html 39 my $HTML2TML = 1 << 1; # test html => finaltml (default tml) 40 my $ROUNDTRIP = 1 << 2; # test tml => => finaltml 41 my $CANNOTWYSIWYG = 1 << 3; # test that notWysiwygEditable returns true 42 # and make the ROUNDTRIP test expect failure 41 43 42 44 # Note: ROUNDTRIP is *not* the same as the combination of … … 47 49 # of the TML, where TML -> HTML -> not quite TML -> HTML 48 50 # -> even worse TML, ad nauseum 51 # 52 # CANNOTWYSIWYG should normally be used in conjunction with ROUNDTRIP 53 # to ensure that notWysiwygEditable is consistent with this plugin's 54 # ROUNDTRIP capabilities. 55 # 56 # CANNOTWYSIWYG and ROUNDTRIP used together document the failure cases, 57 # i.e. they indicate TML that WysiwygPlugin cannot properly translate 58 # to HTML and back. When WysiwygPlugin is modified to support these 59 # cases, CANNOTWYSIWYG should be removed from each corresponding 60 # test case and nonWysiwygEditable should be updated so that the TML 61 # is "WysiwygEditable". 62 # 63 # Use CANNOTWYSIWYG without ROUNDTRIP *only* with an appropriate 64 # explanation. For example: 65 # Can't ROUNDTRIP this TML because perl on the SMURF platform 66 # automagically replaces all instances of 'blue' with 'beautiful'. 49 67 50 68 # Bit mask for selected test types 51 my $mask = $TML2HTML | $HTML2TML | $ROUNDTRIP ;69 my $mask = $TML2HTML | $HTML2TML | $ROUNDTRIP | $CANNOTWYSIWYG; 52 70 53 71 my $protecton = '<span class="WYSIWYG_PROTECTED">'; … … 64 82 # Each testcase is a subhash with fields as follows: 65 83 # exec => $TML2HTML to test TML -> HTML, $HTML2TML to test HTML -> TML, 66 # $ROUNDTRIP to test TML-> ->TML, all other bits are ignored. 84 # $ROUNDTRIP to test TML-> ->TML, $CANNOTWYSIWYG to test 85 # notWysiwygEditable, all other bits are ignored. 67 86 # They may be OR'd togoether to perform multiple tests. 68 87 # For example: $TML2HTML | $HTML2TML to test both … … 161 180 . '<customtag with="attributes"><br /> formatting > preserved<br /></customtag>' 162 181 . $protectoff . '</p>', 163 tml => << BLAH,182 tml => <<'BLAH', 164 183 <customtag with="attributes"> 165 184 formatting > preserved … … 183 202 . '</customtag>' 184 203 . $protectoff . '</p>', 185 tml => << BLAH,204 tml => <<'BLAH', 186 205 <customtag> 187 206 formatting > preserved … … 193 212 BLAH 194 213 }, 214 { 215 exec => $CANNOTWYSIWYG, 216 # Do not perform ROUNDTRIP on this TML, because ROUNDTRIP passes. 217 # The problem with this TML is that the special handling of 218 # <verbatim> in the conversion to HTML messes up the contents 219 # of this custom XML tag, so that the HTML is not representative 220 # of the TML in terms of intellectual content. 221 name => 'VerbatimInsideDot', 222 setup => sub { 223 Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'dot', 224 sub { 1 } ); 225 }, 226 tml => <<'DOT', 227 <dot> 228 digraph G { 229 open [label="<verbatim>"]; 230 content [label="Put arbitrary content here"]; 231 close [label="</verbatim>"]; 232 open -> content -> close; 233 } 234 </dot> 235 DOT 236 }, 237 { 238 exec => $TML2HTML | $ROUNDTRIP, 239 name => 'CustomtagInsideSticky', 240 setup => sub { 241 Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'customtag', 242 sub { 1 } ); 243 }, 244 tml => "<sticky><customtag>this & that\n > the other </customtag></sticky>", 245 html => '<p>' 246 . '<div class="WYSIWYG_STICKY">' 247 . '<customtag>' 248 . 'this & that<br /> > the other ' 249 . '</customtag>' 250 . '</div>' 251 . '</p>' 252 }, 253 { 254 exec => $ROUNDTRIP | $CANNOTWYSIWYG, #SMELL: fix this case 255 name => 'StickyInsideCustomtag', 256 setup => sub { 257 Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'customtag', 258 sub { 1 } ); 259 }, 260 tml => "<customtag>this <sticky>& that\n > the</sticky> other </customtag>", 261 html => '<p>' 262 . $protecton 263 . '<customtag>' 264 . 'this ' 265 . '<div class="WYSIWYG_STICKY">' 266 . '& that<br /> > the' 267 . '</div>' 268 . ' other ' 269 . '</customtag>' 270 . $protectoff 271 . '</p>' 272 }, 273 { 274 exec => $TML2HTML | $ROUNDTRIP, 275 name => 'StickyInsideUnspecifiedCustomtag', 276 setup => sub { 277 }, 278 tml => "<customtag>this <sticky>& that\n > the</sticky> other </customtag>", 279 html => '<p>' 280 . $protecton 281 . '<customtag>' 282 . $protectoff 283 . 'this' 284 . '<div class="WYSIWYG_STICKY">' 285 . '& that<br /> > the' 286 . '</div>' 287 . 'other' 288 . $protecton 289 . '</customtag>' 290 . $protectoff 291 . '</p>' 292 }, 293 { 294 exec => $ROUNDTRIP, 295 name => 'UnspecifiedCustomtagInsideSticky', 296 setup => sub { 297 }, 298 tml => "<sticky><customtag>this & that\n > the other </customtag></sticky>" 299 }, 300 { 301 exec => $TML2HTML | $ROUNDTRIP, 302 name => 'CustomtagInsideLiteral', 303 setup => sub { 304 Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'customtag', 305 sub { 1 } ); 306 }, 307 tml => '<literal><customtag>this & that > the other </customtag></literal>', 308 html => '<p>' 309 . '<div class="WYSIWYG_LITERAL">' 310 . '<customtag>this & that > the other </customtag>' 311 . '</div>' 312 . '</p>' 313 }, 314 { 315 exec => $TML2HTML | $ROUNDTRIP, 316 name => 'UnspecifiedCustomtagInsideLiteral', 317 setup => sub { 318 }, 319 tml => '<literal><customtag>this & that > the other </customtag></literal>', 320 html => '<p>' 321 . '<div class="WYSIWYG_LITERAL">' 322 . '<customtag>this & that > the other </customtag>' 323 . '</div>' 324 . '</p>' 325 }, 326 { 327 exec => $ROUNDTRIP | $CANNOTWYSIWYG, #SMELL: Fix this case 328 name => 'LiteralInsideCustomtag', 329 setup => sub { 330 Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'customtag', 331 sub { 1 } ); 332 }, 333 tml => '<customtag>this <literal>& that > the</literal> other </customtag>', 334 html => '<p>' 335 . '<div class="WYSIWYG_LITERAL">' 336 . '<customtag>this & that > the other </customtag>' 337 . '</div>' 338 . '</p>' 339 }, 340 { 341 exec => $TML2HTML | $ROUNDTRIP, 342 name => 'LiteralInsideUnspecifiedCustomtag', 343 setup => sub { 344 }, 345 tml => '<customtag>this <literal>& that > the</literal> other </customtag>', 346 html => '<p>' 347 . $protecton 348 . '<customtag>' 349 . $protectoff 350 . 'this' 351 . '<div class="WYSIWYG_LITERAL">' 352 . '& that > the' 353 . '</div>' 354 .'other' 355 . $protecton 356 . '</customtag>' 357 . $protectoff 358 . '</p>' 359 }, 195 360 ]; 196 361 … … 205 370 my $fn = 'ExtendedTranslatorTests::testTML2HTML_' . $datum->{name}; 206 371 no strict 'refs'; 207 *$fn = sub { my $this = shift; $this->compareTML_HTML($datum) }; 372 *$fn = sub { 373 my $this = shift; 374 $this->testSpecificSetup($datum); 375 $this->compareTML_HTML($datum); 376 $this->testSpecificCleanup($datum); 377 }; 208 378 use strict 'refs'; 209 379 } … … 211 381 my $fn = 'ExtendedTranslatorTests::testHTML2TML_' . $datum->{name}; 212 382 no strict 'refs'; 213 *$fn = sub { my $this = shift; $this->compareHTML_TML($datum) }; 383 *$fn = sub { 384 my $this = shift; 385 $this->testSpecificSetup($datum); 386 $this->compareHTML_TML($datum); 387 $this->testSpecificCleanup($datum); 388 }; 214 389 use strict 'refs'; 215 390 } … … 217 392 my $fn = 'ExtendedTranslatorTests::testROUNDTRIP_' . $datum->{name}; 218 393 no strict 'refs'; 219 *$fn = sub { my $this = shift; $this->compareRoundTrip($datum) }; 394 *$fn = sub { 395 my $this = shift; 396 $this->testSpecificSetup($datum); 397 $this->compareRoundTrip($datum); 398 $this->testSpecificCleanup($datum); 399 }; 400 use strict 'refs'; 401 } 402 if ( ( $mask & $datum->{exec} ) & $CANNOTWYSIWYG ) { 403 my $fn = 'TranslatorTests::testCANNOTWYSIWYG_' . $datum->{name}; 404 no strict 'refs'; 405 *$fn = sub { 406 my $this = shift; 407 $this->testSpecificSetup($datum); 408 $this->compareNotWysiwygEditable($datum); 409 $this->testSpecificCleanup($datum); 410 }; 220 411 use strict 'refs'; 221 412 } … … 223 414 } 224 415 225 sub set_up { 226 my $this = shift; 227 $this->SUPER::set_up(@_); 228 $Foswiki::cfg{Plugins}{WysiwygPlugin}{Enabled} = 1; 229 230 my $query; 231 eval { 232 require Unit::Request; 233 require Unit::Response; 234 $query = new Unit::Request(""); 235 }; 236 if ($@) { 237 $query = new CGI(""); 238 } 239 $query->path_info("/Current/TestTopic"); 240 $this->{session} = new Foswiki( undef, $query ); 241 $Foswiki::Plugins::SESSION = $this->{session}; 242 } 243 244 sub normaliseEntities { 245 my $text = shift; 246 247 # Convert text entities to &# representation 248 $text =~ s/(&\w+;)/'&#'.ord(HTML::Entities::decode_entities($1)).';'/ge; 249 return $text; 250 } 251 252 sub compareTML_HTML { 416 sub testSpecificSetup { 253 417 my ( $this, $args ) = @_; 254 255 my $page =256 $this->{session}->getScriptUrl( 1, 'view', 'Current', 'TestTopic' );257 $page =~ s/\/Current\/TestTopic.*$//;258 my $html = $args->{html} || '';259 $html =~ s/%!page!%/$page/g;260 my $finaltml = $args->{finaltml} || '';261 $finaltml =~ s/%!page!%/$page/g;262 my $tml = $args->{tml} || '';263 $tml =~ s/%!page!%/$page/g;264 265 418 # Reset the extendable parts of WysiwygPlugin 266 419 %Foswiki::Plugins::WysiwygPlugin::xmltag = (); … … 269 422 # Test-specific setup 270 423 if ( exists $args->{setup} ) { 271 $args->{setup}->(); 272 } 273 274 # convert to HTML 275 my $txer = new Foswiki::Plugins::WysiwygPlugin::TML2HTML(); 276 my $tx = $txer->convert( 277 $tml, 278 { 279 web => 'Current', 280 topic => 'TestTopic', 281 getViewUrl => \&Foswiki::Plugins::WysiwygPlugin::getViewUrl, 282 expandVarsInURL => 283 \&Foswiki::Plugins::WysiwygPlugin::expandVarsInURL, 284 xmltag => \%Foswiki::Plugins::WysiwygPlugin::xmltag, 285 } 286 ); 287 288 # Test-specific cleanup 289 if ( exists $args->{cleanup} ) { 290 $args->{cleanup}->(); 291 } 292 293 $this->assert_html_equals( $html, $tx ); 294 } 295 296 sub compareRoundTrip { 297 my ( $this, $args ) = @_; 298 my $page = 299 $this->{session}->getScriptUrl( 1, 'view', 'Current', 'TestTopic' ); 300 $page =~ s/\/Current\/TestTopic.*$//; 301 302 my $tml = $args->{tml} || ''; 303 $tml =~ s/%!page!%/$page/g; 304 305 # Reset the extendable parts of WysiwygPlugin 306 %Foswiki::Plugins::WysiwygPlugin::xmltag = (); 307 %Foswiki::Plugins::WysiwygPlugin::xmltagPlugin = (); 308 309 # Test-specific setup 310 if ( exists $args->{setup} ) { 311 $args->{setup}->(); 312 } 313 314 # convert to HTML 315 my $txer = new Foswiki::Plugins::WysiwygPlugin::TML2HTML(); 316 my $html = $txer->convert( 317 $tml, 318 { 319 web => 'Current', 320 topic => 'TestTopic', 321 getViewUrl => \&Foswiki::Plugins::WysiwygPlugin::getViewUrl, 322 expandVarsInURL => 323 \&Foswiki::Plugins::WysiwygPlugin::expandVarsInURL, 324 xmltag => \%Foswiki::Plugins::WysiwygPlugin::xmltag, 325 } 326 ); 327 328 # convert back to TML 329 $txer = new Foswiki::Plugins::WysiwygPlugin::HTML2TML(); 330 my $tx = $txer->convert( 331 $html, 332 { 333 web => 'Current', 334 topic => 'TestTopic', 335 convertImage => \&convertImage, 336 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 337 } 338 ); 339 340 # Test-specific cleanup 341 if ( exists $args->{cleanup} ) { 342 $args->{cleanup}->(); 343 } 344 345 my $finaltml = $args->{finaltml} || $tml; 346 $finaltml =~ s/%!page!%/$page/g; 347 $this->_assert_tml_equals( $finaltml, $tx, $args->{name} ); 348 } 349 350 sub compareHTML_TML { 351 my ( $this, $args ) = @_; 352 353 my $page = 354 $this->{session}->getScriptUrl( 1, 'view', 'Current', 'TestTopic' ); 355 $page =~ s/\/Current\/TestTopic.*$//; 356 my $html = $args->{html} || ''; 357 $html =~ s/%!page!%/$page/g; 358 my $tml = $args->{tml} || ''; 359 $tml =~ s/%!page!%/$page/g; 360 my $finaltml = $args->{finaltml} || $tml; 361 $finaltml =~ s/%!page!%/$page/g; 362 363 # Reset the extendable parts of WysiwygPlugin 364 %Foswiki::Plugins::WysiwygPlugin::xmltag = (); 365 %Foswiki::Plugins::WysiwygPlugin::xmltagPlugin = (); 366 367 # Test-specific setup 368 if ( exists $args->{setup} ) { 369 $args->{setup}->(); 370 } 371 372 # convert to TML 373 my $txer = new Foswiki::Plugins::WysiwygPlugin::HTML2TML(); 374 my $tx = $txer->convert( 375 $html, 376 { 377 web => 'Current', 378 topic => 'TestTopic', 379 convertImage => \&convertImage, 380 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 381 } 382 ); 383 384 # Test-specific cleanup 385 if ( exists $args->{cleanup} ) { 386 $args->{cleanup}->(); 387 } 388 389 $this->_assert_tml_equals( $finaltml, $tx, $args->{name} ); 390 } 391 392 sub encode { 393 my $s = shift; 394 395 # used for debugging odd chars 396 # $s =~ s/([\000-\037])/'#'.ord($1)/ge; 397 return $s; 398 } 399 400 sub _assert_tml_equals { 401 my ( $this, $expected, $actual, $name ) = @_; 402 $expected ||= ''; 403 $actual ||= ''; 404 $actual =~ s/\n$//s; 405 $expected =~ s/\n$//s; 406 unless ( $expected eq $actual ) { 407 my $expl = 408 "==$name== Expected TML:\n" 409 . encode($expected) 410 . "\n==$name== Actual TML:\n" 411 . encode($actual) 412 . "\n==$name==\n"; 413 my $i = 0; 414 while ( $i < length($expected) && $i < length($actual) ) { 415 my $e = substr( $expected, $i, 1 ); 416 my $a = substr( $actual, $i, 1 ); 417 if ( $a ne $e ) { 418 $expl .= "<<==== HERE actual "; 419 $expl .= ord($a) . " != expected " . ord($e) . "\n"; 420 last; 421 } 422 $expl .= $a; 423 $i++; 424 } 425 $this->assert( 0, $expl . "\n" ); 424 $args->{setup}->($this); 426 425 } 427 426 } 428 427 429 sub convertImage { 430 my $url = shift; 431 432 if ( $url eq "test_image" ) { 433 return '%TRANSLATEDIMAGE%'; 428 sub testSpecificCleanup { 429 my ( $this, $args ) = @_; 430 if ( exists $args->{cleanup} ) { 431 $args->{cleanup}->($this); 434 432 } 435 433 } 436 434 435 sub TML_HTMLconverterOptions 436 { 437 my $this = shift; 438 my $options = $this->SUPER::TML_HTMLconverterOptions(@_); 439 $options->{xmltag} = \%Foswiki::Plugins::WysiwygPlugin::xmltag; 440 return $options; 441 } 442 437 443 gen_compare_tests(); 438 444 -
branches/Release01x00/WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm
r4434 r4931 36 36 # Bits for test type 37 37 # Fields in test records: 38 my $TML2HTML = 1 << 0; # test tml => html 39 my $HTML2TML = 1 << 1; # test html => finaltml (default tml) 40 my $ROUNDTRIP = 1 << 2; # test tml => => finaltml 38 my $TML2HTML = 1 << 0; # test tml => html 39 my $HTML2TML = 1 << 1; # test html => finaltml (default tml) 40 my $ROUNDTRIP = 1 << 2; # test tml => => finaltml 41 my $CANNOTWYSIWYG = 1 << 3; # test that notWysiwygEditable returns true 42 # and make the ROUNDTRIP test expect failure 41 43 42 44 # Note: ROUNDTRIP is *not* the same as the combination of 43 45 # HTML2TML and TML2HTML. The HTML and TML comparisons are both 44 # somewhat "flexible". This is necess ry because, for example,46 # somewhat "flexible". This is necessary because, for example, 45 47 # the nature of whitespace in the TML may change. 46 48 # ROUNDTRIP tests are intended to isolate gradual degradation 47 49 # of the TML, where TML -> HTML -> not quite TML -> HTML 48 50 # -> even worse TML, ad nauseum 51 # 52 # CANNOTWYSIWYG should normally be used in conjunction with ROUNDTRIP 53 # to ensure that notWysiwygEditable is consistent with this plugin's 54 # ROUNDTRIP capabilities. 55 # 56 # CANNOTWYSIWYG and ROUNDTRIP used together document the failure cases, 57 # i.e. they indicate TML that WysiwygPlugin cannot properly translate 58 # to HTML and back. When WysiwygPlugin is modified to support these 59 # cases, CANNOTWYSIWYG should be removed from each corresponding 60 # test case and nonWysiwygEditable should be updated so that the TML 61 # is "WysiwygEditable". 62 # 63 # Use CANNOTWYSIWYG without ROUNDTRIP *only* with an appropriate 64 # explanation. For example: 65 # Can't ROUNDTRIP this TML because perl on the SMURF platform 66 # automagically replaces all instances of 'blue' with 'beautiful'. 49 67 50 68 # Bit mask for selected test types 51 my $mask = $TML2HTML | $HTML2TML | $ROUNDTRIP ;69 my $mask = $TML2HTML | $HTML2TML | $ROUNDTRIP | $CANNOTWYSIWYG; 52 70 53 71 my $protecton = '<span class="WYSIWYG_PROTECTED">'; … … 64 82 # Each testcase is a subhash with fields as follows: 65 83 # exec => $TML2HTML to test TML -> HTML, $HTML2TML to test HTML -> TML, 66 # $ROUNDTRIP to test TML-> ->TML, all other bits are ignored. 84 # $ROUNDTRIP to test TML-> ->TML, $CANNOTWYSIWYG to test 85 # notWysiwygEditable, all other bits are ignored. 67 86 # They may be OR'd togoether to perform multiple tests. 68 87 # For example: $TML2HTML | $HTML2TML to test both … … 1631 1650 BLAH 1632 1651 tml => "<sticky>Oranges</sticky>\n\n<sticky>Apples</sticky>" 1652 }, 1653 { 1654 exec => $ROUNDTRIP | $CANNOTWYSIWYG, # SMELL: Fix this case 1655 name => 'stickyInsideVerbatimItem1980', 1656 tml => <<'GLUED', 1657 <verbatim><sticky>banana</sticky></verbatim> 1658 GLUED 1659 html => <<'BLAH', 1660 <p> 1661 <pre class="TMLverbatim"><sticky>banana</sticky></pre> 1662 </p> 1663 BLAH 1664 }, 1665 { 1666 exec => $ROUNDTRIP, 1667 name => 'literalInsideVerbatimItem1980', 1668 tml => <<'GLUED', 1669 <verbatim><literal><font color="blue"> *|B|* </font></literal></verbatim> 1670 GLUED 1671 }, 1672 { 1673 exec => $ROUNDTRIP | $CANNOTWYSIWYG, 1674 name => 'verbatimInsideLiteralItem1980', 1675 tml => <<'GLUED', 1676 <literal><font color="blue"> *|B|*<verbatim>%H%</verbatim> </font></literal> 1677 GLUED 1678 }, 1679 { 1680 exec => $TML2HTML | $ROUNDTRIP, 1681 name => 'verbatimInsideStickyItem1980', 1682 tml => <<'GLUED', 1683 <sticky><font color="blue"> *|B|*<verbatim>%H%</verbatim> </font></sticky> 1684 GLUED 1685 html => <<'STUCK' 1686 <p> 1687 <div class="WYSIWYG_STICKY"><verbatim><font color="blue"> *|B|* </font></verbatim></div> 1688 </p> 1689 STUCK 1690 }, 1691 { 1692 exec => $TML2HTML | $ROUNDTRIP, 1693 name => 'literalInsideSticky', 1694 tml => <<'GLUED', 1695 <sticky><literal><font color="blue"> *|B|* </font></literal></sticky> 1696 GLUED 1697 html => <<'STUCK' 1698 <p> 1699 <div class="WYSIWYG_STICKY"><literal><font color="blue"> *|B|* </font></literal></div> 1700 </p> 1701 STUCK 1702 }, 1703 { 1704 exec => $ROUNDTRIP | $CANNOTWYSIWYG, 1705 name => 'stickyInsideLiteral', 1706 tml => <<'GLUED', 1707 <literal><sticky><font color="blue"> *|B|* </font></sticky/></literal> 1708 GLUED 1633 1709 }, 1634 1710 { … … 1953 2029 no strict 'refs'; 1954 2030 *$fn = sub { my $this = shift; $this->compareRoundTrip($datum) }; 2031 use strict 'refs'; 2032 } 2033 if ( ( $mask & $datum->{exec} ) & $CANNOTWYSIWYG ) { 2034 my $fn = 'TranslatorTests::testCANNOTWYSIWYG_' . $datum->{name}; 2035 no strict 'refs'; 2036 *$fn = sub { my $this = shift; $this->compareNotWysiwygEditable($datum) }; 1955 2037 use strict 'refs'; 1956 2038 } … … 2012 2094 } 2013 2095 2096 sub TML_HTMLconverterOptions 2097 { 2098 my $this = shift; 2099 return { 2100 web => 'Current', 2101 topic => 'TestTopic', 2102 convertImage => \&convertImage, 2103 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 2104 }; 2105 } 2106 2014 2107 sub compareTML_HTML { 2015 2108 my ( $this, $args ) = @_; … … 2025 2118 $tml =~ s/%!page!%/$page/g; 2026 2119 2120 my $notEditable = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable( $tml ); 2121 $this->assert(!$notEditable, $notEditable); 2122 2027 2123 my $txer = new Foswiki::Plugins::WysiwygPlugin::TML2HTML(); 2028 2124 my $tx = $txer->convert( 2029 2125 $tml, 2030 { 2031 web => 'Current', 2032 topic => 'TestTopic', 2033 getViewUrl => \&Foswiki::Plugins::WysiwygPlugin::getViewUrl, 2034 expandVarsInURL => 2035 \&Foswiki::Plugins::WysiwygPlugin::expandVarsInURL, 2036 } 2126 $this->TML_HTMLconverterOptions() 2037 2127 ); 2038 2128 … … 2040 2130 } 2041 2131 2042 sub compare RoundTrip{2132 sub compareNotWysiwygEditable { 2043 2133 my ( $this, $args ) = @_; 2134 2044 2135 my $page = 2045 2136 $this->{session}->getScriptUrl( 1, 'view', 'Current', 'TestTopic' ); 2046 2137 $page =~ s/\/Current\/TestTopic.*$//; 2138 my $html = $args->{html} || ''; 2139 $html =~ s/%!page!%/$page/g; 2140 my $finaltml = $args->{finaltml} || ''; 2141 $finaltml =~ s/%!page!%/$page/g; 2142 my $tml = $args->{tml} || ''; 2143 $tml =~ s/%!page!%/$page/g; 2144 2145 my $notEditable = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable( $tml, '' ); 2146 $this->assert($notEditable, "This TML should not be wysiwyg-editable: $tml"); 2147 } 2148 2149 sub compareRoundTrip { 2150 my ( $this, $args ) = @_; 2151 2152 my $page = 2153 $this->{session}->getScriptUrl( 1, 'view', 'Current', 'TestTopic' ); 2154 $page =~ s/\/Current\/TestTopic.*$//; 2047 2155 2048 2156 my $tml = $args->{tml} || ''; … … 2052 2160 my $html = $txer->convert( 2053 2161 $tml, 2054 { 2055 web => 'Current', 2056 topic => 'TestTopic', 2057 getViewUrl => \&Foswiki::Plugins::WysiwygPlugin::getViewUrl, 2058 expandVarsInURL => 2059 \&Foswiki::Plugins::WysiwygPlugin::expandVarsInURL, 2060 } 2162 $this->TML_HTMLconverterOptions() 2061 2163 ); 2062 2164 … … 2064 2166 my $tx = $txer->convert( 2065 2167 $html, 2066 { 2067 web => 'Current', 2068 topic => 'TestTopic', 2069 convertImage => \&convertImage, 2070 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 2071 } 2168 $this->HTML_TMLconverterOptions() 2072 2169 ); 2073 2170 my $finaltml = $args->{finaltml} || $tml; 2074 2171 $finaltml =~ s/%!page!%/$page/g; 2075 $this->_assert_tml_equals( $finaltml, $tx, $args->{name} ); 2172 2173 my $notEditable = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable( $tml, '' ); 2174 if ( ( $mask & $args->{exec} ) & $CANNOTWYSIWYG ) { 2175 $this->assert($notEditable, "This TML should not be wysiwyg-editable: $tml"); 2176 # Expect that roundtrip is not possible if notWysiwygEditable returns true. 2177 # notWysiwygEditable should not return false for anything that *can* be 2178 # roundtripped. 2179 $this->_assert_tml_not_equals( $finaltml, $tx, $args->{name} ); 2180 } 2181 else { 2182 $this->_assert_tml_equals( $finaltml, $tx, $args->{name} ); 2183 $this->assert(!$notEditable, "$args->{name} TML is wysiwyg-editable, but notWysiwygEditable() reports: $notEditable"); 2184 } 2185 2186 } 2187 2188 sub HTML_TMLconverterOptions 2189 { 2190 my $this = shift; 2191 return { 2192 web => 'Current', 2193 topic => 'TestTopic', 2194 convertImage => \&convertImage, 2195 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 2196 }; 2076 2197 } 2077 2198 … … 2092 2213 my $tx = $txer->convert( 2093 2214 $html, 2094 { 2095 web => 'Current', 2096 topic => 'TestTopic', 2097 convertImage => \&convertImage, 2098 rewriteURL => \&Foswiki::Plugins::WysiwygPlugin::postConvertURL, 2099 } 2215 $this->HTML_TMLconverterOptions() 2100 2216 ); 2101 2217 $this->_assert_tml_equals( $finaltml, $tx, $args->{name} ); … … 2139 2255 } 2140 2256 2257 sub _assert_tml_not_equals { 2258 my ( $this, $expected, $actual, $name ) = @_; 2259 $expected ||= ''; 2260 $actual ||= ''; 2261 $actual =~ s/\n$//s; 2262 $expected =~ s/\n$//s; 2263 if ( $expected eq $actual ) { 2264 my $expl = 2265 "==$name== Actual TML unexpectedly correct, remove \$CANNOTWYSIWYG flag:\n" 2266 . encode($actual) 2267 . "\n==$name==\n"; 2268 $this->assert( 0, $expl . "\n" ); 2269 } 2270 } 2271 2141 2272 sub convertImage { 2142 2273 my $url = shift;
Note: See TracChangeset
for help on using the changeset viewer.
