 
 class Trix.Composition extends Trix.BasicObject
   constructor: (@document = new Trix.Document) ->
-    @document.delegate = this
     @currentAttributes = {}
+
+  setDocument: (document) ->
+    unless document.isEqualTo(@document)
+      @document = document
+      @delegate?.compositionDidChangeDocument(document)
 
   # Snapshots
 

    selectedRange: @getSelectedRange()

  restoreSnapshot: ({document, selectedRange}) ->
    @document.replaceDocument(document)
    @setSelectedRange(selectedRange)
    @delegate?.compositionDidRestoreSnapshot?()

  # Document delegate

  didEditDocument: (document) ->
    @delegate?.compositionDidChangeDocument?(@document)

  documentDidAddAttachment: (document, attachment) ->
    @delegate?.compositionDidAddAttachment?(attachment)



  replaceHTML: (html) ->
    document = Trix.Document.fromHTML(html).copyUsingObjectsFromDocument(@document)
    @preserveSelection =>
      @setDocument(document)

  insertFile: (file) ->
    if @delegate?.compositionShouldAcceptFile(file)
