版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

82 строки
2.2 KiB

  1. "use strict";
  2. var OPEN_TAG_NAME_PATTERN = /^<(\S+)/;
  3. var CLOSE_TAG_NAME_PATTERN = /^<\/((?:.|\n|\r\n)*)>$/; // CRLF \r\n
  4. function prettyJSON(obj) {
  5. return JSON.stringify(obj, null, 2);
  6. }
  7. /**
  8. * Clear tree of nodes from everything
  9. * "parentRef" properties so the tree
  10. * can be easily stringified into JSON.
  11. */
  12. function clearAst(ast) {
  13. var cleanAst = ast;
  14. delete cleanAst.parentRef;
  15. if (Array.isArray(ast.content.children)) {
  16. cleanAst.content.children = ast.content.children.map(function (node) {
  17. return clearAst(node);
  18. });
  19. }
  20. return cleanAst;
  21. }
  22. function parseOpenTagName(openTagStartTokenContent) {
  23. var match = openTagStartTokenContent.match(OPEN_TAG_NAME_PATTERN);
  24. if (match === null) {
  25. throw new Error('Unable to parse open tag name.\n' + "".concat(openTagStartTokenContent, " does not match pattern of opening tag."));
  26. }
  27. return match[1];
  28. }
  29. function parseCloseTagName(closeTagTokenContent) {
  30. var match = closeTagTokenContent.match(CLOSE_TAG_NAME_PATTERN);
  31. if (match === null) {
  32. throw new Error('Unable to parse close tag name.\n' + "".concat(closeTagTokenContent, " does not match pattern of closing tag."));
  33. }
  34. return match[1].trim();
  35. }
  36. function calculateTokenCharactersRange(state, _ref) {
  37. var keepBuffer = _ref.keepBuffer;
  38. if (keepBuffer === undefined) {
  39. throw new Error('Unable to calculate characters range for token.\n' + '"keepBuffer" parameter is not specified to decide if ' + 'the decision buffer is a part of characters range.');
  40. }
  41. var startPosition = state.caretPosition - (state.accumulatedContent.length - 1) - state.decisionBuffer.length;
  42. var endPosition;
  43. if (!keepBuffer) {
  44. endPosition = state.caretPosition - state.decisionBuffer.length;
  45. } else {
  46. endPosition = state.caretPosition;
  47. }
  48. return {
  49. startPosition: startPosition,
  50. endPosition: endPosition
  51. };
  52. }
  53. function isWhitespace(_char) {
  54. return _char === ' ' || _char === '\n' || _char === '\t';
  55. }
  56. module.exports = {
  57. prettyJSON: prettyJSON,
  58. clearAst: clearAst,
  59. parseOpenTagName: parseOpenTagName,
  60. parseCloseTagName: parseCloseTagName,
  61. calculateTokenCharactersRange: calculateTokenCharactersRange,
  62. isWhitespace: isWhitespace
  63. };