Fix entity encoding of attribute values
This ensures that single and double quotes are also encoded, previously they would be left as is. Fixes #113
This commit is contained in:
1 parent
e0837fd44e
commit
074b53c18c
4 files changed
+60
-4
No files matched your search
@@ -101,7 +101,11 @@ module Oga
|
||||
full_name = name
|
||||
end
|
||||
|
||||
enc_value = value ? Entities.encode(value) : nil
|
||||
if value
|
||||
enc_value = Entities.encode_attribute(value)
|
||||
else
|
||||
enc_value = nil
|
||||
end
|
||||
|
||||
%Q(#{full_name}="#{enc_value}")
|
||||
end
|
||||
|
||||
@@ -32,6 +32,20 @@ module Oga
|
||||
'<' => '<',
|
||||
}
|
||||
|
||||
##
|
||||
# Hash containing characters and the corresponding XML entities to use
|
||||
# when encoding XML/HTML attribute values.
|
||||
#
|
||||
# @return [Hash]
|
||||
#
|
||||
ENCODE_ATTRIBUTE_MAPPING = {
|
||||
'&' => '&',
|
||||
'>' => '>',
|
||||
'<' => '<',
|
||||
"'" => ''',
|
||||
'"' => '"'
|
||||
}
|
||||
|
||||
##
|
||||
# @return [String]
|
||||
#
|
||||
@@ -56,6 +70,12 @@ module Oga
|
||||
#
|
||||
ENCODE_REGEXP = Regexp.new(ENCODE_MAPPING.keys.join('|'))
|
||||
|
||||
##
|
||||
# @return [Regexp]
|
||||
#
|
||||
ENCODE_ATTRIBUTE_REGEXP =
|
||||
Regexp.new(ENCODE_ATTRIBUTE_MAPPING.keys.join('|'))
|
||||
|
||||
##
|
||||
# Decodes XML entities.
|
||||
#
|
||||
@@ -87,6 +107,16 @@ module Oga
|
||||
def self.encode(input, mapping = ENCODE_MAPPING)
|
||||
input.gsub(ENCODE_REGEXP, mapping)
|
||||
end
|
||||
|
||||
##
|
||||
# Encodes special characters in an XML attribute value.
|
||||
#
|
||||
# @param [String] input
|
||||
# @return [String]
|
||||
#
|
||||
def self.encode_attribute(input)
|
||||
input.gsub(ENCODE_ATTRIBUTE_REGEXP, ENCODE_ATTRIBUTE_MAPPING)
|
||||
end
|
||||
end # Entities
|
||||
end # XML
|
||||
end # Oga
|
||||
Reference in new issue
Block a user