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.

434 lines
9.7 KiB

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