Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/201583

Change subject: Trim leading and trailing whitespace from annotations
......................................................................

Trim leading and trailing whitespace from annotations

This means that even if you do try to create "<a>  Foo  </a>bar" in
the editor (and you have to try pretty hard to do that in the first place),
it'll serialize to "  <a>Foo</a>  bar".

For VE-MW, this prevents ugly wikitext output like [[Foo|Foo  ]]<nowiki/>bar.

Bug: T54037
Change-Id: I0b734651a5b39e16142cb0edc28a594ca3d60231
---
M src/dm/ve.dm.Converter.js
1 file changed, 50 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/83/201583/1

diff --git a/src/dm/ve.dm.Converter.js b/src/dm/ve.dm.Converter.js
index f8c9915..4b2ce79 100644
--- a/src/dm/ve.dm.Converter.js
+++ b/src/dm/ve.dm.Converter.js
@@ -1114,7 +1114,11 @@
        }
 
        function closeAnnotation( annotation ) {
-               var i, len, annotationElement, annotatedChildDomElements;
+               var i, len, annotationElement, annotatedChildDomElements,
+                       matches, first, last,
+                       leading = '',
+                       trailing = '',
+                       origElement = annotation.getOriginalDomElements()[0];
 
                // Add text if needed
                if ( text.length > 0 ) {
@@ -1124,9 +1128,51 @@
 
                annotatedChildDomElements = annotatedDomElementStack.pop();
                annotatedDomElements = 
annotatedDomElementStack[annotatedDomElementStack.length - 1];
+
+               // HACK: Move any leading and trailing whitespace out of the 
annotation, but only if the
+               // annotation didn't originally have leading/trailing whitespace
+               first = annotatedChildDomElements[0];
+               while (
+                       first.nodeType === Node.TEXT_NODE &&
+                       ( matches = first.data.match( /^\s+/ ) ) &&
+                       ( !origElement || !origElement.textContent.match( /^\s/ 
) )
+               ) {
+                       leading += matches[0];
+                       first.deleteData( 0, matches[0].length );
+                       if ( first.data.length === 0 ) {
+                               // Remove empty text node
+                               annotatedChildDomElements.shift();
+                               // Process next text node to see if it also has 
whitespace
+                               first = annotatedChildDomElements[0];
+                       } else {
+                               break;
+                       }
+               }
+               last = 
annotatedChildDomElements[annotatedChildDomElements.length - 1];
+               while (
+                       last.nodeType === Node.TEXT_NODE &&
+                       ( matches = last.data.match( /\s+$/ ) ) &&
+                       ( !origElement || !origElement.textContent.match( /\s$/ 
) )
+               ) {
+                       trailing += matches[0];
+                       last.deleteData( last.data.length - matches[0].length, 
matches[0].length );
+                       if ( last.data.length === 0 ) {
+                               // Remove empty text node
+                               annotatedChildDomElements.pop();
+                               // Process next text node to see if it also has 
whitespace
+                               last = 
annotatedChildDomElements[annotatedChildDomElements.length - 1];
+                       } else {
+                               break;
+                       }
+               }
+
                annotationElement = conv.getDomElementsFromDataElement(
                        annotation.getElement(), doc, annotatedChildDomElements
                )[0];
+
+               if ( leading ) {
+                       annotatedDomElements.push( doc.createTextNode( leading 
) );
+               }
                if ( annotationElement ) {
                        for ( i = 0, len = annotatedChildDomElements.length; i 
< len; i++ ) {
                                annotationElement.appendChild( 
annotatedChildDomElements[i] );
@@ -1137,6 +1183,9 @@
                                annotatedDomElements.push( 
annotatedChildDomElements[i] );
                        }
                }
+               if ( trailing ) {
+                       annotatedDomElements.push( doc.createTextNode( trailing 
) );
+               }
        }
 
        function findEndOfNode( i ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/201583
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b734651a5b39e16142cb0edc28a594ca3d60231
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <roan.katt...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to