From f0e83f6d8a969c7bcee71bafc2715ae9df601cd5 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 6 Jun 2017 10:54:03 -0700 Subject: [PATCH] Simplify in selector args when serializing. --- components/selectors/parser.rs | 32 ++++++++++++++++--- .../html/selectorSerialize.htm.ini | 16 ---------- .../metadata/cssom/selectorSerialize.html.ini | 17 ---------- 3 files changed, 28 insertions(+), 37 deletions(-) delete mode 100644 tests/wpt/metadata-css/cssom-1_dev/html/selectorSerialize.htm.ini delete mode 100644 tests/wpt/metadata/cssom/selectorSerialize.html.ini diff --git a/components/selectors/parser.rs b/components/selectors/parser.rs index 0cb7c6e3f47..77b6e7bf7fe 100644 --- a/components/selectors/parser.rs +++ b/components/selectors/parser.rs @@ -797,6 +797,23 @@ impl ToCss for Combinator { impl ToCss for Component { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { use self::Component::*; + + /// Serialize values (part of the CSS Syntax spec, but currently only used here). + /// https://drafts.csswg.org/css-syntax-3/#serialize-an-anb-value + fn write_affine(dest: &mut W, a: i32, b: i32) -> fmt::Result where W: fmt::Write { + match (a, b) { + (0, 0) => dest.write_char('0'), + + (1, 0) => dest.write_char('n'), + (_, 0) => write!(dest, "{}n", a), + + (0, _) => write!(dest, "{}", b), + (1, _) => write!(dest, "n{:+}", b), + (-1, _) => write!(dest, "-n{:+}", b), + (_, _) => write!(dest, "{}n{:+}", a, b), + } + } + match *self { Combinator(ref c) => { c.to_css(dest) @@ -861,10 +878,17 @@ impl ToCss for Component { FirstOfType => dest.write_str(":first-of-type"), LastOfType => dest.write_str(":last-of-type"), OnlyOfType => dest.write_str(":only-of-type"), - NthChild(a, b) => write!(dest, ":nth-child({}n{:+})", a, b), - NthLastChild(a, b) => write!(dest, ":nth-last-child({}n{:+})", a, b), - NthOfType(a, b) => write!(dest, ":nth-of-type({}n{:+})", a, b), - NthLastOfType(a, b) => write!(dest, ":nth-last-of-type({}n{:+})", a, b), + NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) => { + match *self { + NthChild(_, _) => dest.write_str(":nth-child(")?, + NthLastChild(_, _) => dest.write_str(":nth-last-child(")?, + NthOfType(_, _) => dest.write_str(":nth-of-type(")?, + NthLastOfType(_, _) => dest.write_str(":nth-last-of-type(")?, + _ => unreachable!(), + } + write_affine(dest, a, b)?; + dest.write_char(')') + } NonTSPseudoClass(ref pseudo) => pseudo.to_css(dest), } } diff --git a/tests/wpt/metadata-css/cssom-1_dev/html/selectorSerialize.htm.ini b/tests/wpt/metadata-css/cssom-1_dev/html/selectorSerialize.htm.ini deleted file mode 100644 index 2796fc6ff28..00000000000 --- a/tests/wpt/metadata-css/cssom-1_dev/html/selectorSerialize.htm.ini +++ /dev/null @@ -1,16 +0,0 @@ -[selectorSerialize.htm] - type: testharness - [:nth-child serialization produces canonical form] - expected: FAIL - - [single pseudo (simple) selector "nth-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector "nth-last-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector "nth-of-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector ":nth-last-of-type" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL diff --git a/tests/wpt/metadata/cssom/selectorSerialize.html.ini b/tests/wpt/metadata/cssom/selectorSerialize.html.ini deleted file mode 100644 index 816fb7d846d..00000000000 --- a/tests/wpt/metadata/cssom/selectorSerialize.html.ini +++ /dev/null @@ -1,17 +0,0 @@ -[selectorSerialize.html] - type: testharness - [:nth-child serialization produces canonical form] - expected: FAIL - - [single pseudo (simple) selector "nth-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector "nth-last-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector "nth-of-child" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL - - [single pseudo (simple) selector ":nth-last-of-type" which accepts arguments in the sequence of simple selectors that is not a universal selector] - expected: FAIL -