tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mark.ts (3710B)


      1 import {compareDeep} from "./comparedeep"
      2 import {Attrs, MarkType, Schema} from "./schema"
      3 
      4 /// A mark is a piece of information that can be attached to a node,
      5 /// such as it being emphasized, in code font, or a link. It has a
      6 /// type and optionally a set of attributes that provide further
      7 /// information (such as the target of the link). Marks are created
      8 /// through a `Schema`, which controls which types exist and which
      9 /// attributes they have.
     10 export class Mark {
     11  /// @internal
     12  constructor(
     13    /// The type of this mark.
     14    readonly type: MarkType,
     15    /// The attributes associated with this mark.
     16    readonly attrs: Attrs
     17  ) {}
     18 
     19  /// Given a set of marks, create a new set which contains this one as
     20  /// well, in the right position. If this mark is already in the set,
     21  /// the set itself is returned. If any marks that are set to be
     22  /// [exclusive](#model.MarkSpec.excludes) with this mark are present,
     23  /// those are replaced by this one.
     24  addToSet(set: readonly Mark[]): readonly Mark[] {
     25    let copy, placed = false
     26    for (let i = 0; i < set.length; i++) {
     27      let other = set[i]
     28      if (this.eq(other)) return set
     29      if (this.type.excludes(other.type)) {
     30        if (!copy) copy = set.slice(0, i)
     31      } else if (other.type.excludes(this.type)) {
     32        return set
     33      } else {
     34        if (!placed && other.type.rank > this.type.rank) {
     35          if (!copy) copy = set.slice(0, i)
     36          copy.push(this)
     37          placed = true
     38        }
     39        if (copy) copy.push(other)
     40      }
     41    }
     42    if (!copy) copy = set.slice()
     43    if (!placed) copy.push(this)
     44    return copy
     45  }
     46 
     47  /// Remove this mark from the given set, returning a new set. If this
     48  /// mark is not in the set, the set itself is returned.
     49  removeFromSet(set: readonly Mark[]): readonly Mark[] {
     50    for (let i = 0; i < set.length; i++)
     51      if (this.eq(set[i]))
     52        return set.slice(0, i).concat(set.slice(i + 1))
     53    return set
     54  }
     55 
     56  /// Test whether this mark is in the given set of marks.
     57  isInSet(set: readonly Mark[]) {
     58    for (let i = 0; i < set.length; i++)
     59      if (this.eq(set[i])) return true
     60    return false
     61  }
     62 
     63  /// Test whether this mark has the same type and attributes as
     64  /// another mark.
     65  eq(other: Mark) {
     66    return this == other ||
     67      (this.type == other.type && compareDeep(this.attrs, other.attrs))
     68  }
     69 
     70  /// Convert this mark to a JSON-serializeable representation.
     71  toJSON(): any {
     72    let obj: any = {type: this.type.name}
     73    for (let _ in this.attrs) {
     74      obj.attrs = this.attrs
     75      break
     76    }
     77    return obj
     78  }
     79 
     80  /// Deserialize a mark from JSON.
     81  static fromJSON(schema: Schema, json: any) {
     82    if (!json) throw new RangeError("Invalid input for Mark.fromJSON")
     83    let type = schema.marks[json.type]
     84    if (!type) throw new RangeError(`There is no mark type ${json.type} in this schema`)
     85    let mark = type.create(json.attrs)
     86    type.checkAttrs(mark.attrs)
     87    return mark
     88  }
     89 
     90  /// Test whether two sets of marks are identical.
     91  static sameSet(a: readonly Mark[], b: readonly Mark[]) {
     92    if (a == b) return true
     93    if (a.length != b.length) return false
     94    for (let i = 0; i < a.length; i++)
     95      if (!a[i].eq(b[i])) return false
     96    return true
     97  }
     98 
     99  /// Create a properly sorted mark set from null, a single mark, or an
    100  /// unsorted array of marks.
    101  static setFrom(marks?: Mark | readonly Mark[] | null): readonly Mark[] {
    102    if (!marks || Array.isArray(marks) && marks.length == 0) return Mark.none
    103    if (marks instanceof Mark) return [marks]
    104    let copy = marks.slice()
    105    copy.sort((a, b) => a.type.rank - b.type.rank)
    106    return copy
    107  }
    108 
    109  /// The empty set of marks.
    110  static none: readonly Mark[] = []
    111 }