fix attributes of table caption and table cell

Merging `inTableCaptioin` method into `eatTableRow` with an additional parameter so that table caption attributes can be respected. This patch also distinguishes double pipes (`||`) which start a new table cell and single pipe (`|`) which ends the attributes.

Bug: T324374
Change-Id: If2d4600067c587fe0b6a6edb332fd4e55abec607
This commit is contained in:
bhsd 2024-01-19 03:16:00 +08:00
parent 5235dbb322
commit 8a3d203a03
4 changed files with 23 additions and 22 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -629,14 +629,6 @@ class CodeMirrorModeMediaWiki {
return this.eatWikiText( modeConfig.tags.tableDefinition )( stream, state );
}
inTableCaption( stream, state ) {
if ( stream.sol() && stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
state.tokenize = this.inTable.bind( this );
return this.inTable( stream, state );
}
return this.eatWikiText( modeConfig.tags.tableCaption )( stream, state );
}
inTable( stream, state ) {
if ( stream.sol() ) {
stream.eatSpace();
@ -648,7 +640,7 @@ class CodeMirrorModeMediaWiki {
}
if ( stream.eat( '+' ) ) {
stream.eatSpace();
state.tokenize = this.inTableCaption.bind( this );
state.tokenize = this.eatTableRow( true, false, true );
return this.makeLocalStyle( modeConfig.tags.tableDelimiter, state );
}
if ( stream.eat( '}' ) ) {
@ -668,7 +660,14 @@ class CodeMirrorModeMediaWiki {
return this.eatWikiText( '' )( stream, state );
}
eatTableRow( isStart, isHead ) {
// isStart actually means whether there may be attributes */
eatTableRow( isStart, isHead, isCaption ) {
let tag = '';
if ( isCaption ) {
tag = modeConfig.tags.tableCaption;
} else if ( isHead ) {
tag = modeConfig.tags.strong;
}
return ( stream, state ) => {
if ( stream.sol() ) {
if ( stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
@ -677,22 +676,19 @@ class CodeMirrorModeMediaWiki {
}
} else {
if ( stream.match( /^[^'|{[<&~!]+/ ) ) {
return this.makeStyle( ( isHead ? modeConfig.tags.strong : '' ), state );
return this.makeStyle( tag, state );
}
if (
stream.match( '||' ) ||
( isHead && stream.match( '!!' ) ) ||
( isStart && stream.eat( '|' ) )
) {
if ( stream.match( '||' ) || ( isHead && stream.match( '!!' ) ) ) {
this.isBold = false;
this.isItalic = false;
if ( isStart ) {
state.tokenize = this.eatTableRow( false, isHead );
state.tokenize = this.eatTableRow( true, isHead, isCaption );
return this.makeLocalStyle( modeConfig.tags.tableDelimiter, state );
}
if ( isStart && stream.eat( '|' ) ) {
state.tokenize = this.eatTableRow( false, isHead, isCaption );
return this.makeLocalStyle( modeConfig.tags.tableDelimiter, state );
}
}
const tag = isHead ? modeConfig.tags.strong : '';
return this.eatWikiText( tag )( stream, state );
};
}

View file

@ -78,6 +78,11 @@ const testCases = [
input: '{{{!}} class="wikitable"\n! header\n{{!}}-\n{{!}} cell\n{{!}}}',
output: '<div class="cm-line">{<span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">{{</span><span class="cm-mw-ext-ground cm-mw-parserfunction-name">!</span><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">}}</span> class="wikitable"</div><div class="cm-line">! header</div><div class="cm-line"><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">{{</span><span class="cm-mw-ext-ground cm-mw-parserfunction-name">!</span><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">}}</span>-</div><div class="cm-line"><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">{{</span><span class="cm-mw-ext-ground cm-mw-parserfunction-name">!</span><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">}}</span> cell</div><div class="cm-line"><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">{{</span><span class="cm-mw-ext-ground cm-mw-parserfunction-name">!</span><span class="cm-mw-ext-ground cm-mw-parserfunction-bracket">}}</span>}</div>'
},
{
title: 'T324374: table cell attributes',
input: '{|\n|+ class="z" | Z\n! class="a" | A !! class="b" | B\n|-\n! class="c" | C || class="d" | D\n|-\n| class="e" | E || class="f" | F\n|}',
output: '<div class="cm-line"><span class="cm-mw-table-bracket">{|</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">|+ </span><span class="cm-mw-table-caption">class="z" </span><span class="cm-mw-table-delimiter">|</span><span class="cm-mw-table-caption"> Z</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">! </span><span class="cm-mw-strong">class="a" </span><span class="cm-mw-table-delimiter">|</span><span class="cm-mw-strong"> A </span><span class="cm-mw-table-delimiter">!!</span><span class="cm-mw-strong"> class="b" </span><span class="cm-mw-table-delimiter">|</span><span class="cm-mw-strong"> B</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">|-</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">! </span><span class="cm-mw-strong">class="c" </span><span class="cm-mw-table-delimiter">|</span><span class="cm-mw-strong"> C </span><span class="cm-mw-table-delimiter">||</span><span class="cm-mw-strong"> class="d" </span><span class="cm-mw-table-delimiter">|</span><span class="cm-mw-strong"> D</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">|-</span></div><div class="cm-line"><span class="cm-mw-table-delimiter">| </span>class="e" <span class="cm-mw-table-delimiter">|</span> E <span class="cm-mw-table-delimiter">||</span> class="f" <span class="cm-mw-table-delimiter">|</span> F</div><div class="cm-line"><span class="cm-mw-table-bracket">|}</span></div>'
},
{
title: 'section headings',
input: '== My section ==\nFoo bar\n=== Blah ===\nBaz\n= { =\nText',