class Meteor::Ml::Xhtml4::ParserImpl

XHTML4 parser (XHTML4パーサ)

Constants

BR_1
BR_2
BR_3
CHECKED
CHECKED_M
CHECKED_M1
CHECKED_R
CHECKED_U
CONTENT
CONTENT_TYPE
DISABLED
DISABLED_M
DISABLED_M1
DISABLED_R
DISABLED_U
DISABLE_ELEMENT

DISABLE_ELEMENT = “input|textarea|select|optgroup”

FALSE
HTTP_EQUIV
INPUT
KAIGYO_CODE

KAIGYO_CODE = “r?n|r”.freeze

META
META_S
MULTIPLE
MULTIPLE_M
MULTIPLE_M1
MULTIPLE_R
MULTIPLE_U
NBSP_2
NBSP_3
OPTION
PATTERN_ESCAPE
PATTERN_ESCAPE_CONTENT
PATTERN_UNESCAPE
RADIO
READONLY
READONLY_M
READONLY_M1
READONLY_R
READONLY_TYPE

READONLY_TYPE = “text|password”

READONLY_U
SELECT
SELECTED
SELECTED_M

@@pattern_option = Regexp.new(OPTION) @@pattern_selected = Regexp.new(SELECTED) @@pattern_input = Regexp.new(INPUT) @@pattern_checked = Regexp.new(CHECKED) @@pattern_radio = Regexp.new(RADIO) @@pattern_disable_element = Regexp.new(DISABLE_ELEMENT) @@pattern_disabled = Regexp.new(DISABLED) @@pattern_readonly_type = Regexp.new(READONLY_TYPE) @@pattern_textarea = Regexp.new(TEXTAREA) @@pattern_readonly = Regexp.new(READONLY) @@pattern_select = Regexp.new(SELECT) @@pattern_multiple = Regexp.new(MULTIPLE)

SELECTED_M1
SELECTED_R
SELECTED_U
TABLE_FOR_ESCAPE_

@@pattern_@@match_tag = Regexp.new(@@match_tag) @@pattern_@@match_tag2 = Regexp.new(@@match_tag_2)

TABLE_FOR_ESCAPE_CONTENT_
TEXTAREA
TRUE
TYPE_L
TYPE_U

Public Class Methods

new(*args) click to toggle source

initializer (イニシャライザ) @overload initialize @overload initialize(ps)

@param [Meteor::Parser] ps parser (パーサ)
Calls superclass method Meteor::Core::Kernel::new
# File lib/meteor.rb, line 4884
def initialize(*args)
  super()
  @doc_type = Parser::XHTML4
  case args.length
    when ZERO
      #initialize_0
    when ONE
      initialize_1(args[0])
    else
      raise ArgumentError
  end
end

Public Instance Methods

content_type() click to toggle source

get content type (コンテントタイプを取得する) @return [String] content type (コンテントタイプ)

# File lib/meteor.rb, line 4942
def content_type
  @root.content_type
end
parse() click to toggle source

parse document (ドキュメントを解析する)

# File lib/meteor.rb, line 4921
def parse
  analyze_ml
end

Private Instance Methods

analyze_content_type() click to toggle source

analyze document , set content type (ドキュメントをパースし、コンテントタイプをセットする)

# File lib/meteor.rb, line 4949
def analyze_content_type
  @error_check = false

  element_3(META_S, HTTP_EQUIV, CONTENT_TYPE)

  if !@elm_
    element_3(META, HTTP_EQUIV, CONTENT_TYPE)
  end

  @error_check = true

  if @elm_
    @root.content_type = @elm_.attr(CONTENT)
  else
    @root.content_type = EMPTY
  end
end
analyze_kaigyo_code() click to toggle source

analyze document , set newline (ドキュメントをパースし、改行コードをセットする)

# File lib/meteor.rb, line 4972
def analyze_kaigyo_code
  #改行コード取得

  for a in KAIGYO_CODE
    if @root.document.include?(a)
      @root.kaigyo_code = a
    end
  end
end
analyze_ml() click to toggle source

analyze document (ドキュメントをパースする)

# File lib/meteor.rb, line 4928
def analyze_ml
  #content-typeの取得
  analyze_content_type
  #改行コードの取得
  analyze_kaigyo_code
  @res = nil
end
edit_attrs_(elm, attr_name, attr_value) click to toggle source
Calls superclass method Meteor::Core::Kernel#edit_attrs_
# File lib/meteor.rb, line 4984
def edit_attrs_(elm, attr_name, attr_value)

  if is_match(SELECTED, attr_name) && is_match(OPTION, elm.name)
    edit_attrs_5(elm, attr_value, @@pattern_selected_m, @@pattern_selected_r, SELECTED_U)
  elsif is_match(MULTIPLE, attr_name) && is_match(SELECT, elm.name)
    edit_attrs_5(elm, attr_value, @@pattern_multiple_m, @@pattern_multiple_r, MULTIPLE_U)
  elsif is_match(DISABLED, attr_name) && is_match(DISABLE_ELEMENT, elm.name)
    edit_attrs_5(elm, attr_value, @@pattern_disabled_m, @@pattern_disabled_r, DISABLED_U)
  elsif is_match(CHECKED, attr_name) && is_match(INPUT, elm.name) && is_match(RADIO, get_type(elm))
    edit_attrs_5(elm, attr_value, @@pattern_checked_m, @@pattern_checked_r, CHECKED_U)
  elsif is_match(READONLY, attr_name) && (is_match(TEXTAREA, elm.name) || (is_match(INPUT, elm.name) && is_match(READONLY_TYPE, get_type(elm))))
    edit_attrs_5(elm, attr_value, @@pattern_readonly_m, @@pattern_readonly_r, READONLY_U)
  else
    super(elm, attr_name, attr_value)
  end

end
edit_attrs_5(elm, attr_value, match_p, replace_regex, replace_update) click to toggle source
# File lib/meteor.rb, line 5004
def edit_attrs_5(elm, attr_value, match_p, replace_regex, replace_update)

  #attr_value = escape(attr_value)

  if true.equal?(attr_value) || is_match(TRUE, attr_value)

    @res = match_p.match(elm.attributes)

    if !@res
      #属性文字列の最後に新規の属性を追加する
      if elm.attributes != EMPTY
        elm.attributes = '' << SPACE << elm.attributes.strip
        #else
      end
      elm.attributes << SPACE << replace_update
    else
      #属性の置換
      elm.attributes.gsub!(replace_regex, replace_update)
    end
  elsif false.equal?(attr_value) || is_match(FALSE, attr_value)
    #attr_name属性が存在するなら削除
    #属性の置換
    elm.attributes.gsub!(replace_regex, EMPTY)
  end

end
escape(content) click to toggle source
# File lib/meteor.rb, line 5122
def escape(content)
  #特殊文字の置換
  content = content.gsub(@@pattern_escape, TABLE_FOR_ESCAPE_)

  content
end
escape_content(content, elm) click to toggle source
# File lib/meteor.rb, line 5129
def escape_content(content, elm)
  content.gsub!(@@pattern_escape_content, TABLE_FOR_ESCAPE_CONTENT_)

  content
end
get_attr_map(elm) click to toggle source

get attribute map (属性マップを取得する) @param [Meteor::Element] elm element (要素) @return [Meteor::AttributeMap] attribute map (属性マップ)

# File lib/meteor.rb, line 5105
def get_attr_map(elm)
  attrs = Meteor::AttributeMap.new

  elm.attributes.scan(@@pattern_get_attrs_map) do |a, b|
    if is_match(@@attr_logic, a) && a==b
      attrs.store(a, TRUE)
    else
      attrs.store(a, unescape(b))
    end
  end
  attrs.recordable = true

  attrs
end
get_attr_value_(elm, attr_name) click to toggle source
Calls superclass method Meteor::Core::Kernel#get_attr_value_
# File lib/meteor.rb, line 5033
def get_attr_value_(elm, attr_name)
  if is_match(SELECTED, attr_name) && is_match(OPTION, elm.name)
    get_attr_value_r(elm, attr_name, @@pattern_selected_m1)
  elsif is_match(MULTIPLE, attr_name) && is_match(SELECT, elm.name)
    get_attr_value_r(elm, attr_name, @@pattern_multiple_m1)
  elsif is_match(DISABLED, attr_name) && is_match(DISABLE_ELEMENT, elm.name)
    get_attr_value_r(elm, attr_name, @@pattern_disabled_m1)
  elsif is_match(CHECKED, attr_name) && is_match(INPUT, elm.name) && is_match(RADIO, get_type(elm))
    get_attr_value_r(elm, attr_name, @@pattern_checked_m1)
  elsif is_match(READONLY, attr_name) && (is_match(TEXTAREA, elm.name) || (is_match(INPUT, elm.name) && is_match(READONLY_TYPE, get_type(elm))))
    get_attr_value_r(elm, attr_name, @@pattern_readonly_m1)
  else
    super(elm, attr_name)
  end
end
get_attr_value_r(elm, attr_name, match_p) click to toggle source
# File lib/meteor.rb, line 5063
def get_attr_value_r(elm, attr_name, match_p)

  @res = match_p.match(elm.attributes)

  if @res
    if @res[1]
      if attr_name == @res[1]
        TRUE
      else
        @res[1]
      end
    elsif @res[2]
      if attr_name == @res[2]
        TRUE
      else
        @res[2]
      end
    elsif @res[3]
      if attr_name == @res[3]
        TRUE
      else
        @res[3]
      end
    elsif @res[4]
      if attr_name == @res[4]
        TRUE
      else
        @res[4]
      end
    end
  else
    FALSE
  end
end
get_type(elm) click to toggle source
# File lib/meteor.rb, line 5051
def get_type(elm)
  if !elm.type_value
    elm.type_value = get_attr_value(elm, TYPE_L)
    if !elm.type_value
      elm.type_value = get_attr_value(elm, TYPE_U)
    end
  end
  elm.type_value
end
initialize_1(ps) click to toggle source

initializer (イニシャライザ) @param [Meteor::Parser] ps parser (パーサ)

# File lib/meteor.rb, line 4909
def initialize_1(ps)
  @root.document = String.new(ps.document)
  self.document_hook = String.new(ps.document_hook)
  @root.content_type = String.new(ps.root_element.content_type)
  @root.kaigyo_code = ps.root_element.kaigyo_code
end
unescape(content) click to toggle source
# File lib/meteor.rb, line 5138
def unescape(content)
  #特殊文字の置換
  #「<」<-「&lt;」
  #「>」<-「&gt;」
  #「"」<-「&quotl」
  #「 」<-「&nbsp;」
  #「&」<-「&amp;」
  content.gsub(@@pattern_unescape) do
    case $1
      when AND_3
        AND_1
      when QO_3
        DOUBLE_QUATATION
      when AP_3
        AP_1
      when GT_3
        GT_1
      when LT_3
        LT_1
      when NBSP_3
        SPACE
    end
  end

  content
end
unescape_content(content, elm) click to toggle source
# File lib/meteor.rb, line 5167
def unescape_content(content, elm)
  content_ = unescape(content)

  if (elm.cx || !is_match(@@match_tag_2, elm.name)) && content.include?(BR_2)
      #「<br>」->「¥r?¥n」
      content_.gsub!(@@pattern_br_2, @root.kaigyo_code)
  end

  content_
end