Esanders has uploaded a new change for review.

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


Change subject: Move common linear data methods into LinearData.static
......................................................................

Move common linear data methods into LinearData.static

Some methods apply to all types of LinearData. Also moved to
static so they can be used by the converter on raw data.

Change-Id: I79066d6d3ccde48aea7c0848d56ef86bc70f6656
---
M modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
M modules/ve/dm/ve.dm.LinearData.js
2 files changed, 59 insertions(+), 17 deletions(-)


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

diff --git a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js 
b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
index b608208..1e2dc2a 100644
--- a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
+++ b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
@@ -28,35 +28,22 @@
 
 /**
  * Get the type of the element at a specified offset
- *
- * This will return the same string for close and open elements.
- *
  * @method
  * @param {number} offset Document offset
  * @returns {string} Type of the element
  */
 ve.dm.ElementLinearData.prototype.getType = function ( offset ) {
-       var item = this.getData( offset );
-       return this.isCloseElementData( offset ) ? item.type.substr( 1 ) : 
item.type;
+       return ve.dm.LinearData.static.getType( this.getData( offset ) );
 };
 
 /**
  * Check if data at a given offset is an element.
- *
- * This method assumes that any value that has a type property that's a string 
is an element object.
- *
- * Element data:
- *      <heading> a </heading> <paragraph> b c <img></img> </paragraph>
- *     ^         . ^          ^           . . ^     ^     ^            .
- *
  * @method
  * @param {number} offset Document offset
  * @returns {boolean} Data at offset is an element
  */
 ve.dm.ElementLinearData.prototype.isElementData = function ( offset ) {
-       var item = this.getData( offset );
-       // Data exists at offset and appears to be an element
-       return item !== undefined && typeof item.type === 'string';
+       return ve.dm.LinearData.static.isElementData( this.getData( offset ) );
 };
 
 /**
@@ -66,7 +53,7 @@
  * @returns {boolean} Data at offset is an open element
  */
 ve.dm.ElementLinearData.prototype.isOpenElementData = function ( offset ) {
-       return this.isElementData( offset ) && this.getData( offset 
).type.charAt( 0 ) !== '/';
+       return ve.dm.LinearData.static.isOpenElementData( this.getData( offset 
) );
 };
 
 /**
@@ -76,7 +63,7 @@
  * @returns {boolean} Data at offset is a close element
  */
 ve.dm.ElementLinearData.prototype.isCloseElementData = function ( offset ) {
-       return this.isElementData( offset ) && this.getData( offset 
).type.charAt( 0 ) === '/';
+       return ve.dm.LinearData.static.isCloseElementData( this.getData( offset 
) );
 };
 
 /**
diff --git a/modules/ve/dm/ve.dm.LinearData.js 
b/modules/ve/dm/ve.dm.LinearData.js
index 1535db4..98bb7fb 100644
--- a/modules/ve/dm/ve.dm.LinearData.js
+++ b/modules/ve/dm/ve.dm.LinearData.js
@@ -20,6 +20,61 @@
        this.data = data || [];
 };
 
+/* Static Methods */
+
+ve.dm.LinearData.static = {};
+
+/**
+ * Get the type of an element
+ *
+ * This will return the same string for close and open elements.
+ *
+ * @method
+ * @param {Object} item Element item
+ * @returns {string} Type of the element
+ */
+ve.dm.LinearData.static.getType = function ( item ) {
+       return this.isCloseElementData( item ) ? item.type.substr( 1 ) : 
item.type;
+};
+
+/**
+ * Check if data item is an element.
+ *
+ * This method assumes that any value that has a type property that's a string 
is an element object.
+ *
+ * Element data:
+ *      <heading> a </heading> <paragraph> b c <img></img> </paragraph>
+ *     ^         . ^          ^           . . ^     ^     ^            .
+ *
+ * @method
+ * @param {Object|Array|string} item Linear data item
+ * @returns {boolean} Item is an element
+ */
+ve.dm.LinearData.static.isElementData = function ( item ) {
+       // Data exists and appears to be an element
+       return item !== undefined && typeof item.type === 'string';
+};
+
+/**
+ * Checks if data item is an open element.
+ * @method
+ * @param {Object} item Element item
+ * @returns {boolean} Item is an open element
+ */
+ve.dm.LinearData.static.isOpenElementData = function ( item ) {
+       return this.isElementData( item ) && item.type.charAt( 0 ) !== '/';
+};
+
+/**
+ * Checks if data item is a close element.
+ * @method
+ * @param {Object} item Element item
+ * @returns {boolean} Item is a close element
+ */
+ve.dm.LinearData.static.isCloseElementData = function ( item ) {
+       return this.isElementData( item ) && item.type.charAt( 0 ) === '/';
+};
+
 /* Methods */
 
 /**

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

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

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

Reply via email to