Class: Gammo::FragmentParser
- Defined in:
- lib/gammo/fragment_parser.rb
Overview
Class for parsing a fragment of an HTML input and building an HTML tree.
Constant Summary
Constants inherited from Parser
Constants included from Parser::Constants
Parser::Constants::SPECIAL_ELEMENTS
Constants included from Parser::Foreign
Parser::Foreign::BREAKOUT, Parser::Foreign::MATH_ML_ATTRIBUTE_ADJUSTMENTS, Parser::Foreign::SVG_ATTRIBUTE_ADJUSTMENTS, Parser::Foreign::SVG_TAG_NAME_ADJUSTMENTS
Instance Attribute Summary
Attributes inherited from Parser
#context, #document, #form, #frameset_ok, #head, #scripting
Instance Method Summary collapse
-
#initialize(input, context:, **options) ⇒ Object
constructor
Constructs a parser instance for fragment parsing algorithm.
-
#parse ⇒ Array<Gammo::Node>
Parses a fragment of the current input and builds HTML tree from it.
-
#validate_context(context) ⇒ Object
Validates given context.
Methods included from Parser::Foreign
#adjust_attribute_names, #adjust_foreign_attributes, #html_integration_point?, #in_foreign_content?, #math_ml_text_integration_point?, #parse_foreign_content
Constructor Details
#initialize(input, context:, **options) ⇒ Object
Constructs a parser instance for fragment parsing algorithm.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gammo/fragment_parser.rb', line 12 def initialize(input, context:, **) validate_context(context) super(input, context: context, **) @root = Node::Element.new(tag: Tags::Html, data: Tags::Html.to_s) @tokenizer = Tokenizer.new(input, context: !context.namespace && context.tag.to_s) @open_elements = NodeStack.new([@root]) document.append_child(@root) template_stack << InTemplate if context.tag == Tags::Template reset_insertion_mode while context if context.instance_of?(Node::Element) && context.tag == Tags::Form @form = context break end context = context.parent end end |
Instance Method Details
#parse ⇒ Array<Gammo::Node>
Parses a fragment of the current input and builds HTML tree from it.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gammo/fragment_parser.rb', line 33 def parse super parent = context ? @root : document child = parent.first_child collection = [] while child node = child.next_sibling parent.remove_child(child) collection << child child = node end collection end |
#validate_context(context) ⇒ Object
Validates given context. Raises ParseError if context is not Node.
58 59 60 61 62 63 |
# File 'lib/gammo/fragment_parser.rb', line 58 def validate_context(context) fail ParseError, 'given non-element node in "context"' unless context.instance_of?(Node::Element) unless context.tag == Tags.lookup(context.data) fail ParseError, "inconsistent context node, tag = #{context.tag}, data = #{Tags.lookup(context.data)}" end end |