Catrope has uploaded a new change for review.

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


Change subject: [WIP] Unwrap whitespace-only links
......................................................................

[WIP] Unwrap whitespace-only links

In toDomElements(), detect whitespace-only links and unwrap them,
except if they were originally whitespace-only.

TODO:
* Refactor MWInternalLinkAnnotation a bit so it doesn't duplicate
  the emptiness check
** Make it call its parent's toDomElements()
* Use unicodeJS to determine whether the link contains a word
* Possibly base the emptiness detection on linmod data instead of DOM
** Would require DM to pass data in, which is possible but needs work

Bug: 37835
Change-Id: Ic99ad7b52b6ea43e21bb598177af9e34d2805be4
---
M modules/ve-mw/ce/annotations/ve.ce.MWInternalLinkAnnotation.js
M modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
M modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js
M modules/ve/dm/annotations/ve.dm.LinkAnnotation.js
4 files changed, 21 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/84/74084/1

diff --git a/modules/ve-mw/ce/annotations/ve.ce.MWInternalLinkAnnotation.js 
b/modules/ve-mw/ce/annotations/ve.ce.MWInternalLinkAnnotation.js
index 9c0cd0e..c837fb2 100644
--- a/modules/ve-mw/ce/annotations/ve.ce.MWInternalLinkAnnotation.js
+++ b/modules/ve-mw/ce/annotations/ve.ce.MWInternalLinkAnnotation.js
@@ -24,7 +24,9 @@
        this.$.attr( 'title', model.getAttribute( 'title' ) );
        // Get href from DM rendering
        dmRendering = model.getDomElements()[0];
-       this.$.attr( 'href', dmRendering.getAttribute( 'href' ) );
+       if ( dmRendering ) {
+               this.$.attr( 'href', dmRendering.getAttribute( 'href' ) );
+       }
 };
 
 /* Inheritance */
diff --git a/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js 
b/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
index 44654e6..1f62ed1 100644
--- a/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
+++ b/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
@@ -46,8 +46,10 @@
 
 ve.dm.MWExternalLinkAnnotation.static.toDomElements = function ( dataElement ) 
{
        var parentResult = ve.dm.LinkAnnotation.static.toDomElements.apply( 
this, arguments );
-       parentResult[0].setAttribute( 'rel', dataElement.attributes.rel || 
'mw:ExtLink' );
-       return parentResult;
+       if ( parentResult.length ) {
+               parentResult[0].setAttribute( 'rel', dataElement.attributes.rel 
|| 'mw:ExtLink' );
+               return parentResult;
+       }
 };
 
 /* Methods */
diff --git a/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js 
b/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js
index f0368dc..dcccdca 100644
--- a/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js
+++ b/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js
@@ -47,16 +47,21 @@
                        'hrefPrefix': matches[1],
                        'title': decodeURIComponent( matches[2] ).replace( 
/_/g, ' ' ),
                        'normalizedTitle': normalizedTitle,
-                       'origTitle': matches[2]
+                       'origTitle': matches[2],
+                       'originallyEmpty': $( domElements[0].childNodes 
).text().match( /^\s*$/ )
                }
        };
 };
 
-ve.dm.MWInternalLinkAnnotation.static.toDomElements = function ( dataElement, 
doc ) {
+ve.dm.MWInternalLinkAnnotation.static.toDomElements = function ( dataElement, 
doc, converter, childDomElements ) {
        var href,
                domElement = doc.createElement( 'a' ),
                title = dataElement.attributes.title,
                origTitle = dataElement.attributes.origTitle;
+       // HACK check if the link is whitespace-only. Should be done properly 
using unicodeJS
+       if ( $( childDomElements ).text().match( /^\s*$/ ) && 
!dataElement.attributes.originallyEmpty ) {
+               return [];
+       }
        if ( origTitle && decodeURIComponent( origTitle ).replace( /_/g, ' ' ) 
=== title ) {
                // Restore href from origTitle
                href = origTitle;
diff --git a/modules/ve/dm/annotations/ve.dm.LinkAnnotation.js 
b/modules/ve/dm/annotations/ve.dm.LinkAnnotation.js
index 28bdd6c..90e26c8 100644
--- a/modules/ve/dm/annotations/ve.dm.LinkAnnotation.js
+++ b/modules/ve/dm/annotations/ve.dm.LinkAnnotation.js
@@ -34,13 +34,18 @@
        return {
                'type': 'link',
                'attributes': {
-                       'href': domElements[0].getAttribute( 'href' )
+                       'href': domElements[0].getAttribute( 'href' ),
+                       'originallyEmpty': $( domElements[0].childNodes 
).text().match( /^\s*$/ )
                }
        };
 };
 
-ve.dm.LinkAnnotation.static.toDomElements = function ( dataElement, doc ) {
+ve.dm.LinkAnnotation.static.toDomElements = function ( dataElement, doc, 
converter, childDomElements ) {
        var domElement = doc.createElement( 'a' );
+       // HACK check if the link is whitespace-only. Should be done properly 
using unicodeJS
+       if ( $( childDomElements ).text().match( /^\s*$/ ) && 
!dataElement.attributes.originallyEmpty ) {
+               return [];
+       }
        domElement.setAttribute( 'href', dataElement.attributes.href );
        return [ domElement ];
 };

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic99ad7b52b6ea43e21bb598177af9e34d2805be4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to