Explorar o código

LibWeb: Add the default user agent MathML stylesheet

We now apply MathML's default user agent style sheet along with other
default styles. This sheet is not mixed in with the other styles in
CSS/Default.css because it is a namespaced stylesheet and so has to
be its own sheet.
Jonah hai 1 ano
pai
achega
0b2da4f8c6

+ 13 - 0
Meta/CMake/libweb_generators.cmake

@@ -89,6 +89,19 @@ function (generate_css_implementation)
     add_custom_target(generate_QuirksModeStyleSheetSource.cpp DEPENDS CSS/QuirksModeStyleSheetSource.cpp)
     add_custom_target(generate_QuirksModeStyleSheetSource.cpp DEPENDS CSS/QuirksModeStyleSheetSource.cpp)
     add_dependencies(all_generated generate_QuirksModeStyleSheetSource.cpp)
     add_dependencies(all_generated generate_QuirksModeStyleSheetSource.cpp)
 
 
+    add_custom_command(
+            OUTPUT MathML/MathMLStyleSheetSource.cpp
+            COMMAND "${CMAKE_COMMAND}" -E make_directory CSS
+            COMMAND "${LIBWEB_INPUT_FOLDER}/Scripts/GenerateStyleSheetSource.sh" mathml_stylesheet_source "${LIBWEB_INPUT_FOLDER}/MathML/Default.css" > MathML/MathMLStyleSheetSource.cpp.tmp
+            COMMAND "${CMAKE_COMMAND}" -E copy_if_different MathML/MathMLStyleSheetSource.cpp.tmp MathML/MathMLStyleSheetSource.cpp
+            COMMAND "${CMAKE_COMMAND}" -E remove MathML/MathMLStyleSheetSource.cpp.tmp
+            VERBATIM
+            DEPENDS "${LIBWEB_INPUT_FOLDER}/Scripts/GenerateStyleSheetSource.sh"
+            MAIN_DEPENDENCY "${LIBWEB_INPUT_FOLDER}/MathML/Default.css"
+    )
+    add_custom_target(generate_MathMLStyleSheetSource.cpp DEPENDS MathML/MathMLStyleSheetSource.cpp)
+    add_dependencies(all_generated generate_MathMLStyleSheetSource.cpp)
+
     set(CSS_GENERATED_TO_INSTALL
     set(CSS_GENERATED_TO_INSTALL
         "CSS/EasingFunctions.h"
         "CSS/EasingFunctions.h"
         "CSS/Enums.h"
         "CSS/Enums.h"

+ 7 - 0
Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn

@@ -213,6 +213,13 @@ embed_as_string_view("generate_quirks_mode_stylesheet_source") {
   namespace = "Web::CSS"
   namespace = "Web::CSS"
 }
 }
 
 
+embed_as_string_view("generate_mathml_stylesheet_source") {
+  input = "MathML/Default.css"
+  output = "$target_gen_dir/MathML/MathMLStyleSheetSource.cpp"
+  variable_name = "mathml_stylesheet_source"
+  namespace = "Web::MathML"
+}
+
 source_set("all_generated") {
 source_set("all_generated") {
   generated_deps = [
   generated_deps = [
     ":generate_aria_roles",
     ":generate_aria_roles",

+ 19 - 19
Tests/LibWeb/Layout/expected/css-namespace-tag-name-selector.txt

@@ -1,41 +1,41 @@
 Viewport <#document> at (0,0) content-size 800x600 children: not-inline
 Viewport <#document> at (0,0) content-size 800x600 children: not-inline
   BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
   BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
-    BlockContainer <body> at (8,8) content-size 784x207.3125 children: not-inline
-      BlockContainer <(anonymous)> at (8,8) content-size 784x161.65625 children: inline
-        line 0 width: 413.453125, height: 161.65625, bottom: 161.65625, baseline: 152
+    BlockContainer <body> at (8,8) content-size 784x299.65625 children: not-inline
+      BlockContainer <(anonymous)> at (8,8) content-size 784x152 children: inline
+        line 0 width: 302, height: 152, bottom: 152, baseline: 152
           frag 0 from SVGSVGBox start: 0, length: 0, rect: [9,9 300x150]
           frag 0 from SVGSVGBox start: 0, length: 0, rect: [9,9 300x150]
-          frag 1 from TextNode start: 0, length: 1, rect: [310,146 8x17.46875]
-            " "
-          frag 2 from TextNode start: 0, length: 5, rect: [320,126 99.453125x43.671875]
-            "Hello"
         SVGSVGBox <svg> at (9,9) content-size 300x150 [SVG] children: inline
         SVGSVGBox <svg> at (9,9) content-size 300x150 [SVG] children: inline
           InlineNode <a>
           InlineNode <a>
             SVGTextBox <text> at (9,9) content-size 0x0 children: inline
             SVGTextBox <text> at (9,9) content-size 0x0 children: inline
               TextNode <#text>
               TextNode <#text>
         TextNode <#text>
         TextNode <#text>
         InlineNode <math>
         InlineNode <math>
-          InlineNode <a>
-            TextNode <#text>
+      BlockContainer <a> at (9,161) content-size 100x100 children: inline
+        line 0 width: 99.453125, height: 43.671875, bottom: 43.671875, baseline: 33.828125
+          frag 0 from TextNode start: 0, length: 5, rect: [9,161 99.453125x43.671875]
+            "Hello"
         TextNode <#text>
         TextNode <#text>
-      BlockContainer <div> at (9,170.65625) content-size 782x43.65625 children: inline
+      BlockContainer <(anonymous)> at (8,262) content-size 784x0 children: inline
+        TextNode <#text>
+      BlockContainer <div> at (9,263) content-size 782x43.65625 children: inline
         line 0 width: 101.453125, height: 43.65625, bottom: 43.65625, baseline: 33.828125
         line 0 width: 101.453125, height: 43.65625, bottom: 43.65625, baseline: 33.828125
-          frag 0 from TextNode start: 0, length: 5, rect: [10,170.65625 99.453125x43.671875]
+          frag 0 from TextNode start: 0, length: 5, rect: [10,263 99.453125x43.671875]
             "Hello"
             "Hello"
         InlineNode <a>
         InlineNode <a>
           TextNode <#text>
           TextNode <#text>
-      BlockContainer <(anonymous)> at (8,215.3125) content-size 784x0 children: inline
+      BlockContainer <(anonymous)> at (8,307.65625) content-size 784x0 children: inline
         TextNode <#text>
         TextNode <#text>
 
 
 PaintableWithLines (Viewport<#document>) [0,0 800x600]
 PaintableWithLines (Viewport<#document>) [0,0 800x600]
   PaintableWithLines (BlockContainer<HTML>) [0,0 800x600]
   PaintableWithLines (BlockContainer<HTML>) [0,0 800x600]
-    PaintableWithLines (BlockContainer<BODY>) [8,8 784x207.3125]
-      PaintableWithLines (BlockContainer(anonymous)) [8,8 784x161.65625] overflow: [8,8 784x161.671875]
+    PaintableWithLines (BlockContainer<BODY>) [8,8 784x299.65625]
+      PaintableWithLines (BlockContainer(anonymous)) [8,8 784x152]
         SVGSVGPaintable (SVGSVGBox<svg>) [8,8 302x152]
         SVGSVGPaintable (SVGSVGBox<svg>) [8,8 302x152]
-        TextPaintable (TextNode<#text>)
         InlinePaintable (InlineNode<math>)
         InlinePaintable (InlineNode<math>)
-          InlinePaintable (InlineNode<a>)
-            TextPaintable (TextNode<#text>)
-      PaintableWithLines (BlockContainer<DIV>) [8,169.65625 784x45.65625] overflow: [9,170.65625 782x43.671875]
+      PaintableWithLines (BlockContainer<a>) [8,160 102x102]
+        TextPaintable (TextNode<#text>)
+      PaintableWithLines (BlockContainer(anonymous)) [8,262 784x0]
+      PaintableWithLines (BlockContainer<DIV>) [8,262 784x45.65625] overflow: [9,263 782x43.671875]
         InlinePaintable (InlineNode<A>)
         InlinePaintable (InlineNode<A>)
           TextPaintable (TextNode<#text>)
           TextPaintable (TextNode<#text>)
-      PaintableWithLines (BlockContainer(anonymous)) [8,215.3125 784x0]
+      PaintableWithLines (BlockContainer(anonymous)) [8,307.65625 784x0]

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -633,6 +633,7 @@ set(GENERATED_SOURCES
     CSS/QuirksModeStyleSheetSource.cpp
     CSS/QuirksModeStyleSheetSource.cpp
     CSS/TransformFunctions.cpp
     CSS/TransformFunctions.cpp
     CSS/ValueID.cpp
     CSS/ValueID.cpp
+    MathML/MathMLStyleSheetSource.cpp
 )
 )
 
 
 serenity_lib(LibWeb web)
 serenity_lib(LibWeb web)

+ 11 - 0
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -195,6 +195,16 @@ static CSSStyleSheet& quirks_mode_stylesheet(DOM::Document const& document)
     return *sheet;
     return *sheet;
 }
 }
 
 
+static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document)
+{
+    static JS::Handle<CSSStyleSheet> sheet;
+    if (!sheet.cell()) {
+        extern StringView mathml_stylesheet_source;
+        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), mathml_stylesheet_source));
+    }
+    return *sheet;
+}
+
 template<typename Callback>
 template<typename Callback>
 void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
 void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
 {
 {
@@ -202,6 +212,7 @@ void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback c
         callback(default_stylesheet(document()));
         callback(default_stylesheet(document()));
         if (document().in_quirks_mode())
         if (document().in_quirks_mode())
             callback(quirks_mode_stylesheet(document()));
             callback(quirks_mode_stylesheet(document()));
+        callback(mathml_stylesheet(document()));
     }
     }
     if (cascade_origin == CascadeOrigin::Author) {
     if (cascade_origin == CascadeOrigin::Author) {
         for (auto const& sheet : document().style_sheets().sheets())
         for (auto const& sheet : document().style_sheets().sheets())

+ 1 - 0
Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp

@@ -35,6 +35,7 @@
 #include <LibWeb/HighResolutionTime/TimeOrigin.h>
 #include <LibWeb/HighResolutionTime/TimeOrigin.h>
 #include <LibWeb/Infra/CharacterTypes.h>
 #include <LibWeb/Infra/CharacterTypes.h>
 #include <LibWeb/Infra/Strings.h>
 #include <LibWeb/Infra/Strings.h>
+#include <LibWeb/MathML/TagNames.h>
 #include <LibWeb/Namespace.h>
 #include <LibWeb/Namespace.h>
 #include <LibWeb/SVG/TagNames.h>
 #include <LibWeb/SVG/TagNames.h>
 
 

+ 119 - 0
Userland/Libraries/LibWeb/MathML/Default.css

@@ -0,0 +1,119 @@
+/* https://w3c.github.io/mathml-core/#user-agent-stylesheet */
+@namespace url(http://www.w3.org/1998/Math/MathML);
+
+/* Universal rules */
+* {
+  font-size: math;
+  display: block math;
+  writing-mode: horizontal-tb !important;
+}
+
+/* The <math> element */
+math {
+  direction: ltr;
+  text-indent: 0;
+  letter-spacing: normal;
+  line-height: normal;
+  word-spacing: normal;
+  font-family: math;
+  font-size: inherit;
+  font-style: normal;
+  font-weight: normal;
+  display: inline math;
+  math-shift: normal;
+  math-style: compact;
+  math-depth: 0;
+}
+math[display="block" i] {
+  display: block math;
+  math-style: normal;
+}
+math[display="inline" i] {
+  display: inline math;
+  math-style: compact;
+}
+
+/* <mrow>-like elements */
+semantics > :not(:first-child) {
+  display: none;
+}
+maction > :not(:first-child) {
+  display: none;
+}
+merror {
+  border: 1px solid red;
+  background-color: lightYellow;
+}
+mphantom {
+  visibility: hidden;
+}
+
+/* Token elements */
+mi {
+  text-transform: math-auto;
+}
+
+/* Tables */
+mtable {
+  display: inline-table;
+  math-style: compact;
+}
+mtr {
+  display: table-row;
+}
+mtd {
+  display: table-cell;
+  /* Centering inside table cells should rely on box alignment properties.
+     See https://github.com/w3c/mathml-core/issues/156 */
+  text-align: center;
+  padding: 0.5ex 0.4em;
+}
+
+/* Fractions */
+mfrac {
+  padding-inline-start: 1px;
+  padding-inline-end: 1px;
+}
+mfrac > * {
+  math-depth: auto-add;
+  math-style: compact;
+}
+mfrac > :nth-child(2) {
+  math-shift: compact;
+}
+
+/* Other rules for scriptlevel, displaystyle and math-shift */
+mroot > :not(:first-child) {
+  math-depth: add(2);
+  math-style: compact;
+}
+mroot, msqrt {
+  math-shift: compact;
+}
+msub > :not(:first-child),
+msup > :not(:first-child),
+msubsup > :not(:first-child),
+mmultiscripts > :not(:first-child),
+munder > :not(:first-child),
+mover > :not(:first-child),
+munderover > :not(:first-child) {
+  math-depth: add(1);
+  math-style: compact;
+}
+munder[accentunder="true" i] > :nth-child(2),
+mover[accent="true" i] > :nth-child(2),
+munderover[accentunder="true" i] > :nth-child(2),
+munderover[accent="true" i] > :nth-child(3) {
+  font-size: inherit;
+}
+msub > :nth-child(2),
+msubsup > :nth-child(2),
+mmultiscripts > :nth-child(even),
+mmultiscripts > mprescripts ~ :nth-child(odd),
+mover[accent="true" i] > :first-child,
+munderover[accent="true" i] > :first-child {
+  math-shift: compact;
+}
+mmultiscripts > mprescripts ~ :nth-child(even) {
+  math-shift: inherit;
+}