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

91 строка
2.0 KiB

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