mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-26 19:56:49 +00:00
getDataFromDom -> getModelFromDom
Following on from getDomFromModel, this returns a document model instead of element linear data. The only instance that hasn't been replaced is in rich paste, where we need to sanitize the converted data before constructing the document model. This should be cleaned up in a later commit. Change-Id: I37a2b641632af2cb515e3409deed5cd1fa358af5
This commit is contained in:
parent
c8608fd7bc
commit
6d34e344ee
|
@ -1066,38 +1066,31 @@ ve.init.mw.Target.prototype.getEditNotices = function () {
|
|||
ve.init.mw.Target.prototype.setUpSurface = function ( doc, callback ) {
|
||||
var target = this;
|
||||
setTimeout( function () {
|
||||
// Build linmod
|
||||
var store = new ve.dm.IndexValueStore(),
|
||||
internalList = new ve.dm.InternalList(),
|
||||
innerWhitespace = new Array( 2 ),
|
||||
data = ve.dm.converter.getDataFromDom( doc, store, internalList, innerWhitespace );
|
||||
// Build model
|
||||
var dmDoc = ve.dm.converter.getModelFromDom( doc );
|
||||
setTimeout( function () {
|
||||
// Build DM tree
|
||||
var dmDoc = new ve.dm.Document( data, doc, undefined, internalList, innerWhitespace );
|
||||
// Create ui.Surface (also creates ce.Surface and dm.Surface and builds CE tree)
|
||||
target.surface = new ve.ui.Surface( dmDoc, target.surfaceOptions );
|
||||
target.surface.$element.addClass( 've-init-mw-viewPageTarget-surface' );
|
||||
setTimeout( function () {
|
||||
// Create ui.Surface (also creates ce.Surface and dm.Surface and builds CE tree)
|
||||
target.surface = new ve.ui.Surface( dmDoc, target.surfaceOptions );
|
||||
target.surface.$element.addClass( 've-init-mw-viewPageTarget-surface' );
|
||||
setTimeout( function () {
|
||||
// Initialize surface
|
||||
target.surface.getContext().hide();
|
||||
target.$document = target.surface.$element.find( '.ve-ce-documentNode' );
|
||||
target.$element.append( target.surface.$element );
|
||||
target.setUpToolbar();
|
||||
target.$document.attr( {
|
||||
'lang': mw.config.get( 'wgVisualEditor' ).pageLanguageCode,
|
||||
'dir': mw.config.get( 'wgVisualEditor' ).pageLanguageDir
|
||||
} );
|
||||
// Add appropriately mw-content-ltr or mw-content-rtl class
|
||||
target.surface.view.$element.addClass(
|
||||
'mw-content-' + mw.config.get( 'wgVisualEditor' ).pageLanguageDir
|
||||
);
|
||||
target.active = true;
|
||||
// Now that the surface is attached to the document and ready,
|
||||
// let it initialize itself
|
||||
target.surface.initialize();
|
||||
setTimeout( callback );
|
||||
// Initialize surface
|
||||
target.surface.getContext().hide();
|
||||
target.$document = target.surface.$element.find( '.ve-ce-documentNode' );
|
||||
target.$element.append( target.surface.$element );
|
||||
target.setUpToolbar();
|
||||
target.$document.attr( {
|
||||
'lang': mw.config.get( 'wgVisualEditor' ).pageLanguageCode,
|
||||
'dir': mw.config.get( 'wgVisualEditor' ).pageLanguageDir
|
||||
} );
|
||||
// Add appropriately mw-content-ltr or mw-content-rtl class
|
||||
target.surface.view.$element.addClass(
|
||||
'mw-content-' + mw.config.get( 'wgVisualEditor' ).pageLanguageDir
|
||||
);
|
||||
target.active = true;
|
||||
// Now that the surface is attached to the document and ready,
|
||||
// let it initialize itself
|
||||
target.surface.initialize();
|
||||
setTimeout( callback );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -9,8 +9,8 @@ QUnit.module( 've.dm.MWConverter' );
|
|||
|
||||
/* Tests */
|
||||
|
||||
QUnit.test( 'getDataFromDom', function ( assert ) {
|
||||
ve.test.utils.runGetDataFromDomTests( assert, ve.copy( ve.dm.mwExample.domToDataCases ) );
|
||||
QUnit.test( 'getModelFromDom', function ( assert ) {
|
||||
ve.test.utils.runGetModelFromDomTests( assert, ve.copy( ve.dm.mwExample.domToDataCases ) );
|
||||
} );
|
||||
|
||||
QUnit.test( 'getDomFromModel', function ( assert ) {
|
||||
|
|
|
@ -1002,7 +1002,7 @@ ve.ce.Surface.prototype.afterPaste = function () {
|
|||
if ( beforePasteData.context ) {
|
||||
internalListRange = doc.getInternalList().getListNode().getOuterRange();
|
||||
context = new ve.dm.ElementLinearData(
|
||||
store,
|
||||
doc.getStore(),
|
||||
ve.copy( beforePasteData.context )
|
||||
);
|
||||
if ( this.pasteSpecial ) {
|
||||
|
|
|
@ -380,6 +380,20 @@ ve.dm.Converter.prototype.getDomElementFromDataAnnotation = function ( dataAnnot
|
|||
return domElement;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an HTML document to a document model.
|
||||
* @param {HTMLDocument} doc HTML document to convert
|
||||
* @returns {ve.dm.Document} Document model
|
||||
*/
|
||||
ve.dm.Converter.prototype.getModelFromDom = function ( doc ) {
|
||||
var internalList = new ve.dm.InternalList(),
|
||||
innerWhitespace = new Array( 2 ),
|
||||
data = this.getDataFromDom( doc, new ve.dm.IndexValueStore(), internalList, innerWhitespace ),
|
||||
model = new ve.dm.Document( data, doc, undefined, internalList, innerWhitespace );
|
||||
|
||||
return model;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an HTML document to a linear model.
|
||||
* @param {HTMLDocument} doc HTML document to convert
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
* @class
|
||||
* @extends ve.Document
|
||||
* @constructor
|
||||
* @param {HTMLDocument|Array|ve.dm.ElementLinearData|ve.dm.FlatLinearData} data HTML document,
|
||||
* raw linear model data, ElementLinearData or FlatLinearData to be split
|
||||
* @param {Array|ve.dm.ElementLinearData|ve.dm.FlatLinearData} data Raw linear model data,
|
||||
* ElementLinearData or FlatLinearData to be split
|
||||
* @param {HTMLDocument} [htmlDocument] HTML document the data was converted from, if any.
|
||||
* If omitted, a new document will be created. If data is an HTMLDocument, this parameter is
|
||||
* ignored.
|
||||
|
@ -51,10 +51,6 @@ ve.dm.Document = function VeDmDocument( data, htmlDocument, parentDocument, inte
|
|||
} else if ( data instanceof ve.dm.FlatLinearData ) {
|
||||
// Element + Meta linear data
|
||||
fullData = data;
|
||||
} else if ( !ve.isArray( data ) && typeof data === 'object' ) {
|
||||
// HTMLDocument
|
||||
fullData = ve.dm.converter.getDataFromDom( data, new ve.dm.IndexValueStore(), this.getInternalList(), this.getInnerWhitespace() );
|
||||
htmlDocument = data;
|
||||
} else {
|
||||
// Raw linear model data
|
||||
fullData = new ve.dm.FlatLinearData(
|
||||
|
|
|
@ -1367,8 +1367,7 @@ QUnit.test( 'getNearestWordRange', function ( assert ) {
|
|||
} );
|
||||
|
||||
QUnit.test( 'sanitize', function ( assert ) {
|
||||
var i, fullData, result, data,
|
||||
store, internalList, innerWhitespace,
|
||||
var i, model, data,
|
||||
count = 0,
|
||||
bold = new ve.dm.TextStyleBoldAnnotation( { 'type': 'textStyle/bold', 'attributes': { 'nodeName': 'b' } } ),
|
||||
cases = [
|
||||
|
@ -1434,13 +1433,8 @@ QUnit.test( 'sanitize', function ( assert ) {
|
|||
QUnit.expect( count );
|
||||
|
||||
for ( i = 0; i < cases.length; i++ ) {
|
||||
store = new ve.dm.IndexValueStore();
|
||||
internalList = new ve.dm.InternalList();
|
||||
innerWhitespace = new Array( 2 );
|
||||
|
||||
fullData = ve.dm.converter.getDataFromDom( ve.createDocumentFromHtml( cases[i].html ), store, internalList, innerWhitespace );
|
||||
result = ve.dm.Document.static.splitData( fullData, true );
|
||||
data = result.elementData;
|
||||
model = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( cases[i].html ) );
|
||||
data = model.data;
|
||||
data.sanitize( cases[i].rules || {}, cases[i].plainText );
|
||||
assert.deepEqualWithDomElements( data.data, cases[i].data, cases[i].msg + ': data' );
|
||||
if ( cases[i].store ) {
|
||||
|
|
|
@ -39,8 +39,8 @@ QUnit.test( 'getDomElementsFromDataElement', 20, function ( assert ) {
|
|||
}
|
||||
} );
|
||||
|
||||
QUnit.test( 'getDataFromDom', function ( assert ) {
|
||||
ve.test.utils.runGetDataFromDomTests( assert, ve.copy( ve.dm.example.domToDataCases ) );
|
||||
QUnit.test( 'getModelFromDom', function ( assert ) {
|
||||
ve.test.utils.runGetModelFromDomTests( assert, ve.copy( ve.dm.example.domToDataCases ) );
|
||||
} );
|
||||
|
||||
QUnit.test( 'getDomFromModel', function ( assert ) {
|
||||
|
|
|
@ -9,7 +9,7 @@ QUnit.module( 've.dm.Document' );
|
|||
|
||||
/* Tests */
|
||||
|
||||
QUnit.test( 'constructor', 12, function ( assert ) {
|
||||
QUnit.test( 'constructor', 11, function ( assert ) {
|
||||
var data, htmlDoc,
|
||||
doc = ve.dm.example.createExampleDocument();
|
||||
assert.equalNodeTree( doc.getDocumentNode(), ve.dm.example.tree, 'node tree matches example data' );
|
||||
|
@ -38,8 +38,6 @@ QUnit.test( 'constructor', 12, function ( assert ) {
|
|||
htmlDoc = ve.createDocumentFromHtml( 'abcd' );
|
||||
doc = new ve.dm.Document( [ 'a', 'b', 'c', 'd' ], htmlDoc );
|
||||
assert.equal( doc.getHtmlDocument(), htmlDoc, 'Provided HTML document is used' );
|
||||
doc = new ve.dm.Document( htmlDoc, ve.createDocumentFromHtml( 'efgh' ) );
|
||||
assert.equal( doc.getHtmlDocument(), htmlDoc, 'Second parameter ignored if first parameter is a document' );
|
||||
|
||||
data = new ve.dm.ElementLinearData(
|
||||
new ve.dm.IndexValueStore(),
|
||||
|
|
|
@ -52,8 +52,8 @@ ve.test.utils.runFormatConverterTest = function ( assert, range, type, attribute
|
|||
surface.destroy();
|
||||
};
|
||||
|
||||
ve.test.utils.runGetDataFromDomTests = function( assert, cases ) {
|
||||
var msg, doc, store, i, length, hash, data, html, n = 0;
|
||||
ve.test.utils.runGetModelFromDomTests = function( assert, cases ) {
|
||||
var msg, model, i, length, hash, html, n = 0;
|
||||
|
||||
for ( msg in cases ) {
|
||||
if ( cases[msg].head !== undefined || cases[msg].body !== undefined ) {
|
||||
|
@ -67,22 +67,17 @@ ve.test.utils.runGetDataFromDomTests = function( assert, cases ) {
|
|||
|
||||
for ( msg in cases ) {
|
||||
if ( cases[msg].head !== undefined || cases[msg].body !== undefined ) {
|
||||
doc = new ve.dm.Document( [] );
|
||||
store = doc.getStore();
|
||||
|
||||
html = '<head>' + ( cases[msg].head || '' ) + '</head><body>' + cases[msg].body + '</body>';
|
||||
data = ve.dm.converter.getDataFromDom(
|
||||
ve.createDocumentFromHtml( html ), store, doc.getInternalList(), doc.getInnerWhitespace()
|
||||
);
|
||||
ve.dm.example.preprocessAnnotations( cases[msg].data, store );
|
||||
assert.deepEqualWithDomElements( data.getData(), cases[msg].data, msg + ': data' );
|
||||
assert.deepEqual( doc.getInnerWhitespace(), cases[msg].innerWhitespace || new Array( 2 ), msg + ': inner whitespace' );
|
||||
model = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( html ) );
|
||||
ve.dm.example.preprocessAnnotations( cases[msg].data, model.getStore() );
|
||||
assert.deepEqualWithDomElements( model.getFullData(), cases[msg].data, msg + ': data' );
|
||||
assert.deepEqual( model.getInnerWhitespace(), cases[msg].innerWhitespace || new Array( 2 ), msg + ': inner whitespace' );
|
||||
// check storeItems have been added to store
|
||||
if ( cases[msg].storeItems ) {
|
||||
for ( i = 0, length = cases[msg].storeItems.length; i < length; i++ ) {
|
||||
hash = cases[msg].storeItems[i].hash || OO.getHash( cases[msg].storeItems[i].value );
|
||||
assert.deepEqualWithDomElements(
|
||||
store.value( store.indexOfHash( hash ) ) || {},
|
||||
model.getStore().value( model.getStore().indexOfHash( hash ) ) || {},
|
||||
cases[msg].storeItems[i].value,
|
||||
msg + ': store item ' + i + ' found'
|
||||
);
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
* @param {Object} [config] Configuration options
|
||||
*/
|
||||
ve.ui.Surface = function VeUiSurface( dataOrDoc, config ) {
|
||||
var documentModel;
|
||||
|
||||
// Parent constructor
|
||||
OO.ui.Element.call( this, config );
|
||||
|
||||
|
@ -28,9 +30,17 @@ ve.ui.Surface = function VeUiSurface( dataOrDoc, config ) {
|
|||
this.$localOverlayBlockers = this.$( '<div>' );
|
||||
this.$localOverlayControls = this.$( '<div>' );
|
||||
this.$localOverlayMenus = this.$( '<div>' );
|
||||
this.model = new ve.dm.Surface(
|
||||
dataOrDoc instanceof ve.dm.Document ? dataOrDoc : new ve.dm.Document( dataOrDoc )
|
||||
);
|
||||
if ( dataOrDoc instanceof ve.dm.Document ) {
|
||||
// ve.dm.Document
|
||||
documentModel = dataOrDoc;
|
||||
} else if ( dataOrDoc instanceof ve.dm.LinearData || ve.isArray( dataOrDoc ) ) {
|
||||
// LinearData or raw linear data
|
||||
documentModel = new ve.dm.Document( dataOrDoc );
|
||||
} else {
|
||||
// HTMLDocument
|
||||
documentModel = ve.dm.converter.getModelFromDom( dataOrDoc );
|
||||
}
|
||||
this.model = new ve.dm.Surface( documentModel );
|
||||
this.view = new ve.ce.Surface( this.model, this, { '$': this.$ } );
|
||||
this.context = new ve.ui.Context( this, { '$': this.$ } );
|
||||
this.dialogs = new ve.ui.WindowSet( this, ve.ui.dialogFactory, { '$': this.$ } );
|
||||
|
|
Loading…
Reference in a new issue