dedentLines.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.dedentLines = void 0;
  6. /**
  7. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. const getIndentationLength = line => {
  13. const result = /^( {2})+/.exec(line);
  14. return result === null ? 0 : result[0].length;
  15. };
  16. const dedentLine = line => line.slice(getIndentationLength(line)); // Return true if:
  17. // "key": "value has multiple lines\n…
  18. // "key has multiple lines\n…
  19. const hasUnmatchedDoubleQuoteMarks = string => {
  20. let n = 0;
  21. let i = string.indexOf('"', 0);
  22. while (i !== -1) {
  23. if (i === 0 || string[i - 1] !== '\\') {
  24. n += 1;
  25. }
  26. i = string.indexOf('"', i + 1);
  27. }
  28. return n % 2 !== 0;
  29. };
  30. const isFirstLineOfTag = line => /^( {2})*\</.test(line); // The length of the output array is the index of the next input line.
  31. // Push dedented lines of start tag onto output and return true;
  32. // otherwise return false because:
  33. // * props include a multiline string (or text node, if props have markup)
  34. // * start tag does not close
  35. const dedentStartTag = (input, output) => {
  36. let line = input[output.length];
  37. output.push(dedentLine(line));
  38. if (line.includes('>')) {
  39. return true;
  40. }
  41. while (output.length < input.length) {
  42. line = input[output.length];
  43. if (hasUnmatchedDoubleQuoteMarks(line)) {
  44. return false; // because props include a multiline string
  45. } else if (isFirstLineOfTag(line)) {
  46. // Recursion only if props have markup.
  47. if (!dedentMarkup(input, output)) {
  48. return false;
  49. }
  50. } else {
  51. output.push(dedentLine(line));
  52. if (line.includes('>')) {
  53. return true;
  54. }
  55. }
  56. }
  57. return false;
  58. }; // Push dedented lines of markup onto output and return true;
  59. // otherwise return false because:
  60. // * props include a multiline string
  61. // * text has more than one adjacent line
  62. // * markup does not close
  63. const dedentMarkup = (input, output) => {
  64. let line = input[output.length];
  65. if (!dedentStartTag(input, output)) {
  66. return false;
  67. }
  68. if (input[output.length - 1].includes('/>')) {
  69. return true;
  70. }
  71. let isText = false;
  72. const stack = [];
  73. stack.push(getIndentationLength(line));
  74. while (stack.length > 0 && output.length < input.length) {
  75. line = input[output.length];
  76. if (isFirstLineOfTag(line)) {
  77. if (line.includes('</')) {
  78. output.push(dedentLine(line));
  79. stack.pop();
  80. } else {
  81. if (!dedentStartTag(input, output)) {
  82. return false;
  83. }
  84. if (!input[output.length - 1].includes('/>')) {
  85. stack.push(getIndentationLength(line));
  86. }
  87. }
  88. isText = false;
  89. } else {
  90. if (isText) {
  91. return false; // because text has more than one adjacent line
  92. }
  93. const indentationLengthOfTag = stack[stack.length - 1];
  94. output.push(line.slice(indentationLengthOfTag + 2));
  95. isText = true;
  96. }
  97. }
  98. return stack.length === 0;
  99. }; // Return lines unindented by heuristic;
  100. // otherwise return null because:
  101. // * props include a multiline string
  102. // * text has more than one adjacent line
  103. // * markup does not close
  104. const dedentLines = input => {
  105. const output = [];
  106. while (output.length < input.length) {
  107. const line = input[output.length];
  108. if (hasUnmatchedDoubleQuoteMarks(line)) {
  109. return null;
  110. } else if (isFirstLineOfTag(line)) {
  111. if (!dedentMarkup(input, output)) {
  112. return null;
  113. }
  114. } else {
  115. output.push(dedentLine(line));
  116. }
  117. }
  118. return output;
  119. };
  120. exports.dedentLines = dedentLines;