Skip to content

Commit dae599a

Browse files
committed
Fix the newline workarounds (for Parsoid newline handling) for headings
* Parsoid outputs bare newlines after a heading unless it's followed by a <p>, so strip leading and trailing newlines in all bare text * Adding a leading newline in <p>s is only needed if preceded by a heading, don't add it otherwise * Headings need a bare newline after them unless followed by a <p> * Headings also need a bare newline before them if preceded by a <pre> Change-Id: Ib02f800b26453541604e920fbb3845c51cdc6dea
1 parent 13c44dd commit dae599a

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

modules/ve2/dm/ve.dm.Converter.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ ve.dm.Converter.prototype.getDataFromDom = function( domElement, annotations, da
301301
text = text.replace( /\n+$/, '' );
302302
}
303303
if ( text === '' ) {
304+
// Don't produce an empty text node or an empty paragraph
304305
break;
305306
}
306307
// HACK: strip implied leading and trailing newlines in <p> tags
@@ -339,6 +340,15 @@ ve.dm.Converter.prototype.getDataFromDom = function( domElement, annotations, da
339340
}
340341
}
341342

343+
if ( !branchIsContent ) {
344+
// If it's bare content, strip leading and trailing newlines
345+
text = text.replace( /^\n+/, '' ).replace( /\n+$/, '' );
346+
if ( text === '' ) {
347+
// Don't produce an empty text node
348+
break;
349+
}
350+
}
351+
342352
// Start auto-wrapping of bare content
343353
if ( !wrapping && !alreadyWrapped && !branchIsContent ) {
344354
data.push( { 'type': 'paragraph' } );
@@ -379,9 +389,8 @@ ve.dm.Converter.prototype.getDomFromData = function( data ) {
379389
// This reverses the newline stripping done in getDataFromDom()
380390
/*
381391
* Leading newlines:
382-
* If the previous sibling is a paragraph, do not add any leading newlines
383-
* If there is no previous sibling, do not add any leading newlines
384-
* Otherwise, add 1 leading newline
392+
* If the previous sibling is a heading, add 1 leading newline
393+
* Otherwise, do not add any leading newlines
385394
*
386395
* Trailing newlines:
387396
* If the next sibling is a paragraph, add 2 trailing newlines
@@ -391,7 +400,7 @@ ve.dm.Converter.prototype.getDomFromData = function( data ) {
391400
if ( node.nodeName.toLowerCase() === 'p' ) {
392401
if (
393402
node.previousSibling &&
394-
node.previousSibling.nodeName.toLowerCase() !== 'p'
403+
node.previousSibling.nodeName.toLowerCase().match( /h\d/ )
395404
) {
396405
text = "\n" + text;
397406
}
@@ -583,6 +592,19 @@ ve.dm.Converter.prototype.getDomFromData = function( data ) {
583592
.each( function() {
584593
this.data = fixupText( this.data, this.parentNode );
585594
} );
595+
// And add newlines after headings too
596+
$( container ).find( 'h1, h2, h3, h4, h5, h6' ).each( function() {
597+
// If there is no next sibling, we don't need to add a newline
598+
// If the next sibling is a paragraph, fixupText() has taken care of it
599+
// Otherwise, add a newline after the heading
600+
if ( this.nextSibling && this.nextSibling.nodeName.toLowerCase() !== 'p' ) {
601+
this.parentNode.insertBefore( document.createTextNode( "\n" ), this.nextSibling );
602+
}
603+
// If the previous sibling exists and is a pre, we need to add a newline before
604+
if ( this.previousSibling && this.previousSibling.nodeName.toLowerCase() === 'pre' ) {
605+
this.parentNode.insertBefore( document.createTextNode( "\n" ), this );
606+
}
607+
} );
586608
return container;
587609
};
588610

0 commit comments

Comments
 (0)