Page MenuHomePhabricator

Error while building VE document model with a translation section containing block image
Closed, ResolvedPublic

Description

We have this code to insert the placeholder translation to target document:
https://github.com/wikimedia/mediawiki-extensions-ContentTranslation/blob/master/modules/ve-cx/init/ve.init.mw.CXTarget.js#L511

ve.init.mw.CXTarget.prototype.gotPlaceholderTranslation = function ( placeholder, data ) {
	var pasteDoc, newCursorRange, docLen, fragmentRange,
		surfaceModel = this.getSurface().getModel(),
		cxid = placeholder.getModel().getAttribute( 'cxid' ),
		fragment = surfaceModel.getLinearFragment( placeholder.getModel().getOuterRange(), true /* noAutoSelect */ );

	pasteDoc = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( data ) );
	docLen = pasteDoc.getInternalList().getListNode().getOuterRange().start;

	fragment.insertContent( [
		{ type: 'cxSection', attributes: { style: 'section', cxid: cxid } },
		// Put a temporary paragraph inside the section so the cursor has somewhere
		// sensible to go, preventing scrollCursorIntoView from triggering a jump
		{ type: 'paragraph' },
		{ type: '/paragraph' },
		{ type: '/cxSection' }
	] );
	fragment
		.collapseToStart().adjustLinearSelection( 1, 3 )
		.insertDocument( pasteDoc, new ve.Range( 1, docLen - 1 ) );

	fragmentRange = fragment.getSelection().getCoveringRange();

	// Select first content offset within new content
	newCursorRange = new ve.Range( surfaceModel.getDocument().data.getNearestContentOffset( fragmentRange.start, 1 ) );
	if ( fragmentRange.containsRange( newCursorRange ) ) {
		surfaceModel.setLinearSelection( newCursorRange );
	}
};

When the translation of section is as given below, an internal error is thrown from ve.dm.TreeModifier

<section id="cxTargetSection22">
<figure data-cx="{&#34;adapted&#34;:true,&#34;imageSource&#34;:&#34;//upload.wikimedia.org/wikipedia/commons/thumb/a/a3/Gimchi.jpg/175px-Gimchi.jpg&#34;,&#34;resource&#34;:&#34;./File:Gimchi.jpg&#34;}" id="mwbA" typeof="mw:Image/Thumb">
<a href="./படிமம்:Gimchi.jpg" id="mwbQ">
<img data-file-height="1512" data-file-type="bitmap" data-file-width="2016" height="131" id="mwbg" resource="./படிமம்:Gimchi.jpg" src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a3/Gimchi.jpg/175px-Gimchi.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/a/a3/Gimchi.jpg/350px-Gimchi.jpg 2x, //upload.wikimedia.org/wikipedia/commons/thumb/a/a3/Gimchi.jpg/263px-Gimchi.jpg 1.5x" width="175"></img>
</a>
<figcaption id="mwbw">
<span class="cx-segment" data-segmentid="107">
<a class="cx-link" data-cx="{&#34;adapted&#34;:true,&#34;targetTitle&#34;:{&#34;title&#34;:&#34;கிம்ச்சி&#34;,&#34;pagelanguage&#34;:&#34;ta&#34;,&#34;thumbnail&#34;:{&#34;source&#34;:&#34;https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Various_kimchi.jpg/80px-Various_kimchi.jpg&#34;,&#34;width&#34;:80,&#34;height&#34;:53}},&#34;sourceTitle&#34;:{&#34;title&#34;:&#34;Kimchi&#34;,&#34;pagelanguage&#34;:&#34;en&#34;,&#34;thumbnail&#34;:{&#34;source&#34;:&#34;https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Various_kimchi.jpg/80px-Various_kimchi.jpg&#34;,&#34;width&#34;:80,&#34;height&#34;:53},&#34;terms&#34;:{&#34;description&#34;:[&#34;Korean vegetable dish&#34;]}}}" data-linkid="108" href="கிம்ச்சி" id="mwcA" rel="mw:WikiLink" title="கிம்ச்சி">Kimchi</a>
ஒரு மிகவும் பொதுவான பக்க dish உள்ள கொரியா.</span>
</figcaption>
</figure>

From ve.dm.TreeModifier.prototype.processInsert:479 is throwing this error:

Error: Expected closing for article but got closing for cxBlockImageNode

This is happening for all sections with block images. Sections with paragraphs and different kind of contents are working fine though.

Event Timeline

Restricted Application added subscribers: revi, Aklapper. · View Herald Transcript

I tried to get the model at the place of inserting block image and it does not look right.

image.png (387×766 px, 203 KB)

The /cxBlockImage node should be under a /cxSection.

After debugging this with the help of @santhosh , It seems ve.dm.Document#fixupInsertion is returning unbalanced data . Before the fixup, we have:

cxBlockImageNode,
    mwImageCaption,
        paragraph,
        /paragraph,
    /mwImageCaption,
/cxBlockImageNode

After the fixup, we have:

    cxBlockImageNode,
    /cxBlockImageNode,
/cxBlockImageNode,
mwBlockImage,
    mwImageCaption,
        paragraph,
        /paragraph,
    /mwImageCaption,
/cxBlockImageNode,       // <--- What???
article

Obviously that's internally inconsistent regardless of the insertion context, but for what it's worth, the insertion context looks totally fine:

article
    cxSection

It's probably either a flaw in fixupInsertion or something odd about the class properties here, or maybe both. I'll investigate further.

To test this you need cxserver at https://gerrit.wikimedia.org/r/c/419129/ (not merged yet)

This patch is merged. So to reproduce the issue, one would need cxserver master and Content Translation extension https://gerrit.wikimedia.org/r/c/419151/ with $wgContentTranslationVersion = 2;

Thanks @dchan for helping to debug the issue

Oh, of course - we have to allow mwImageCaption to have cxBlockImageNode as a parent ... I'll upload a patch

dchan claimed this task.

See https://gerrit.wikimedia.org/r/#/c/419151/1..2/modules/ve-cx/dm/ve.dm.CXBlockImageNode.js .

(Marking the issue as resolved immediately, since it only affected that change set, not master)

Pginer-WMF triaged this task as Medium priority.Mar 19 2018, 7:58 AM