mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 02:23:58 +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,14 +1066,8 @@ ve.init.mw.Target.prototype.getEditNotices = function () {
|
||||||
ve.init.mw.Target.prototype.setUpSurface = function ( doc, callback ) {
|
ve.init.mw.Target.prototype.setUpSurface = function ( doc, callback ) {
|
||||||
var target = this;
|
var target = this;
|
||||||
setTimeout( function () {
|
setTimeout( function () {
|
||||||
// Build linmod
|
// Build model
|
||||||
var store = new ve.dm.IndexValueStore(),
|
var dmDoc = ve.dm.converter.getModelFromDom( doc );
|
||||||
internalList = new ve.dm.InternalList(),
|
|
||||||
innerWhitespace = new Array( 2 ),
|
|
||||||
data = ve.dm.converter.getDataFromDom( doc, store, internalList, innerWhitespace );
|
|
||||||
setTimeout( function () {
|
|
||||||
// Build DM tree
|
|
||||||
var dmDoc = new ve.dm.Document( data, doc, undefined, internalList, innerWhitespace );
|
|
||||||
setTimeout( function () {
|
setTimeout( function () {
|
||||||
// Create ui.Surface (also creates ce.Surface and dm.Surface and builds CE tree)
|
// 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 = new ve.ui.Surface( dmDoc, target.surfaceOptions );
|
||||||
|
@ -1100,7 +1094,6 @@ ve.init.mw.Target.prototype.setUpSurface = function ( doc, callback ) {
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,8 +9,8 @@ QUnit.module( 've.dm.MWConverter' );
|
||||||
|
|
||||||
/* Tests */
|
/* Tests */
|
||||||
|
|
||||||
QUnit.test( 'getDataFromDom', function ( assert ) {
|
QUnit.test( 'getModelFromDom', function ( assert ) {
|
||||||
ve.test.utils.runGetDataFromDomTests( assert, ve.copy( ve.dm.mwExample.domToDataCases ) );
|
ve.test.utils.runGetModelFromDomTests( assert, ve.copy( ve.dm.mwExample.domToDataCases ) );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( 'getDomFromModel', function ( assert ) {
|
QUnit.test( 'getDomFromModel', function ( assert ) {
|
||||||
|
|
|
@ -1002,7 +1002,7 @@ ve.ce.Surface.prototype.afterPaste = function () {
|
||||||
if ( beforePasteData.context ) {
|
if ( beforePasteData.context ) {
|
||||||
internalListRange = doc.getInternalList().getListNode().getOuterRange();
|
internalListRange = doc.getInternalList().getListNode().getOuterRange();
|
||||||
context = new ve.dm.ElementLinearData(
|
context = new ve.dm.ElementLinearData(
|
||||||
store,
|
doc.getStore(),
|
||||||
ve.copy( beforePasteData.context )
|
ve.copy( beforePasteData.context )
|
||||||
);
|
);
|
||||||
if ( this.pasteSpecial ) {
|
if ( this.pasteSpecial ) {
|
||||||
|
|
|
@ -380,6 +380,20 @@ ve.dm.Converter.prototype.getDomElementFromDataAnnotation = function ( dataAnnot
|
||||||
return domElement;
|
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.
|
* Convert an HTML document to a linear model.
|
||||||
* @param {HTMLDocument} doc HTML document to convert
|
* @param {HTMLDocument} doc HTML document to convert
|
||||||
|
|
|
@ -16,8 +16,8 @@
|
||||||
* @class
|
* @class
|
||||||
* @extends ve.Document
|
* @extends ve.Document
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {HTMLDocument|Array|ve.dm.ElementLinearData|ve.dm.FlatLinearData} data HTML document,
|
* @param {Array|ve.dm.ElementLinearData|ve.dm.FlatLinearData} data Raw linear model data,
|
||||||
* raw linear model data, ElementLinearData or FlatLinearData to be split
|
* ElementLinearData or FlatLinearData to be split
|
||||||
* @param {HTMLDocument} [htmlDocument] HTML document the data was converted from, if any.
|
* @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
|
* If omitted, a new document will be created. If data is an HTMLDocument, this parameter is
|
||||||
* ignored.
|
* ignored.
|
||||||
|
@ -51,10 +51,6 @@ ve.dm.Document = function VeDmDocument( data, htmlDocument, parentDocument, inte
|
||||||
} else if ( data instanceof ve.dm.FlatLinearData ) {
|
} else if ( data instanceof ve.dm.FlatLinearData ) {
|
||||||
// Element + Meta linear data
|
// Element + Meta linear data
|
||||||
fullData = 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 {
|
} else {
|
||||||
// Raw linear model data
|
// Raw linear model data
|
||||||
fullData = new ve.dm.FlatLinearData(
|
fullData = new ve.dm.FlatLinearData(
|
||||||
|
|
|
@ -1367,8 +1367,7 @@ QUnit.test( 'getNearestWordRange', function ( assert ) {
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( 'sanitize', function ( assert ) {
|
QUnit.test( 'sanitize', function ( assert ) {
|
||||||
var i, fullData, result, data,
|
var i, model, data,
|
||||||
store, internalList, innerWhitespace,
|
|
||||||
count = 0,
|
count = 0,
|
||||||
bold = new ve.dm.TextStyleBoldAnnotation( { 'type': 'textStyle/bold', 'attributes': { 'nodeName': 'b' } } ),
|
bold = new ve.dm.TextStyleBoldAnnotation( { 'type': 'textStyle/bold', 'attributes': { 'nodeName': 'b' } } ),
|
||||||
cases = [
|
cases = [
|
||||||
|
@ -1434,13 +1433,8 @@ QUnit.test( 'sanitize', function ( assert ) {
|
||||||
QUnit.expect( count );
|
QUnit.expect( count );
|
||||||
|
|
||||||
for ( i = 0; i < cases.length; i++ ) {
|
for ( i = 0; i < cases.length; i++ ) {
|
||||||
store = new ve.dm.IndexValueStore();
|
model = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( cases[i].html ) );
|
||||||
internalList = new ve.dm.InternalList();
|
data = model.data;
|
||||||
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;
|
|
||||||
data.sanitize( cases[i].rules || {}, cases[i].plainText );
|
data.sanitize( cases[i].rules || {}, cases[i].plainText );
|
||||||
assert.deepEqualWithDomElements( data.data, cases[i].data, cases[i].msg + ': data' );
|
assert.deepEqualWithDomElements( data.data, cases[i].data, cases[i].msg + ': data' );
|
||||||
if ( cases[i].store ) {
|
if ( cases[i].store ) {
|
||||||
|
|
|
@ -39,8 +39,8 @@ QUnit.test( 'getDomElementsFromDataElement', 20, function ( assert ) {
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( 'getDataFromDom', function ( assert ) {
|
QUnit.test( 'getModelFromDom', function ( assert ) {
|
||||||
ve.test.utils.runGetDataFromDomTests( assert, ve.copy( ve.dm.example.domToDataCases ) );
|
ve.test.utils.runGetModelFromDomTests( assert, ve.copy( ve.dm.example.domToDataCases ) );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( 'getDomFromModel', function ( assert ) {
|
QUnit.test( 'getDomFromModel', function ( assert ) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ QUnit.module( 've.dm.Document' );
|
||||||
|
|
||||||
/* Tests */
|
/* Tests */
|
||||||
|
|
||||||
QUnit.test( 'constructor', 12, function ( assert ) {
|
QUnit.test( 'constructor', 11, function ( assert ) {
|
||||||
var data, htmlDoc,
|
var data, htmlDoc,
|
||||||
doc = ve.dm.example.createExampleDocument();
|
doc = ve.dm.example.createExampleDocument();
|
||||||
assert.equalNodeTree( doc.getDocumentNode(), ve.dm.example.tree, 'node tree matches example data' );
|
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' );
|
htmlDoc = ve.createDocumentFromHtml( 'abcd' );
|
||||||
doc = new ve.dm.Document( [ 'a', 'b', 'c', 'd' ], htmlDoc );
|
doc = new ve.dm.Document( [ 'a', 'b', 'c', 'd' ], htmlDoc );
|
||||||
assert.equal( doc.getHtmlDocument(), htmlDoc, 'Provided HTML document is used' );
|
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(
|
data = new ve.dm.ElementLinearData(
|
||||||
new ve.dm.IndexValueStore(),
|
new ve.dm.IndexValueStore(),
|
||||||
|
|
|
@ -52,8 +52,8 @@ ve.test.utils.runFormatConverterTest = function ( assert, range, type, attribute
|
||||||
surface.destroy();
|
surface.destroy();
|
||||||
};
|
};
|
||||||
|
|
||||||
ve.test.utils.runGetDataFromDomTests = function( assert, cases ) {
|
ve.test.utils.runGetModelFromDomTests = function( assert, cases ) {
|
||||||
var msg, doc, store, i, length, hash, data, html, n = 0;
|
var msg, model, i, length, hash, html, n = 0;
|
||||||
|
|
||||||
for ( msg in cases ) {
|
for ( msg in cases ) {
|
||||||
if ( cases[msg].head !== undefined || cases[msg].body !== undefined ) {
|
if ( cases[msg].head !== undefined || cases[msg].body !== undefined ) {
|
||||||
|
@ -67,22 +67,17 @@ ve.test.utils.runGetDataFromDomTests = function( assert, cases ) {
|
||||||
|
|
||||||
for ( msg in cases ) {
|
for ( msg in cases ) {
|
||||||
if ( cases[msg].head !== undefined || cases[msg].body !== undefined ) {
|
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>';
|
html = '<head>' + ( cases[msg].head || '' ) + '</head><body>' + cases[msg].body + '</body>';
|
||||||
data = ve.dm.converter.getDataFromDom(
|
model = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( html ) );
|
||||||
ve.createDocumentFromHtml( html ), store, doc.getInternalList(), doc.getInnerWhitespace()
|
ve.dm.example.preprocessAnnotations( cases[msg].data, model.getStore() );
|
||||||
);
|
assert.deepEqualWithDomElements( model.getFullData(), cases[msg].data, msg + ': data' );
|
||||||
ve.dm.example.preprocessAnnotations( cases[msg].data, store );
|
assert.deepEqual( model.getInnerWhitespace(), cases[msg].innerWhitespace || new Array( 2 ), msg + ': inner whitespace' );
|
||||||
assert.deepEqualWithDomElements( data.getData(), cases[msg].data, msg + ': data' );
|
|
||||||
assert.deepEqual( doc.getInnerWhitespace(), cases[msg].innerWhitespace || new Array( 2 ), msg + ': inner whitespace' );
|
|
||||||
// check storeItems have been added to store
|
// check storeItems have been added to store
|
||||||
if ( cases[msg].storeItems ) {
|
if ( cases[msg].storeItems ) {
|
||||||
for ( i = 0, length = cases[msg].storeItems.length; i < length; i++ ) {
|
for ( i = 0, length = cases[msg].storeItems.length; i < length; i++ ) {
|
||||||
hash = cases[msg].storeItems[i].hash || OO.getHash( cases[msg].storeItems[i].value );
|
hash = cases[msg].storeItems[i].hash || OO.getHash( cases[msg].storeItems[i].value );
|
||||||
assert.deepEqualWithDomElements(
|
assert.deepEqualWithDomElements(
|
||||||
store.value( store.indexOfHash( hash ) ) || {},
|
model.getStore().value( model.getStore().indexOfHash( hash ) ) || {},
|
||||||
cases[msg].storeItems[i].value,
|
cases[msg].storeItems[i].value,
|
||||||
msg + ': store item ' + i + ' found'
|
msg + ': store item ' + i + ' found'
|
||||||
);
|
);
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
* @param {Object} [config] Configuration options
|
* @param {Object} [config] Configuration options
|
||||||
*/
|
*/
|
||||||
ve.ui.Surface = function VeUiSurface( dataOrDoc, config ) {
|
ve.ui.Surface = function VeUiSurface( dataOrDoc, config ) {
|
||||||
|
var documentModel;
|
||||||
|
|
||||||
// Parent constructor
|
// Parent constructor
|
||||||
OO.ui.Element.call( this, config );
|
OO.ui.Element.call( this, config );
|
||||||
|
|
||||||
|
@ -28,9 +30,17 @@ ve.ui.Surface = function VeUiSurface( dataOrDoc, config ) {
|
||||||
this.$localOverlayBlockers = this.$( '<div>' );
|
this.$localOverlayBlockers = this.$( '<div>' );
|
||||||
this.$localOverlayControls = this.$( '<div>' );
|
this.$localOverlayControls = this.$( '<div>' );
|
||||||
this.$localOverlayMenus = this.$( '<div>' );
|
this.$localOverlayMenus = this.$( '<div>' );
|
||||||
this.model = new ve.dm.Surface(
|
if ( dataOrDoc instanceof ve.dm.Document ) {
|
||||||
dataOrDoc instanceof ve.dm.Document ? dataOrDoc : new ve.dm.Document( dataOrDoc )
|
// 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.view = new ve.ce.Surface( this.model, this, { '$': this.$ } );
|
||||||
this.context = new ve.ui.Context( this, { '$': this.$ } );
|
this.context = new ve.ui.Context( this, { '$': this.$ } );
|
||||||
this.dialogs = new ve.ui.WindowSet( this, ve.ui.dialogFactory, { '$': this.$ } );
|
this.dialogs = new ve.ui.WindowSet( this, ve.ui.dialogFactory, { '$': this.$ } );
|
||||||
|
|
Loading…
Reference in a new issue