Added NodeNameSet class

This class can be used to more easily create a Set containing both
lowercase and uppercase element names.
This commit is contained in:
Yorick Peterse 2015-04-22 00:54:29 +02:00
parent 8135074a62
commit 4b21a2fadc
4 changed files with 30 additions and 4 deletions

View File

@ -7,6 +7,7 @@ require_relative 'oga/version'
require_relative 'oga/oga' require_relative 'oga/oga'
require_relative 'oga/lru' require_relative 'oga/lru'
require_relative 'oga/entity_decoder' require_relative 'oga/entity_decoder'
require_relative 'oga/node_name_set'
# Load these first so that the native extensions don't have to define the # Load these first so that the native extensions don't have to define the
# Oga::XML namespace. # Oga::XML namespace.

16
lib/oga/node_name_set.rb Normal file
View File

@ -0,0 +1,16 @@
module Oga
##
# Class for storing (HTML) element names in a set and automatically adding
# their uppercase equivalents.
#
class NodeNameSet < Set
##
# @param [Array] values
#
def initialize(values = [])
values = values + values.map(&:upcase)
super(values)
end
end # NodeNameSet
end # Oga

View File

@ -4,9 +4,9 @@ module Oga
# Names of the HTML void elements that should be handled when HTML lexing # Names of the HTML void elements that should be handled when HTML lexing
# is enabled. # is enabled.
# #
# @return [Set] # @return [Oga::NodeNameSet]
# #
HTML_VOID_ELEMENTS = Set.new([ HTML_VOID_ELEMENTS = NodeNameSet.new([
'area', 'area',
'base', 'base',
'br', 'br',
@ -24,7 +24,5 @@ module Oga
'track', 'track',
'wbr' 'wbr'
]) ])
HTML_VOID_ELEMENTS.merge(HTML_VOID_ELEMENTS.map { |name| name.upcase })
end # XML end # XML
end # Oga end # Oga

View File

@ -0,0 +1,11 @@
require 'spec_helper'
describe Oga::NodeNameSet do
describe '#initialize' do
it 'adds uppercase equivalents of the input strings' do
set = described_class.new(%w{foo bar})
set.to_a.should == %w{foo bar FOO BAR}
end
end
end