2022-08-26 18:51:02 +02:00
|
|
|
import Attributes from './attributes.mjs'
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2020-07-19 13:01:01 +02:00
|
|
|
function Snippet(def, anchor, debug = false) {
|
2019-08-03 15:03:33 +02:00
|
|
|
this.def = def
|
|
|
|
this.anchor = anchor
|
|
|
|
this.attributes = new Attributes()
|
2020-07-23 11:54:55 +02:00
|
|
|
Object.defineProperty(this, 'debug', { value: debug, configurable: true })
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
2018-07-23 11:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 10:26:04 +02:00
|
|
|
/** Adds the raise method for a snippet not created through the proxy **/
|
|
|
|
Snippet.prototype.withRaise = function (raise = false) {
|
|
|
|
if (raise) Object.defineProperty(this, 'raise', { value: raise })
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
2018-08-17 20:24:10 +02:00
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2020-07-18 16:48:29 +02:00
|
|
|
Snippet.prototype.attr = function (name, value, overwrite = false) {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (overwrite) this.attributes.set(name, value)
|
|
|
|
else this.attributes.add(name, value)
|
2018-08-17 20:24:10 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-08-17 20:24:10 +02:00
|
|
|
|
2018-08-03 14:20:28 +02:00
|
|
|
/** Returns a deep copy of this */
|
2020-07-18 16:48:29 +02:00
|
|
|
Snippet.prototype.clone = function () {
|
2020-07-23 11:54:55 +02:00
|
|
|
let clone = new Snippet(this.def, this.anchor.clone(), this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
clone.attributes = this.attributes.clone()
|
2018-08-03 14:20:28 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return clone
|
|
|
|
}
|
2018-08-03 14:20:28 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
export default Snippet
|