Skip to content

Commit 17d024b

Browse files
committed
a bunch of small/superficial changes to make the ruby implementation more ruby-like
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40936
1 parent a92c7c5 commit 17d024b

22 files changed

+245
-267
lines changed

lib/html5/filters/inject_meta_charset.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def each
2121
when :EmptyTag
2222
if token[:name].downcase == "meta"
2323
# replace charset with actual encoding
24-
token[:data].each_with_index do |(name,value),index|
24+
token[:data].each_with_index do |(name, value), index|
2525
if name == 'charset'
26-
token[:data][index][1]=@encoding
26+
token[:data][index][1] = @encoding
2727
meta_found = true
2828
end
2929
end
3030

3131
# replace charset with actual encoding
3232
has_http_equiv_content_type = false
3333
content_index = -1
34-
token[:data].each_with_index do |(name,value),i|
34+
token[:data].each_with_index do |(name, value), i|
3535
if name.downcase == 'charset'
3636
token[:data][i] = ['charset', @encoding]
3737
meta_found = true
@@ -43,15 +43,14 @@ def each
4343
end
4444
end
4545

46-
if not meta_found
47-
if has_http_equiv_content_type and content_index >= 0
48-
token[:data][content_index][1] =
49-
'text/html; charset=%s' % @encoding
46+
if !meta_found
47+
if has_http_equiv_content_type && content_index >= 0
48+
token[:data][content_index][1] = 'text/html; charset=%s' % @encoding
5049
meta_found = true
5150
end
5251
end
5352

54-
elsif token[:name].downcase == "head" and not meta_found
53+
elsif token[:name].downcase == "head" && !meta_found
5554
# insert meta into empty head
5655
yield(:type => :StartTag, :name => "head", :data => token[:data])
5756
yield(:type => :EmptyTag, :name => "meta",
@@ -62,11 +61,10 @@ def each
6261
end
6362

6463
when :EndTag
65-
if token[:name].downcase == "head" and pending.any?
64+
if token[:name].downcase == "head" && pending.any?
6665
# insert meta into head (if necessary) and flush pending queue
6766
yield pending.shift
68-
yield(:type => :EmptyTag, :name => "meta",
69-
:data => [["charset", @encoding]]) if not meta_found
67+
yield(:type => :EmptyTag, :name => "meta", :data => [["charset", @encoding]]) if !meta_found
7068
yield pending.shift while pending.any?
7169
meta_found = true
7270
state = :post_head

lib/html5/filters/optionaltags.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def is_optional_start(tagname, previous, nexttok)
7575
if type == :StartTag
7676
# omit the thead and tfoot elements' end tag when they are
7777
# immediately followed by a tbody element. See is_optional_end.
78-
if previous and previous[:type] == :EndTag and \
79-
%w(tbody thead tfoot).include?(previous[:name])
78+
if previous and previous[:type] == :EndTag && %w(tbody thead tfoot).include?(previous[:name])
8079
return false
8180
end
8281

@@ -85,7 +84,7 @@ def is_optional_start(tagname, previous, nexttok)
8584
return false
8685
end
8786
end
88-
return false
87+
return false
8988
end
9089

9190
def is_optional_end(tagname, nexttok)

lib/html5/html5parser.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def self.parse(stream, options = {})
2828
def self.parseFragment(stream, options = {})
2929
container = options.delete(:container) || 'div'
3030
encoding = options.delete(:encoding)
31-
new(options).parseFragment(stream,container,encoding)
31+
new(options).parseFragment(stream, container, encoding)
3232
end
3333

3434
@@phases = %w( initial rootElement beforeHead inHead afterHead inBody inTable inCaption
@@ -44,8 +44,8 @@ def initialize(options = {})
4444

4545
@tokenizer = HTMLTokenizer
4646
@tree = TreeBuilders::REXML::TreeBuilder
47-
48-
options.each { |name, value| instance_variable_set("@#{name}", value) }
47+
48+
options.each {|name, value| instance_variable_set("@#{name}", value) }
4949

5050
@tree = @tree.new
5151

@@ -126,11 +126,11 @@ def _parse(stream, innerHTML, encoding, container = 'div')
126126
# element)
127127
def parse(stream, encoding=nil)
128128
_parse(stream, false, encoding)
129-
return @tree.getDocument
129+
@tree.getDocument
130130
end
131-
131+
132132
# Parse a HTML fragment into a well-formed tree fragment
133-
133+
134134
# container - name of the element we're setting the innerHTML property
135135
# if set to nil, default to 'div'
136136
#
@@ -142,7 +142,7 @@ def parse(stream, encoding=nil)
142142
# element)
143143
def parseFragment(stream, container='div', encoding=nil)
144144
_parse(stream, true, encoding, container)
145-
return @tree.getFragment
145+
@tree.getFragment
146146
end
147147

148148
def parseError(data = 'XXX ERROR MESSAGE NEEDED')
@@ -174,7 +174,7 @@ def normalizeToken(token)
174174
# to a dict so that [["x", "y"], ["x", "z"]] becomes {"x": "y"}
175175

176176
unless token[:data].empty?
177-
data = token[:data].reverse.map { |attr, value| [attr.tr(ASCII_UPPERCASE, ASCII_LOWERCASE), value] }
177+
data = token[:data].reverse.map {|attr, value| [attr.tr(ASCII_UPPERCASE, ASCII_LOWERCASE), value] }
178178
token[:data] = Hash[*data.flatten]
179179
end
180180

@@ -183,22 +183,22 @@ def normalizeToken(token)
183183
token[:name] = token[:name].downcase
184184
end
185185

186-
return token
186+
token
187187
end
188188

189189
@@new_modes = {
190-
'select' => :inSelect,
191-
'td' => :inCell,
192-
'th' => :inCell,
193-
'tr' => :inRow,
194-
'tbody' => :inTableBody,
195-
'thead' => :inTableBody,
196-
'tfoot' => :inTableBody,
197-
'caption' => :inCaption,
190+
'select' => :inSelect,
191+
'td' => :inCell,
192+
'th' => :inCell,
193+
'tr' => :inRow,
194+
'tbody' => :inTableBody,
195+
'thead' => :inTableBody,
196+
'tfoot' => :inTableBody,
197+
'caption' => :inCaption,
198198
'colgroup' => :inColumnGroup,
199-
'table' => :inTable,
200-
'head' => :inBody,
201-
'body' => :inBody,
199+
'table' => :inTable,
200+
'head' => :inBody,
201+
'body' => :inBody,
202202
'frameset' => :inFrameset
203203
}
204204

@@ -210,7 +210,7 @@ def resetInsertionMode
210210
@tree.openElements.reverse.each do |node|
211211
nodeName = node.name
212212

213-
if node == @tree.openElements[0]
213+
if node == @tree.openElements.first
214214
last = true
215215
unless ['td', 'th'].include?(nodeName)
216216
# XXX

lib/html5/html5parser/after_body_phase.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AfterBodyPhase < Phase
88
def processComment(data)
99
# This is needed because data is to be appended to the <html> element
1010
# here and not to whatever is currently open.
11-
@tree.insertComment(data, @tree.openElements[0])
11+
@tree.insertComment(data, @tree.openElements.first)
1212
end
1313

1414
def processCharacters(data)

lib/html5/html5parser/after_head_phase.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module HTML5
44
class AfterHeadPhase < Phase
5-
5+
66
handle_start 'html', 'body', 'frameset', %w( base link meta script style title ) => 'FromHead'
77

88
def processEOF

lib/html5/html5parser/in_body_phase.rb

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def processSpaceCharactersDropNewline(data)
6060
alias processSpaceCharacters processSpaceCharactersNonPre
6161

6262
if (data.length > 0 and data[0] == ?\n &&
63-
%w[pre textarea].include?(@tree.openElements[-1].name) && !@tree.openElements[-1].hasContent)
63+
%w[pre textarea].include?(@tree.openElements.last.name) && !@tree.openElements.last.hasContent)
6464
data = data[1..-1]
6565
end
6666

@@ -95,8 +95,7 @@ def startTagTitle(name, attributes)
9595
def startTagBody(name, attributes)
9696
@parser.parseError(_('Unexpected start tag (body).'))
9797

98-
if (@tree.openElements.length == 1 or
99-
@tree.openElements[1].name != 'body')
98+
if (@tree.openElements.length == 1 || @tree.openElements[1].name != 'body')
10099
assert @parser.innerHTML
101100
else
102101
attributes.each do |attr, value|
@@ -132,17 +131,14 @@ def startTagListItem(name, attributes)
132131
if stopName.include?(node.name)
133132
poppedNodes = (0..i).collect { @tree.openElements.pop }
134133
if i >= 1
135-
@parser.parseError(_("Missing end tag%s (%s)" % [
136-
(i>1 ? 's' : ''),
137-
poppedNodes.reverse.map {|item| item.name}.join(', ')]))
134+
@parser.parseError(_("Missing end tag%s (%s)" % [(i>1 ? 's' : ''), poppedNodes.reverse.map{|item| item.name}.join(', ')]))
138135
end
139136
break
140137
end
141138

142139
# Phrasing elements are all non special, non scoping, non
143140
# formatting elements
144-
break if ((SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name) and
145-
not ['address', 'div'].include?(node.name))
141+
break if ((SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name) && !%w[address div].include?(node.name))
146142
end
147143

148144
# Always insert an <li> element.
@@ -320,7 +316,7 @@ def startTagOther(name, attributes)
320316

321317
def endTagP(name)
322318
@tree.generateImpliedEndTags('p') if in_scope?('p')
323-
@parser.parseError(_('Unexpected end tag (p).')) unless @tree.openElements[-1].name == 'p'
319+
@parser.parseError(_('Unexpected end tag (p).')) unless @tree.openElements.last.name == 'p'
324320
if in_scope?('p')
325321
@tree.openElements.pop while in_scope?('p')
326322
else
@@ -338,7 +334,7 @@ def endTagBody(name)
338334
@parser.parseError
339335
return
340336
end
341-
unless @tree.openElements[-1].name == 'body'
337+
unless @tree.openElements.last.name == 'body'
342338
@parser.parseError(_("Unexpected end tag (body). Missing end tag (#{@tree.openElements[-1].name})."))
343339
end
344340
@parser.phase = @parser.phases[:afterBody]
@@ -355,7 +351,7 @@ def endTagBlock(name)
355351

356352
@tree.generateImpliedEndTags if in_scope?(name)
357353

358-
unless @tree.openElements[-1].name == name
354+
unless @tree.openElements.last.name == name
359355
@parser.parseError(_("End tag (#{name}) seen too early. Expected other end tag."))
360356
end
361357

@@ -368,7 +364,7 @@ def endTagForm(name)
368364
if in_scope?(name)
369365
@tree.generateImpliedEndTags
370366
end
371-
if @tree.openElements[-1].name != name
367+
if @tree.openElements.last.name != name
372368
@parser.parseError(_("End tag (form) seen too early. Ignored."))
373369
else
374370
@tree.openElements.pop
@@ -380,9 +376,8 @@ def endTagListItem(name)
380376
# AT Could merge this with the Block case
381377
@tree.generateImpliedEndTags(name) if in_scope?(name)
382378

383-
unless @tree.openElements[-1].name == name
384-
@parser.parseError(_("End tag (#{name}) seen too early. " +
385-
'Expected other end tag.'))
379+
unless @tree.openElements.last.name == name
380+
@parser.parseError(_("End tag (#{name}) seen too early. " + 'Expected other end tag.'))
386381
end
387382

388383
remove_open_elements_until(name) if in_scope?(name)
@@ -396,13 +391,13 @@ def endTagHeading(name)
396391
end
397392
end
398393

399-
unless @tree.openElements[-1].name == name
394+
unless @tree.openElements.last.name == name
400395
@parser.parseError(_("Unexpected end tag (#{name}). Expected other end tag."))
401396
end
402397

403398
HEADING_ELEMENTS.each do |element|
404399
if in_scope?(element)
405-
remove_open_elements_until { |element| HEADING_ELEMENTS.include?(element.name) }
400+
remove_open_elements_until {|element| HEADING_ELEMENTS.include?(element.name)}
406401
break
407402
end
408403
end
@@ -415,7 +410,7 @@ def endTagFormatting(name)
415410
while true
416411
# Step 1 paragraph 1
417412
afeElement = @tree.elementInActiveFormattingElements(name)
418-
if not afeElement or (@tree.openElements.include?(afeElement) and not in_scope?(afeElement.name))
413+
if !afeElement or (@tree.openElements.include?(afeElement) && !in_scope?(afeElement.name))
419414
@parser.parseError(_("End tag (#{name}) violates step 1, paragraph 1 of the adoption agency algorithm."))
420415
return
421416
# Step 1 paragraph 2
@@ -426,7 +421,7 @@ def endTagFormatting(name)
426421
end
427422

428423
# Step 1 paragraph 3
429-
if afeElement != @tree.openElements[-1]
424+
if afeElement != @tree.openElements.last
430425
@parser.parseError(_("End tag (#{name}) violates step 1, paragraph 3 of the adoption agency algorithm."))
431426
end
432427

@@ -443,7 +438,7 @@ def endTagFormatting(name)
443438

444439
# Step 3
445440
if furthestBlock.nil?
446-
element = remove_open_elements_until { |element| element == afeElement }
441+
element = remove_open_elements_until {|element| element == afeElement }
447442
@tree.activeFormattingElements.delete(element)
448443
return
449444
end
@@ -523,13 +518,13 @@ def endTagFormatting(name)
523518
def endTagButtonMarqueeObject(name)
524519
@tree.generateImpliedEndTags if in_scope?(name)
525520

526-
unless @tree.openElements[-1].name == name
521+
unless @tree.openElements.last.name == name
527522
@parser.parseError(_("Unexpected end tag (#{name}). Expected other end tag first."))
528523
end
529524

530525
if in_scope?(name)
531526
remove_open_elements_until(name)
532-
527+
533528
@tree.clearActiveFormattingElements
534529
end
535530
end
@@ -552,7 +547,7 @@ def endTagNone(name)
552547
end
553548

554549
def endTagCdataTextAreaXmp(name)
555-
if @tree.openElements[-1].name == name
550+
if @tree.openElements.last.name == name
556551
@tree.openElements.pop
557552
else
558553
@parser.parseError(_("Unexpected end tag (#{name}). Ignored."))
@@ -573,11 +568,11 @@ def endTagOther(name)
573568
if node.name == name
574569
@tree.generateImpliedEndTags
575570

576-
unless @tree.openElements[-1].name == name
571+
unless @tree.openElements.last.name == name
577572
@parser.parseError(_("Unexpected end tag (#{name})."))
578573
end
579574

580-
remove_open_elements_until { |element| element == node }
575+
remove_open_elements_until {|element| element == node }
581576

582577
break
583578
else
@@ -593,7 +588,7 @@ def endTagOther(name)
593588

594589
def addFormattingElement(name, attributes)
595590
@tree.insertElement(name, attributes)
596-
@tree.activeFormattingElements.push(@tree.openElements[-1])
591+
@tree.activeFormattingElements.push(@tree.openElements.last)
597592
end
598593

599594
end

lib/html5/html5parser/in_caption_phase.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class InCaptionPhase < Phase
1010
handle_end 'caption', 'table', %w( body col colgroup html tbody td tfoot th thead tr ) => 'Ignore'
1111

1212
def ignoreEndTagCaption
13-
not in_scope?('caption', true)
13+
!in_scope?('caption', true)
1414
end
1515

1616
def processCharacters(data)

lib/html5/html5parser/in_cell_phase.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def startTagOther(name, attributes)
3232
def endTagTableCell(name)
3333
if in_scope?(name, true)
3434
@tree.generateImpliedEndTags(name)
35-
if @tree.openElements[-1].name != name
35+
if @tree.openElements.last.name != name
3636
@parser.parseError("Got table cell end tag (#{name}) while required end tags are missing.")
3737

3838
remove_open_elements_until(name)

0 commit comments

Comments
 (0)