test/javascript/test_markdown.rb in spontaneous-0.2.0.alpha2 vs test/javascript/test_markdown.rb in spontaneous-0.2.0.alpha3

- old
+ new

@@ -7,11 +7,11 @@ # Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eget augue leo, et consequat sem. Duis rutrum tortor nec ipsum vestibulum in elementum sem euismod. Nullam lacinia aliquet erat, sit amet mollis neque ultricies ut. Etiam lobortis feugiat condimentum. Sed bibendum vehicula malesuada. Nullam et metus sit amet mi placerat dapibus. Donec facilisis dignissim eros, id placerat tellus blandit vitae. Maecenas sed eros a odio adipiscing egestas a vel orci. Praesent volutpat tempor felis id pretium. Aliquam leo ipsum, tincidunt feugiat porttitor consectetur, scelerisque sed lectus. Proin tristique tristique enim, volutpat auctor felis blandit et. Pellentesque non feugiat lacus. def setup @page = page - @page.x(<<-JS) + @page.eval(<<-JS) var fake_input = function(start, end, value) { var array = []; array.value = value; array[0] = { selectionStart: start, @@ -23,23 +23,29 @@ }; array.val = function(new_value) { if (new_value) { this.value = new_value; } return this.value; }; + array.focus = function() {}; + array.bind = function() { return array; }; return array; } JS end def style(command, sel_start, sel_end, value) - state = @page.x(<<-JS) + state = @page.eval(<<-JS) var input = fake_input(#{sel_start}, #{sel_end}, #{value.inspect}) var command = new Spontaneous.FieldTypes.MarkdownField.#{command}(input) command.execute(); Spontaneous.FieldTypes.MarkdownField.TextCommand.get_state(input) JS - result = Hash[state.to_ary] + result = { + "before" => state["before"], + "middle" => state["middle"], + "after" => state["after"] + } result["value"] = result["before"] + result["middle"] + result["after"] result end context "Editor selection" do @@ -88,9 +94,43 @@ end should "work across multiple lines with existing styles" do state = style(style, 8, 8, "Lorem ipsum\n\ndolor sit #{mark}amet#{mark}") state['value'].should == "Lorem #{mark}ipsum#{mark}\n\ndolor sit #{mark}amet#{mark}" end + end + end + context "for links" do + setup do + @page.eval("Spontaneous.Location.retrieve = function() {}") + @page.eval("Spontaneous.Popover.open = function() {}") + end + should "expand selection to full link if cursor inside a link text" do + state = style('Link', 8, 8, "Before [link](http://example.com/page?param1=a&param2=b) after") + state["middle"].should == "[link](http://example.com/page?param1=a&param2=b)" + end + should "expand selection to full link if cursor inside a link URL" do + state = style('Link', 14, 14, "Before [link](http://example.com/page?param1=a&param2=b) after") + state["middle"].should == "[link](http://example.com/page?param1=a&param2=b)" + end + should "expand selection to full link if cursor inside a link URL with brackets afterwards" do + state = style('Link', 14, 14, "Before [link](http://example.com/page?param1=a&param2=b) (after)") + state["middle"].should == "[link](http://example.com/page?param1=a&param2=b)" + end + should "expand selection to full link if cursor between link text & URL" do + state = style('Link', 13, 13, "Before [link](http://example.com/page?param1=a&param2=b) after") + state["middle"].should == "[link](http://example.com/page?param1=a&param2=b)" + end + should "not greedily expand forward to other brackets within text" do + state = style('Link', 7, 11, "Before link (after)") + state["middle"].should == "link" + end + should "not greedily expand to other brackets within text" do + state = style('Link', 9, 13, "(Before) link after") + state["middle"].should == "link" + end + should "properly extract link from surrounding brackets" do + state = style('Link', 9, 9, "Before ([link](http://example.com/page?param1=a&param2=b)) after") + state["middle"].should == "[link](http://example.com/page?param1=a&param2=b)" end end end end end