{classNames} = Trix.config.css @TEST_IMAGE_URL = "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" createDocument = (parts...) -> blocks = for part in parts [string, textAttributes, blockAttributes] = part text = Trix.Text.textForStringWithAttributes(string, textAttributes) new Trix.Block text, blockAttributes new Trix.Document blocks blockComment = "" cursorTarget = Trix.makeElement( tagName: "span" textContent: Trix.ZERO_WIDTH_SPACE data: trixSelection: true, trixCursorTarget: true, trixSerialize: false ).outerHTML removeWhitespace = (string) -> string.replace(/\s/g, "") @fixtures = "bold text": document: createDocument(["abc", bold: true]) html: "
#{blockComment}abc
" serializedHTML: "
abc
" "bold, italic text": document: createDocument(["abc", bold: true, italic: true]) html: "
#{blockComment}abc
" "text with newline": document: createDocument(["ab\nc"]) html: "
#{blockComment}ab
c
" "text with link": document: createDocument(["abc", href: "http://example.com"]) html: """
#{blockComment}abc
""" "text with link and formatting": document: createDocument(["abc", italic: true, href: "http://example.com"]) html: """
#{blockComment}abc
""" "partially formatted link": document: new Trix.Document [ new Trix.Block new Trix.Text [ new Trix.StringPiece "ab", href: "http://example.com" new Trix.StringPiece "c", href: "http://example.com", italic: true ] ] html: """
#{blockComment}abc
""" "spaces 1": document: createDocument([" a"]) html: """
#{blockComment} a
""" "spaces 2": document: createDocument([" a"]) html: """
#{blockComment}  a
""" "spaces 3": document: createDocument([" a"]) html: """
#{blockComment}   a
""" "spaces 4": document: createDocument([" a "]) html: """
#{blockComment} a 
""" "spaces 5": document: createDocument(["a b"]) html: """
#{blockComment}a  b
""" "spaces 6": document: createDocument(["a b"]) html: """
#{blockComment}a   b
""" "spaces 7": document: createDocument(["a b"]) html: """
#{blockComment}a    b
""" "spaces 8": document: createDocument(["a b "]) html: """
#{blockComment}a b 
""" "spaces 9": document: createDocument(["a b c"]) html: """
#{blockComment}a b c
""" "spaces 10": document: createDocument(["a "]) html: """
#{blockComment}a 
""" "spaces 11": document: createDocument(["a "]) html: """
#{blockComment}a  
""" "spaces and formatting": document: new Trix.Document [ new Trix.Block new Trix.Text [ new Trix.StringPiece " a " new Trix.StringPiece "b", href: "http://b.com" new Trix.StringPiece " " new Trix.StringPiece "c", bold: true new Trix.StringPiece " d" new Trix.StringPiece " e ", italic: true new Trix.StringPiece " f " ] ] html: """
#{blockComment} a b c d e  f  
""" "quote formatted block": document: createDocument(["abc", {}, ["quote"]]) html: "
#{blockComment}abc
" "code formatted block": document: createDocument(["123", {}, ["code"]]) html: "
#{blockComment}123
" "code with newline": document: createDocument(["12\n3", {}, ["code"]]) html: "
#{blockComment}12\n3
" "multiple blocks with block comments in their text": document: createDocument(["a#{blockComment}b", {}, ["quote"]], ["#{blockComment}c", {}, ["code"]]) html: "
#{blockComment}a<!--block-->b
#{blockComment}<!--block-->c
" serializedHTML: "
a<!--block-->b
<!--block-->c
" "unordered list with one item": document: createDocument(["a", {}, ["bulletList", "bullet"]]) html: "" "unordered list with bold text": document: createDocument(["a", { bold: true }, ["bulletList", "bullet"]]) html: "" "unordered list with partially formatted text": document: new Trix.Document [ new Trix.Block( new Trix.Text([ new Trix.StringPiece("a") new Trix.StringPiece("b", italic: true) ]), ["bulletList", "bullet"] ) ] html: "" "unordered list with two items": document: createDocument(["a", {}, ["bulletList", "bullet"]], ["b", {}, ["bulletList", "bullet"]]) html: "" "unordered list surrounded by unformatted blocks": document: createDocument(["a"], ["b", {}, ["bulletList", "bullet"]], ["c"]) html: "
#{blockComment}a
#{blockComment}c
" "ordered list": document: createDocument(["a", {}, ["numberList", "number"]]) html: "
  1. #{blockComment}a
" "ordered list and an unordered list": document: createDocument(["a", {}, ["bulletList", "bullet"]], ["b", {}, ["numberList", "number"]]) html: "
  1. #{blockComment}b
" "empty block with attributes": document: createDocument(["", {}, ["quote"]]) html: "
#{blockComment}
" "image attachment": do -> attrs = url: TEST_IMAGE_URL, filename: "example.png", filesize: 98203, contentType: "image/png", width: 1, height: 1 attachment = new Trix.Attachment attrs text = Trix.Text.textForAttachmentWithAttributes(attachment) image = Trix.makeElement("img", src: attrs.url, "data-trix-mutable": true, width: 1, height: 1) image.dataset.trixStoreKey = ["imageElement", attachment.id, image.src, image.width, image.height].join("/") caption = Trix.makeElement(tagName: "figcaption", className: classNames.attachment.caption) caption.innerHTML = """#{attrs.filename} 95.9 KB""" figure = Trix.makeElement tagName: "figure" className: "attachment attachment-preview png" data = trixAttachment: JSON.stringify(attachment) trixContentType: "image/png" trixId: attachment.id figure.dataset[key] = value for key, value of data figure.setAttribute("contenteditable", false) figure.appendChild(image) figure.appendChild(caption) serializedFigure = figure.cloneNode(true) for attribute in ["data-trix-id", "data-trix-mutable", "data-trix-store-key", "contenteditable"] serializedFigure.removeAttribute(attribute) for element in serializedFigure.querySelectorAll("[#{attribute}]") element.removeAttribute(attribute) html: "
#{blockComment}#{cursorTarget}#{figure.outerHTML}#{cursorTarget}
" serializedHTML: "
#{serializedFigure.outerHTML}
" document: new Trix.Document [new Trix.Block text] "text with newlines and image attachment": do -> stringText = Trix.Text.textForStringWithAttributes("a\nb") attrs = url: TEST_IMAGE_URL, filename: "example.png", filesize: 98203, contentType: "image/png", width: 1, height: 1 attachment = new Trix.Attachment attrs attachmentText = Trix.Text.textForAttachmentWithAttributes(attachment) image = Trix.makeElement("img", src: attrs.url, "data-trix-mutable": true, width: 1, height: 1) image.dataset.trixStoreKey = ["imageElement", attachment.id, image.src, image.width, image.height].join("/") caption = Trix.makeElement(tagName: "figcaption", className: classNames.attachment.caption) caption.innerHTML = """#{attrs.filename} 95.9 KB""" figure = Trix.makeElement tagName: "figure" className: "attachment attachment-preview png" data = trixAttachment: JSON.stringify(attachment) trixContentType: "image/png" trixId: attachment.id figure.dataset[key] = value for key, value of data figure.setAttribute("contenteditable", false) figure.appendChild(image) figure.appendChild(caption) serializedFigure = figure.cloneNode(true) for attribute in ["data-trix-id", "data-trix-mutable", "data-trix-store-key", "contenteditable"] serializedFigure.removeAttribute(attribute) for element in serializedFigure.querySelectorAll("[#{attribute}]") element.removeAttribute(attribute) text = stringText.appendText(attachmentText) html: "
#{blockComment}a
b#{cursorTarget}#{figure.outerHTML}#{cursorTarget}
" serializedHTML: "
a
b#{serializedFigure.outerHTML}
" document: new Trix.Document [new Trix.Block text] "image attachment with edited caption": do -> attrs = url: TEST_IMAGE_URL, filename: "example.png", filesize: 123, contentType: "image/png", width: 1, height: 1 attachment = new Trix.Attachment attrs textAttrs = caption: "Example" text = Trix.Text.textForAttachmentWithAttributes(attachment, textAttrs) image = Trix.makeElement("img", src: attrs.url, "data-trix-mutable": true, width: 1, height: 1) image.dataset.trixStoreKey = ["imageElement", attachment.id, image.src, image.width, image.height].join("/") caption = Trix.makeElement(tagName: "figcaption", className: "#{classNames.attachment.caption} #{classNames.attachment.captionEdited}", textContent: "Example") figure = Trix.makeElement tagName: "figure" className: "attachment attachment-preview png" data = trixAttachment: JSON.stringify(attachment) trixContentType: "image/png" trixId: attachment.id trixAttributes: JSON.stringify(textAttrs) figure.dataset[key] = value for key, value of data figure.setAttribute("contenteditable", false) figure.appendChild(image) figure.appendChild(caption) html: "
#{blockComment}#{cursorTarget}#{figure.outerHTML}#{cursorTarget}
" document: new Trix.Document [new Trix.Block text] "file attachment": do -> attrs = href: "http://example.com/example.pdf", filename: "example.pdf", filesize: 34038769, contentType: "application/pdf" attachment = new Trix.Attachment attrs text = Trix.Text.textForAttachmentWithAttributes(attachment) figure = Trix.makeElement tagName: "figure" className: "attachment attachment-file pdf" data = trixAttachment: JSON.stringify(attachment) trixContentType: "application/pdf" trixId: attachment.id link = Trix.makeElement("a", href: attrs.href) link.dataset[key] = value for key, value of data link.setAttribute("contenteditable", false) link.appendChild(figure) caption = """
#{attrs.filename} 32.46 MB
""" figure.innerHTML = caption html: """
#{blockComment}#{cursorTarget}#{link.outerHTML}#{cursorTarget}
""" document: new Trix.Document [new Trix.Block text] "pending file attachment": do -> attrs = filename: "example.pdf", filesize: 34038769, contentType: "application/pdf" attachment = new Trix.Attachment attrs attachment.file = {} text = Trix.Text.textForAttachmentWithAttributes(attachment) figure = Trix.makeElement tagName: "figure" className: "attachment attachment-file pdf" data = trixAttachment: JSON.stringify(attachment) trixContentType: "application/pdf" trixId: attachment.id trixSerialize: false figure.dataset[key] = value for key, value of data figure.setAttribute("contenteditable", false) progress = Trix.makeElement tagName: "progress" attributes: class: "progress" value: 0 max: 100 data: trixMutable: true trixStoreKey: ["progressElement", attachment.id].join("/") caption = """
#{attrs.filename} 32.46 MB
""" figure.innerHTML = caption + progress.outerHTML html: """
#{blockComment}#{cursorTarget}#{figure.outerHTML}#{cursorTarget}
""" document: new Trix.Document [new Trix.Block text] "content attachment": do -> content = """

ruby-build 20150413 is out, with definitions for 2.2.2, 2.1.6, and 2.0.0-p645 to address recent security issues: https://t.co/YEwV6NtRD8

— Sam Stephenson (@sstephenson) April 13, 2015
""" href = "https://twitter.com/sstephenson/status/587715996783218688" contentType = "embed/twitter" attachment = new Trix.Attachment {content, contentType, href} text = Trix.Text.textForAttachmentWithAttributes(attachment) figure = Trix.makeElement tagName: "figure" className: "attachment attachment-content" figure.innerHTML = content caption = Trix.makeElement(tagName: "figcaption", className: classNames.attachment.caption) figure.appendChild(caption) data = trixAttachment: JSON.stringify(attachment) trixContentType: contentType trixId: attachment.id figure.dataset[key] = value for key, value of data figure.setAttribute("contenteditable", false) html: """
#{blockComment}#{cursorTarget}#{figure.outerHTML}#{cursorTarget}
""" document: new Trix.Document [new Trix.Block text] "nested quote and code formatted block": document: createDocument(["ab3", {}, ["quote", "code"]]) html: "
#{blockComment}ab3
" "nested code and quote formatted block": document: createDocument(["ab3", {}, ["code", "quote"]]) html: "
#{blockComment}ab3
" "nested code blocks in quote": document: createDocument( ["a\n", {}, ["quote"]], ["b", {}, ["quote", "code"]], ["\nc\n", {}, ["quote"]], ["d", {}, ["quote", "code"]] ) html: removeWhitespace """
#{blockComment} a

          #{blockComment}
          b
        
#{blockComment}
c

          #{blockComment}
          d
        
""" serializedHTML: removeWhitespace """
a

          b
        

c

          d
        
""" "nested code, quote, and list in quote": document: createDocument( ["a\n", {}, ["quote"]], ["b", {}, ["quote", "code"]], ["\nc\n", {}, ["quote"]], ["d", {}, ["quote", "quote"]], ["\ne\n", {}, ["quote"]], ["f", {}, ["quote", "bulletList", "bullet"]] ) html: removeWhitespace """
#{blockComment} a

        #{blockComment}
        b
      
#{blockComment}
c

#{blockComment} d
#{blockComment}
e

""" serializedHTML: removeWhitespace """
a

          b
        

c

d

e

""" "nested quote and list": document: createDocument(["ab3", {}, ["quote", "bulletList", "bullet"]]) html: "
" "nested list and quote": document: createDocument(["ab3", {}, ["bulletList", "bullet", "quote"]]) html: "" "nested lists and quotes": document: createDocument(["a", {}, ["bulletList", "bullet", "quote"]], ["b", {}, ["bulletList", "bullet", "quote"]]) html: "" "nested quote and list with two items": document: createDocument(["a", {}, ["quote", "bulletList", "bullet"]], ["b", {}, ["quote", "bulletList", "bullet"]]) html: "
" "nested unordered lists": document: createDocument(["a", {}, ["bulletList", "bullet"]], ["b", {}, ["bulletList", "bullet", "bulletList", "bullet"]], ["c", {}, ["bulletList", "bullet", "bulletList", "bullet"]]) html: "" "nested lists": document: createDocument(["a", {}, ["numberList", "number"]], ["b", {}, ["numberList", "number", "bulletList", "bullet"]], ["c", {}, ["numberList", "number", "bulletList", "bullet"]]) html: "
  1. #{blockComment}a
" "blocks beginning with newlines": document: createDocument(["\na", {}, ["quote"]], ["\nb", {}, []], ["\nc", {}, ["quote"]]) html: "
#{blockComment}
a
#{blockComment}
b
#{blockComment}
c
" "blocks beginning with formatted text": document: createDocument(["a", { bold: true }, ["quote"]], ["b", { italic: true }, []], ["c", { bold: true }, ["quote"]]) html: "
#{blockComment}a
#{blockComment}b
#{blockComment}c
" "text with newlines before block": document: createDocument(["a\nb"], ["c", {}, ["quote"]]) html: "
#{blockComment}a
b
#{blockComment}c
" "empty heading block": document: createDocument(["", {}, ["heading1"]]) html: "

#{blockComment}

" "two adjacent headings": document: createDocument( ["a", {}, ["heading1"]], ["b", {}, ["heading1"]]) html: "

#{blockComment}a

#{blockComment}b

" "heading in ordered list": document: createDocument(["a", {}, ["numberList", "number", "heading1"]]) html: "
  1. #{blockComment}a

" "headings with formatted text": document: createDocument(["a", { bold: true }, ["heading1"]], ["b", { italic: true, bold: true }, ["heading1"]]) html: "

#{blockComment}a

#{blockComment}b

" @eachFixture = (callback) -> for name, details of @fixtures callback(name, details)