Skip to content

Class-related methods fail to meet w3c standard for whitespace handling #3072

@zzzzBov

Description

@zzzzBov

I opened a related discussion about whitespace on WICG that may be worth a read before any changes are necessarily made regarding this.

the *Class methods have consistently failed to handle "space characters" correctly, and I'd reported a related bug a few years ago.

There are exactly 5 characters that are used to separate classes in HTML/CSS. Those characters in ascending unicode order are:

  • tab (\t, U+0009)
  • line feed (\n, U+000A)
  • form feed (\f, U+000C)
  • carriage return (\r, U+000D)
  • space ( , U+0020)

When other characters that are matched by the regular expression pattern \s are used in classes, jQuery fails to behave correctly according to the spec.

A quick example of this failure:

$({
  nodeType: 1,
  getAttribute: function () {
    return 'foo'
  },
  setAttribute: function (name, val) {
    console.assert(val === 'foo foo\u00A0bar', 'no-break space should not be treated as a class separator');
    console.log(JSON.stringify(val));
  }
}).addClass('foo\u00A0bar');

which produces an assertion failure and 'foo bar' logged to console.

fiddle

The problem is that classes.js uses rnotwhite which is (/\S+/g) and not an appropriate regular expression to use for splitting strings of classes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions