stylo: Need to support all the nsHTMLStyleSheet bits

RESOLVED FIXED in Firefox 55

Status

()

Core
CSS Parsing and Computation
P1
normal
RESOLVED FIXED
2 months ago
23 days ago

People

(Reporter: bz, Assigned: manishearth)

Tracking

(Blocks: 1 bug)

Trunk
mozilla55
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(firefox54 affected, firefox55 fixed)

Details

MozReview Requests

()

Submitter Diff Changes Open Issues Last Updated
Loading...
Error loading review requests:

Attachments

(4 attachments, 2 obsolete attachments)

I'm not sure whether we handle all this already and bug 1341712 is the only issue this testcase hits:

  data:text/html,<!DOCTYPE html><table><tr><th>text</th></tr><tr><td>Long text

but chances are we don't handle it.
Boris, can you elaborate a bit more on what needs to be done here?
Flags: needinfo?(bzbarsky)
Priority: -- → P2
Gecko uses nsHTMLStyleSheet to implement various bits, by creating nsIStyleRule instances that are walked by nsHTMLStyleSheet::RulesMatching for certain nodes.  Specifically, these implement:

1) The "link", "alink", and "vlink" attributes on <body>.  These are mapped into style, but not for the body itself.  Instead, during attribute mapping for <body> they're mapped into HTMLColorRules hanging off nsHTMLStyleSheet that are then walked in RulesMatching.  I did just check: these don't work in stylo.  Or servo.  Note that this also needs to trigger restyles on visited/active state transitions for <a> elements, href attribute changes, etc.

2) The default text alignment behavior for <th>, which can't be expressed in parseable CSS.  That is, this is where NS_STYLE_TEXT_ALIGN_MOZ_CENTER_OR_INHERIT specified values of text-align come from.  See TableTHRule::MapRuleInfoInto.  Doesn't work in stylo.

3) Quirks mode tables have interesting color behavior.  See TableQuirkColorRule.  Specifically, if not otherwise specified they get the non-parseable specified value "NS_STYLE_COLOR_INHERIT_FROM_BODY", which then needs to be handled specially when determining computed style.  It does what it says on the tin: inherits the color from the body.  If there is no body, things are a bit complicated.  In practice it starts off as the default color, and after that is changes whenever we compute style for the body, and hence can be stale if the body is removed.  For quirks mode documents the weirdness around the no-body case doesn't obviously matter...  I just checked, and this is broken in both stylo and servo.

4) The "content style rules" of the element.  This is SVG mapped attributes, HTML mapped attributes, HTMLBodyElement::WalkContentStyleRules, HTMLTableCellElement::WalkContentStyleRules, nsXULElement::WalkContentStyleRules.  I think we have these either implemented or with existing bugs covering them.  Again, this includes triggering a restyle when these things change.  Which, by the way, I'm not sure we did right for SVG.  We need to check.  :(

5) Something about the "xml:lang" attribute and how it maps into CSS lang stuff.  I thought we had a bug on this, but I don't see on right now.  Xidorn may know what the state of that is.

6) Some MathML bits, again dealing with lang.
Flags: needinfo?(bzbarsky)
Thanks for the writeup boris. Given that bug 1341647 and bug 1341648 (which are a subset of the issues described above) are already in Manish's bucket, and given that he has experience with the rule mapping stuff, I think it makes sense for him to take it.

Manish, feel free to split this out into sub-bugs or do it all in this bug, whatever's easier.
Assignee: nobody → manishearth
Priority: P2 → P1
(Assignee)

Updated

a month ago
Status: NEW → ASSIGNED
(Assignee)

Updated

a month ago
Duplicate of this bug: 1341648
(Assignee)

Updated

a month ago
Duplicate of this bug: 1341647
(Assignee)

Comment 6

a month ago
I'm working on this next. Marked bug 1341647 and bug 1341648 as dupes of this since they deal with the extra content style rules; I'll do all of them at once.

The visited stuff I think is going to be discussed in taipei, along with bug 1328509.


nsXULElement::WalkContentStyleRules seems to not do anything?
> nsXULElement::WalkContentStyleRules seems to not do anything?

Indeed.  So nothing to do there.  I wonder why it exists.  Maybe a leftover from when nsXULElement had a totally different implementation from all other elements....  We should just kill it off.  ;)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 12

a month ago
Added patch for the <td> stuff and <body>. Seems to work. Working on the the other bits now.


I removed the TABLE_ATTRS_DIRTY optimization (added in bug 211636). We construct nsMappedAttributes all the time, it's not expensive (may have been 7 years ago). Without this change we can't use the existing lazy resolution framework for handling mapped attributes and would have to add a second pass for these elements.

<table> elements without <td> or <th> are where we lose out by early computing (also cases where there's a lot of flipping of `cellpadding`), and those should be rare.

For <body>, I cached an extra PDB on the body element. Unlike the mapped attrs, this is not lazily computed -- I figured that since there's usually just one of these it's fine to eagerly compute it instead of creating another caching mechanism. The nsFrameLoader stuff means that changing the margin on the frame will only affect the first <body> element, if the document has multiple bodies.
> <table> elements without <td> or <th> are where we lose out by early computing

That's pretty rare, yeah.

> (also cases where there's a lot of flipping of `cellpadding`

Or just cases when a <table> is repeatedly removed from and inserted into the DOM.  But I guess recreating mapped attributes on BindToTree is the normal thing to do...

I might make more sense to do it on adopt; worth filing a bug on that.  The current behavior where mapped attrs don't affect style unless you're in the doc is kind of strange.

> changing the margin on the frame will only affect the first <body> element,
> if the document has multiple bodies.

That doesn't match either the spec or Gecko's current behavior (which match).  Or Chrome's current behavior (which matches Gecko and the spec).  Or Safari's which doesn't update either the first or any other <body>.  I haven't checked Edge...
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 17

a month ago
heycam thinks you should review.

I will be adding more patches as I go through the remaining bits, but these first two are ready for review (and should be largely independent from the next ones, aside from probably sharing the Gecko_GetExtraContentStyleRules function).
Comment hidden (mozreview-request)
Attachment #8851770 - Flags: review?(cam)
Attachment #8851836 - Flags: review?(cam)
Flags: needinfo?(bzbarsky)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Updated

a month ago
Depends on: 1341648
(Assignee)

Updated

a month ago
Depends on: 1341647
Comment hidden (mozreview-request)
(Assignee)

Updated

a month ago
Attachment #8851770 - Attachment is obsolete: true
(Assignee)

Updated

a month ago
Attachment #8852275 - Attachment is obsolete: true
Comment hidden (mozreview-request)
(Assignee)

Updated

a month ago
Attachment #8851836 - Flags: review?(cam)
(Assignee)

Comment 24

a month ago
Stuff needing bz's review split out into bug 1341648 and bug 1341647, what's left is mostly pure servo-side changes.

More patches may be added. r?heycam
Flags: needinfo?(bzbarsky)
(Assignee)

Comment 25

a month ago
We have a test failure on layout/reftests/w3c-css/submitted/text3/text-align-match-parent-root-rtl.html caused by this.

The gist of it is that `<html dir=rtl style="text-align:match-parent">...` should have right-aligned content. match-parent is working in general, but not on the root element.


I'm poking into this and I'm not sure why this works in Gecko, either. When cascading, we do set the initial value of `direction` to be `aContext->GetBidi()`'s bidi-ness, and the nsStyleVisibility constructor does this too, but GetBidi() doesn't seem to have a dependence on the dir of the html element.

In stylo, we already have a dependence on GetBidi() in nsStyleVisibility's constructor (though it's different from the one for `direction: initial` -- `direction: initial` will use `GET_BIDI_OPTION_DIRECTION(presContext->GetBidi()) == IBMBIDI_TEXTDIRECTION_RTL` whereas the constructor uses `presContext->GetBidi() == IBMBIDI_TEXTDIRECTION_RTL`, which seems wrong -- but fixing it does not improve things since in Stylo for the given document `GetBidi()` is still LTR).


So I'm not sure what's missing here. Somehow, gecko gets the initial value for `direction` of that document as RTL (though I can't see where), but Stylo does not, despite it doing basically the same thing here.


dholbert said that jfkthame might have ideas. Thoughts?

I think that the above patch can be landed independently of that bug, however, since at its core it's a bug in how `direction` is cascaded, not text-align (just happens to affect text-align)
Flags: needinfo?(jfkthame)
(Assignee)

Comment 26

a month ago
More curiousness: `direction: initial` sets to LTR on a document with `dir=rtl`.

I am even more confused as to why that match-parent test works in gecko.
(Assignee)

Comment 27

a month ago
Ah, there's an if(parent) there which I missed -- the whole code is skipped. I didn't realize parent was the parent /element/, not style.

We have an is_root_element flag which I can use here.
Flags: needinfo?(jfkthame)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 30

a month ago
The <table> color quirk isn't quite working yet because the body color is not being set. The gecko code doing this is weird, trying to copy it over.
Comment hidden (mozreview-request)
(Assignee)

Comment 32

a month ago
The <table> quirk works now, but fails layout/reftests/bugs/655549-1.html , because the body style is not updated. I'll have to look in to why GetContext() isn't called in that case and where else this code needs to be kept.

Comment 33

a month ago
mozreview-review
Comment on attachment 8851836 [details]
Bug 1341714 - Part 1: stylo: Add support for text-align match-parent and the <th> -moz-center-or-inherit behavior ;

https://reviewboard.mozilla.org/r/124038/#review127538

::: servo/components/style/properties/longhand/inherited_text.mako.rs:305
(Diff revision 9)
> +    ${helpers.gecko_keyword_conversion(Keyword('text-align',
> +                                               """left right center justify -moz-left -moz-right
> +                                                -moz-center char end""",
> +                                                gecko_strip_moz_prefix=False), type="T")}

Nit: Indent this one level.

::: servo/components/style/properties/longhand/inherited_text.mako.rs:327
(Diff revision 9)
> +            Keyword(computed_value::T),
> +            MatchParent,
> +            MozCenterOrInherit,
> +        }
> +        pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
> +            if let Ok(key) = input.try(computed_value::T::parse) {

Maybe add a comment in here saying that MozCenterOrInherit is value that can't be parsed, but can be set by presentation attributes.

::: servo/components/style/properties/longhand/inherited_text.mako.rs:339
(Diff revision 9)
> +        impl ToCss for SpecifiedValue {
> +            fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
> +                match *self {
> +                    SpecifiedValue::Keyword(key) => key.to_css(dest),
> +                    SpecifiedValue::MatchParent => dest.write_str("match-parent"),
> +                    SpecifiedValue::MozCenterOrInherit => Ok(()),

It might be useful to serialize this anyway, in case we add a way to dump the contents/results of presentation attribute mapping for debugging.

::: servo/components/style/properties/longhand/inherited_text.mako.rs:362
(Diff revision 9)
> +                        // on the root <html> element we should still respect the dir
> +                        // but the parent dir of that element is LTR even if it's <html dir=rtl>
> +                        // and will only be RTL if certain prefs have been set.
> +                        // In that case, the default behavior here will set it to left,
> +                        // but we want to set it to right -- instead set it to `start`, which
> +                        // will do the right thing in this case (but not the general case)
> +                        if context.is_root_element {
> +                            return computed_value::T::start;
> +                        }

Unfortunately I got confused by this comment. :-)

As I understand it from reading nsRuleNode::ComputeVisibilityData, if we have a match-parent value, and no parent styles to inherit from, then we leave mTextAlign set to the value it got during the nsStyleText constuctor, which is the LTR or RTL, based on nsPresContext::GetBidi().

Does that mean that the behaviour here is wrong in the case that the pres context and <html dir> differ, because the start value will be resolved against the <html> element's direction property during layout?

Is the a reason we can't/shouldn't just say copy the value from the initial Visibility struct in this case?

Comment 34

a month ago
mozreview-review
Comment on attachment 8852732 [details]
Bug 1341714 - Part 2: stylo: Add support for <table> color quirk ;

https://reviewboard.mozilla.org/r/124890/#review127564

::: servo/components/style/values/specified/color.rs:94
(Diff revision 2)
>                  Color::MozDefaultColor => dest.write_str("-moz-default-color"),
>                  Color::MozDefaultBackgroundColor => dest.write_str("-moz-default-background-color"),
>                  Color::MozHyperlinktext => dest.write_str("-moz-hyperlinktext"),
>                  Color::MozActiveHyperlinktext => dest.write_str("-moz-activehyperlinktext"),
>                  Color::MozVisitedHyperlinktext => dest.write_str("-moz-visitedhyperlinktext"),
> +                Color::InheritFromBodyQuirk => Ok(()),

And maybe here too.  Not important, though.
Attachment #8852732 - Flags: review?(cam) → review+
(Assignee)

Comment 35

a month ago
mozreview-review-reply
Comment on attachment 8852732 [details]
Bug 1341714 - Part 2: stylo: Add support for <table> color quirk ;

https://reviewboard.mozilla.org/r/124890/#review127564

> And maybe here too.  Not important, though.

We've been pretty consistent about not serializing to things that can't be specified, e.g. with the system font stuff and other things. I'd rather not serialize this.
(Assignee)

Comment 36

a month ago
mozreview-review-reply
Comment on attachment 8851836 [details]
Bug 1341714 - Part 1: stylo: Add support for text-align match-parent and the <th> -moz-center-or-inherit behavior ;

https://reviewboard.mozilla.org/r/124038/#review127538

> Unfortunately I got confused by this comment. :-)
> 
> As I understand it from reading nsRuleNode::ComputeVisibilityData, if we have a match-parent value, and no parent styles to inherit from, then we leave mTextAlign set to the value it got during the nsStyleText constuctor, which is the LTR or RTL, based on nsPresContext::GetBidi().
> 
> Does that mean that the behaviour here is wrong in the case that the pres context and <html dir> differ, because the start value will be resolved against the <html> element's direction property during layout?
> 
> Is the a reason we can't/shouldn't just say copy the value from the initial Visibility struct in this case?

> mTextAlign set to the value it got during the nsStyleText constuctor, which is the LTR or RTL, based on nsPresContext::GetBidi().

... _almost_ right :)

`mTextAlign` has a default value of start. `mDirection` is what has a bidi-dependent initial value.

Yes, we could copy it from the init struct. Or call `initial_value()` here.
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 39

a month ago
Just coming back to the original list of bits to note the status:

> 1) The "link", "alink", and "vlink" attributes on <body>.

bug 1328509, and will be discussed in taipei

> 2) The default text alignment behavior for <th>
> ...
> 3) Quirks mode tables have interesting color behavior. 

Fixed in this bug

> This is SVG mapped attributes


bug 1329093 (fixed)

> HTML mapped attributes,

bug 1330041 (fixed)


> HTMLBodyElement::WalkContentStyleRules

bug 1341647 (has patches, in review process)

> HTMLTableCellElement::WalkContentStyleRules

bug 1341648 (has patches, in review process)


> nsXULElement::WalkContentStyleRules. 

> Which, by the way, I'm not sure we did right for SVG.

we didn't (in fact, we got it wrong for all pres attrs), but bug 1341647 fixes it on the side

> 5) Something about the "xml:lang" attribute and how it maps into CSS lang stuff. 
> 
> 6) Some MathML bits, again dealing with lang.

I thought xidorn was removing this stuff, but this is a different thing. Apparently mathml gets a default `lang` value of `x_math`, and `xml:lang` overrides regular `lang`. This should be easy to support in this same bug using the same tricks. Will do.
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 42

a month ago
Fixed mathml. Relevant tests may not pass because of bug 1352275 and bug 1352277.


xml:lang is going to be ... fun :) xml:lang is really just a mapped attribute that overrides `lang`, but it's not included in the nsMappedAttributes framework (which seems to only handle HTML mapped attributes). The straightforward solution here is to simply check if there's an xml:lang attr on the element, and if so push a pres hint *after* we push the regular pres hints (so that it has priority). This does mean repeatedly synthesizing PDBs (we cache every other preshint PDB on the stylesheet or in a lazy static), but I suspect xml:lang on HTML is rare enough that it doesn't matter? It also means that synthesize_presentational_blah now needs to query attributes on the element, which might be expensive (I'm not sure). I'll write up this solution unless others have objections. The only alternative I can think of is making the mapped attr framework capable of handling just this one XML namespace attribute. Doable.

There's also a bug where the body base color isn't updated when dynamically set. Apparently we can't do what gecko does here; bholley suggested instead just walking up the tree till a <body> is found. This will change behavior for malformed documents containing multiple bodies or elements outside of bodies, but that probably doesn't matter.
Comment hidden (mozreview-request)
(In reply to Manish Goregaokar [:manishearth] from comment #42)
> There's also a bug where the body base color isn't updated when dynamically
> set. Apparently we can't do what gecko does here; bholley suggested instead
> just walking up the tree till a <body> is found. This will change behavior
> for malformed documents containing multiple bodies or elements outside of
> bodies, but that probably doesn't matter.

I think we can be precise without much effort. We can just add a Gecko_IsBodyElement predicate, or just duplicate the logic of nsDocument::GetBodyElement.
> bug 1328509, and will be discussed in taipei

I see no mention of these attributes in that bug.  Please file a separate bug for them, depending on bug 1328509; otherwise they will just slip through the cracks.

> There's also a bug where the body base color isn't updated when dynamically set

What do you mean by "base color"?

> Apparently we can't do what gecko does here

Why not?

> This will change behavior for malformed documents containing multiple bodies or elements outside of bodies,
> but that probably doesn't matter.

You mean apart from being a spec violation?
(In reply to Boris Zbarsky [:bz] (still a bit busy) (if a patch has no decent message, automatic r-) from comment #45)
> > bug 1328509, and will be discussed in taipei
> 
> I see no mention of these attributes in that bug.  Please file a separate
> bug for them, depending on bug 1328509; otherwise they will just slip
> through the cracks.
> 
> > There's also a bug where the body base color isn't updated when dynamically set
> 
> What do you mean by "base color"?

The color of the body, which ends up being used as the inherited color for tables in quirks mode.

> 
> > Apparently we can't do what gecko does here
> 
> Why not?

Because gecko caches the current color on the prescontext in GetContext, which is guaranteed to happen before any table style structs are computed in gecko, but generally happens long after table style structs are computed in servo (since they're computed eagerly, during the traversal).

> 
> > This will change behavior for malformed documents containing multiple bodies or elements outside of bodies,
> > but that probably doesn't matter.
> 
> You mean apart from being a spec violation?

As I mentioned in comment 44, I think we can do the right thing here.
(Assignee)

Comment 47

a month ago
> You mean apart from being a spec violation?

Oh, no, quite the opposite, if anything we would be more correct than Gecko here. I'm not 100% aware of how Gecko does this calculation, but it feels like in a document with multiple bodies you'd have issues with the color sometimes being picked off the wrong body (whichever was restyled the most recently). With the proposed solution for servo we'd be more consistent and correct.
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 50

29 days ago
Fixed the color thing.
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 53

29 days ago
Added support for xml:lang. This will query the attributes each time looking for xml:lang. It feels expensive to me, but Gecko does this too -- Gecko will query the attributes each time nsHTMLStyleSheet::RulesMatching(elem) is called. Gecko does cache lang rules in some sort of hashtable -- this means that it doesn't have to clone the string each time. We could do something similar with the PropertyDeclarationBlock if we wish. I'm not sure if any of this is performance-sensitive enough to matter, the way we do things may mean that this is completely unnecessary.

Kind of feel like it would be nicer if we just let the mapped attr functionality deal with xml:lang; the only issue here is the namespace.

Comment 54

27 days ago
mozreview-review
Comment on attachment 8851836 [details]
Bug 1341714 - Part 1: stylo: Add support for text-align match-parent and the <th> -moz-center-or-inherit behavior ;

https://reviewboard.mozilla.org/r/124038/#review128384

::: servo/components/style/properties/longhand/inherited_text.mako.rs:363
(Diff revisions 9 - 10)
>                          // on the root <html> element we should still respect the dir
>                          // but the parent dir of that element is LTR even if it's <html dir=rtl>
>                          // and will only be RTL if certain prefs have been set.
>                          // In that case, the default behavior here will set it to left,
>                          // but we want to set it to right -- instead set it to `start`, which
>                          // will do the right thing in this case (but not the general case)

This comment needs updating now that we're not returning "start".
Attachment #8851836 - Flags: review?(cam) → review+

Comment 55

27 days ago
mozreview-review
Comment on attachment 8853193 [details]
Bug 1341714 - Part 3: stylo: Add support for <math> default language;

https://reviewboard.mozilla.org/r/125276/#review128390
Attachment #8853193 - Flags: review?(cam) → review+

Comment 56

27 days ago
mozreview-review
Comment on attachment 8853648 [details]
Bug 1341714 - Part 4: stylo: Add support for xml:lang;

https://reviewboard.mozilla.org/r/125744/#review128392

Gecko seems to make the |-x-lang: x-math| rule take precedence over one mapped by xml:lang="".  Seems like we should do the same, unless there's a specific reason to?

::: layout/style/ServoBindings.h:163
(Diff revision 1)
>  bool Gecko_MatchesElement(mozilla::CSSPseudoClassType type, RawGeckoElementBorrowed element);
>  nsIAtom* Gecko_LocalName(RawGeckoElementBorrowed element);
>  nsIAtom* Gecko_Namespace(RawGeckoElementBorrowed element);
>  nsIAtom* Gecko_GetElementId(RawGeckoElementBorrowed element);
>  
> +nsIAtom* Gecko_GetXMLLangRule(RawGeckoElementBorrowed element);

"Rule" sounds like the wrong word here.  "Value" or "AttrValue"?
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
(Assignee)

Comment 61

26 days ago
Updated.

Comment 62

25 days ago
mozreview-review
Comment on attachment 8853648 [details]
Bug 1341714 - Part 4: stylo: Add support for xml:lang;

https://reviewboard.mozilla.org/r/125744/#review129196
Attachment #8853648 - Flags: review?(cam) → review+
(Assignee)

Comment 63

25 days ago
https://github.com/servo/servo/pull/16266
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)

Comment 68

25 days ago
Pushed by manishearth@gmail.com:
https://hg.mozilla.org/integration/autoland/rev/7f2767f80f13
Part 1: stylo: Add support for text-align match-parent and the <th> -moz-center-or-inherit behavior ; r=heycam
https://hg.mozilla.org/integration/autoland/rev/083b02023bc2
Part 2: stylo: Add support for <table> color quirk ; r=heycam
https://hg.mozilla.org/integration/autoland/rev/6e5447e93e85
Part 3: stylo: Add support for <math> default language; r=heycam
https://hg.mozilla.org/integration/autoland/rev/5175ea846fe0
Part 4: stylo: Add support for xml:lang; r=heycam

Comment 69

25 days ago
bugherder
https://hg.mozilla.org/mozilla-central/rev/7f2767f80f13
https://hg.mozilla.org/mozilla-central/rev/083b02023bc2
https://hg.mozilla.org/mozilla-central/rev/6e5447e93e85
https://hg.mozilla.org/mozilla-central/rev/5175ea846fe0
Status: ASSIGNED → RESOLVED
Last Resolved: 25 days ago
status-firefox55: --- → fixed
Resolution: --- → FIXED
Target Milestone: --- → mozilla55
(Assignee)

Updated

23 days ago
Duplicate of this bug: 1341712
You need to log in before you can comment on or make changes to this bug.