Skip to content

Commit

Permalink
add size format and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dchenk committed Jan 3, 2019
1 parent a9960b6 commit ec868ac
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fmt.Println(string(html))
- Text color
- Italic
- Link
- Size
- Strikethrough
- Superscript/Subscript
- Underline
Expand Down
14 changes: 14 additions & 0 deletions inline_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func (bf *bkgFormat) HasFormat(o *Op) bool {
return o.Attrs["background"] == bf.c
}

// sizeFormat is used for inline strings of named sizes such as "huge" or "small".
type sizeFormat string

func (sf sizeFormat) Fmt() *Format {
return &Format{
Val: "ql-size-" + string(sf),
Place: Class,
}
}

func (sf sizeFormat) HasFormat(o *Op) bool {
return o.Attrs["size"] == string(sf)
}

// script (sup and sub)

type scriptFormat struct {
Expand Down
10 changes: 7 additions & 3 deletions raw_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
)

type rawOp struct {
Insert interface{} `json:"insert"`
Attrs map[string]interface{} `json:"attributes"`
// Insert is the string containing the data.
Insert interface{} `json:"insert"`

// Attrs contains the "attributes" property of the op.
Attrs map[string]interface{} `json:"attributes"`
}

// makeOp takes a raw Delta op as extracted from the JSON and turns it into an Op to make it usable for rendering.
Expand Down Expand Up @@ -42,7 +45,8 @@ func (ro *rawOp) makeOp(o *Op) error {
}

if ro.Attrs != nil {
for attr := range ro.Attrs { // the map was already made
// The map was already made
for attr := range ro.Attrs {
o.Attrs[attr] = extractString(ro.Attrs[attr])
}
}
Expand Down
2 changes: 2 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ func (o *Op) getFormatter(keyword string, customFormats func(string, *Op) Format
}
case "bold":
return new(boldFormat)
case "size":
return sizeFormat(o.Attrs["size"])
case "italic":
return new(italicFormat)
case "underline":
Expand Down
133 changes: 93 additions & 40 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,86 @@ package quill
import (
"bytes"
"io/ioutil"
"strconv"
"testing"
)

func TestSimple(t *testing.T) {

cases := []string{
`[{"insert": "\n"}]`, // blank
`[{"insert": "line1\nline2\n"}]`, // two paragraphs (single op)
`[{"insert": "line1\n\nline3\n"}]`, // blank line
`[{"insert": "bkqt"}, {"attributes": {"blockquote": true}, "insert": "\n"}]`, // blockquote
`[{"attributes": {"color": "#a10000"}, "insert": "colored"}, {"insert": "\n"}]`, // color
`[{"attributes":{"strike":true},"insert":"striked"},{"insert":"\n"}]`, // strikethrough
`[{"insert":"abc "},{"attributes":{"bold":true},"insert":"bld"},{"attributes":{"list":"bullet"},"insert":"\n"}]`, // list
`[{"insert":{"image":"source-url"}},{"insert":"\n"}]`, // image
`[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`, // image
`[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"colored"},{"insert":" plain\n"}]`, // background
`[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`, // underlined
`[{"insert":"plain"},{"attributes":{"script":"super"},"insert":"super"},{"insert":"\n"}]`, // superscript
`[{"insert":"plain"},{"attributes":{"script":"sub"},"insert":"sub"},{"insert":"\n"}]`, // subscript
cases := map[string]struct {
ops string
want string
}{
"empty": {
ops: `[{"insert": "\n"}]`,
want: "<p><br></p>",
},
"two paragraphs (single op)": {
ops: `[{"insert": "line1\nline2\n"}]`,
want: "<p>line1</p><p>line2</p>",
},
"blank line": {
ops: `[{"insert": "line1\n\nline3\n"}]`,
want: "<p>line1</p><p><br></p><p>line3</p>",
},
"blockquote": {
ops: `[{"insert": "bkqt"}, {"attributes": {"blockquote": true}, "insert": "\n"}]`,
want: "<blockquote>bkqt</blockquote>",
},
"color": {
ops: `[{"attributes": {"color": "#a10000"}, "insert": "colored"}, {"insert": "\n"}]`,
want: `<p><span style="color:#a10000;">colored</span></p>`,
},
"strikethrough": {
ops: `[{"attributes":{"strike":true},"insert":"striked"},{"insert":"\n"}]`,
want: "<p><s>striked</s></p>",
},
"list": {
ops: `[{"insert":"abc "},{"attributes":{"bold":true},"insert":"bld"},{"attributes":{"list":"bullet"},"insert":"\n"}]`,
want: "<ul><li>abc <strong>bld</strong></li></ul>",
},
"image": {
ops: `[{"insert":{"image":"source-url"}},{"insert":"\n"}]`,
want: `<p><img src="source-url"></p>`,
},
"image wrapped": {
ops: `[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`,
want: `<p>text <img src="source-url"> more text</p>`,
},
"background": {
ops: `[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"bkg colored"},{"insert":" plain\n"}]`,
want: `<p>abc <span style="background-color:#66a3e0;">bkg colored</span> plain</p>`,
},
"underlined": {
ops: `[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`,
want: "<p><u>underlined</u></p>",
},
"size": {
ops: `[{"insert":"stuff "},
{"insert":"large","attributes":{"size":"large"}},{"insert":" other "},
{"insert":"small","attributes":{"size":"small"}},{"insert":"\n"}]`,
want: `<p>stuff <span class="ql-size-large">large</span> other <span class="ql-size-small">small</span></p>`,
},
"superscript": {
ops: `[{"insert":"plain"},{"attributes":{"script":"super"},"insert":"super"},{"insert":"\n"}]`,
want: "<p>plain<sup>super</sup></p>",
},
"subscript": {
ops: `[{"insert":"plain"},{"attributes":{"script":"sub"},"insert":"sub"},{"insert":"\n"}]`,
want: "<p>plain<sub>sub</sub></p>",
},
}

want := []string{
"<p><br></p>",
"<p>line1</p><p>line2</p>",
"<p>line1</p><p><br></p><p>line3</p>",
"<blockquote>bkqt</blockquote>",
`<p><span style="color:#a10000;">colored</span></p>`,
"<p><s>striked</s></p>",
"<ul><li>abc <strong>bld</strong></li></ul>",
`<p><img src="source-url"></p>`,
`<p>text <img src="source-url"> more text</p>`,
`<p>abc <span style="background-color:#66a3e0;">colored</span> plain</p>`,
"<p><u>underlined</u></p>",
"<p>plain<sup>super</sup></p>",
"<p>plain<sub>sub</sub></p>",
}

for i := range cases {

bts, err := Render([]byte(cases[i]))
if err != nil {
t.Fatalf("%s", err)
}
if string(bts) != want[i] {
t.Errorf("bad rendering (index %d); got: %s", i, bts)
}

for k, tc := range cases {
t.Run(k, func(t *testing.T) {
got, err := Render([]byte(tc.ops))
if err != nil {
t.Fatalf("%s", err)
}
if string(got) != tc.want {
t.Errorf("bad rendering; got: %s", got)
}
})
}

}
Expand Down Expand Up @@ -85,6 +118,26 @@ func TestRender(t *testing.T) {

}

func TestClassesList(t *testing.T) {
cases := []struct {
classes []string
expect string
}{
{nil, ""},
{[]string{}, ""},
{[]string{"abc"}, ` class="abc"`},
{[]string{"abc", "ee-abcd"}, ` class="abc ee-abcd"`},
}
for i, tc := range cases {
t.Run("case_"+strconv.Itoa(i), func(t *testing.T) {
got := classesList(tc.classes)
if got != tc.expect {
t.Errorf("expected %q but got %q", tc.expect, got)
}
})
}
}

func BenchmarkRender_ops1(b *testing.B) {
bts, err := ioutil.ReadFile("./testdata/ops1.json")
if err != nil {
Expand Down

0 comments on commit ec868ac

Please sign in to comment.