Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

safe.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module.exports = safe
  2. var patternCompile = require('./pattern-compile')
  3. var patternInScope = require('./pattern-in-scope')
  4. function safe(context, input, config) {
  5. var value = (config.before || '') + (input || '') + (config.after || '')
  6. var positions = []
  7. var result = []
  8. var infos = {}
  9. var index = -1
  10. var before
  11. var after
  12. var position
  13. var pattern
  14. var expression
  15. var match
  16. var start
  17. var end
  18. while (++index < context.unsafe.length) {
  19. pattern = context.unsafe[index]
  20. if (!patternInScope(context.stack, pattern)) {
  21. continue
  22. }
  23. expression = patternCompile(pattern)
  24. while ((match = expression.exec(value))) {
  25. before = 'before' in pattern || pattern.atBreak
  26. after = 'after' in pattern
  27. position = match.index + (before ? match[1].length : 0)
  28. if (positions.indexOf(position) === -1) {
  29. positions.push(position)
  30. infos[position] = {before: before, after: after}
  31. } else {
  32. if (infos[position].before && !before) {
  33. infos[position].before = false
  34. }
  35. if (infos[position].after && !after) {
  36. infos[position].after = false
  37. }
  38. }
  39. }
  40. }
  41. positions.sort(numerical)
  42. start = config.before ? config.before.length : 0
  43. end = value.length - (config.after ? config.after.length : 0)
  44. index = -1
  45. while (++index < positions.length) {
  46. position = positions[index]
  47. if (
  48. // Character before or after matched:
  49. position < start ||
  50. position >= end
  51. ) {
  52. continue
  53. }
  54. // If this character is supposed to be escaped because it has a condition on
  55. // the next character, and the next character is definitly being escaped,
  56. // then skip this escape.
  57. if (
  58. position + 1 < end &&
  59. positions[index + 1] === position + 1 &&
  60. infos[position].after &&
  61. !infos[position + 1].before &&
  62. !infos[position + 1].after
  63. ) {
  64. continue
  65. }
  66. if (start !== position) {
  67. // If we have to use a character reference, an ampersand would be more
  68. // correct, but as backslashes only care about punctuation, either will
  69. // do the trick
  70. result.push(escapeBackslashes(value.slice(start, position), '\\'))
  71. }
  72. start = position
  73. if (
  74. /[!-/:-@[-`{-~]/.test(value.charAt(position)) &&
  75. (!config.encode || config.encode.indexOf(value.charAt(position)) === -1)
  76. ) {
  77. // Character escape.
  78. result.push('\\')
  79. } else {
  80. // Character reference.
  81. result.push(
  82. '&#x' + value.charCodeAt(position).toString(16).toUpperCase() + ';'
  83. )
  84. start++
  85. }
  86. }
  87. result.push(escapeBackslashes(value.slice(start, end), config.after))
  88. return result.join('')
  89. }
  90. function numerical(a, b) {
  91. return a - b
  92. }
  93. function escapeBackslashes(value, after) {
  94. var expression = /\\(?=[!-/:-@[-`{-~])/g
  95. var positions = []
  96. var results = []
  97. var index = -1
  98. var start = 0
  99. var whole = value + after
  100. var match
  101. while ((match = expression.exec(whole))) {
  102. positions.push(match.index)
  103. }
  104. while (++index < positions.length) {
  105. if (start !== positions[index]) {
  106. results.push(value.slice(start, positions[index]))
  107. }
  108. results.push('\\')
  109. start = positions[index]
  110. }
  111. results.push(value.slice(start))
  112. return results.join('')
  113. }