dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / parser / html_parser_7.lucb
#==============================================================================================
#
# html_parser_7 - HTML::HTMLParser: the "in body" insertion mode, part 3, and the token
# adjustments for foreign content
#
# DESCRIPTION:
# Port of LibWeb/HTML/Parser/HTMLParser.cpp (lines 3076-3441): the last entries of
# handle_in_body (select, option, optgroup, ruby, math, svg, the ignored table start tags,
# any other start tag, any other end tag) and the MathML, SVG and foreign attribute and
# tag name adjustments. The C++ `static` adjustment tables are built where they are used:
# their FlyStrings are interned at run time.
#
#==============================================================================================
# mark: The in body insertion mode, part 3 ======================================================
## handle_in_body continued (HTMLParser.cpp:3076-3282): the entries after "noembed".
func html_parser_handle_in_body_continued_2(this: HtmlParser*, token: html_syntax.HtmlToken*):
# -> A start tag whose tag name is "select"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_select):
# If the parser was created as part of the HTML fragment parsing algorithm (fragment case)
# and the context element passed to that algorithm is a select element:
if this.m_parsing_fragment and html_parser_local_name_is(this.m_context_element else trap("VERIFY: context element"), html_tag_names_select):
# 1. Parse error.
# 2. Ignore the token.
html_log_parse_error()
# Otherwise, if the stack of open elements has a select element in scope:
elif html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_select):
# 1. Parse error.
# 2. Ignore the token.
html_log_parse_error()
# 3. Pop elements from the stack of open elements until a select element has been popped from the stack.
html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_select)
# Otherwise:
else:
# 1. Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# 2. Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
# 3. Set the frameset-ok flag to "not ok".
this.m_frameset_ok = false
return
# -> A start tag whose tag name is "option"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_option):
# If the stack of open elements has a select element in scope:
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_select):
# 1. Generate implied end tags except for optgroup elements.
html_parser_generate_implied_end_tags(this, html_tag_names_optgroup)
# 2. If the stack of open elements has an option element in scope, then this is a parse error.
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_option):
html_log_parse_error()
# Otherwise, if the current node is an option element, then pop the current node from the stack of open elements.
elif html_parser_local_name_is(html_parser_current_element(this), html_tag_names_option):
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
# Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
return
# -> A start tag whose tag name is "optgroup"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_optgroup):
# If the stack of open elements has a select element in scope:
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_select):
# 1. Generate implied end tags.
html_parser_generate_implied_end_tags(this)
# 2. If the stack of open elements has an option element in scope or has an optgroup element in scope, then
# this is a parse error.
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_option) or html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_optgroup):
html_log_parse_error()
# Otherwise, if the current node is an option element, then pop the current node from the stack of open elements.
elif html_parser_local_name_is(html_parser_current_element(this), html_tag_names_option):
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
# Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
return
# -> A start tag whose tag name is one of: "rb", "rtc"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_rb, html_tag_names_rtc]):
# If the stack of open elements has a ruby element in scope, then generate implied end tags. If the current node is not now a ruby element, this is a parse error.
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_ruby):
html_parser_generate_implied_end_tags(this)
if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_ruby):
html_log_parse_error()
# Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
return
# -> A start tag whose tag name is one of: "rp", "rt"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_rp, html_tag_names_rt]):
# If the stack of open elements has a ruby element in scope, then generate implied end tags, except for rtc elements. If the current node is not now a rtc element or a ruby element, this is a parse error.
if html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_ruby):
html_parser_generate_implied_end_tags(this, html_tag_names_rtc)
if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_rtc) and not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_ruby):
html_log_parse_error()
# Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
return
# -> A start tag whose tag name is "math"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_math):
# Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# Adjust MathML attributes for the token. (This fixes the case of MathML attributes that are not all lowercase.)
html_parser_adjust_mathml_attributes(this, token)
# Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink.)
html_parser_adjust_foreign_attributes(token)
# Insert a foreign element for the token, with MathML namespace and false.
_ = html_parser_insert_foreign_element(this, token, namespace_math_ml, HtmlParserOnlyAddToElementStack.no)
# If the token has its self-closing flag set, pop the current node off the stack of open elements and acknowledge the token's self-closing flag.
if html_syntax.html_token_is_self_closing(token):
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
html_syntax.html_token_acknowledge_self_closing_flag_if_set(token)
return
# -> A start tag whose tag name is "svg"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_svg):
# Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# Adjust SVG attributes for the token. (This fixes the case of SVG attributes that are not all lowercase.)
html_parser_adjust_svg_attributes(this, token)
# Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink in SVG.)
html_parser_adjust_foreign_attributes(token)
# Insert a foreign element for the token, with SVG namespace and false.
_ = html_parser_insert_foreign_element(this, token, namespace_svg, HtmlParserOnlyAddToElementStack.no)
# If the token has its self-closing flag set, pop the current node off the stack of open elements and acknowledge the token's self-closing flag.
if html_syntax.html_token_is_self_closing(token):
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
html_syntax.html_token_acknowledge_self_closing_flag_if_set(token)
return
# -> A start tag whose tag name is one of: "caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"
if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_caption, html_tag_names_col, html_tag_names_colgroup, html_tag_names_frame, html_tag_names_head, html_tag_names_tbody, html_tag_names_td, html_tag_names_tfoot, html_tag_names_th, html_tag_names_thead, html_tag_names_tr]):
# Parse error. Ignore the token.
html_log_parse_error()
return
# -> Any other start tag
if html_syntax.html_token_is_start_tag(token):
# Reconstruct the active formatting elements, if any.
html_parser_reconstruct_the_active_formatting_elements(this)
# Insert an HTML element for the token.
_ = html_parser_insert_html_element(this, token)
return
# -> Any other end tag
if html_syntax.html_token_is_end_tag(token):
html_parser_handle_in_body_any_other_end_tag(this, token)
return
## handle_in_body's `AnyOtherEndTag:` label: the "any other end tag" entry (HTMLParser.cpp:3247-3281).
func html_parser_handle_in_body_any_other_end_tag(this: HtmlParser*, token: html_syntax.HtmlToken*):
let elements = &this.m_stack_of_open_elements.m_elements
# 1. Initialize node to be the current node (the bottommost node of the stack).
# 2. Loop: If node is an HTML element with the same tag name as the token, then:
var i = (isize)ak.vector_size(elements) - 1
while i >= 0:
let node = *ak.vector_at(elements, (usize)i)
if html_parser_local_name_is(node, html_syntax.html_token_tag_name(token)):
# 1. Generate implied end tags, except for HTML elements with the same tag name as the token.
html_parser_generate_implied_end_tags(this, html_syntax.html_token_tag_name(token))
# 2. If node is not the current node, then this is a parse error.
if node != html_parser_current_element(this):
html_log_parse_error()
# 3. Pop all the nodes from the current node up to node, including node, then stop these steps.
while html_parser_current_element(this) != node:
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
break
# 3. Otherwise, if node is in the special category, then this is a parse error; ignore the token, and return.
if html_parser_is_special_tag(dom_element_local_name(node), dom_element_namespace_uri(node)):
html_log_parse_error()
return
# 4. Set node to the previous entry in the stack of open elements.
# 5. Return to the step labeled loop.
i -= 1
# mark: Adjusting tokens for foreign content ====================================================
## Ported from HTMLParser.cpp:3284 Web::HTML::HTMLParser::adjust_mathml_attributes.
pub func html_parser_adjust_mathml_attributes(this: HtmlParser*, token: html_syntax.HtmlToken*):
html_syntax.html_token_adjust_attribute_name(token, ak.fly_string_from_str("definitionurl"), ak.fly_string_from_str("definitionURL"))
## Ported from HTMLParser.cpp:3289 Web::HTML::HTMLParser::adjust_svg_tag_names.
pub func html_parser_adjust_svg_tag_names(this: HtmlParser*, token: html_syntax.HtmlToken*):
# static TagNameAdjustment adjustments[] = { { from, to }, ... }
let adjustments: (str, str)[37] = [
("altglyph", "altGlyph"),
("altglyphdef", "altGlyphDef"),
("altglyphitem", "altGlyphItem"),
("animatecolor", "animateColor"),
("animatemotion", "animateMotion"),
("animatetransform", "animateTransform"),
("clippath", "clipPath"),
("feblend", "feBlend"),
("fecolormatrix", "feColorMatrix"),
("fecomponenttransfer", "feComponentTransfer"),
("fecomposite", "feComposite"),
("feconvolvematrix", "feConvolveMatrix"),
("fediffuselighting", "feDiffuseLighting"),
("fedisplacementmap", "feDisplacementMap"),
("fedistantlight", "feDistantLight"),
("fedropshadow", "feDropShadow"),
("feflood", "feFlood"),
("fefunca", "feFuncA"),
("fefuncb", "feFuncB"),
("fefuncg", "feFuncG"),
("fefuncr", "feFuncR"),
("fegaussianblur", "feGaussianBlur"),
("feimage", "feImage"),
("femerge", "feMerge"),
("femergenode", "feMergeNode"),
("femorphology", "feMorphology"),
("feoffset", "feOffset"),
("fepointlight", "fePointLight"),
("fespecularlighting", "feSpecularLighting"),
("fespotlight", "feSpotLight"),
("fetile", "feTile"),
("feturbulence", "feTurbulence"),
("foreignobject", "foreignObject"),
("glyphref", "glyphRef"),
("lineargradient", "linearGradient"),
("radialgradient", "radialGradient"),
("textpath", "textPath"),
]
for adjustment in adjustments:
html_syntax.html_token_adjust_tag_name(token, ak.fly_string_from_str(adjustment.0), ak.fly_string_from_str(adjustment.1))
## Ported from HTMLParser.cpp:3341 Web::HTML::HTMLParser::adjust_svg_attributes.
pub func html_parser_adjust_svg_attributes(this: HtmlParser*, token: html_syntax.HtmlToken*):
# static AttributeAdjustment adjustments[] = { { from, to }, ... }
let adjustments: (str, str)[58] = [
("attributename", "attributeName"),
("attributetype", "attributeType"),
("basefrequency", "baseFrequency"),
("baseprofile", "baseProfile"),
("calcmode", "calcMode"),
("clippathunits", "clipPathUnits"),
("diffuseconstant", "diffuseConstant"),
("edgemode", "edgeMode"),
("filterunits", "filterUnits"),
("glyphref", "glyphRef"),
("gradienttransform", "gradientTransform"),
("gradientunits", "gradientUnits"),
("kernelmatrix", "kernelMatrix"),
("kernelunitlength", "kernelUnitLength"),
("keypoints", "keyPoints"),
("keysplines", "keySplines"),
("keytimes", "keyTimes"),
("lengthadjust", "lengthAdjust"),
("limitingconeangle", "limitingConeAngle"),
("markerheight", "markerHeight"),
("markerunits", "markerUnits"),
("markerwidth", "markerWidth"),
("maskcontentunits", "maskContentUnits"),
("maskunits", "maskUnits"),
("numoctaves", "numOctaves"),
("pathlength", "pathLength"),
("patterncontentunits", "patternContentUnits"),
("patterntransform", "patternTransform"),
("patternunits", "patternUnits"),
("pointsatx", "pointsAtX"),
("pointsaty", "pointsAtY"),
("pointsatz", "pointsAtZ"),
("preservealpha", "preserveAlpha"),
("preserveaspectratio", "preserveAspectRatio"),
("primitiveunits", "primitiveUnits"),
("refx", "refX"),
("refy", "refY"),
("repeatcount", "repeatCount"),
("repeatdur", "repeatDur"),
("requiredextensions", "requiredExtensions"),
("requiredfeatures", "requiredFeatures"),
("specularconstant", "specularConstant"),
("specularexponent", "specularExponent"),
("spreadmethod", "spreadMethod"),
("startoffset", "startOffset"),
("stddeviation", "stdDeviation"),
("stitchtiles", "stitchTiles"),
("surfacescale", "surfaceScale"),
("systemlanguage", "systemLanguage"),
("tablevalues", "tableValues"),
("targetx", "targetX"),
("targety", "targetY"),
("textlength", "textLength"),
("viewbox", "viewBox"),
("viewtarget", "viewTarget"),
("xchannelselector", "xChannelSelector"),
("ychannelselector", "yChannelSelector"),
("zoomandpan", "zoomAndPan"),
]
for adjustment in adjustments:
html_syntax.html_token_adjust_attribute_name(token, ak.fly_string_from_str(adjustment.0), ak.fly_string_from_str(adjustment.1))
## A row of adjust_foreign_attributes' table (`struct ForeignAttributeAdjustment`).
struct HtmlParserForeignAttributeAdjustment:
var attribute_name: str
var prefix: str?
var local_name: str
var namespace_: ak.FlyString
## Ported from HTMLParser.cpp:3415 Web::HTML::HTMLParser::adjust_foreign_attributes.
# https://html.spec.whatwg.org/multipage/parsing.html#adjust-foreign-attributes
pub func html_parser_adjust_foreign_attributes(token: html_syntax.HtmlToken*):
let adjustments: HtmlParserForeignAttributeAdjustment[11] = [
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:actuate", prefix = "xlink", local_name = "actuate", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:arcrole", prefix = "xlink", local_name = "arcrole", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:href", prefix = "xlink", local_name = "href", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:role", prefix = "xlink", local_name = "role", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:show", prefix = "xlink", local_name = "show", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:title", prefix = "xlink", local_name = "title", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xlink:type", prefix = "xlink", local_name = "type", namespace_ = namespace_x_link),
HtmlParserForeignAttributeAdjustment(attribute_name = "xml:lang", prefix = "xml", local_name = "lang", namespace_ = namespace_xml),
HtmlParserForeignAttributeAdjustment(attribute_name = "xml:space", prefix = "xml", local_name = "space", namespace_ = namespace_xml),
HtmlParserForeignAttributeAdjustment(attribute_name = "xmlns", prefix = none, local_name = "xmlns", namespace_ = namespace_xmlns),
HtmlParserForeignAttributeAdjustment(attribute_name = "xmlns:xlink", prefix = "xmlns", local_name = "xlink", namespace_ = namespace_xmlns),
]
for adjustment in adjustments:
var prefix: ak.FlyString? = none
if let prefix_text = adjustment.prefix:
prefix = ak.fly_string_from_str(prefix_text)
html_syntax.html_token_adjust_foreign_attribute(token, ak.fly_string_from_str(adjustment.attribute_name), prefix, ak.fly_string_from_str(adjustment.local_name), adjustment.namespace_)