Changeset 28762
- Timestamp:
- 06/17/2014 05:56:08 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r28761 r28762 25 25 * 26 26 * @param string $text The text to be formatted 27 * @param bool $reset Set to true for unit testing. Translated patterns will reset. 27 28 * @return string The string replaced with html entities 28 29 */ 29 function wptexturize($text ) {30 function wptexturize($text, $reset = false) { 30 31 global $wp_cockneyreplace; 31 32 static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements, 32 33 $default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true; 33 34 34 if ( false === $run_texturize ) { 35 // If there's nothing to do, just stop. 36 if ( empty( $text ) || false === $run_texturize ) { 35 37 return $text; 36 38 } 37 39 38 // No need to set up these static variables more than once39 if ( ! isset( $static_characters ) ) {40 // Set up static variables. Run once only. 41 if ( $reset || ! isset( $static_characters ) ) { 40 42 /** 41 43 * Filter whether to skip running `wptexturize()`. … … 124 126 } 125 127 126 // Apostrophe in a word. No spaces or double apostrophes.128 // Apostrophe in a word. No spaces, double apostrophes, or other punctuation. 127 129 if ( "'" != $apos ) { 128 $dynamic[ '/(?<!' . $spaces . ')\'(?!\Z| \'|' . $spaces . ')/' ] = $apos;130 $dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;"\'(){}<>[\]\-]|' . $spaces . ')/' ] = $apos; 129 131 } 130 132 … … 149 151 } 150 152 151 // Single quotes followed by spaces or a period.152 if ( "'" != =$closing_single_quote ) {153 $dynamic[ '/\'(?=\Z| \.|' . $spaces . ')/' ] = $closing_single_quote;153 // Single quotes followed by spaces or ending punctuation. 154 if ( "'" != $closing_single_quote ) { 155 $dynamic[ '/\'(?=\Z|[.,)}>\-\]]|' . $spaces . ')/' ] = $closing_single_quote; 154 156 } 155 157 … … 162 164 $dynamic_characters = array_keys( $dynamic ); 163 165 $dynamic_replacements = array_values( $dynamic ); 164 }165 166 // If there's nothing to do, just stop.167 if ( empty( $text ) ) {168 return $text;169 166 } 170 167 -
trunk/tests/phpunit/tests/formatting/WPTexturize.php
r28761 r28762 190 190 $nbsp = "\xC2\xA0"; 191 191 192 $this->assertEquals( ' – ', wptexturize( ' - ' ) ); 192 $this->assertEquals( ' – ', wptexturize( ' - ' ) ); 193 193 $this->assertEquals( ' – ', wptexturize( ' - ' ) ); 194 194 $this->assertEquals( ' – ', wptexturize( ' - ' ) ); … … 198 198 $this->assertEquals( "$nbsp– ", wptexturize( "$nbsp- ") ); 199 199 200 $this->assertEquals( ' — ', wptexturize( ' -- ' ) ); 200 $this->assertEquals( ' — ', wptexturize( ' -- ' ) ); 201 201 $this->assertEquals( ' — ', wptexturize( ' -- ' ) ); 202 202 $this->assertEquals( ' — ', wptexturize( ' -- ' ) ); … … 725 725 ), 726 726 array( 727 "word word', she said", 728 "word word’, she said", 729 ), 730 array( 727 731 "word word'", 728 732 "word word’", … … 1363 1367 ); 1364 1368 } 1369 1370 /** 1371 * Make sure translation actually works. 1372 * 1373 * Also make sure apostrophes and closing quotes aren't being confused by default. 1374 * 1375 * @ticket 27426 1376 * @dataProvider data_tag_avoidance 1377 */ 1378 function test_translate( $input, $output ) { 1379 add_filter( 'gettext_with_context', array( $this, 'filter_translate' ), 10, 4 ); 1380 1381 $result = wptexturize( $input, true ); 1382 1383 remove_filter( 'gettext_with_context', array( $this, 'filter_translate' ), 10, 4 ); 1384 wptexturize( 'reset', true ); 1385 1386 return $this->assertEquals( $output, $result ); 1387 } 1388 1389 function filter_translate( $translations, $text, $context, $domain ) { 1390 switch ($text) { 1391 case '–' : return '!endash!'; 1392 case '—' : return '!emdash!'; 1393 case '‘' : return '!openq1!'; 1394 case '’' : 1395 if ( 'apostrophe' == $context ) { 1396 return '!apos!'; 1397 } else { 1398 return '!closeq1!'; 1399 } 1400 case '“' : return '!openq2!'; 1401 case '”' : return '!closeq2!'; 1402 case '′' : return '!prime1!'; 1403 case '″' : return '!prime2!'; 1404 default : return $translations; 1405 } 1406 } 1407 1408 function data_translate() { 1409 return array( 1410 array( 1411 "word '99 word", 1412 "word !apos!99 word", 1413 ), 1414 array( 1415 "word'99 word", 1416 "word!apos!99 word", 1417 ), 1418 array( 1419 "word 'test sentence' word", 1420 "word !openq1!test sentence!closeq1! word", 1421 ), 1422 array( 1423 "'test sentence'", 1424 "!openq1!test sentence!closeq1!", 1425 ), 1426 array( 1427 'word "test sentence" word', 1428 'word !openq2!test sentence!closeq2! word', 1429 ), 1430 array( 1431 '"test sentence"', 1432 '!openq2!test sentence!closeq2!', 1433 ), 1434 array( 1435 "word 'word word", 1436 "word !openq1!word word", 1437 ), 1438 array( 1439 "word ('word word", 1440 "word (!openq1!word word", 1441 ), 1442 array( 1443 "word ['word word", 1444 "word [!openq1!word word", 1445 ), 1446 array( 1447 'word 99" word', 1448 'word 99!prime2! word', 1449 ), 1450 array( 1451 'word 99"word', 1452 'word 99!prime2!word', 1453 ), 1454 array( 1455 'word99" word', 1456 'word99!prime2! word', 1457 ), 1458 array( 1459 'word99"word', 1460 'word99!prime2!word', 1461 ), 1462 array( 1463 "word 99' word", 1464 "word 99!prime1! word", 1465 ), 1466 array( 1467 "word99' word", 1468 "word99!prime1! word", 1469 ), 1470 array( 1471 "word word's word", 1472 "word word!apos!s word", 1473 ), 1474 array( 1475 "word word'. word", 1476 "word word!closeq1!. word", 1477 ), 1478 array( 1479 "word ]'. word", 1480 "word ]!closeq1!. word", 1481 ), 1482 array( 1483 'word "word word', 1484 'word !openq2!word word', 1485 ), 1486 array( 1487 'word ("word word', 1488 'word (!openq2!word word', 1489 ), 1490 array( 1491 'word ["word word', 1492 'word [!openq2!word word', 1493 ), 1494 array( 1495 'word word" word', 1496 'word word!closeq2! word', 1497 ), 1498 array( 1499 'word word") word', 1500 'word word!closeq2!) word', 1501 ), 1502 array( 1503 'word word"] word', 1504 'word word!closeq2!] word', 1505 ), 1506 array( 1507 'word word"', 1508 'word word!closeq2!', 1509 ), 1510 array( 1511 'word word"word', 1512 'word word!closeq2!word', 1513 ), 1514 array( 1515 'test sentence".', 1516 'test sentence!closeq2!.', 1517 ), 1518 array( 1519 'test sentence."', 1520 'test sentence.!closeq2!', 1521 ), 1522 array( 1523 'test sentence." word', 1524 'test sentence.!closeq2! word', 1525 ), 1526 array( 1527 "word word' word", 1528 "word word!closeq1! word", 1529 ), 1530 array( 1531 "word word'. word", 1532 "word word!closeq1!. word", 1533 ), 1534 array( 1535 "word word'.word", 1536 "word word!closeq1!.word", 1537 ), 1538 array( 1539 "word word'", 1540 "word word!closeq1!", 1541 ), 1542 array( 1543 "test sentence'.", 1544 "test sentence!closeq1!.", 1545 ), 1546 array( 1547 "test sentence.'", 1548 "test sentence.!closeq1!", 1549 ), 1550 array( 1551 "test sentence'. word", 1552 "test sentence!closeq1!. word", 1553 ), 1554 array( 1555 "test sentence.' word", 1556 "test sentence.!closeq1! word", 1557 ), 1558 array( 1559 "word 'tain't word", 1560 "word !apos!tain!apos!t word", 1561 ), 1562 array( 1563 "word 'twere word", 1564 "word !apos!twere word", 1565 ), 1566 array( 1567 'word "42.00" word', 1568 'word !openq2!42.00!closeq2! word', 1569 ), 1570 array( 1571 "word '42.00' word", 1572 "word !openq1!42.00!closeq1! word", 1573 ), 1574 array( 1575 "word word'. word", 1576 "word word!closeq1!. word", 1577 ), 1578 array( 1579 "word word'.word", 1580 "word word!closeq1!.word", 1581 ), 1582 array( 1583 "word word', she said", 1584 "word word!closeq1!, she said", 1585 ), 1586 ); 1587 } 1365 1588 }
Note: See TracChangeset
for help on using the changeset viewer.