Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. 'use strict'
  2. let { isClean, my } = require('./symbols')
  3. let Declaration = require('./declaration')
  4. let Comment = require('./comment')
  5. let Node = require('./node')
  6. let parse, Rule, AtRule, Root
  7. function cleanSource(nodes) {
  8. return nodes.map(i => {
  9. if (i.nodes) i.nodes = cleanSource(i.nodes)
  10. delete i.source
  11. return i
  12. })
  13. }
  14. function markTreeDirty(node) {
  15. node[isClean] = false
  16. if (node.proxyOf.nodes) {
  17. for (let i of node.proxyOf.nodes) {
  18. markTreeDirty(i)
  19. }
  20. }
  21. }
  22. class Container extends Node {
  23. append(...children) {
  24. for (let child of children) {
  25. let nodes = this.normalize(child, this.last)
  26. for (let node of nodes) this.proxyOf.nodes.push(node)
  27. }
  28. this.markDirty()
  29. return this
  30. }
  31. cleanRaws(keepBetween) {
  32. super.cleanRaws(keepBetween)
  33. if (this.nodes) {
  34. for (let node of this.nodes) node.cleanRaws(keepBetween)
  35. }
  36. }
  37. each(callback) {
  38. if (!this.proxyOf.nodes) return undefined
  39. let iterator = this.getIterator()
  40. let index, result
  41. while (this.indexes[iterator] < this.proxyOf.nodes.length) {
  42. index = this.indexes[iterator]
  43. result = callback(this.proxyOf.nodes[index], index)
  44. if (result === false) break
  45. this.indexes[iterator] += 1
  46. }
  47. delete this.indexes[iterator]
  48. return result
  49. }
  50. every(condition) {
  51. return this.nodes.every(condition)
  52. }
  53. getIterator() {
  54. if (!this.lastEach) this.lastEach = 0
  55. if (!this.indexes) this.indexes = {}
  56. this.lastEach += 1
  57. let iterator = this.lastEach
  58. this.indexes[iterator] = 0
  59. return iterator
  60. }
  61. getProxyProcessor() {
  62. return {
  63. get(node, prop) {
  64. if (prop === 'proxyOf') {
  65. return node
  66. } else if (!node[prop]) {
  67. return node[prop]
  68. } else if (
  69. prop === 'each' ||
  70. (typeof prop === 'string' && prop.startsWith('walk'))
  71. ) {
  72. return (...args) => {
  73. return node[prop](
  74. ...args.map(i => {
  75. if (typeof i === 'function') {
  76. return (child, index) => i(child.toProxy(), index)
  77. } else {
  78. return i
  79. }
  80. })
  81. )
  82. }
  83. } else if (prop === 'every' || prop === 'some') {
  84. return cb => {
  85. return node[prop]((child, ...other) =>
  86. cb(child.toProxy(), ...other)
  87. )
  88. }
  89. } else if (prop === 'root') {
  90. return () => node.root().toProxy()
  91. } else if (prop === 'nodes') {
  92. return node.nodes.map(i => i.toProxy())
  93. } else if (prop === 'first' || prop === 'last') {
  94. return node[prop].toProxy()
  95. } else {
  96. return node[prop]
  97. }
  98. },
  99. set(node, prop, value) {
  100. if (node[prop] === value) return true
  101. node[prop] = value
  102. if (prop === 'name' || prop === 'params' || prop === 'selector') {
  103. node.markDirty()
  104. }
  105. return true
  106. }
  107. }
  108. }
  109. index(child) {
  110. if (typeof child === 'number') return child
  111. if (child.proxyOf) child = child.proxyOf
  112. return this.proxyOf.nodes.indexOf(child)
  113. }
  114. insertAfter(exist, add) {
  115. let existIndex = this.index(exist)
  116. let nodes = this.normalize(add, this.proxyOf.nodes[existIndex]).reverse()
  117. existIndex = this.index(exist)
  118. for (let node of nodes) this.proxyOf.nodes.splice(existIndex + 1, 0, node)
  119. let index
  120. for (let id in this.indexes) {
  121. index = this.indexes[id]
  122. if (existIndex < index) {
  123. this.indexes[id] = index + nodes.length
  124. }
  125. }
  126. this.markDirty()
  127. return this
  128. }
  129. insertBefore(exist, add) {
  130. let existIndex = this.index(exist)
  131. let type = existIndex === 0 ? 'prepend' : false
  132. let nodes = this.normalize(
  133. add,
  134. this.proxyOf.nodes[existIndex],
  135. type
  136. ).reverse()
  137. existIndex = this.index(exist)
  138. for (let node of nodes) this.proxyOf.nodes.splice(existIndex, 0, node)
  139. let index
  140. for (let id in this.indexes) {
  141. index = this.indexes[id]
  142. if (existIndex <= index) {
  143. this.indexes[id] = index + nodes.length
  144. }
  145. }
  146. this.markDirty()
  147. return this
  148. }
  149. normalize(nodes, sample) {
  150. if (typeof nodes === 'string') {
  151. nodes = cleanSource(parse(nodes).nodes)
  152. } else if (typeof nodes === 'undefined') {
  153. nodes = []
  154. } else if (Array.isArray(nodes)) {
  155. nodes = nodes.slice(0)
  156. for (let i of nodes) {
  157. if (i.parent) i.parent.removeChild(i, 'ignore')
  158. }
  159. } else if (nodes.type === 'root' && this.type !== 'document') {
  160. nodes = nodes.nodes.slice(0)
  161. for (let i of nodes) {
  162. if (i.parent) i.parent.removeChild(i, 'ignore')
  163. }
  164. } else if (nodes.type) {
  165. nodes = [nodes]
  166. } else if (nodes.prop) {
  167. if (typeof nodes.value === 'undefined') {
  168. throw new Error('Value field is missed in node creation')
  169. } else if (typeof nodes.value !== 'string') {
  170. nodes.value = String(nodes.value)
  171. }
  172. nodes = [new Declaration(nodes)]
  173. } else if (nodes.selector || nodes.selectors) {
  174. nodes = [new Rule(nodes)]
  175. } else if (nodes.name) {
  176. nodes = [new AtRule(nodes)]
  177. } else if (nodes.text) {
  178. nodes = [new Comment(nodes)]
  179. } else {
  180. throw new Error('Unknown node type in node creation')
  181. }
  182. let processed = nodes.map(i => {
  183. /* c8 ignore next */
  184. if (!i[my]) Container.rebuild(i)
  185. i = i.proxyOf
  186. if (i.parent) i.parent.removeChild(i)
  187. if (i[isClean]) markTreeDirty(i)
  188. if (typeof i.raws.before === 'undefined') {
  189. if (sample && typeof sample.raws.before !== 'undefined') {
  190. i.raws.before = sample.raws.before.replace(/\S/g, '')
  191. }
  192. }
  193. i.parent = this.proxyOf
  194. return i
  195. })
  196. return processed
  197. }
  198. prepend(...children) {
  199. children = children.reverse()
  200. for (let child of children) {
  201. let nodes = this.normalize(child, this.first, 'prepend').reverse()
  202. for (let node of nodes) this.proxyOf.nodes.unshift(node)
  203. for (let id in this.indexes) {
  204. this.indexes[id] = this.indexes[id] + nodes.length
  205. }
  206. }
  207. this.markDirty()
  208. return this
  209. }
  210. push(child) {
  211. child.parent = this
  212. this.proxyOf.nodes.push(child)
  213. return this
  214. }
  215. removeAll() {
  216. for (let node of this.proxyOf.nodes) node.parent = undefined
  217. this.proxyOf.nodes = []
  218. this.markDirty()
  219. return this
  220. }
  221. removeChild(child) {
  222. child = this.index(child)
  223. this.proxyOf.nodes[child].parent = undefined
  224. this.proxyOf.nodes.splice(child, 1)
  225. let index
  226. for (let id in this.indexes) {
  227. index = this.indexes[id]
  228. if (index >= child) {
  229. this.indexes[id] = index - 1
  230. }
  231. }
  232. this.markDirty()
  233. return this
  234. }
  235. replaceValues(pattern, opts, callback) {
  236. if (!callback) {
  237. callback = opts
  238. opts = {}
  239. }
  240. this.walkDecls(decl => {
  241. if (opts.props && !opts.props.includes(decl.prop)) return
  242. if (opts.fast && !decl.value.includes(opts.fast)) return
  243. decl.value = decl.value.replace(pattern, callback)
  244. })
  245. this.markDirty()
  246. return this
  247. }
  248. some(condition) {
  249. return this.nodes.some(condition)
  250. }
  251. walk(callback) {
  252. return this.each((child, i) => {
  253. let result
  254. try {
  255. result = callback(child, i)
  256. } catch (e) {
  257. throw child.addToError(e)
  258. }
  259. if (result !== false && child.walk) {
  260. result = child.walk(callback)
  261. }
  262. return result
  263. })
  264. }
  265. walkAtRules(name, callback) {
  266. if (!callback) {
  267. callback = name
  268. return this.walk((child, i) => {
  269. if (child.type === 'atrule') {
  270. return callback(child, i)
  271. }
  272. })
  273. }
  274. if (name instanceof RegExp) {
  275. return this.walk((child, i) => {
  276. if (child.type === 'atrule' && name.test(child.name)) {
  277. return callback(child, i)
  278. }
  279. })
  280. }
  281. return this.walk((child, i) => {
  282. if (child.type === 'atrule' && child.name === name) {
  283. return callback(child, i)
  284. }
  285. })
  286. }
  287. walkComments(callback) {
  288. return this.walk((child, i) => {
  289. if (child.type === 'comment') {
  290. return callback(child, i)
  291. }
  292. })
  293. }
  294. walkDecls(prop, callback) {
  295. if (!callback) {
  296. callback = prop
  297. return this.walk((child, i) => {
  298. if (child.type === 'decl') {
  299. return callback(child, i)
  300. }
  301. })
  302. }
  303. if (prop instanceof RegExp) {
  304. return this.walk((child, i) => {
  305. if (child.type === 'decl' && prop.test(child.prop)) {
  306. return callback(child, i)
  307. }
  308. })
  309. }
  310. return this.walk((child, i) => {
  311. if (child.type === 'decl' && child.prop === prop) {
  312. return callback(child, i)
  313. }
  314. })
  315. }
  316. walkRules(selector, callback) {
  317. if (!callback) {
  318. callback = selector
  319. return this.walk((child, i) => {
  320. if (child.type === 'rule') {
  321. return callback(child, i)
  322. }
  323. })
  324. }
  325. if (selector instanceof RegExp) {
  326. return this.walk((child, i) => {
  327. if (child.type === 'rule' && selector.test(child.selector)) {
  328. return callback(child, i)
  329. }
  330. })
  331. }
  332. return this.walk((child, i) => {
  333. if (child.type === 'rule' && child.selector === selector) {
  334. return callback(child, i)
  335. }
  336. })
  337. }
  338. get first() {
  339. if (!this.proxyOf.nodes) return undefined
  340. return this.proxyOf.nodes[0]
  341. }
  342. get last() {
  343. if (!this.proxyOf.nodes) return undefined
  344. return this.proxyOf.nodes[this.proxyOf.nodes.length - 1]
  345. }
  346. }
  347. Container.registerParse = dependant => {
  348. parse = dependant
  349. }
  350. Container.registerRule = dependant => {
  351. Rule = dependant
  352. }
  353. Container.registerAtRule = dependant => {
  354. AtRule = dependant
  355. }
  356. Container.registerRoot = dependant => {
  357. Root = dependant
  358. }
  359. module.exports = Container
  360. Container.default = Container
  361. /* c8 ignore start */
  362. Container.rebuild = node => {
  363. if (node.type === 'atrule') {
  364. Object.setPrototypeOf(node, AtRule.prototype)
  365. } else if (node.type === 'rule') {
  366. Object.setPrototypeOf(node, Rule.prototype)
  367. } else if (node.type === 'decl') {
  368. Object.setPrototypeOf(node, Declaration.prototype)
  369. } else if (node.type === 'comment') {
  370. Object.setPrototypeOf(node, Comment.prototype)
  371. } else if (node.type === 'root') {
  372. Object.setPrototypeOf(node, Root.prototype)
  373. }
  374. node[my] = true
  375. if (node.nodes) {
  376. node.nodes.forEach(child => {
  377. Container.rebuild(child)
  378. })
  379. }
  380. }
  381. /* c8 ignore stop */