Index: trunk/phase3/tests/parser/parserTests.txt |
— | — | @@ -5481,10 +5481,13 @@ |
5482 | 5482 | !! input |
5483 | 5483 | ==a== |
5484 | 5484 | {| STYLE=__TOC__ |
| 5485 | +|foo |
5485 | 5486 | !! result |
5486 | 5487 | <h2><span class="editsection">[<a href="https://www.mediawiki.org/index.php?title=Parser_test&action=edit&section=1" title="Edit section: a">edit</a>]</span> <span class="mw-headline" id="a">a</span></h2> |
5487 | 5488 | <table style="__TOC__"> |
5488 | | -<tr><td></td></tr> |
| 5489 | +<tr> |
| 5490 | +<td>foo</td> |
| 5491 | +</tr> |
5489 | 5492 | </table> |
5490 | 5493 | |
5491 | 5494 | !! end |
— | — | @@ -5531,12 +5534,10 @@ |
5532 | 5535 | http://===r:::https://b |
5533 | 5536 | |
5534 | 5537 | {| |
| 5538 | + |
5535 | 5539 | !!result |
5536 | 5540 | <p><a rel="nofollow" class="external free" href="http://===r:::https://b">http://===r:::https://b</a> |
5537 | 5541 | </p> |
5538 | | -<table> |
5539 | | -<tr><td></td></tr> |
5540 | | -</table> |
5541 | 5542 | |
5542 | 5543 | !! end |
5543 | 5544 | |
Index: trunk/phase3/includes/parser/Parser.php |
— | — | @@ -829,21 +829,21 @@ |
830 | 830 | foreach ( $lines as $outLine ) { |
831 | 831 | $line = trim( $outLine ); |
832 | 832 | |
833 | | - if ( $line === '') { //empty line, go to next line |
834 | | - $out .= $outLine."\n"; |
| 833 | + if ( $line === '' ) { // empty line, go to next line |
| 834 | + $out .= $outLine . "\n"; |
835 | 835 | continue; |
836 | 836 | } |
837 | | - $first_chars = $line[0]; |
838 | | - if ( strlen($line) > 1) { |
839 | | - $first_chars .= in_array($line[1], array('}', '+', '-')) ? $line[1] : ''; |
| 837 | + $firstChars = $line[0]; |
| 838 | + if ( strlen( $line ) > 1 ) { |
| 839 | + $firstChars .= in_array( $line[1], array( '}', '+', '-' ) ) ? $line[1] : ''; |
840 | 840 | } |
841 | 841 | $matches = array(); |
842 | 842 | |
843 | 843 | if ( preg_match( '/^(:*)\{\|(.*)$/', $line , $matches ) ) { |
844 | 844 | $tables[] = array(); |
845 | | - $table =& $this->last($tables); |
846 | | - $table[0] = array(); //first row |
847 | | - $current_row =& $table[0]; |
| 845 | + $table =& $this->last( $tables ); |
| 846 | + $table[0] = array(); // first row |
| 847 | + $currentRow =& $table[0]; |
848 | 848 | |
849 | 849 | $table['indent'] = strlen( $matches[1] ); |
850 | 850 | |
— | — | @@ -853,79 +853,79 @@ |
854 | 854 | if ( $attributes !== '' ) { |
855 | 855 | $table['attributes'] = $attributes; |
856 | 856 | } |
857 | | - } else if ( !isset($tables[0]) ) { |
| 857 | + } else if ( !isset( $tables[0] ) ) { |
858 | 858 | // we're outside the table |
859 | 859 | |
860 | | - $out .= $outLine."\n"; |
861 | | - } else if ( $first_chars === '|}' ) { |
| 860 | + $out .= $outLine . "\n"; |
| 861 | + } else if ( $firstChars === '|}' ) { |
862 | 862 | // trim the |} code from the line |
863 | 863 | $line = substr ( $line , 2 ); |
864 | 864 | |
865 | 865 | // Shorthand for last row |
866 | | - $last_row =& $this->last($table); |
| 866 | + $lastRow =& $this->last( $table ); |
867 | 867 | |
868 | 868 | // a thead at the end becomes a tfoot, unless there is only one row |
869 | 869 | // Do this before deleting empty last lines to allow headers at the bottom of tables |
870 | | - if ( isset($last_row['type'] ) && $last_row['type'] == 'thead' && isset($table[1])) { |
871 | | - $last_row['type'] = 'tfoot'; |
872 | | - for($i = 0; isset($last_row[$i]); $i++ ) { |
873 | | - $last_row[$i]['type'] = 'th'; |
| 870 | + if ( isset( $lastRow['type'] ) && $lastRow['type'] == 'thead' && isset( $table[1] ) ) { |
| 871 | + $lastRow['type'] = 'tfoot'; |
| 872 | + for ( $i = 0; isset( $lastRow[$i] ); $i++ ) { |
| 873 | + $lastRow[$i]['type'] = 'th'; |
874 | 874 | } |
875 | 875 | } |
876 | 876 | |
877 | 877 | // Delete empty last lines |
878 | | - if ( empty($last_row) ) { |
879 | | - $last_row = NULL; |
| 878 | + if ( empty( $lastRow ) ) { |
| 879 | + $lastRow = NULL; |
880 | 880 | } |
881 | | - $curtable = array_pop($tables); |
882 | | - $o = $this->printTableHtml( $curtable ) . $line; |
| 881 | + $curtable = array_pop( $tables ); |
| 882 | + $o = $this->generateTableHTML( $curtable ) . $line; |
883 | 883 | |
884 | | - if ( count($tables) > 0 ) { |
885 | | - $table =& $this->last($tables); |
886 | | - $current_row =& $this->last($table); |
887 | | - $current_element =& $this->last($current_row); |
| 884 | + if ( count( $tables ) > 0 ) { |
| 885 | + $table =& $this->last( $tables ); |
| 886 | + $currentRow =& $this->last( $table ); |
| 887 | + $currentElement =& $this->last( $currentRow ); |
888 | 888 | |
889 | | - $output =& $current_element['content']; |
| 889 | + $output =& $currentElement['content']; |
890 | 890 | } else { |
891 | 891 | $output =& $out; |
892 | 892 | } |
893 | 893 | |
894 | 894 | $output .= $o; |
895 | 895 | |
896 | | - } else if ( $first_chars === '|-' ) { |
| 896 | + } else if ( $firstChars === '|-' ) { |
897 | 897 | // start a new row element |
898 | 898 | // but only when we haven't started one already |
899 | | - if( count($current_row) != 0 ) { |
| 899 | + if ( count( $currentRow ) != 0 ) { |
900 | 900 | $table[] = array(); |
901 | | - $current_row =& $this->last($table); |
| 901 | + $currentRow =& $this->last( $table ); |
902 | 902 | } |
903 | 903 | // Get the attributes, there's nothing else useful in $line now |
904 | 904 | $line = substr ( $line , 2 ); |
905 | 905 | $attributes = $this->mStripState->unstripBoth( $line ); |
906 | 906 | $attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' ); |
907 | | - if( $attributes !== '') { |
908 | | - $current_row['attributes'] = $attributes; |
| 907 | + if ( $attributes !== '' ) { |
| 908 | + $currentRow['attributes'] = $attributes; |
909 | 909 | } |
910 | 910 | |
911 | | - } else if ( $first_chars === '|+' ) { |
| 911 | + } else if ( $firstChars === '|+' ) { |
912 | 912 | // a table caption |
913 | 913 | $line = substr ( $line , 2 ); |
914 | 914 | |
915 | | - $c = $this->getCellAttr($line , 'caption'); |
| 915 | + $c = $this->getCellAttr( $line , 'caption' ); |
916 | 916 | $table['caption'] = array(); |
917 | 917 | $table['caption']['content'] = $c[0]; |
918 | | - if(isset($c[1])) $table['caption']['attributes'] = $c[1]; |
919 | | - unset($c); |
| 918 | + if ( isset( $c[1] ) ) $table['caption']['attributes'] = $c[1]; |
| 919 | + unset( $c ); |
920 | 920 | |
921 | 921 | $output =& $table['caption']; |
922 | | - } else if ( $first_chars === '|' || $first_chars === '!' || $first_chars === '!+' ) { |
| 922 | + } else if ( $firstChars === '|' || $firstChars === '!' || $firstChars === '!+' ) { |
923 | 923 | // Which kind of cells are we dealing with |
924 | | - $this_tag = 'td'; |
| 924 | + $currentTag = 'td'; |
925 | 925 | $line = substr ( $line , 1 ); |
926 | 926 | |
927 | | - if ( $first_chars === '!' || $first_chars === '!+' ) { |
| 927 | + if ( $firstChars === '!' || $firstChars === '!+' ) { |
928 | 928 | $line = str_replace ( '!!' , '||' , $line ); |
929 | | - $this_tag = 'th'; |
| 929 | + $currentTag = 'th'; |
930 | 930 | } |
931 | 931 | |
932 | 932 | // Split up multiple cells on the same line. |
— | — | @@ -933,29 +933,29 @@ |
934 | 934 | $line = ''; // save memory |
935 | 935 | |
936 | 936 | // decide whether thead to tbody |
937 | | - if ( !array_key_exists('type', $current_row) ) { |
938 | | - $current_row['type'] = ( $first_chars === '!' ) ? 'thead' : 'tbody' ; |
939 | | - } else if( $first_chars === '|' ) { |
940 | | - $current_row['type'] = 'tbody'; |
| 937 | + if ( !array_key_exists( 'type', $currentRow ) ) { |
| 938 | + $currentRow['type'] = ( $firstChars === '!' ) ? 'thead' : 'tbody' ; |
| 939 | + } else if ( $firstChars === '|' ) { |
| 940 | + $currentRow['type'] = 'tbody'; |
941 | 941 | } |
942 | 942 | |
943 | 943 | // Loop through each table cell |
944 | 944 | foreach ( $cells as $cell ) { |
945 | 945 | // a new cell |
946 | | - $current_row[] = array(); |
947 | | - $current_element =& $this->last($current_row); |
| 946 | + $currentRow[] = array(); |
| 947 | + $currentElement =& $this->last( $currentRow ); |
948 | 948 | |
949 | | - $current_element['type'] = $this_tag; |
| 949 | + $currentElement['type'] = $currentTag; |
950 | 950 | |
951 | | - $c = $this->getCellAttr($cell , $this_tag); |
952 | | - $current_element['content'] = $c[0]; |
953 | | - if(isset($c[1])) $current_element['attributes'] = $c[1]; |
954 | | - unset($c); |
| 951 | + $c = $this->getCellAttr( $cell , $currentTag ); |
| 952 | + $currentElement['content'] = $c[0]; |
| 953 | + if ( isset( $c[1] ) ) $currentElement['attributes'] = $c[1]; |
| 954 | + unset( $c ); |
955 | 955 | } |
956 | | - $output =& $current_element['content']; |
| 956 | + $output =& $currentElement['content']; |
957 | 957 | |
958 | 958 | } else { |
959 | | - $output .= $outLine."\n"; |
| 959 | + $output .= $outLine . "\n"; |
960 | 960 | } |
961 | 961 | } |
962 | 962 | |
— | — | @@ -964,11 +964,11 @@ |
965 | 965 | $out = substr( $out, 0, -1 ); |
966 | 966 | } |
967 | 967 | |
968 | | - #Close any unclosed tables |
969 | | - if (isset($tables) && count($tables) > 0 ) { |
970 | | - for ($i = 0; $i < count($tables); $i++) { |
971 | | - $curtable = array_pop($tables); |
972 | | - $out .= $this->printTableHtml( $curtable ); |
| 968 | + # Close any unclosed tables |
| 969 | + if ( isset( $tables ) && count( $tables ) > 0 ) { |
| 970 | + for ( $i = 0; $i < count( $tables ); $i++ ) { |
| 971 | + $curtable = array_pop( $tables ); |
| 972 | + $out .= $this->generateTableHTML( $curtable ); |
973 | 973 | } |
974 | 974 | } |
975 | 975 | |
— | — | @@ -985,30 +985,30 @@ |
986 | 986 | * |
987 | 987 | * @private |
988 | 988 | */ |
989 | | - function getCellAttr ($cell , $tag_name) { |
| 989 | + function getCellAttr ( $cell, $tagName ) { |
990 | 990 | $content = null; |
991 | 991 | $attributes = null; |
992 | 992 | |
993 | 993 | $cell = trim ( $cell ); |
994 | 994 | |
995 | 995 | // A cell could contain both parameters and data |
996 | | - $cell_data = explode ( '|' , $cell , 2 ); |
| 996 | + $cellData = explode ( '|' , $cell , 2 ); |
997 | 997 | |
998 | 998 | // Bug 553: Note that a '|' inside an invalid link should not |
999 | 999 | // be mistaken as delimiting cell parameters |
1000 | | - if ( strpos( $cell_data[0], '[[' ) !== false ) { |
| 1000 | + if ( strpos( $cellData[0], '[[' ) !== false ) { |
1001 | 1001 | $content = trim ( $cell ); |
1002 | 1002 | } |
1003 | | - else if ( count ( $cell_data ) == 1 ) { |
1004 | | - $content = trim ( $cell_data[0] ); |
| 1003 | + else if ( count ( $cellData ) == 1 ) { |
| 1004 | + $content = trim ( $cellData[0] ); |
1005 | 1005 | } |
1006 | 1006 | else { |
1007 | | - $attributes = $this->mStripState->unstripBoth( $cell_data[0] ); |
1008 | | - $attributes = Sanitizer::fixTagAttributes( $attributes , $tag_name ); |
| 1007 | + $attributes = $this->mStripState->unstripBoth( $cellData[0] ); |
| 1008 | + $attributes = Sanitizer::fixTagAttributes( $attributes , $tagName ); |
1009 | 1009 | |
1010 | | - $content = trim ( $cell_data[1] ); |
| 1010 | + $content = trim ( $cellData[1] ); |
1011 | 1011 | } |
1012 | | - return array($content, $attributes); |
| 1012 | + return array( $content, $attributes ); |
1013 | 1013 | } |
1014 | 1014 | |
1015 | 1015 | |
— | — | @@ -1017,79 +1017,79 @@ |
1018 | 1018 | * |
1019 | 1019 | * @private |
1020 | 1020 | */ |
1021 | | - function printTableHtml (&$t) { |
1022 | | - $r = "\n"; |
1023 | | - $r .= str_repeat( '<dl><dd>' , $t['indent'] ); |
1024 | | - $r .= '<table'; |
1025 | | - $r .= isset($t['attributes']) ? $t['attributes'] : ''; |
1026 | | - $r .= '>'; |
1027 | | - unset($t['attributes']); |
| 1021 | + function generateTableHTML ( &$table ) { |
| 1022 | + $return = "\n"; |
| 1023 | + $return .= str_repeat( '<dl><dd>' , $table['indent'] ); |
| 1024 | + $return .= '<table'; |
| 1025 | + $return .= isset( $table['attributes'] ) ? $table['attributes'] : ''; |
| 1026 | + $return .= '>'; |
| 1027 | + unset( $table['attributes'] ); |
1028 | 1028 | |
1029 | | - if ( isset($t['caption']) ) { |
1030 | | - $r .= "\n<caption"; |
1031 | | - $r .= isset($t['caption']['attributes']) ? $t['caption']['attributes'] : ''; |
1032 | | - $r .= '>'; |
1033 | | - $r .= $t['caption']['content']; |
1034 | | - $r .= '</caption>'; |
| 1029 | + if ( isset( $table['caption'] ) ) { |
| 1030 | + $return .= "\n<caption"; |
| 1031 | + $return .= isset( $table['caption']['attributes'] ) ? $table['caption']['attributes'] : ''; |
| 1032 | + $return .= '>'; |
| 1033 | + $return .= $table['caption']['content']; |
| 1034 | + $return .= '</caption>'; |
1035 | 1035 | } |
1036 | | - $last_section = ''; |
| 1036 | + $lastSection = ''; |
1037 | 1037 | $empty = true; |
1038 | 1038 | $simple = true; |
1039 | 1039 | |
1040 | | - //If we only have tbodies, mark table as simple |
1041 | | - for($i = 0; isset($t[$i]); $i++ ) { |
1042 | | - if ( !count( $t[$i]) ) continue; |
1043 | | - if ( !$last_section ) { |
1044 | | - $last_section = $t[$i]['type']; |
1045 | | - } else if ($last_section != $t[$i]['type']) { |
1046 | | - $simple = false; |
1047 | | - break; |
1048 | | - } |
| 1040 | + // If we only have tbodies, mark table as simple |
| 1041 | + for ( $i = 0; isset( $table[$i] ); $i++ ) { |
| 1042 | + if ( !count( $table[$i] ) ) continue; |
| 1043 | + if ( !$lastSection ) { |
| 1044 | + $lastSection = $table[$i]['type']; |
| 1045 | + } else if ( $lastSection != $table[$i]['type'] ) { |
| 1046 | + $simple = false; |
| 1047 | + break; |
| 1048 | + } |
1049 | 1049 | } |
1050 | | - $last_section = ''; |
1051 | | - for($i = 0; isset($t[$i]); $i++ ) { |
| 1050 | + $lastSection = ''; |
| 1051 | + for ( $i = 0; isset( $table[$i] ); $i++ ) { |
1052 | 1052 | // Check for empty tables |
1053 | | - if ( count( $t[$i]) ) { |
| 1053 | + if ( count( $table[$i] ) ) { |
1054 | 1054 | $empty = false; |
1055 | 1055 | } else { |
1056 | | - continue; |
| 1056 | + continue; |
1057 | 1057 | } |
1058 | | - if( $t[$i]['type'] != $last_section && !$simple ) { |
1059 | | - $r .= "\n<" . $t[$i]['type'] . '>'; |
| 1058 | + if ( $table[$i]['type'] != $lastSection && !$simple ) { |
| 1059 | + $return .= "\n<" . $table[$i]['type'] . '>'; |
1060 | 1060 | } |
1061 | 1061 | |
1062 | | - $r .= "\n<tr"; |
1063 | | - $r .= isset($t[$i]['attributes']) ? $t[$i]['attributes'] : ''; |
1064 | | - $r .= '>'; |
1065 | | - for($j = 0; isset($t[$i][$j]); $j++ ) { |
1066 | | - $r .= "\n<" . $t[$i][$j]['type']; |
1067 | | - $r .= isset($t[$i][$j]['attributes']) ? $t[$i][$j]['attributes'] : ''; |
1068 | | - $r .= '>'; |
| 1062 | + $return .= "\n<tr"; |
| 1063 | + $return .= isset( $table[$i]['attributes'] ) ? $table[$i]['attributes'] : ''; |
| 1064 | + $return .= '>'; |
| 1065 | + for ( $j = 0; isset( $table[$i][$j] ); $j++ ) { |
| 1066 | + $return .= "\n<" . $table[$i][$j]['type']; |
| 1067 | + $return .= isset( $table[$i][$j]['attributes'] ) ? $table[$i][$j]['attributes'] : ''; |
| 1068 | + $return .= '>'; |
1069 | 1069 | |
1070 | | - $r .= $t[$i][$j]['content']; |
| 1070 | + $return .= $table[$i][$j]['content']; |
1071 | 1071 | |
1072 | | - $r .= '</' . $t[$i][$j]['type'] . '>'; |
1073 | | - unset($t[$i][$j]); |
| 1072 | + $return .= '</' . $table[$i][$j]['type'] . '>'; |
| 1073 | + unset( $table[$i][$j] ); |
1074 | 1074 | } |
1075 | | - $r .= "\n</tr>"; |
| 1075 | + $return .= "\n</tr>"; |
1076 | 1076 | |
1077 | | - if( ( !isset($t[$i+1]) && !$simple )|| ( isset($t[$i+1]) && ($t[$i]['type'] != $t[$i+1]['type'])) ) { |
1078 | | - $r .= '</' . $t[$i]['type'] . '>'; |
| 1077 | + if ( ( !isset( $table[$i + 1] ) && !$simple ) || ( isset( $table[$i + 1] ) && ( $table[$i]['type'] != $table[$i + 1]['type'] ) ) ) { |
| 1078 | + $return .= '</' . $table[$i]['type'] . '>'; |
1079 | 1079 | } |
1080 | | - $last_section = $t[$i]['type']; |
1081 | | - unset($t[$i]); |
| 1080 | + $lastSection = $table[$i]['type']; |
| 1081 | + unset( $table[$i] ); |
1082 | 1082 | } |
1083 | 1083 | if ( $empty ) { |
1084 | | - if ( isset($t['caption']) ) { |
1085 | | - $r .= "\n<tr><td></td></tr>"; |
| 1084 | + if ( isset( $table['caption'] ) ) { |
| 1085 | + $return .= "\n<tr><td></td></tr>"; |
1086 | 1086 | } else { |
1087 | 1087 | return ''; |
1088 | 1088 | } |
1089 | 1089 | } |
1090 | | - $r .= "\n</table>"; |
1091 | | - $r .= str_repeat( '</dd></dl>' , $t['indent'] ); |
| 1090 | + $return .= "\n</table>"; |
| 1091 | + $return .= str_repeat( '</dd></dl>' , $table['indent'] ); |
1092 | 1092 | |
1093 | | - return $r; |
| 1093 | + return $return; |
1094 | 1094 | } |
1095 | 1095 | |
1096 | 1096 | /** |
— | — | @@ -1100,8 +1100,8 @@ |
1101 | 1101 | * |
1102 | 1102 | * @private |
1103 | 1103 | */ |
1104 | | - function &last (&$arr) { |
1105 | | - for($i = count($arr); (!isset($arr[$i]) && $i > 0); $i--) { } |
| 1104 | + function &last ( &$arr ) { |
| 1105 | + for ( $i = count( $arr ); ( !isset( $arr[$i] ) && $i > 0 ); $i-- ) { } |
1106 | 1106 | return $arr[$i]; |
1107 | 1107 | } |
1108 | 1108 | |