Node-Red configuration
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

node.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. 'use strict'
  2. let { isClean, my } = require('./symbols')
  3. let CssSyntaxError = require('./css-syntax-error')
  4. let Stringifier = require('./stringifier')
  5. let stringify = require('./stringify')
  6. function cloneNode(obj, parent) {
  7. let cloned = new obj.constructor()
  8. for (let i in obj) {
  9. if (!Object.prototype.hasOwnProperty.call(obj, i)) {
  10. /* c8 ignore next 2 */
  11. continue
  12. }
  13. if (i === 'proxyCache') continue
  14. let value = obj[i]
  15. let type = typeof value
  16. if (i === 'parent' && type === 'object') {
  17. if (parent) cloned[i] = parent
  18. } else if (i === 'source') {
  19. cloned[i] = value
  20. } else if (Array.isArray(value)) {
  21. cloned[i] = value.map(j => cloneNode(j, cloned))
  22. } else {
  23. if (type === 'object' && value !== null) value = cloneNode(value)
  24. cloned[i] = value
  25. }
  26. }
  27. return cloned
  28. }
  29. class Node {
  30. constructor(defaults = {}) {
  31. this.raws = {}
  32. this[isClean] = false
  33. this[my] = true
  34. for (let name in defaults) {
  35. if (name === 'nodes') {
  36. this.nodes = []
  37. for (let node of defaults[name]) {
  38. if (typeof node.clone === 'function') {
  39. this.append(node.clone())
  40. } else {
  41. this.append(node)
  42. }
  43. }
  44. } else {
  45. this[name] = defaults[name]
  46. }
  47. }
  48. }
  49. addToError(error) {
  50. error.postcssNode = this
  51. if (error.stack && this.source && /\n\s{4}at /.test(error.stack)) {
  52. let s = this.source
  53. error.stack = error.stack.replace(
  54. /\n\s{4}at /,
  55. `$&${s.input.from}:${s.start.line}:${s.start.column}$&`
  56. )
  57. }
  58. return error
  59. }
  60. after(add) {
  61. this.parent.insertAfter(this, add)
  62. return this
  63. }
  64. assign(overrides = {}) {
  65. for (let name in overrides) {
  66. this[name] = overrides[name]
  67. }
  68. return this
  69. }
  70. before(add) {
  71. this.parent.insertBefore(this, add)
  72. return this
  73. }
  74. cleanRaws(keepBetween) {
  75. delete this.raws.before
  76. delete this.raws.after
  77. if (!keepBetween) delete this.raws.between
  78. }
  79. clone(overrides = {}) {
  80. let cloned = cloneNode(this)
  81. for (let name in overrides) {
  82. cloned[name] = overrides[name]
  83. }
  84. return cloned
  85. }
  86. cloneAfter(overrides = {}) {
  87. let cloned = this.clone(overrides)
  88. this.parent.insertAfter(this, cloned)
  89. return cloned
  90. }
  91. cloneBefore(overrides = {}) {
  92. let cloned = this.clone(overrides)
  93. this.parent.insertBefore(this, cloned)
  94. return cloned
  95. }
  96. error(message, opts = {}) {
  97. if (this.source) {
  98. let { end, start } = this.rangeBy(opts)
  99. return this.source.input.error(
  100. message,
  101. { column: start.column, line: start.line },
  102. { column: end.column, line: end.line },
  103. opts
  104. )
  105. }
  106. return new CssSyntaxError(message)
  107. }
  108. getProxyProcessor() {
  109. return {
  110. get(node, prop) {
  111. if (prop === 'proxyOf') {
  112. return node
  113. } else if (prop === 'root') {
  114. return () => node.root().toProxy()
  115. } else {
  116. return node[prop]
  117. }
  118. },
  119. set(node, prop, value) {
  120. if (node[prop] === value) return true
  121. node[prop] = value
  122. if (
  123. prop === 'prop' ||
  124. prop === 'value' ||
  125. prop === 'name' ||
  126. prop === 'params' ||
  127. prop === 'important' ||
  128. /* c8 ignore next */
  129. prop === 'text'
  130. ) {
  131. node.markDirty()
  132. }
  133. return true
  134. }
  135. }
  136. }
  137. markDirty() {
  138. if (this[isClean]) {
  139. this[isClean] = false
  140. let next = this
  141. while ((next = next.parent)) {
  142. next[isClean] = false
  143. }
  144. }
  145. }
  146. next() {
  147. if (!this.parent) return undefined
  148. let index = this.parent.index(this)
  149. return this.parent.nodes[index + 1]
  150. }
  151. positionBy(opts, stringRepresentation) {
  152. let pos = this.source.start
  153. if (opts.index) {
  154. pos = this.positionInside(opts.index, stringRepresentation)
  155. } else if (opts.word) {
  156. stringRepresentation = this.toString()
  157. let index = stringRepresentation.indexOf(opts.word)
  158. if (index !== -1) pos = this.positionInside(index, stringRepresentation)
  159. }
  160. return pos
  161. }
  162. positionInside(index, stringRepresentation) {
  163. let string = stringRepresentation || this.toString()
  164. let column = this.source.start.column
  165. let line = this.source.start.line
  166. for (let i = 0; i < index; i++) {
  167. if (string[i] === '\n') {
  168. column = 1
  169. line += 1
  170. } else {
  171. column += 1
  172. }
  173. }
  174. return { column, line }
  175. }
  176. prev() {
  177. if (!this.parent) return undefined
  178. let index = this.parent.index(this)
  179. return this.parent.nodes[index - 1]
  180. }
  181. rangeBy(opts) {
  182. let start = {
  183. column: this.source.start.column,
  184. line: this.source.start.line
  185. }
  186. let end = this.source.end
  187. ? {
  188. column: this.source.end.column + 1,
  189. line: this.source.end.line
  190. }
  191. : {
  192. column: start.column + 1,
  193. line: start.line
  194. }
  195. if (opts.word) {
  196. let stringRepresentation = this.toString()
  197. let index = stringRepresentation.indexOf(opts.word)
  198. if (index !== -1) {
  199. start = this.positionInside(index, stringRepresentation)
  200. end = this.positionInside(index + opts.word.length, stringRepresentation)
  201. }
  202. } else {
  203. if (opts.start) {
  204. start = {
  205. column: opts.start.column,
  206. line: opts.start.line
  207. }
  208. } else if (opts.index) {
  209. start = this.positionInside(opts.index)
  210. }
  211. if (opts.end) {
  212. end = {
  213. column: opts.end.column,
  214. line: opts.end.line
  215. }
  216. } else if (typeof opts.endIndex === 'number') {
  217. end = this.positionInside(opts.endIndex)
  218. } else if (opts.index) {
  219. end = this.positionInside(opts.index + 1)
  220. }
  221. }
  222. if (
  223. end.line < start.line ||
  224. (end.line === start.line && end.column <= start.column)
  225. ) {
  226. end = { column: start.column + 1, line: start.line }
  227. }
  228. return { end, start }
  229. }
  230. raw(prop, defaultType) {
  231. let str = new Stringifier()
  232. return str.raw(this, prop, defaultType)
  233. }
  234. remove() {
  235. if (this.parent) {
  236. this.parent.removeChild(this)
  237. }
  238. this.parent = undefined
  239. return this
  240. }
  241. replaceWith(...nodes) {
  242. if (this.parent) {
  243. let bookmark = this
  244. let foundSelf = false
  245. for (let node of nodes) {
  246. if (node === this) {
  247. foundSelf = true
  248. } else if (foundSelf) {
  249. this.parent.insertAfter(bookmark, node)
  250. bookmark = node
  251. } else {
  252. this.parent.insertBefore(bookmark, node)
  253. }
  254. }
  255. if (!foundSelf) {
  256. this.remove()
  257. }
  258. }
  259. return this
  260. }
  261. root() {
  262. let result = this
  263. while (result.parent && result.parent.type !== 'document') {
  264. result = result.parent
  265. }
  266. return result
  267. }
  268. toJSON(_, inputs) {
  269. let fixed = {}
  270. let emitInputs = inputs == null
  271. inputs = inputs || new Map()
  272. let inputsNextIndex = 0
  273. for (let name in this) {
  274. if (!Object.prototype.hasOwnProperty.call(this, name)) {
  275. /* c8 ignore next 2 */
  276. continue
  277. }
  278. if (name === 'parent' || name === 'proxyCache') continue
  279. let value = this[name]
  280. if (Array.isArray(value)) {
  281. fixed[name] = value.map(i => {
  282. if (typeof i === 'object' && i.toJSON) {
  283. return i.toJSON(null, inputs)
  284. } else {
  285. return i
  286. }
  287. })
  288. } else if (typeof value === 'object' && value.toJSON) {
  289. fixed[name] = value.toJSON(null, inputs)
  290. } else if (name === 'source') {
  291. let inputId = inputs.get(value.input)
  292. if (inputId == null) {
  293. inputId = inputsNextIndex
  294. inputs.set(value.input, inputsNextIndex)
  295. inputsNextIndex++
  296. }
  297. fixed[name] = {
  298. end: value.end,
  299. inputId,
  300. start: value.start
  301. }
  302. } else {
  303. fixed[name] = value
  304. }
  305. }
  306. if (emitInputs) {
  307. fixed.inputs = [...inputs.keys()].map(input => input.toJSON())
  308. }
  309. return fixed
  310. }
  311. toProxy() {
  312. if (!this.proxyCache) {
  313. this.proxyCache = new Proxy(this, this.getProxyProcessor())
  314. }
  315. return this.proxyCache
  316. }
  317. toString(stringifier = stringify) {
  318. if (stringifier.stringify) stringifier = stringifier.stringify
  319. let result = ''
  320. stringifier(this, i => {
  321. result += i
  322. })
  323. return result
  324. }
  325. warn(result, text, opts) {
  326. let data = { node: this }
  327. for (let i in opts) data[i] = opts[i]
  328. return result.warn(text, data)
  329. }
  330. get proxyOf() {
  331. return this
  332. }
  333. }
  334. module.exports = Node
  335. Node.default = Node