lib/csl/node.rb in csl-1.0.0.pre21 vs lib/csl/node.rb in csl-1.0.0.pre22
- old
+ new
@@ -26,21 +26,21 @@
def default_attributes
@default_attributes ||= {}
end
- def hide_default_attributes?
- @hide_default_attributes ||= true
- end
+ def hide_default_attributes?
+ !@show_default_attributes
+ end
- def hide_default_attributes!
- @hide_default_attributes = true
- end
+ def hide_default_attributes!
+ @show_default_attributes = false
+ end
- def show_default_attributes!
- @hide_default_attributes = false
- end
+ def show_default_attributes!
+ @show_default_attributes = true
+ end
def constantize(name)
pattern = /:#{name.to_s.tr('-', '')}$/i
klass = types.detect { |t| t.matches?(pattern) }
@@ -244,15 +244,15 @@
end
copy
end
- # @return [Boolean] whether or not the node has default attributes
- def has_default_attributes?
- !default_attributes.empty?
- end
- alias has_defaults? has_default_attributes?
+ # @return [Boolean] whether or not the node has default attributes
+ def has_default_attributes?
+ !default_attributes.empty?
+ end
+ alias has_defaults? has_default_attributes?
# Iterates through the Node's attributes
def each
if block_given?
attributes.each_pair(&Proc.new)
@@ -261,34 +261,34 @@
to_enum
end
end
alias each_pair each
- # @param name [#to_sym] the name of the attribute
- # @return [Boolean] whether or not key is set to the default value
- def default_attribute?(name)
- defaults = self.class.default_attributes
- name, value = name.to_sym, attributes.fetch(name)
-
- return false unless !value.nil? || defaults.key?(name)
- defaults[name] == value
- end
+ # @param name [#to_sym] the name of the attribute
+ # @return [Boolean] whether or not key is set to the default value
+ def default_attribute?(name)
+ defaults = self.class.default_attributes
+ name, value = name.to_sym, attributes.fetch(name)
- # @return [Hash] the attributes currently set to their default values
- def default_attributes
- attributes.to_hash.select do |name, _|
- default_attribute?(name)
- end
- end
+ return false unless !value.nil? || defaults.key?(name)
+ defaults[name] == value
+ end
- # @return [Hash] the attributes currently not set to their default values
- def custom_attributes
- attributes.to_hash.reject do |name, _|
- default_attribute?(name)
- end
- end
+ # @return [Hash] the attributes currently set to their default values
+ def default_attributes
+ attributes.to_hash.select do |name, _|
+ default_attribute?(name)
+ end
+ end
+ # @return [Hash] the attributes currently not set to their default values
+ def custom_attributes
+ attributes.to_hash.reject do |name, _|
+ default_attribute?(name)
+ end
+ end
+
# Returns true if the node contains an attribute with the passed-in name;
# false otherwise.
def attribute?(name)
attributes.fetch(name, false)
end
@@ -450,13 +450,15 @@
end
private
def attribute_assignments
- a = self.class.hide_default_attributes? ? custom_attributes : attributes
- a.map { |name, value|
- value.nil? ? nil : [name, value.to_s.inspect].join('=')
+ attrs = self.class.hide_default_attributes? ?
+ custom_attributes : attributes.to_hash
+
+ attrs.map { |name, value|
+ value.nil? ? nil : [name, CSL.encode_xml_attr(value.to_s)].join('=')
}.compact
end
def match_conditions_for(name, conditions)
case name
@@ -492,11 +494,11 @@
end
attr_accessor :text
def to_s
- text.to_s.strip
+ CSL.encode_xml_text text.to_s.strip
end
# TextNodes quack like a string.
# def_delegators :to_s, *String.instance_methods(false).reject do |m|
# m.to_s =~ /^\W|!$|(?:^(?:hash|eql?|to_s|length|size|inspect)$)/
@@ -528,10 +530,10 @@
def empty?
text.nil? || text.empty?
end
def tags
- ["<#{attribute_assignments.unshift(nodename).join(' ')}>#{text}</#{nodename}>"]
+ ["<#{attribute_assignments.unshift(nodename).join(' ')}>#{to_s}</#{nodename}>"]
end
def inspect
"#<#{[self.class.name, text.inspect, *attribute_assignments].join(' ')}>"
end
\ No newline at end of file