# code-block/utils

module

<a id="functions">

## Functions

<a id="function-canBeCodeBlock">

### `canBeCodeBlock( schema, element ) → boolean` _(internal)_

Checks if an [Element](module_engine_model_element-ModelElement.md) can become a code block.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L273)

#### Parameters

* `schema: ModelSchema`

  Model's schema.

* `element: ModelElement`

  The element to be checked.

#### Returns

* `boolean`

  Check result.

<a id="function-getCodeBlockAriaAnnouncement">

### `getCodeBlockAriaAnnouncement( t, languageDefs, element, direction ) → string` _(internal)_

Get the translated message read by the screen reader when you enter or exit an element with your cursor.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L286)

#### Parameters

* `t: LocaleTranslate`
* `languageDefs: Array<CodeBlockLanguageDefinition>`
* `element: ModelElement`
* `direction: 'enter' | 'leave'`

#### Returns

* `string`

<a id="function-getIndentOutdentPositions">

### `getIndentOutdentPositions( model ) → Array<ModelPosition>` _(internal)_

Returns an array of all model positions within the selection that represent code block lines.

If the selection is collapsed, it returns the exact selection anchor position:

```html
<codeBlock>[]foo</codeBlock>		->	 <codeBlock>^foo</codeBlock>
<codeBlock>foo[]bar</codeBlock>	 ->	 <codeBlock>foo^bar</codeBlock>
```

Otherwise, it returns positions **before** each text node belonging to all code blocks contained by the selection:

```html
<codeBlock>								<codeBlock>
	foo[bar								   ^foobar
	<softBreak></softBreak>		 ->		<softBreak></softBreak>
	baz]qux								   ^bazqux
</codeBlock>							   </codeBlock>
```

It also works across other non–code blocks:

```html
<codeBlock>								<codeBlock>
	foo[bar								   ^foobar
</codeBlock>							   </codeBlock>
<paragraph>text</paragraph>		 ->	 <paragraph>text</paragraph>
<codeBlock>								<codeBlock>
	baz]qux								   ^bazqux
</codeBlock>							   </codeBlock>
```

**Note:** The positions are in reverse order so they do not get outdated when iterating over them and the writer inserts or removes elements at the same time.

**Note:** The position is located after the leading white spaces in the text node.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L207)

#### Parameters

* `model: Model`

#### Returns

* `Array<ModelPosition>`

<a id="function-getLeadingWhiteSpaces">

### `getLeadingWhiteSpaces( textNode ) → string` _(internal)_

For a given model text node, it returns white spaces that precede other characters in that node. This corresponds to the indentation part of the code block line.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L121)

#### Parameters

* `textNode: ModelText`

#### Returns

* `string`

<a id="function-getNormalizedAndLocalizedLanguageDefinitions">

### `getNormalizedAndLocalizedLanguageDefinitions( editor ) → Array<CodeBlockLanguageDefinition>` _(internal)_

Returns code block languages as defined in `config.codeBlock.languages` but processed:

* To consider the editor localization, i.e. to display [`CodeBlockLanguageDefinition`](module_code-block_codeblockconfig-CodeBlockLanguageDefinition.md) in the correct language. There is no way to use [`t`](module_utils_locale-Locale.md#member-t) when the user configuration is defined because the editor does not exist yet.
* To make sure each definition has a CSS class associated with it even if not specified in the original configuration.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L37)

#### Parameters

* `editor: Editor`

#### Returns

* `Array<CodeBlockLanguageDefinition>`

<a id="function-getPropertyAssociation">

### `getPropertyAssociation( languageDefs, key, value ) → Record<string, string>` _(internal)_

Returns an object associating certain language definition properties with others. For instance:

For:

```typescript
const definitions = {
	{ language: 'php', class: 'language-php', label: 'PHP' },
	{ language: 'javascript', class: 'js', label: 'JavaScript' },
};

getPropertyAssociation( definitions, 'class', 'language' );
```

returns:

```typescript
{
	'language-php': 'php',
	'js': 'javascript'
}
```

and

```typescript
getPropertyAssociation( definitions, 'language', 'label' );
```

returns:

```typescript
{
	'php': 'PHP',
	'javascript': 'JavaScript'
}
```

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L94)

#### Parameters

* `languageDefs: Array<CodeBlockLanguageDefinition>`
* `key: keyof CodeBlockLanguageDefinition`
* `value: keyof CodeBlockLanguageDefinition`

#### Returns

* `Record<string, string>`

<a id="function-getTextNodeAtLineStart">

### `getTextNodeAtLineStart( position, model ) → ModelText | null` _(internal)_

For given position, finds the closest position that is at the beginning of a line of code and returns a text node that is at the beginning of the line (or `null` if there's no text node at the beginning of a given line).

Line beings at the start of a code block element and after each `softBreak` element.

Note: even though code block doesn't allow inline elements other than `<softBreak>` by default, some features may overwrite this rule, so such inline elements are taken into account.

Some examples of expected results:

```
<codeBlock>^</codeBlock>                                ->   null
<codeBlock>^foobar</codeBlock>                          ->   <codeBlock>[foobar]</codeBlock>
<codeBlock>foobar^</codeBlock>                          ->   <codeBlock>[foobar]</codeBlock>
<codeBlock>foo^bar</codeBlock>                          ->   <codeBlock>[foobar]</codeBlock>
<codeBlock>foo^<softBreak />bar</codeBlock>             ->   <codeBlock>[foo]<softBreak />bar</codeBlock>
<codeBlock>foo<softBreak />bar^</codeBlock>             ->   <codeBlock>foo<softBreak />[bar]</codeBlock>
<codeBlock>foo<softBreak />b^ar</codeBlock>             ->   <codeBlock>foo<softBreak />[bar]</codeBlock>
<codeBlock>foo<softBreak />^bar</codeBlock>             ->   <codeBlock>foo<softBreak />[bar]</codeBlock>
<codeBlock>^<element /></codeBlock>                     ->   null
<codeBlock><element />^</codeBlock>                     ->   null
<codeBlock>foo^<element /></codeBlock>                  ->   <codeBlock>[foo]<element /></codeBlock>
<codeBlock>foo<element />^</codeBlock>                  ->   <codeBlock>[foo]<element /></codeBlock>
<codeBlock>foo<element />bar^</codeBlock>               ->   <codeBlock>[foo]<element />bar</codeBlock>
<codeBlock><element />bar^</codeBlock>                  ->   null
<codeBlock>foo<softBreak />^<softBreak /></codeBlock>   ->   null
<codeBlock>foo<softBreak />^<element /></codeBlock>     ->   null
<codeBlock>foo<softBreak /><element />^</codeBlock>     ->   null
<codeBlock>foo<softBreak />bar<element />^</codeBlock>  ->   <codeBlock>foo<softBreak />[bar]<element /></codeBlock>
<codeBlock>foo<softBreak /><element />ba^r</codeBlock>  ->   null
```

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L347)

#### Parameters

* `position: ModelPosition`
* `model: Model`

#### Returns

* `ModelText | null`

<a id="function-isModelSelectionInCodeBlock">

### `isModelSelectionInCodeBlock( selection ) → boolean` _(internal)_

Checks if any of the blocks within the model selection is a code block.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L259)

#### Parameters

* `selection: ModelDocumentSelection`

#### Returns

* `boolean`

<a id="function-rawSnippetTextToViewDocumentFragment">

### `rawSnippetTextToViewDocumentFragment( writer, text ) → ViewDocumentFragment` _(internal)_

For plain text containing the code (a snippet), it returns a document fragment containing view text nodes separated by `<br>` elements (in place of new line characters "\n"), for instance:

Input:

```typescript
"foo()\n
bar()"
```

Output:

```html
<DocumentFragment>
	"foo()"
	<br/>
	"bar()"
</DocumentFragment>
```

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-code-block/src/utils.ts#L149)

#### Parameters

* `writer: ViewUpcastWriter`

* `text: string`

  The raw code text to be converted.

#### Returns

* `ViewDocumentFragment`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
