Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS attribute selectors: how to escape newlines within attribute value?

I have a link with an href attribute that has carriage returns in its value (HTML cannot change):

<a href="
    http://google.com
">Testing</a> 

I originally thought a backslash can be used to escape a carriage return character (U+000D) when used inside of a string, but then read this in the CSS spec:

Any character (except a hexadecimal digit, linefeed, carriage return, or form feed) can be escaped with a backslash to remove its special meaning. For example, "\"" is a string consisting of one double quote.

Is this possible? I've tried using \a and \n as well without any luck.

http://jsfiddle.net/AHuvh/1/

like image 874
Adrift Avatar asked Dec 02 '13 21:12

Adrift


People also ask

How do you escape a CSS selector?

CSS escapes. CSS represents escaped characters in a different way. Escapes start with a backslash followed by the hexadecimal number that represents the character's hexadecimal Unicode code point value. If there is a following character that is not in the range A–F, a–f or 0–9, that is all you need.

What is the use of * In attribute selector?

The [attribute*="value"] selector is used to select elements whose attribute value contains a specified value. The following example selects all elements with a class attribute value that contains "te": Note: The value does not have to be a whole word!

When working with attribute selectors how can you select elements which contain a particular attribute value?

[attribute$=”value”] Selector: This selector is used to select all the elements whose attribute value ends with the specified value. The value doesn't need to be a whole word. [attribute*=”value”] Selector: This selector selects all the elements whose attribute value contains the specified value present anywhere.

How do I style a data attribute in CSS?

To use this selector, add a pipe character (|) before the equals sign. For example, li[data-years|="1900"] will select list items with a data-years value of “1900-2000”, but not the list item with a data-years value of “1800-1900”. Value ends with: attribute value ends with the selected term.

How to select elements with the specified attribute in CSS?

CSS [attribute|="value"] Selector. The [attribute|="value"] selector is used to select elements with the specified attribute starting with the specified value.

What is CSS selector in CSS with example?

CSS [attribute] Selector. The [attribute] selector is used to select elements with a specified attribute. The following example selects all <a> elements with a target attribute: Example. a [target] {. background-color: yellow; }. Try it Yourself ».

How to style HTML elements with specific attributes or attribute values?

It is possible to style HTML elements that have specific attributes or attribute values. The [attribute] selector is used to select elements with a specified attribute. The [attribute="value\ selector is used to select elements with a specified attribute and value. The following example selects all <a> elements with a target="_blank" attribute:

Which selector is used to select elements with an attribute value?

The [attribute~="value"] selector is used to select elements with an attribute value containing a specified word.


1 Answers

The reason why the selector that you have doesn't work:

a[href="\
    http://google.com\
"]

Is because:

First, inside a string, a backslash followed by a newline is ignored (i.e., the string is deemed not to contain either the backslash or the newline). Outside a string, a backslash followed by a newline stands for itself (i.e., a DELIM followed by a newline).

This is mentioned in the paragraph above the one you quote from section 4.1.3, and why it says that newlines are one of the characters that cannot be escaped with a backslash.

This makes it equivalent to the following selector:

a[href="    http://google.com"]

Which would only match your element if its attribute value did not contain any newlines.

That said, it is in fact possible to match an element by an attribute value containing newlines. However, CSS makes this a little complicated:

  1. To represent a newline in a CSS string, you need to use the \a escape sequence (case insensitive, up to 6 hex digits). This is stated in section 4.3.7, on strings (which are treated the same whether in a property value or an attribute selector):

    A string cannot directly contain a newline. To include a newline in a string, use an escape representing the line feed character in ISO-10646 (U+000A), such as "\A" or "\00000a". This character represents the generic notion of "newline" in CSS.

    \n has no special meaning in CSS; in fact, it's the same as n.

  2. If a space directly follows an escape sequence such as \a, that space must be doubled so that the space character directly following the escape sequence can be consumed. Refer to section 4.1.3 again, which states:

    Only one white space character is ignored after a hexadecimal escape. Note that this means that a "real" space after the escape sequence must be doubled.

    This may be why you couldn't get it to work even with \a. Understandably, it's an incredibly obscure rule, especially when working with whitespace characters.

This results in the following selector, with the styles applying correctly:

a[href="\a     http://google.com\a"]

Notice that there are five space characters between the first \a and the URL, whereas in comparison there are only four spaces following the newline in your HTML.

like image 62
BoltClock Avatar answered Oct 25 '22 15:10

BoltClock