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