Permalink
Browse files

New manipulation method: append; closes gh-9

  • Loading branch information...
jferris committed Mar 31, 2010
1 parent 978fd82 commit b829d227cae7b51bd75647168bf2a0d680931d0f
Showing with 29 additions and 3 deletions.
  1. +16 −3 lib/effigy/view.rb
  2. +13 −0 spec/effigy/view_spec.rb
View
@@ -136,13 +136,13 @@ def remove_class(selector, *class_names)
#
# @param [String] selector a CSS or XPath string describing the element to
# transform
# @param [String] html the new contents of the selected element. Markup is
# @param [String] inner_html the new contents of the selected element. Markup is
# not escaped.
# @example
# html('p', '<b>Welcome!</b>')
# find('p').html('<b>Welcome!</b>')
def html(selector, html)
select(selector).inner_html = html
def html(selector, inner_html)
select(selector).inner_html = inner_html
end
# Replaces the selected element with live markup.
@@ -157,6 +157,19 @@ def replace_with(selector, html)
select(selector).after(html).unlink
end
# Adds the given markup to the end of the selected element.
#
# @param [String] selector a CSS or XPath string describing the element to
# which this HTML should be appended
# @param [String] html_to_append the new markup to append to the selected
# element. Markup is not escaped.
def append(selector, html_to_append)
node = select(selector)
current_context.fragment(html_to_append).children.each do |child|
node << child
end
end
# Selects an element or elements for chained transformation.
#
# If given a block, the selection will be in effect during the block.
View
@@ -127,6 +127,19 @@ module Effigy
html.should_not have_selector('test')
end
it "should append text to an element" do
template = %{<test>start</test>}
view = Effigy::View.new
html = view.render(template) do
view.append 'test', '<p>middle</p><p>end</p>'
end
html.should include("<test>start")
html.should have_selector('test p', :contents => 'middle')
html.should have_selector('test p', :contents => 'end')
end
it "should render html by default" do
template = %{<html/>}
html = Effigy::View.new.render(template)

0 comments on commit b829d22

Please sign in to comment.