@@ -472,40 +472,42 @@ def check_bom(str)
472472 def scanning_meta ( str )
473473 require 'strscan'
474474 ss = StringScanner . new ( str )
475- if ss . scan_until ( /<meta[\t \n \f \r ]*/ )
476- attrs = { } # attribute_list
477- got_pragma = false
478- need_pragma = nil
479- charset = nil
480-
481- # step: Attributes
482- while attr = get_attribute ( ss )
483- name , value = *attr
484- next if attrs [ name ]
485- attrs [ name ] = true
486- case name
487- when 'http-equiv'
488- got_pragma = true if value == 'content-type'
489- when 'content'
490- encoding = extracting_encodings_from_meta_elements ( value )
491- unless charset
492- charset = encoding
475+ catch ( :invalid_meta_tag ) do
476+ if ss . scan_until ( /<meta[\t \n \f \r ]*/ )
477+ attrs = { } # attribute_list
478+ got_pragma = false
479+ need_pragma = nil
480+ charset = nil
481+
482+ # step: Attributes
483+ while attr = get_attribute ( ss )
484+ name , value = *attr
485+ next if attrs [ name ]
486+ attrs [ name ] = true
487+ case name
488+ when 'http-equiv'
489+ got_pragma = true if value == 'content-type'
490+ when 'content'
491+ encoding = extracting_encodings_from_meta_elements ( value )
492+ unless charset
493+ charset = encoding
494+ end
495+ need_pragma = true
496+ when 'charset'
497+ need_pragma = false
498+ charset = value
493499 end
494- need_pragma = true
495- when 'charset'
496- need_pragma = false
497- charset = value
498500 end
499- end
500501
501- # step: Processing
502- return if need_pragma . nil?
503- return if need_pragma && !got_pragma
502+ # step: Processing
503+ return if need_pragma . nil?
504+ return if need_pragma && !got_pragma
504505
505- charset = Encoding . find ( charset ) rescue nil
506- return unless charset
507- charset = Encoding ::UTF_8 if charset == Encoding ::UTF_16
508- return charset # tentative
506+ charset = Encoding . find ( charset ) rescue nil
507+ return unless charset
508+ charset = Encoding ::UTF_8 if charset == Encoding ::UTF_16
509+ return charset # tentative
510+ end
509511 end
510512 nil
511513 end
@@ -518,7 +520,7 @@ def get_attribute(ss)
518520 end
519521 name = ss . scan ( /[^=\t \n \f \r \/ >]*/ )
520522 name . downcase!
521- raise if name . empty?
523+ throw :invalid_meta_tag if name . empty?
522524 ss . skip ( /[\t \n \f \r ]*/ )
523525 if ss . getch != '='
524526 value = ''
@@ -528,18 +530,18 @@ def get_attribute(ss)
528530 case ss . peek ( 1 )
529531 when '"'
530532 ss . getch
531- value = ss . scan ( /[^"]+ / )
533+ value = ss . scan ( /[^"]* / )
532534 value . downcase!
533535 ss . getch
534536 when "'"
535537 ss . getch
536- value = ss . scan ( /[^']+ / )
538+ value = ss . scan ( /[^']* / )
537539 value . downcase!
538540 ss . getch
539541 when '>'
540542 value = ''
541543 else
542- value = ss . scan ( /[^\t \n \f \r >]+/ )
544+ throw :invalid_meta_tag unless value = ss . scan ( /[^\t \n \f \r >]+/ )
543545 value . downcase!
544546 end
545547 [ name , value ]
0 commit comments