Sha256: 5e439d8ca0979ee5201d8265c305bad880a21b7eb86daa73a51d5ffd4d2bbaa6

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

class Trix.Watchdog.Serializer
  constructor: (@element) ->
    @id = 0
    @serializeTree()
    @serializeSelection()

  serializeTree: ->
    @ids = new Map
    @tree = @serializeNode(@element)

  serializeNode: (node) ->
    object = id: ++@id, name: node.nodeName
    @ids.set(node, object.id)

    switch node.nodeType
      when Node.ELEMENT_NODE
        @serializeElementToObject(node, object)
        @serializeElementChildrenToObject(node, object)

      when Node.TEXT_NODE, Node.COMMENT_NODE
        @serializeNodeValueToObject(node, object)

    object

  serializeElementToObject: (node, object) ->
    attributes = {}
    hasAttributes = false

    for {name} in node.attributes
      if node.hasAttribute(name)
        value = node.getAttribute(name)
        value = "data:" if name is "src" and value[0...5] is "data:"
        attributes[name] = value
        hasAttributes = true

    if hasAttributes
      object.attributes = attributes

  serializeElementChildrenToObject: (node, object) ->
    if node.childNodes.length
      object.children = for childNode in node.childNodes
        @serializeNode(childNode)

  serializeNodeValueToObject: (node, object) ->
    object.value = node.nodeValue

  serializeSelection: ->
    selection = window.getSelection()
    return unless selection.rangeCount > 0

    range = selection.getRangeAt(0)
    startId = @ids.get(range?.startContainer)
    endId = @ids.get(range?.endContainer)

    if startId and endId
      @selection =
        start: id: startId, offset: range.startOffset
        end: id: endId, offset: range.endOffset

  getSnapshot: ->
    {@tree, @selection}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/vendor/trix/src/trix/inspector/watchdog/serializer.coffee
vapid-0.1.2 lib/vapid/vendor/trix/src/trix/inspector/watchdog/serializer.coffee
vapid-0.1.1 lib/vapid/vendor/trix/src/trix/inspector/watchdog/serializer.coffee
vapid-0.1.0 lib/vapid/vendor/trix/src/trix/inspector/watchdog/serializer.coffee