def Elem.new(name, *args)
attrs = []
children = []
context = nil
args.flatten.each {|arg|
case arg
when Context
raise ArgumentError, "multiple context" if context
context = arg
when Hash
arg.each {|k, v| attrs << [k, v] }
when HTree::Doc
arg.children.each {|c|
next if HTree::XMLDecl === c
next if HTree::DocType === c
children << c
}
when HTree::Node
children << arg
when String
children << Text.new(arg)
else
raise TypeError, "unexpected argument: #{arg.inspect}"
end
}
context ||= DefaultContext
if children.empty? && args.all? {|arg| Hash === arg || Context === arg }
children = nil
end
new!(STag.new(name, attrs, context), children)
end