aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_text/fragment.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2018-04-13 16:23:04 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2018-04-13 16:23:04 -0700
commitf1d74871e7f00e8bbde3501a759487ac8cc4c3fc (patch)
tree2f1fa680bd1e6a356256fdc59cab5a6af8d74da7 /lib/action_text/fragment.rb
parent3bc244abc1800c7617cbfbbe1dd2597053a638c9 (diff)
downloadrails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.tar.gz
rails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.tar.bz2
rails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.zip
Rename from Active Text to Action Text
This is more like Action View than Active Model.
Diffstat (limited to 'lib/action_text/fragment.rb')
-rw-r--r--lib/action_text/fragment.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/action_text/fragment.rb b/lib/action_text/fragment.rb
new file mode 100644
index 0000000000..63b088f3e1
--- /dev/null
+++ b/lib/action_text/fragment.rb
@@ -0,0 +1,55 @@
+module ActionText
+ class Fragment
+ class << self
+ def wrap(fragment_or_html)
+ case fragment_or_html
+ when self
+ fragment_or_html
+ when Nokogiri::HTML::DocumentFragment
+ new(fragment_or_html)
+ else
+ from_html(fragment_or_html)
+ end
+ end
+
+ def from_html(html)
+ new(ActionText::HtmlConversion.fragment_for_html(html.to_s.strip))
+ end
+ end
+
+ attr_reader :source
+
+ def initialize(source)
+ @source = source
+ end
+
+ def find_all(selector)
+ source.css(selector)
+ end
+
+ def update
+ yield source = self.source.clone
+ self.class.new(source)
+ end
+
+ def replace(selector)
+ update do |source|
+ source.css(selector).each do |node|
+ node.replace(yield(node).to_s)
+ end
+ end
+ end
+
+ def to_plain_text
+ @plain_text ||= PlainTextConversion.node_to_plain_text(source)
+ end
+
+ def to_html
+ @html ||= HtmlConversion.node_to_html(source)
+ end
+
+ def to_s
+ to_html
+ end
+ end
+end