mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-24 22:35:41 +00:00
Kill all but one of the Parsoid compat hacks, we don't need them any more
Also remove traces of Parsoid workarounds in tests. Tests are now passing, yay :) Change-Id: I8a59fc92c567c3595849e3e9377ce6eb6acde280
This commit is contained in:
parent
c889292adf
commit
444961af2f
|
@ -308,58 +308,20 @@ ve.dm.Converter.prototype.getDataFromDom = function( domElement, annotations, da
|
|||
data = data.concat( createAlien( childDomElement, branchIsContent ) );
|
||||
break;
|
||||
case Node.TEXT_NODE:
|
||||
// HACK: strip trailing newlines in <li> tags. Workaround for a Parsoid bug
|
||||
// HACK: strip trailing newline in <li> tags. Workaround for a Parsoid bug
|
||||
text = childDomElement.data;
|
||||
if ( domElement.nodeName.toLowerCase() === 'li' ) {
|
||||
text = text.replace( /\n+$/, '' );
|
||||
}
|
||||
if ( text === '' ) {
|
||||
// Don't produce an empty text node or an empty paragraph
|
||||
break;
|
||||
}
|
||||
// HACK: strip implied leading and trailing newlines in <p> tags
|
||||
// Workaround for a Parsoid bug
|
||||
/*
|
||||
* Leading newlines:
|
||||
* If the previous sibling is a paragraph, do not strip leading newlines
|
||||
* If there is no previous sibling, do not strip leading newlines
|
||||
* Otherwise, strip 1 leading newline
|
||||
*
|
||||
* Trailing newlines:
|
||||
* If the next sibling is a paragraph, strip 2 trailing newlines
|
||||
* If there is no next sibling, do not strip trailing newlines
|
||||
* Otherwise, strip 1 trailing newline
|
||||
*/
|
||||
contentNode = childDomElement.parentNode;
|
||||
if ( contentNode.nodeName.toLowerCase() === 'p' ) {
|
||||
if (
|
||||
contentNode.previousSibling &&
|
||||
contentNode.previousSibling.nodeName.toLowerCase() !== 'p' &&
|
||||
text.charAt( 0 ) === '\n'
|
||||
) {
|
||||
text = text.substr( 1 );
|
||||
}
|
||||
if ( contentNode.nextSibling ) {
|
||||
// Strip one trailing newline
|
||||
if ( text.charAt( text.length - 1 ) === "\n" ) {
|
||||
text = text.substr( 0, text.length - 1 );
|
||||
}
|
||||
if ( contentNode.nextSibling.nodeName.toLowerCase() === 'p' ) {
|
||||
// Strip another one
|
||||
if ( text.charAt( text.length - 1 ) === "\n" ) {
|
||||
text = text.substr( 0, text.length - 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
text = text.replace( /\n$/, '' );
|
||||
}
|
||||
|
||||
if ( !branchIsContent ) {
|
||||
// If it's bare content, strip leading and trailing newlines
|
||||
// FIXME these newlines should be turned into placeholders instead
|
||||
text = text.replace( /^\n+/, '' ).replace( /\n+$/, '' );
|
||||
if ( text === '' ) {
|
||||
// Don't produce an empty text node
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( text === '' ) {
|
||||
// Don't produce an empty text node or an empty paragraph
|
||||
break;
|
||||
}
|
||||
|
||||
// Start auto-wrapping of bare content
|
||||
|
@ -375,6 +337,8 @@ ve.dm.Converter.prototype.getDataFromDom = function( domElement, annotations, da
|
|||
break;
|
||||
case Node.COMMENT_NODE:
|
||||
// TODO: Preserve comments by inserting them into the linear model too
|
||||
// Could use placeholders for this too, although they'd need to be
|
||||
// inline in certain cases
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -419,40 +383,6 @@ ve.dm.Converter.prototype.getDomFromData = function( data ) {
|
|||
container = document.createElement( 'div' ),
|
||||
domElement = container,
|
||||
annotationStack = {}; // { hash: DOMnode }
|
||||
|
||||
function fixupText( text, node ) {
|
||||
// HACK reintroduce newlines needed to make Parsoid not freak out
|
||||
// This reverses the newline stripping done in getDataFromDom()
|
||||
/*
|
||||
* Leading newlines:
|
||||
* If the previous sibling is a heading, add 1 leading newline
|
||||
* Otherwise, do not add any leading newlines
|
||||
*
|
||||
* Trailing newlines:
|
||||
* If the next sibling is a paragraph, add 2 trailing newlines
|
||||
* If there is no next sibling, do not add any trailing newlines
|
||||
* Otherwise, add 1 trailing newline
|
||||
*/
|
||||
if ( node.parentNode.nodeName.toLowerCase() === 'p' ) {
|
||||
if (
|
||||
node.parentNode.previousSibling &&
|
||||
node.parentNode.previousSibling.nodeName.toLowerCase().match( /h\d/ ) &&
|
||||
!node.previousSibling
|
||||
) {
|
||||
text = "\n" + text;
|
||||
}
|
||||
if ( node.parentNode.nextSibling && !node.nextSibling ) {
|
||||
// Add one trailing newline
|
||||
text += "\n";
|
||||
if ( node.parentNode.nextSibling.nodeName.toLowerCase() === 'p' ) {
|
||||
// Add another one
|
||||
text += "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
for ( i = 0; i < data.length; i++ ) {
|
||||
if ( typeof data[i] === 'string' ) {
|
||||
// Text
|
||||
|
@ -579,64 +509,6 @@ ve.dm.Converter.prototype.getDomFromData = function( data ) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HACK: do postprocessing on the data to work around bugs in Parsoid concerning paragraphs
|
||||
// inside list items
|
||||
$( container ).find( 'li, dd, dt' ).each( function() {
|
||||
var $sublists = $(this).children( 'ul, ol, dl' ),
|
||||
$firstChild = $(this.firstChild),
|
||||
$lastChild = $(this.lastChild);
|
||||
if ( $firstChild.is( 'p' ) ) {
|
||||
// Unwrap the first paragraph, unless it has stx=html
|
||||
datamw = $.parseJSON( $firstChild.attr( 'data-rt' ) ) || {};
|
||||
if ( datamw.stx !== 'html' ) {
|
||||
$firstChild.replaceWith( $firstChild.contents() );
|
||||
}
|
||||
}
|
||||
|
||||
// Append a newline to the end of the <li> , provided its last child is not a list
|
||||
if ( $lastChild.is( ':not(ul,ol)' ) ) {
|
||||
$(this).append( "\n" );
|
||||
}
|
||||
// Append a newline before every sublist that is preceded by something
|
||||
$sublists.each( function() {
|
||||
if ( this.previousSibling ) {
|
||||
if ( this.previousSibling.nodeName.toLowerCase() === 'text' ) {
|
||||
this.previousSibling.data += "\n";
|
||||
} else {
|
||||
this.parentNode.insertBefore( document.createTextNode( "\n" ), this );
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// HACK more postprocessing, this time to add newlines to paragraphs so Parsoid doesn't freak out
|
||||
$( container )
|
||||
// Get all text nodes
|
||||
.find( '*' )
|
||||
.contents()
|
||||
.filter( function() {
|
||||
// Text nodes only
|
||||
return this.nodeType === 3 &&
|
||||
// Exclude text nodes within lists
|
||||
$( this.parentNode ).closest( 'li, dd, dt' ).length === 0;
|
||||
} )
|
||||
.each( function() {
|
||||
this.data = fixupText( this.data, this );
|
||||
} );
|
||||
// And add newlines after headings too
|
||||
$( container ).find( 'h1, h2, h3, h4, h5, h6' ).each( function() {
|
||||
// If there is no next sibling, we don't need to add a newline
|
||||
// If the next sibling is a paragraph, fixupText() has taken care of it
|
||||
// Otherwise, add a newline after the heading
|
||||
if ( this.nextSibling && this.nextSibling.nodeName.toLowerCase() !== 'p' ) {
|
||||
this.parentNode.insertBefore( document.createTextNode( "\n" ), this.nextSibling );
|
||||
}
|
||||
// If the previous sibling exists and is a pre, we need to add a newline before
|
||||
if ( this.previousSibling && this.previousSibling.nodeName.toLowerCase() === 'pre' ) {
|
||||
this.parentNode.insertBefore( document.createTextNode( "\n" ), this );
|
||||
}
|
||||
} );
|
||||
return container;
|
||||
};
|
||||
|
||||
|
|
|
@ -22,20 +22,17 @@ ve.dm.example.html =
|
|||
'<p>d</p>' +
|
||||
'<ul>' +
|
||||
'<li>' +
|
||||
'e' + // Not wrapped in a <p> due to Parsoid behavior
|
||||
"\n" + // Workaround for Parsoid bug
|
||||
'<p>e</p>' +
|
||||
'<ul>' +
|
||||
'<li>' +
|
||||
'f' + // Not wrapped in a <p> due to Parsoid behavior
|
||||
"\n" + // Workaround for Parsoid bug
|
||||
'<p>f</p>' +
|
||||
'</li>' +
|
||||
'</ul>' +
|
||||
'</li>' +
|
||||
'</ul>' +
|
||||
'<ol>' +
|
||||
'<li>' +
|
||||
'g' + // Not wrapped in a <p> due to Parsoid behavior
|
||||
"\n" + // Workaround for Parsoid bug
|
||||
'<p>g</p>' +
|
||||
'</li>' +
|
||||
'</ol>' +
|
||||
'</td>' +
|
||||
|
@ -441,10 +438,7 @@ ve.dm.example.domToDataCases = {
|
|||
'data': ve.dm.example.data
|
||||
},
|
||||
'list item with space followed by link': {
|
||||
// This HTML is weird because of workarounds for Parsoid bugs:
|
||||
// * newline before </li>
|
||||
// * first paragraph in an <li> not wrapped in <p>
|
||||
'html': '<ul><li> <a rel="mw:WikiLink" href="/Foo_bar" data-rt="{"sHref":"foo bar"}">bar</a>\n</li></ul>',
|
||||
'html': '<ul><li><p> <a rel="mw:WikiLink" href="/Foo_bar" data-rt="{"sHref":"foo bar"}">bar</a></p></li></ul>',
|
||||
'data': [
|
||||
{ 'type': 'list', 'attributes': { 'style': 'bullet' } },
|
||||
{ 'type': 'listItem' },
|
||||
|
|
Loading…
Reference in a new issue