Przeglądaj źródła

Merge branch 'master' into t/ckeditor5/479

Aleksander Nowodzinski 7 lat temu
rodzic
commit
38126c291a

+ 15 - 15
packages/ckeditor5-engine/src/conversion/conversion.js

@@ -141,13 +141,13 @@ export default class Conversion {
 	 * For example, the model `<paragraph>Foo</paragraph>` is `<p>Foo</p>` in the view.
 	 *
 	 *		// A simple conversion from the `paragraph` model element to the `<p>` view element (and vice versa).
-	 *		conversion.elementToElement( { model: 'paragraph', view: 'p' } );
+	 *		editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
 	 *
 	 *		// Override other converters by specifying a converter definition with a higher priority.
-	 *		conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
+	 *		editor.conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
 	 *
 	 *		// View specified as an object instead of a string.
-	 *		conversion.elementToElement( {
+	 *		editor.conversion.elementToElement( {
 	 *			model: 'fancyParagraph',
 	 *			view: {
 	 *				name: 'p',
@@ -156,7 +156,7 @@ export default class Conversion {
 	 *		} );
 	 *
 	 *		// Use `upcastAlso` to define other view elements that should also be converted to a `paragraph` element.
-	 *		conversion.elementToElement( {
+	 *		editor.conversion.elementToElement( {
 	 *			model: 'paragraph',
 	 *			view: 'p',
 	 *			upcastAlso: [
@@ -171,7 +171,7 @@ export default class Conversion {
 	 *		} );
 	 *
 	 *		// `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
-	 *		conversion.elementToElement( {
+	 *		editor.conversion.elementToElement( {
 	 *			model: 'heading',
 	 *			view: 'h2',
 	 *			// Convert "headling-like" paragraphs to headings.
@@ -227,13 +227,13 @@ export default class Conversion {
 	 * For example, a model text node with `"Foo"` as data and the `bold` attribute is `<strong>Foo</strong>` in the view.
 	 *
 	 *		// A simple conversion from the `bold=true` attribute to the `<strong>` view element (and vice versa).
-	 *		conversion.attributeToElement( { model: 'bold', view: 'strong' } );
+	 *		editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
 	 *
 	 *		// Override other converters by specifying a converter definition with a higher priority.
-	 *		conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
+	 *		editor.conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
 	 *
 	 *		// View specified as an object instead of a string.
-	 *		conversion.attributeToElement( {
+	 *		editor.conversion.attributeToElement( {
 	 *			model: 'bold',
 	 *			view: {
 	 *				name: 'span',
@@ -243,7 +243,7 @@ export default class Conversion {
 	 *
 	 *		// Use `config.model.name` to define the conversion only from a given node type, `$text` in this case.
 	 *		// The same attribute on different elements may then be handled by a different converter.
-	 *		conversion.attributeToElement( {
+	 *		editor.conversion.attributeToElement( {
 	 *			model: {
 	 *				key: 'textDecoration',
 	 *				values: [ 'underline', 'lineThrough' ],
@@ -266,7 +266,7 @@ export default class Conversion {
 	 *		} );
 	 *
 	 *		// Use `upcastAlso` to define other view elements that should also be converted to the `bold` attribute.
-	 *		conversion.attributeToElement( {
+	 *		editor.conversion.attributeToElement( {
 	 *			model: 'bold',
 	 *			view: 'strong',
 	 *			upcastAlso: [
@@ -300,7 +300,7 @@ export default class Conversion {
 	 *
 	 *		// Conversion from and to a model attribute key whose value is an enum (`fontSize=big|small`).
 	 *		// `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
-	 *		conversion.attributeToElement( {
+	 *		editor.conversion.attributeToElement( {
 	 *			model: {
 	 *				key: 'fontSize',
 	 *				values: [ 'big', 'small' ]
@@ -402,10 +402,10 @@ export default class Conversion {
 	 * should be set up.
 	 *
 	 *		// A simple conversion from the `source` model attribute to the `src` view attribute (and vice versa).
-	 *		conversion.attributeToAttribute( { model: 'source', view: 'src' } );
+	 *		editor.conversion.attributeToAttribute( { model: 'source', view: 'src' } );
 	 *
 	 *		// Attribute values are strictly specified.
-	 *		conversion.attributeToAttribute( {
+	 *		editor.conversion.attributeToAttribute( {
 	 *			model: {
 	 *				name: 'image',
 	 *				key: 'aside',
@@ -421,7 +421,7 @@ export default class Conversion {
 	 *		} );
 	 *
 	 *		// Set the style attribute.
-	 *		conversion.attributeToAttribute( {
+	 *		editor.conversion.attributeToAttribute( {
 	 *			model: {
 	 *				name: 'image',
 	 *				key: 'aside',
@@ -442,7 +442,7 @@ export default class Conversion {
 	 *
 	 *		// Conversion from and to a model attribute key whose value is an enum (`align=right|center`).
 	 *		// Use `upcastAlso` to define other view elements that should also be converted to the `align=right` attribute.
-	 *		conversion.attributeToAttribute( {
+	 *		editor.conversion.attributeToAttribute( {
 	 *			model: {
 	 *				key: 'align',
 	 *				values: [ 'right', 'center' ]

+ 10 - 0
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -43,10 +43,15 @@ import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
 /**
  * Writes the content of a model {@link module:engine/model/document~Document document} to an HTML-like string.
  *
+ *		getData( editor.model ); // -> '<paragraph>Foo![]</paragraph>'
+ *
  * **Note:** A {@link module:engine/model/text~Text text} node that contains attributes will be represented as:
  *
  *		<$text attribute="value">Text data</$text>
  *
+ * **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
+ * debugging and testing.
+ *
  * @param {module:engine/model/model~Model} model
  * @param {Object} [options]
  * @param {Boolean} [options.withoutSelection=false] Whether to write the selection. When set to `true`, the selection will
@@ -77,6 +82,8 @@ getData._stringify = stringify;
 /**
  * Sets the content of a model {@link module:engine/model/document~Document document} provided as an HTML-like string.
  *
+ *		setData( editor.model, '<paragraph>Foo![]</paragraph>' );
+ *
  * **Note:** Remember to register elements in the {@link module:engine/model/model~Model#schema model's schema} before
  * trying to use them.
  *
@@ -84,6 +91,9 @@ getData._stringify = stringify;
  *
  *		<$text attribute="value">Text data</$text>
  *
+ * **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
+ * debugging and testing.
+ *
  * @param {module:engine/model/model~Model} model
  * @param {String} data HTML-like string to write into the document.
  * @param {Object} options

+ 37 - 37
packages/ckeditor5-engine/src/model/schema.js

@@ -530,42 +530,6 @@ export default class Schema {
 		}
 	}
 
-	/**
-	 * Takes a flat range and an attribute name. Traverses the range recursively and deeply to find and return all ranges
-	 * inside the given range on which the attribute can be applied.
-	 *
-	 * This is a helper function for {@link ~Schema#getValidRanges}.
-	 *
-	 * @private
-	 * @param {module:engine/model/range~Range} range Range to process.
-	 * @param {String} attribute The name of the attribute to check.
-	 * @returns {Iterable.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
-	 */
-	* _getValidRangesForRange( range, attribute ) {
-		let start = range.start;
-		let end = range.start;
-
-		for ( const item of range.getItems( { shallow: true } ) ) {
-			if ( item.is( 'element' ) ) {
-				yield* this._getValidRangesForRange( Range._createIn( item ), attribute );
-			}
-
-			if ( !this.checkAttribute( item, attribute ) ) {
-				if ( !start.isEqual( end ) ) {
-					yield new Range( start, end );
-				}
-
-				start = Position._createAfter( item );
-			}
-
-			end = Position._createAfter( item );
-		}
-
-		if ( !start.isEqual( end ) ) {
-			yield new Range( start, end );
-		}
-	}
-
 	/**
 	 * Basing on given `position`, finds and returns a {@link module:engine/model/range~Range range} which is
 	 * nearest to that `position` and is a correct range for selection.
@@ -624,7 +588,7 @@ export default class Schema {
 	 * as long as {@link module:engine/model/schema~Schema#isLimit limit element},
 	 * {@link module:engine/model/schema~Schema#isObject object element} or top-most ancestor won't be reached.
 	 *
-	 * @params {module:engine/model/node~Node} node Node for which allowed parent should be found.
+	 * @params {module:engine/model/node~Node|String} node Node for which allowed parent should be found or its name.
 	 * @params {module:engine/model/position~Position} position Position from searching will start.
 	 * @returns {module:engine/model/element~Element|null} element Allowed parent or null if nothing was found.
 	 */
@@ -738,6 +702,42 @@ export default class Schema {
 			return false;
 		}
 	}
+
+	/**
+	 * Takes a flat range and an attribute name. Traverses the range recursively and deeply to find and return all ranges
+	 * inside the given range on which the attribute can be applied.
+	 *
+	 * This is a helper function for {@link ~Schema#getValidRanges}.
+	 *
+	 * @private
+	 * @param {module:engine/model/range~Range} range Range to process.
+	 * @param {String} attribute The name of the attribute to check.
+	 * @returns {Iterable.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
+	 */
+	* _getValidRangesForRange( range, attribute ) {
+		let start = range.start;
+		let end = range.start;
+
+		for ( const item of range.getItems( { shallow: true } ) ) {
+			if ( item.is( 'element' ) ) {
+				yield* this._getValidRangesForRange( Range._createIn( item ), attribute );
+			}
+
+			if ( !this.checkAttribute( item, attribute ) ) {
+				if ( !start.isEqual( end ) ) {
+					yield new Range( start, end );
+				}
+
+				start = Position._createAfter( item );
+			}
+
+			end = Position._createAfter( item );
+		}
+
+		if ( !start.isEqual( end ) ) {
+			yield new Range( start, end );
+		}
+	}
 }
 
 mix( Schema, ObservableMixin );

+ 12 - 0
packages/ckeditor5-engine/tests/model/schema.js

@@ -1579,6 +1579,12 @@ describe( 'Schema', () => {
 			expect( allowedParent ).to.equal( r1bQ );
 		} );
 
+		it( 'should return position ancestor that allows to insert given node to it - works with a string too', () => {
+			const allowedParent = schema.findAllowedParent( 'paragraph', Position._createAt( r1bQp, 0 ) );
+
+			expect( allowedParent ).to.equal( r1bQ );
+		} );
+
 		it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
 			const node = new Text( 'text' );
 
@@ -1622,6 +1628,12 @@ describe( 'Schema', () => {
 
 			expect( parent ).to.null;
 		} );
+
+		it( 'should return null when there is no allowed ancestor for given position – works with a string too', () => {
+			const parent = schema.findAllowedParent( 'section', Position._createAt( r1bQp, 0 ) );
+
+			expect( parent ).to.null;
+		} );
 	} );
 
 	describe( 'removeDisallowedAttributes()', () => {