@20minutes/draft-convert 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +2 -3
  2. package/esm/blockEntities.js +58 -61
  3. package/esm/blockInlineStyles.js +64 -87
  4. package/esm/convertFromHTML.js +442 -478
  5. package/esm/convertToHTML.js +71 -93
  6. package/esm/default/defaultBlockHTML.js +33 -30
  7. package/esm/default/defaultInlineHTML.js +16 -16
  8. package/esm/encodeBlock.js +36 -40
  9. package/esm/index.js +1 -1
  10. package/esm/util/accumulateFunction.js +7 -9
  11. package/esm/util/blockTypeObjectFunction.js +7 -9
  12. package/esm/util/getBlockTags.js +19 -19
  13. package/esm/util/getElementHTML.js +24 -28
  14. package/esm/util/getElementTagLength.js +14 -16
  15. package/esm/util/getNestedBlockTags.js +20 -25
  16. package/esm/util/parseHTML.js +15 -15
  17. package/esm/util/rangeSort.js +6 -6
  18. package/esm/util/splitReactElement.js +27 -13
  19. package/esm/util/styleObjectFunction.js +6 -8
  20. package/esm/util/updateMutation.js +57 -47
  21. package/lib/blockEntities.js +74 -69
  22. package/lib/blockInlineStyles.js +81 -96
  23. package/lib/convertFromHTML.js +459 -487
  24. package/lib/convertToHTML.js +93 -107
  25. package/lib/default/defaultBlockHTML.js +47 -35
  26. package/lib/default/defaultInlineHTML.js +29 -21
  27. package/lib/encodeBlock.js +50 -46
  28. package/lib/index.js +25 -23
  29. package/lib/util/accumulateFunction.js +13 -11
  30. package/lib/util/blockTypeObjectFunction.js +13 -11
  31. package/lib/util/getBlockTags.js +35 -27
  32. package/lib/util/getElementHTML.js +40 -36
  33. package/lib/util/getElementTagLength.js +28 -22
  34. package/lib/util/getNestedBlockTags.js +35 -32
  35. package/lib/util/parseHTML.js +22 -18
  36. package/lib/util/rangeSort.js +13 -9
  37. package/lib/util/splitReactElement.js +42 -19
  38. package/lib/util/styleObjectFunction.js +12 -10
  39. package/lib/util/updateMutation.js +64 -51
  40. package/package.json +31 -35
  41. package/dist/draft-convert.js +0 -440
  42. package/dist/draft-convert.min.js +0 -1
@@ -1,6 +1,6 @@
1
- export default (function (r1, r2) {
2
- if (r1.offset === r2.offset) {
3
- return r2.length - r1.length;
4
- }
5
- return r1.offset - r2.offset;
6
- });
1
+ export default ((r1, r2)=>{
2
+ if (r1.offset === r2.offset) {
3
+ return r2.length - r1.length;
4
+ }
5
+ return r1.offset - r2.offset;
6
+ });
@@ -1,18 +1,32 @@
1
1
  import invariant from 'invariant';
2
2
  import React from 'react';
3
3
  import ReactDOMServer from 'react-dom/server';
4
-
5
4
  // see http://w3c.github.io/html/syntax.html#writing-html-documents-elements
6
- var VOID_TAGS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
5
+ const VOID_TAGS = [
6
+ 'area',
7
+ 'base',
8
+ 'br',
9
+ 'col',
10
+ 'embed',
11
+ 'hr',
12
+ 'img',
13
+ 'input',
14
+ 'link',
15
+ 'meta',
16
+ 'param',
17
+ 'source',
18
+ 'track',
19
+ 'wbr'
20
+ ];
7
21
  export default function splitReactElement(element) {
8
- if (VOID_TAGS.indexOf(element.type) !== -1) {
9
- return ReactDOMServer.renderToStaticMarkup(element);
10
- }
11
- var tags = ReactDOMServer.renderToStaticMarkup(/*#__PURE__*/React.cloneElement(element, {}, '\r')).split('\r');
12
- invariant(tags.length > 1, "convertToHTML: Element of type ".concat(element.type, " must render children"));
13
- invariant(tags.length < 3, "convertToHTML: Element of type ".concat(element.type, " cannot use carriage return character"));
14
- return {
15
- start: tags[0],
16
- end: tags[1]
17
- };
18
- }
22
+ if (VOID_TAGS.indexOf(element.type) !== -1) {
23
+ return ReactDOMServer.renderToStaticMarkup(element);
24
+ }
25
+ const tags = ReactDOMServer.renderToStaticMarkup(/*#__PURE__*/ React.cloneElement(element, {}, '\r')).split('\r');
26
+ invariant(tags.length > 1, `convertToHTML: Element of type ${element.type} must render children`);
27
+ invariant(tags.length < 3, `convertToHTML: Element of type ${element.type} cannot use carriage return character`);
28
+ return {
29
+ start: tags[0],
30
+ end: tags[1]
31
+ };
32
+ }
@@ -1,8 +1,6 @@
1
- export default (function (object) {
2
- return function (style) {
3
- if (typeof object === 'function') {
4
- return object(style);
5
- }
6
- return object[style];
7
- };
8
- });
1
+ export default ((object)=>(style)=>{
2
+ if (typeof object === 'function') {
3
+ return object(style);
4
+ }
5
+ return object[style];
6
+ });
@@ -1,48 +1,58 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
1
  export default function updateMutation(mutation, originalOffset, originalLength, newLength, prefixLength, suffixLength) {
5
- // three cases we can reasonably adjust - disjoint mutations that
6
- // happen later on where the offset will need to be changed,
7
- // mutations that completely contain the new one where we can adjust
8
- // the length, and mutations that occur partially within the new one.
9
- var lengthDiff = newLength - originalLength;
10
- var mutationAfterChange = originalOffset + originalLength <= mutation.offset;
11
- if (mutationAfterChange) {
12
- return _objectSpread(_objectSpread({}, mutation), {}, {
13
- offset: mutation.offset + lengthDiff
14
- });
15
- }
16
- var mutationContainsChange = originalOffset >= mutation.offset && originalOffset + originalLength <= mutation.offset + mutation.length;
17
- if (mutationContainsChange) {
18
- return _objectSpread(_objectSpread({}, mutation), {}, {
19
- length: mutation.length + lengthDiff
20
- });
21
- }
22
- var mutationWithinPrefixChange = mutation.offset >= originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && prefixLength > 0;
23
- if (mutationWithinPrefixChange) {
24
- return _objectSpread(_objectSpread({}, mutation), {}, {
25
- offset: mutation.offset + prefixLength
26
- });
27
- }
28
- var mutationContainsPrefix = mutation.offset < originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && mutation.offset + mutation.length > originalOffset && prefixLength > 0;
29
- if (mutationContainsPrefix) {
30
- return [_objectSpread(_objectSpread({}, mutation), {}, {
31
- length: originalOffset - mutation.offset
32
- }), _objectSpread(_objectSpread({}, mutation), {}, {
33
- offset: originalOffset + prefixLength,
34
- length: mutation.offset - originalOffset + mutation.length
35
- })];
36
- }
37
- var mutationContainsSuffix = mutation.offset >= originalOffset && mutation.offset + mutation.length > originalOffset + originalLength && originalOffset + originalLength > mutation.offset && suffixLength > 0;
38
- if (mutationContainsSuffix) {
39
- return [_objectSpread(_objectSpread({}, mutation), {}, {
40
- offset: mutation.offset + prefixLength,
41
- length: originalOffset + originalLength - mutation.offset
42
- }), _objectSpread(_objectSpread({}, mutation), {}, {
43
- offset: originalOffset + originalLength + prefixLength + suffixLength,
44
- length: mutation.offset + mutation.length - (originalOffset + originalLength)
45
- })];
46
- }
47
- return mutation;
48
- }
2
+ // three cases we can reasonably adjust - disjoint mutations that
3
+ // happen later on where the offset will need to be changed,
4
+ // mutations that completely contain the new one where we can adjust
5
+ // the length, and mutations that occur partially within the new one.
6
+ const lengthDiff = newLength - originalLength;
7
+ const mutationAfterChange = originalOffset + originalLength <= mutation.offset;
8
+ if (mutationAfterChange) {
9
+ return {
10
+ ...mutation,
11
+ offset: mutation.offset + lengthDiff
12
+ };
13
+ }
14
+ const mutationContainsChange = originalOffset >= mutation.offset && originalOffset + originalLength <= mutation.offset + mutation.length;
15
+ if (mutationContainsChange) {
16
+ return {
17
+ ...mutation,
18
+ length: mutation.length + lengthDiff
19
+ };
20
+ }
21
+ const mutationWithinPrefixChange = mutation.offset >= originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && prefixLength > 0;
22
+ if (mutationWithinPrefixChange) {
23
+ return {
24
+ ...mutation,
25
+ offset: mutation.offset + prefixLength
26
+ };
27
+ }
28
+ const mutationContainsPrefix = mutation.offset < originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && mutation.offset + mutation.length > originalOffset && prefixLength > 0;
29
+ if (mutationContainsPrefix) {
30
+ return [
31
+ {
32
+ ...mutation,
33
+ length: originalOffset - mutation.offset
34
+ },
35
+ {
36
+ ...mutation,
37
+ offset: originalOffset + prefixLength,
38
+ length: mutation.offset - originalOffset + mutation.length
39
+ }
40
+ ];
41
+ }
42
+ const mutationContainsSuffix = mutation.offset >= originalOffset && mutation.offset + mutation.length > originalOffset + originalLength && originalOffset + originalLength > mutation.offset && suffixLength > 0;
43
+ if (mutationContainsSuffix) {
44
+ return [
45
+ {
46
+ ...mutation,
47
+ offset: mutation.offset + prefixLength,
48
+ length: originalOffset + originalLength - mutation.offset
49
+ },
50
+ {
51
+ ...mutation,
52
+ offset: originalOffset + originalLength + prefixLength + suffixLength,
53
+ length: mutation.offset + mutation.length - (originalOffset + originalLength)
54
+ }
55
+ ];
56
+ }
57
+ return mutation;
58
+ }
@@ -1,74 +1,79 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
2
  Object.defineProperty(exports, "__esModule", {
5
- value: true
3
+ value: true
6
4
  });
7
- exports["default"] = void 0;
8
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
- var _updateMutation = _interopRequireDefault(require("./util/updateMutation"));
11
- var _rangeSort = _interopRequireDefault(require("./util/rangeSort"));
12
- var _getElementHTML = _interopRequireDefault(require("./util/getElementHTML"));
13
- var _getElementTagLength = _interopRequireDefault(require("./util/getElementTagLength"));
14
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
15
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
- var converter = function converter() {
17
- var entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
18
- var originalText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
19
- return originalText;
20
- };
21
- var _default = exports["default"] = function _default(block, entityMap) {
22
- var entityConverter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : converter;
23
- var resultText = (0, _toConsumableArray2["default"])(block.text);
24
- var getEntityHTML = entityConverter;
25
- if (entityConverter.__isMiddleware) {
26
- getEntityHTML = entityConverter(converter);
27
- }
28
- if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
29
- var entities = block.entityRanges.sort(_rangeSort["default"]);
30
- var styles = block.inlineStyleRanges;
31
- var _loop = function _loop(index) {
32
- var entityRange = entities[index];
33
- var entity = entityMap[entityRange.key];
34
- var originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
35
- var entityHTML = getEntityHTML(entity, originalText);
36
- var elementHTML = (0, _getElementHTML["default"])(entityHTML, originalText);
37
- var converted;
38
- if (!!elementHTML || elementHTML === '') {
39
- converted = (0, _toConsumableArray2["default"])(elementHTML);
40
- } else {
41
- converted = originalText;
42
- }
43
- var prefixLength = (0, _getElementTagLength["default"])(entityHTML, 'start');
44
- var suffixLength = (0, _getElementTagLength["default"])(entityHTML, 'end');
45
- var updateLaterMutation = function updateLaterMutation(mutation, mutationIndex) {
46
- if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
47
- return (0, _updateMutation["default"])(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
48
- }
49
- return mutation;
50
- };
51
- var updateLaterMutations = function updateLaterMutations(mutationList) {
52
- return mutationList.reduce(function (acc, mutation, mutationIndex) {
53
- var updatedMutation = updateLaterMutation(mutation, mutationIndex);
54
- if (Array.isArray(updatedMutation)) {
55
- return acc.concat(updatedMutation);
56
- }
57
- return acc.concat([updatedMutation]);
58
- }, []);
59
- };
60
- entities = updateLaterMutations(entities);
61
- styles = updateLaterMutations(styles);
62
- resultText = [].concat((0, _toConsumableArray2["default"])(resultText.slice(0, entityRange.offset)), (0, _toConsumableArray2["default"])(converted), (0, _toConsumableArray2["default"])(resultText.slice(entityRange.offset + entityRange.length)));
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation"));
12
+ const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
13
+ const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML"));
14
+ const _getElementTagLength = /*#__PURE__*/ _interop_require_default(require("./util/getElementTagLength"));
15
+ function _interop_require_default(obj) {
16
+ return obj && obj.__esModule ? obj : {
17
+ default: obj
63
18
  };
64
- for (var index = 0; index < entities.length; index += 1) {
65
- _loop(index);
19
+ }
20
+ const converter = (entity = {}, originalText = '')=>originalText;
21
+ const _default = (block, entityMap, entityConverter = converter)=>{
22
+ let resultText = [
23
+ ...block.text
24
+ ];
25
+ let getEntityHTML = entityConverter;
26
+ if (entityConverter.__isMiddleware) {
27
+ getEntityHTML = entityConverter(converter);
66
28
  }
67
- return _objectSpread(_objectSpread({}, block), {}, {
68
- text: resultText.join(''),
69
- inlineStyleRanges: styles,
70
- entityRanges: entities
71
- });
72
- }
73
- return block;
74
- };
29
+ if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
30
+ let entities = block.entityRanges.sort(_rangeSort.default);
31
+ let styles = block.inlineStyleRanges;
32
+ for(let index = 0; index < entities.length; index += 1){
33
+ const entityRange = entities[index];
34
+ const entity = entityMap[entityRange.key];
35
+ const originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
36
+ const entityHTML = getEntityHTML(entity, originalText);
37
+ const elementHTML = (0, _getElementHTML.default)(entityHTML, originalText);
38
+ let converted;
39
+ if (!!elementHTML || elementHTML === '') {
40
+ converted = [
41
+ ...elementHTML
42
+ ];
43
+ } else {
44
+ converted = originalText;
45
+ }
46
+ const prefixLength = (0, _getElementTagLength.default)(entityHTML, 'start');
47
+ const suffixLength = (0, _getElementTagLength.default)(entityHTML, 'end');
48
+ const updateLaterMutation = (mutation, mutationIndex)=>{
49
+ if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
50
+ return (0, _updateMutation.default)(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
51
+ }
52
+ return mutation;
53
+ };
54
+ const updateLaterMutations = (mutationList)=>mutationList.reduce((acc, mutation, mutationIndex)=>{
55
+ const updatedMutation = updateLaterMutation(mutation, mutationIndex);
56
+ if (Array.isArray(updatedMutation)) {
57
+ return acc.concat(updatedMutation);
58
+ }
59
+ return acc.concat([
60
+ updatedMutation
61
+ ]);
62
+ }, []);
63
+ entities = updateLaterMutations(entities);
64
+ styles = updateLaterMutations(styles);
65
+ resultText = [
66
+ ...resultText.slice(0, entityRange.offset),
67
+ ...converted,
68
+ ...resultText.slice(entityRange.offset + entityRange.length)
69
+ ];
70
+ }
71
+ return {
72
+ ...block,
73
+ text: resultText.join(''),
74
+ inlineStyleRanges: styles,
75
+ entityRanges: entities
76
+ };
77
+ }
78
+ return block;
79
+ };
@@ -1,105 +1,90 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
2
  Object.defineProperty(exports, "__esModule", {
5
- value: true
3
+ value: true
6
4
  });
7
- exports["default"] = void 0;
8
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
- var _invariant = _interopRequireDefault(require("invariant"));
10
- var _styleObjectFunction = _interopRequireDefault(require("./util/styleObjectFunction"));
11
- var _accumulateFunction = _interopRequireDefault(require("./util/accumulateFunction"));
12
- var _getElementHTML = _interopRequireDefault(require("./util/getElementHTML"));
13
- var _rangeSort = _interopRequireDefault(require("./util/rangeSort"));
14
- var _defaultInlineHTML = _interopRequireDefault(require("./default/defaultInlineHTML"));
15
- var subtractStyles = function subtractStyles(original, toRemove) {
16
- return original.filter(function (el) {
17
- return !toRemove.some(function (elToRemove) {
18
- return elToRemove.style === el.style;
19
- });
20
- });
21
- };
22
- var popEndingStyles = function popEndingStyles(styleStack, endingStyles) {
23
- return endingStyles.reduceRight(function (stack, style) {
24
- var styleToRemove = stack[stack.length - 1];
25
- (0, _invariant["default"])(styleToRemove.style === style.style, "Style ".concat(styleToRemove.style, " to be removed doesn't match expected ").concat(style.style));
26
- return stack.slice(0, -1);
27
- }, styleStack);
28
- };
29
- var characterStyles = function characterStyles(offset, ranges) {
30
- return ranges.filter(function (range) {
31
- return offset >= range.offset && offset < range.offset + range.length;
32
- });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
12
+ const _styleObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/styleObjectFunction"));
13
+ const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction"));
14
+ const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML"));
15
+ const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
16
+ const _defaultInlineHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultInlineHTML"));
17
+ function _interop_require_default(obj) {
18
+ return obj && obj.__esModule ? obj : {
19
+ default: obj
20
+ };
21
+ }
22
+ const subtractStyles = (original, toRemove)=>original.filter((el)=>!toRemove.some((elToRemove)=>elToRemove.style === el.style));
23
+ const popEndingStyles = (styleStack, endingStyles)=>endingStyles.reduceRight((stack, style)=>{
24
+ const styleToRemove = stack[stack.length - 1];
25
+ (0, _invariant.default)(styleToRemove.style === style.style, `Style ${styleToRemove.style} to be removed doesn't match expected ${style.style}`);
26
+ return stack.slice(0, -1);
27
+ }, styleStack);
28
+ const characterStyles = (offset, ranges)=>ranges.filter((range)=>offset >= range.offset && offset < range.offset + range.length);
29
+ const rangeIsSubset = (firstRange, secondRange)=>{
30
+ // returns true if the second range is a subset of the first
31
+ const secondStartWithinFirst = firstRange.offset <= secondRange.offset;
32
+ const secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
33
+ return secondStartWithinFirst && secondEndWithinFirst;
33
34
  };
34
- var rangeIsSubset = function rangeIsSubset(firstRange, secondRange) {
35
- // returns true if the second range is a subset of the first
36
- var secondStartWithinFirst = firstRange.offset <= secondRange.offset;
37
- var secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
38
- return secondStartWithinFirst && secondEndWithinFirst;
35
+ const latestStyleLast = (s1, s2)=>{
36
+ // make sure longer-lasting styles are added first
37
+ const s2endIndex = s2.offset + s2.length;
38
+ const s1endIndex = s1.offset + s1.length;
39
+ return s2endIndex - s1endIndex;
39
40
  };
40
- var latestStyleLast = function latestStyleLast(s1, s2) {
41
- // make sure longer-lasting styles are added first
42
- var s2endIndex = s2.offset + s2.length;
43
- var s1endIndex = s1.offset + s1.length;
44
- return s2endIndex - s1endIndex;
41
+ const getStylesToReset = (remainingStyles, newStyles)=>{
42
+ let i = 0;
43
+ while(i < remainingStyles.length){
44
+ if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
45
+ i += 1;
46
+ } else {
47
+ return remainingStyles.slice(i);
48
+ }
49
+ }
50
+ return [];
45
51
  };
46
- var getStylesToReset = function getStylesToReset(remainingStyles, newStyles) {
47
- var i = 0;
48
- while (i < remainingStyles.length) {
49
- if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
50
- i += 1;
52
+ const appendStartMarkup = (inlineHTML, string, styleRange)=>string + (0, _getElementHTML.default)(inlineHTML(styleRange.style)).start;
53
+ const prependEndMarkup = (inlineHTML, string, styleRange)=>(0, _getElementHTML.default)(inlineHTML(styleRange.style)).end + string;
54
+ const defaultCustomInlineHTML = (next)=>(style)=>next(style);
55
+ defaultCustomInlineHTML.__isMiddleware = true;
56
+ const _default = (rawBlock, customInlineHTML = defaultCustomInlineHTML)=>{
57
+ (0, _invariant.default)(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
58
+ let inlineHTML;
59
+ if (customInlineHTML.__isMiddleware === true) {
60
+ inlineHTML = customInlineHTML(_defaultInlineHTML.default);
51
61
  } else {
52
- return remainingStyles.slice(i);
62
+ inlineHTML = (0, _accumulateFunction.default)((0, _styleObjectFunction.default)(customInlineHTML), (0, _styleObjectFunction.default)(_defaultInlineHTML.default));
53
63
  }
54
- }
55
- return [];
56
- };
57
- var appendStartMarkup = function appendStartMarkup(inlineHTML, string, styleRange) {
58
- return string + (0, _getElementHTML["default"])(inlineHTML(styleRange.style)).start;
59
- };
60
- var prependEndMarkup = function prependEndMarkup(inlineHTML, string, styleRange) {
61
- return (0, _getElementHTML["default"])(inlineHTML(styleRange.style)).end + string;
62
- };
63
- var defaultCustomInlineHTML = function defaultCustomInlineHTML(next) {
64
- return function (style) {
65
- return next(style);
66
- };
64
+ let result = '';
65
+ let styleStack = [];
66
+ const sortedRanges = rawBlock.inlineStyleRanges.sort(_rangeSort.default);
67
+ const originalTextArray = [
68
+ ...rawBlock.text
69
+ ];
70
+ for(let i = 0; i < originalTextArray.length; i += 1){
71
+ const styles = characterStyles(i, sortedRanges);
72
+ const endingStyles = subtractStyles(styleStack, styles);
73
+ const newStyles = subtractStyles(styles, styleStack);
74
+ const remainingStyles = subtractStyles(styleStack, endingStyles);
75
+ // reset styles: look for any already existing styles that will need to
76
+ // end before styles that are being added on this character. to solve this
77
+ // close out those current tags and all nested children,
78
+ // then open new ones nested within the new styles.
79
+ const resetStyles = getStylesToReset(remainingStyles, newStyles);
80
+ const openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
81
+ const openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
82
+ const endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
83
+ result += endingStyleTags + openingStyleTags + originalTextArray[i];
84
+ styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
85
+ styleStack = styleStack.concat(openingStyles);
86
+ (0, _invariant.default)(styleStack.length === styles.length, `Character ${i}: ${styleStack.length - styles.length} styles left on stack that should no longer be there`);
87
+ }
88
+ result = styleStack.reduceRight((res, openStyle)=>res + (0, _getElementHTML.default)(inlineHTML(openStyle.style)).end, result);
89
+ return result;
67
90
  };
68
- defaultCustomInlineHTML.__isMiddleware = true;
69
- var _default = exports["default"] = function _default(rawBlock) {
70
- var customInlineHTML = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultCustomInlineHTML;
71
- (0, _invariant["default"])(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
72
- var inlineHTML;
73
- if (customInlineHTML.__isMiddleware === true) {
74
- inlineHTML = customInlineHTML(_defaultInlineHTML["default"]);
75
- } else {
76
- inlineHTML = (0, _accumulateFunction["default"])((0, _styleObjectFunction["default"])(customInlineHTML), (0, _styleObjectFunction["default"])(_defaultInlineHTML["default"]));
77
- }
78
- var result = '';
79
- var styleStack = [];
80
- var sortedRanges = rawBlock.inlineStyleRanges.sort(_rangeSort["default"]);
81
- var originalTextArray = (0, _toConsumableArray2["default"])(rawBlock.text);
82
- for (var i = 0; i < originalTextArray.length; i += 1) {
83
- var styles = characterStyles(i, sortedRanges);
84
- var endingStyles = subtractStyles(styleStack, styles);
85
- var newStyles = subtractStyles(styles, styleStack);
86
- var remainingStyles = subtractStyles(styleStack, endingStyles);
87
-
88
- // reset styles: look for any already existing styles that will need to
89
- // end before styles that are being added on this character. to solve this
90
- // close out those current tags and all nested children,
91
- // then open new ones nested within the new styles.
92
- var resetStyles = getStylesToReset(remainingStyles, newStyles);
93
- var openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
94
- var openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
95
- var endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
96
- result += endingStyleTags + openingStyleTags + originalTextArray[i];
97
- styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
98
- styleStack = styleStack.concat(openingStyles);
99
- (0, _invariant["default"])(styleStack.length === styles.length, "Character ".concat(i, ": ").concat(styleStack.length - styles.length, " styles left on stack that should no longer be there"));
100
- }
101
- result = styleStack.reduceRight(function (res, openStyle) {
102
- return res + (0, _getElementHTML["default"])(inlineHTML(openStyle.style)).end;
103
- }, result);
104
- return result;
105
- };