ckeditor/ckeditor5-engine

View on GitHub
src/view/downcastwriter.js

Summary

Maintainability
F
6 days
Test Coverage

File downcastwriter.js has 668 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/**
 * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

Severity: Major
Found in src/view/downcastwriter.js - About 1 day to fix

    DowncastWriter has 46 functions (exceeds 20 allowed). Consider refactoring.
    Open

    export default class DowncastWriter {
        /**
         * @param {module:engine/view/document~Document} document The view document instance.
         */
        constructor( document ) {
    Severity: Minor
    Found in src/view/downcastwriter.js - About 6 hrs to fix

      Function _wrapAttributeElement has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
      Open

          _wrapAttributeElement( wrapper, toWrap ) {
              if ( !canBeJoined( wrapper, toWrap ) ) {
                  return false;
              }
      
      
      Severity: Minor
      Found in src/view/downcastwriter.js - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function clear has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
      Open

          clear( range, element ) {
              validateRangeContainer( range, this.document );
      
              // Create walker on given range.
              // We walk backward because when we remove element during walk it modifies range end position.
      Severity: Minor
      Found in src/view/downcastwriter.js - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function _breakAttributes has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          _breakAttributes( position, forceSplitText = false ) {
              const positionOffset = position.offset;
              const positionParent = position.parent;
      
              // If position is placed inside EmptyElement - throw an exception as we cannot break inside.
      Severity: Minor
      Found in src/view/downcastwriter.js - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function _unwrapChildren has 46 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          _unwrapChildren( parent, startOffset, endOffset, unwrapElement ) {
              let i = startOffset;
              const unwrapPositions = [];
      
              // Iterate over each element between provided offsets inside parent.
      Severity: Minor
      Found in src/view/downcastwriter.js - About 1 hr to fix

        Function _unwrapChildren has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

            _unwrapChildren( parent, startOffset, endOffset, unwrapElement ) {
                let i = startOffset;
                const unwrapPositions = [];
        
                // Iterate over each element between provided offsets inside parent.
        Severity: Minor
        Found in src/view/downcastwriter.js - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function _unwrapAttributeElement has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

            _unwrapAttributeElement( wrapper, toUnwrap ) {
                if ( !canBeJoined( wrapper, toUnwrap ) ) {
                    return false;
                }
        
        
        Severity: Minor
        Found in src/view/downcastwriter.js - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function _wrapChildren has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
        Open

            _wrapChildren( parent, startOffset, endOffset, wrapElement ) {
                let i = startOffset;
                const wrapPositions = [];
        
                while ( i < endOffset ) {
        Severity: Minor
        Found in src/view/downcastwriter.js - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function _breakAttributes has 38 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            _breakAttributes( position, forceSplitText = false ) {
                const positionOffset = position.offset;
                const positionParent = position.parent;
        
                // If position is placed inside EmptyElement - throw an exception as we cannot break inside.
        Severity: Minor
        Found in src/view/downcastwriter.js - About 1 hr to fix

          Function _wrapAttributeElement has 38 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              _wrapAttributeElement( wrapper, toWrap ) {
                  if ( !canBeJoined( wrapper, toWrap ) ) {
                      return false;
                  }
          
          
          Severity: Minor
          Found in src/view/downcastwriter.js - About 1 hr to fix

            Function _wrapChildren has 37 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                _wrapChildren( parent, startOffset, endOffset, wrapElement ) {
                    let i = startOffset;
                    const wrapPositions = [];
            
                    while ( i < endOffset ) {
            Severity: Minor
            Found in src/view/downcastwriter.js - About 1 hr to fix

              Function _unwrapAttributeElement has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  _unwrapAttributeElement( wrapper, toUnwrap ) {
                      if ( !canBeJoined( wrapper, toUnwrap ) ) {
                          return false;
                      }
              
              
              Severity: Minor
              Found in src/view/downcastwriter.js - About 1 hr to fix

                Function mergeAttributes has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    mergeAttributes( position ) {
                        const positionOffset = position.offset;
                        const positionParent = position.parent;
                
                        // When inside text node - nothing to merge.
                Severity: Minor
                Found in src/view/downcastwriter.js - About 1 hr to fix

                  Function clear has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      clear( range, element ) {
                          validateRangeContainer( range, this.document );
                  
                          // Create walker on given range.
                          // We walk backward because when we remove element during walk it modifies range end position.
                  Severity: Minor
                  Found in src/view/downcastwriter.js - About 1 hr to fix

                    Function mergeAttributes has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                    Open

                        mergeAttributes( position ) {
                            const positionOffset = position.offset;
                            const positionParent = position.parent;
                    
                            // When inside text node - nothing to merge.
                    Severity: Minor
                    Found in src/view/downcastwriter.js - About 1 hr to fix

                    Cognitive Complexity

                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                    A method's cognitive complexity is based on a few simple rules:

                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                    • Code is considered more complex for each "break in the linear flow of the code"
                    • Code is considered more complex when "flow breaking structures are nested"

                    Further reading

                    Function wrap has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                    Open

                        wrap( range, attribute ) {
                            if ( !( attribute instanceof AttributeElement ) ) {
                                throw new CKEditorError( 'view-writer-wrap-invalid-attribute', this.document );
                            }
                    
                    
                    Severity: Minor
                    Found in src/view/downcastwriter.js - About 35 mins to fix

                    Cognitive Complexity

                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                    A method's cognitive complexity is based on a few simple rules:

                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                    • Code is considered more complex for each "break in the linear flow of the code"
                    • Code is considered more complex when "flow breaking structures are nested"

                    Further reading

                    Function insert has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                    Open

                        insert( position, nodes ) {
                            nodes = isIterable( nodes ) ? [ ...nodes ] : [ nodes ];
                    
                            // Check if nodes to insert are instances of AttributeElements, ContainerElements, EmptyElements, UIElements or Text.
                            validateNodesToInsert( nodes, this.document );
                    Severity: Minor
                    Found in src/view/downcastwriter.js - About 35 mins to fix

                    Cognitive Complexity

                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                    A method's cognitive complexity is based on a few simple rules:

                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                    • Code is considered more complex for each "break in the linear flow of the code"
                    • Code is considered more complex when "flow breaking structures are nested"

                    Further reading

                    Function _addToClonedElementsGroup has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                    Open

                        _addToClonedElementsGroup( element ) {
                            // Add only if the element is in document tree.
                            if ( !element.root.is( 'rootElement' ) ) {
                                return;
                            }
                    Severity: Minor
                    Found in src/view/downcastwriter.js - About 35 mins to fix

                    Cognitive Complexity

                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                    A method's cognitive complexity is based on a few simple rules:

                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                    • Code is considered more complex for each "break in the linear flow of the code"
                    • Code is considered more complex when "flow breaking structures are nested"

                    Further reading

                    Avoid too many return statements within this function.
                    Open

                                return this.mergeAttributes( new Position( nodeBefore, count ) );
                    Severity: Major
                    Found in src/view/downcastwriter.js - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                              return true;
                      Severity: Major
                      Found in src/view/downcastwriter.js - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                                return position;
                        Severity: Major
                        Found in src/view/downcastwriter.js - About 30 mins to fix

                          Avoid too many return statements within this function.
                          Open

                                  return true;
                          Severity: Major
                          Found in src/view/downcastwriter.js - About 30 mins to fix

                            Avoid too many return statements within this function.
                            Open

                                            return false;
                            Severity: Major
                            Found in src/view/downcastwriter.js - About 30 mins to fix

                              Avoid too many return statements within this function.
                              Open

                                              return this._breakAttributes( newPosition, forceSplitText );
                              Severity: Major
                              Found in src/view/downcastwriter.js - About 30 mins to fix

                                Avoid too many return statements within this function.
                                Open

                                                return this._breakAttributes( newPosition, forceSplitText );
                                Severity: Major
                                Found in src/view/downcastwriter.js - About 30 mins to fix

                                  Function validateNodesToInsert has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                  function validateNodesToInsert( nodes, errorContext ) {
                                      for ( const node of nodes ) {
                                          if ( !validNodesToInsert.some( ( validNode => node instanceof validNode ) ) ) { // eslint-disable-line no-use-before-define
                                              /**
                                               * Inserted nodes should be valid to insert. of {@link module:engine/view/attributeelement~AttributeElement AttributeElement},
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js - About 25 mins to fix

                                  Cognitive Complexity

                                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                  A method's cognitive complexity is based on a few simple rules:

                                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                  • Code is considered more complex for each "break in the linear flow of the code"
                                  • Code is considered more complex when "flow breaking structures are nested"

                                  Further reading

                                  Function _removeFromClonedElementsGroup has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      _removeFromClonedElementsGroup( element ) {
                                          // Traverse the element's children recursively to find other attribute elements that also got removed.
                                          // The loop is at the beginning so we can make fast returns later in the code.
                                          if ( element.is( 'element' ) ) {
                                              for ( const child of element.getChildren() ) {
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js - About 25 mins to fix

                                  Cognitive Complexity

                                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                  A method's cognitive complexity is based on a few simple rules:

                                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                  • Code is considered more complex for each "break in the linear flow of the code"
                                  • Code is considered more complex when "flow breaking structures are nested"

                                  Further reading

                                  Similar blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                      setStyle( property, value, element ) {
                                          if ( isPlainObject( property ) && element === undefined ) {
                                              element = value;
                                          }
                                  
                                  
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js and 1 other location - About 55 mins to fix
                                  src/view/upcastwriter.js on lines 295..300

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 53.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  Similar blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                          if ( element.is( 'element' ) ) {
                                              for ( const child of element.getChildren() ) {
                                                  this._addToClonedElementsGroup( child );
                                              }
                                          }
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js and 1 other location - About 40 mins to fix
                                  src/view/downcastwriter.js on lines 1692..1696

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 49.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  Similar blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                          if ( element.is( 'element' ) ) {
                                              for ( const child of element.getChildren() ) {
                                                  this._removeFromClonedElementsGroup( child );
                                              }
                                          }
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js and 1 other location - About 40 mins to fix
                                  src/view/downcastwriter.js on lines 1654..1658

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 49.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  Similar blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                              if ( !toUnwrap.hasStyle( key ) || toUnwrap.getStyle( key ) !== wrapper.getStyle( key ) ) {
                                                  return false;
                                              }
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js and 1 other location - About 35 mins to fix
                                  src/view/downcastwriter.js on lines 1464..1466

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 46.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  Similar blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                              if ( !toUnwrap.hasAttribute( key ) || toUnwrap.getAttribute( key ) !== wrapper.getAttribute( key ) ) {
                                                  return false;
                                              }
                                  Severity: Minor
                                  Found in src/view/downcastwriter.js and 1 other location - About 35 mins to fix
                                  src/view/downcastwriter.js on lines 1477..1479

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 46.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  There are no issues that match your filters.

                                  Category
                                  Status