Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraigmccraig committed May 24, 2011
1 parent 92e2fad commit abeea38
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
12 changes: 12 additions & 0 deletions lib/rsxml/namespace.rb
Expand Up @@ -149,6 +149,18 @@ def exploded_namespace_declarations(ns)
end]
end

# produces a Hash of compact namespace attributes from a
# Hash of namespace bindings
def namespace_attributes(ns)
Hash[ns.map do |prefix, uri|
if prefix==""
["xmlns", uri]
else
[["xmlns", prefix].join(":"), uri]
end
end]
end

# merges two sets of namespace bindings, raising error on clash
def merge_namespace_bindings(ns1, ns2)
m = ns1.clone
Expand Down
28 changes: 7 additions & 21 deletions lib/rsxml/visitor.rb
Expand Up @@ -40,11 +40,13 @@ def initialize(xml_builder=nil)
@xml = xml_builder || Builder::XmlMarkup.new
end

def tag(context, name, attrs)
def tag(context, name, attrs, ns_decls)
qname = Namespace::compact_qname(context.ns_stack, name)
qattrs = Namespace::compact_attr_qnames(context.ns_stack, attrs)
ns_attrs = Namespace::namespace_attributes(ns_decls)
attrs = qattrs.merge(ns_attrs)

xml.__send__(qname, qattrs) do
xml.__send__(qname, attrs) do
yield
end
end
Expand Down Expand Up @@ -85,33 +87,17 @@ def compact_attr_names(attrs)
Hash[attrs.map{|qname,value| [compact_qname(qname), value]}]
end

# split exploded attributes into attrs and namespace related attrs
def partition_namespace_decls(attrs)
attrs = []
namespaces = []
attrs.each do |qname,value|
local_name, prefix, uri = qname
if prefix=="xmlns" || (prefix==nil && local_name=="xmlns")
namespaces << [qname, uri]
else
attrs << [qname,value]
end
end
[Hash[attrs], Hash[namespaces]]
end

def tag(context, tag, attrs)

attrs, ns_attrs = partition_namespace_decls(attrs)
def tag(context, tag, attrs, ns_decls)

tag, attrs = tag_transformer.call(context, tag, attrs) if tag_transformer

opts[:style] ||= :exploded
if opts[:style] == :xml
tag = compact_qname(tag)
attrs = compact_attr_names(attrs)
ns_attrs = Namespace.namespace_attributes(ns_decls)
attrs = attrs.merge(ns_attrs)
elsif opts[:style] == :exploded
attrs = partition_namespace_decls(attrs)
end

el = [tag, (attrs if attrs.size>0)].compact
Expand Down
7 changes: 7 additions & 0 deletions spec/rsxml/namespace_spec.rb
Expand Up @@ -225,6 +225,13 @@ module Rsxml
end
end

describe "namespace_attributes" do
it "should produce a Hash of namespace declaration attributes" do
Namespace.namespace_attributes({""=>"http://default.com/default", "foo"=>"http://foo.com/foo"}).should ==
{"xmlns"=>"http://default.com/default", "xmlns:foo"=>"http://foo.com/foo"}
end
end

describe "non_ns_attrs_ns_bindings" do
it "should extract non-ns attributes and explicit namespace bindings" do
Namespace.non_ns_attrs_ns_bindings([{"foo"=>"http://foo.com/foo"}],
Expand Down
11 changes: 0 additions & 11 deletions spec/rsxml/visitor_spec.rb
Expand Up @@ -29,17 +29,6 @@ module Rsxml
end

describe Visitor::BuildRsxmlVisitor do
describe "partition_namespace_decls" do
it "should remove default and prefixed namespace decls from exploded attributes" do
Visitor::BuildRsxmlVisitor.new.partition_namespace_decls({"xmlns"=>"http://default.com/default",
["foo", "xmlns"]=>"http://foo.com/foo",
["bar", "foo", "http://foo.com/foo"]=>"barbar",
"baz"=>"bazbaz"}).should ==
{["bar", "foo", "http://foo.com/foo"]=>"barbar",
"baz"=>"bazbaz"}
end
end

describe "compact_qname" do
it "should compact exploded qnames" do
Visitor::BuildRsxmlVisitor.new.compact_qname(["foo", "bar", "http://bar.com/bar"]).should ==
Expand Down

0 comments on commit abeea38

Please sign in to comment.