1require "rss/utils"
2
3module RSS
4  module XML
5    class Element
6      include Enumerable
7
8      attr_reader :name, :prefix, :uri, :attributes, :children
9      def initialize(name, prefix=nil, uri=nil, attributes={}, children=[])
10        @name = name
11        @prefix = prefix
12        @uri = uri
13        @attributes = attributes
14        if children.is_a?(String) or !children.respond_to?(:each)
15          @children = [children]
16        else
17          @children = children
18        end
19      end
20
21      def [](name)
22        @attributes[name]
23      end
24
25      def []=(name, value)
26        @attributes[name] = value
27      end
28
29      def <<(child)
30        @children << child
31      end
32
33      def each(&block)
34        @children.each(&block)
35      end
36
37      def ==(other)
38        other.kind_of?(self.class) and
39          @name == other.name and
40          @uri == other.uri and
41          @attributes == other.attributes and
42          @children == other.children
43      end
44
45      def to_s
46        rv = "<#{full_name}"
47        attributes.each do |key, value|
48          rv << " #{Utils.html_escape(key)}=\"#{Utils.html_escape(value)}\""
49        end
50        if children.empty?
51          rv << "/>"
52        else
53          rv << ">"
54          children.each do |child|
55            rv << child.to_s
56          end
57          rv << "</#{full_name}>"
58        end
59        rv
60      end
61
62      def full_name
63        if @prefix
64          "#{@prefix}:#{@name}"
65        else
66          @name
67        end
68      end
69    end
70  end
71end
72