Skip to content

Commit

Permalink
Packages: Type autop, escape-html, html-entities (#20669)
Browse files Browse the repository at this point in the history
* Type autop package
* Type escape-html package
* Type html-entities package
  • Loading branch information
sirreal authored Mar 31, 2020
1 parent 7e964a8 commit b4bfbb7
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 10 deletions.
6 changes: 6 additions & 0 deletions packages/autop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 2.3.0 (2019-05-21)

### Bug Fix
Expand Down
1 change: 1 addition & 0 deletions packages/autop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.8.3"
Expand Down
25 changes: 16 additions & 9 deletions packages/autop/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The regular expression for an HTML element.
*
* @type {string}
* @type {RegExp}
*/
const htmlSplitRegex = ( () => {
/* eslint-disable no-multi-spaces */
Expand Down Expand Up @@ -52,17 +52,23 @@ const htmlSplitRegex = ( () => {
* Separate HTML elements and comments from the text.
*
* @param {string} input The text which has to be formatted.
* @return {Array} The formatted text.
* @return {string[]} The formatted text.
*/
function htmlSplit( input ) {
const parts = [];
let workingInput = input;

let match;
while ( ( match = workingInput.match( htmlSplitRegex ) ) ) {
parts.push( workingInput.slice( 0, match.index ) );
// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
// If the `g` flag is omitted, `index` is included.
// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
// Assert `match.index` is a number.
const index = /** @type {number} */ ( match.index );

parts.push( workingInput.slice( 0, index ) );
parts.push( match[ 0 ] );
workingInput = workingInput.slice( match.index + match[ 0 ].length );
workingInput = workingInput.slice( index + match[ 0 ].length );
}

if ( workingInput.length ) {
Expand All @@ -75,9 +81,9 @@ function htmlSplit( input ) {
/**
* Replace characters or phrases within HTML elements only.
*
* @param {string} haystack The text which has to be formatted.
* @param {Object} replacePairs In the form {from: 'to', ...}.
* @return {string} The formatted text.
* @param {string} haystack The text which has to be formatted.
* @param {Record<string,string>} replacePairs In the form {from: 'to', }.
* @return {string} The formatted text.
*/
function replaceInHtmlTags( haystack, replacePairs ) {
// Find all elements.
Expand Down Expand Up @@ -337,6 +343,7 @@ export function removep( html ) {
'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
const blocklist1 = blocklist + '|div|p';
const blocklist2 = blocklist + '|pre';
/** @type {string[]} */
const preserve = [];
let preserveLinebreaks = false;
let preserveBr = false;
Expand Down Expand Up @@ -399,7 +406,7 @@ export function removep( html ) {
html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );

// Replace <br> tags with line breaks.
html = html.replace( /(\s*)<br ?\/?>\s*/gi, function( match, space ) {
html = html.replace( /(\s*)<br ?\/?>\s*/gi, function( _, space ) {
if ( space && space.indexOf( '\n' ) !== -1 ) {
return '\n\n';
}
Expand Down Expand Up @@ -470,7 +477,7 @@ export function removep( html ) {
// Restore preserved tags.
if ( preserve.length ) {
html = html.replace( /<wp-preserve>/g, function() {
return preserve.shift();
return /** @type {string} */ ( preserve.shift() );
} );
}

Expand Down
9 changes: 9 additions & 0 deletions packages/autop/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"references": [ { "path": "../dom-ready" } ],
"include": [ "src/**/*" ]
}
6 changes: 6 additions & 0 deletions packages/escape-html/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 1.2.0 (2019-03-20)

- Add fix for WordPress wptexturize greater-than tokenize bug (see https://core.trac.wordpress.org/ticket/45387)
Expand Down
1 change: 1 addition & 0 deletions packages/escape-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.8.3"
Expand Down
8 changes: 8 additions & 0 deletions packages/escape-html/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/**/*" ]
}
6 changes: 6 additions & 0 deletions packages/html-entities/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 2.0.2 (2018-12-12)

## 2.0.1 (2018-11-21)
Expand Down
1 change: 1 addition & 0 deletions packages/html-entities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.8.3"
},
Expand Down
21 changes: 20 additions & 1 deletion packages/html-entities/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {HTMLTextAreaElement} */
let _decodeTextArea;

/**
Expand Down Expand Up @@ -36,5 +37,23 @@ export function decodeEntities( html ) {
_decodeTextArea.innerHTML = html;
const decoded = _decodeTextArea.textContent;
_decodeTextArea.innerHTML = '';
return decoded;

/**
* Cast to string, HTMLTextAreaElement should always have `string` textContent.
*
* > The `textContent` property of the `Node` interface represents the text content of the
* > node and its descendants.
* >
* > Value: A string or `null`
* >
* > * If the node is a `document` or a Doctype, `textContent` returns `null`.
* > * If the node is a CDATA section, comment, processing instruction, or text node,
* > textContent returns the text inside the node, i.e., the `Node.nodeValue`.
* > * For other node types, `textContent returns the concatenation of the textContent of
* > every child node, excluding comments and processing instructions. (This is an empty
* > string if the node has no children.)
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
*/
return /** @type {string} */ ( decoded );
}
8 changes: 8 additions & 0 deletions packages/html-entities/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/**/*" ]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"references": [
{ "path": "bin" },
{ "path": "packages/a11y" },
{ "path": "packages/autop" },
{ "path": "packages/blob" },
{ "path": "packages/dom-ready" },
{ "path": "packages/escape-html" },
{ "path": "packages/html-entities" },
{ "path": "packages/i18n" },
{ "path": "packages/is-shallow-equal" },
{ "path": "packages/priority-queue" },
Expand Down

0 comments on commit b4bfbb7

Please sign in to comment.