You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

42 lines
994 B

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
  13. var _require = require("immutable"),
  14. OrderedSet = _require.OrderedSet;
  15. var substr = UnicodeUtils.substr;
  16. var EMPTY_SET = OrderedSet();
  17. /**
  18. * Convert to native JavaScript string lengths to determine ranges.
  19. */
  20. function decodeInlineStyleRanges(text, ranges) {
  21. var styles = Array(text.length).fill(EMPTY_SET);
  22. if (ranges) {
  23. ranges.forEach(function (range) {
  24. var cursor = substr(text, 0, range.offset).length;
  25. var end = cursor + substr(text, range.offset, range.length).length;
  26. while (cursor < end) {
  27. styles[cursor] = styles[cursor].add(range.style);
  28. cursor++;
  29. }
  30. });
  31. }
  32. return styles;
  33. }
  34. module.exports = decodeInlineStyleRanges;