W3C validator issue in magento product view page

1.2k views Asked by At

I have error in product view page on w3c validator

my product is downloadable product. i have a custom option for that product enter image description here

when i validate the test-product page in w3c validator it shows a error like this

there is no attribute "price"

…" id="options_21_2" value="27" price="0" />

Error line:

<ul id="options-21-list" class="options-list"><li><input type="checkbox" class="checkbox product-custom-option" onclick="opConfig.reloadPrice()" name="options[21][]" id="options_21_2" value="27" price="0" /><span class="label"><label for="options_21_2">Test Product</label></span></li></ul> 

help to fix this issue.

1

There are 1 answers

2
Jürgen Thelen On BEST ANSWER

Background

The problem here is, that price is a custom attribute. Even though it's usable by almost all browsers per JavaScript the way you posted it, it's not a valid (X)HTML attribute, like id or name are, for example.

The W3C validator validates your source code against the DTD (Document Type Definition) found in the <!DOCTYPE .. > declaration of your document.

Magento CE/EE 1.x versions use a XHTML 1.0 Strict DTD by default.

A DTD declares which rules a document must follow to be valid for the given document type. It defines which element types are allowed, which attributes a specific element can have, which entities can be used, etc.

If you check the linked DTD above, you'll see that there's no price attribute defined anywhere in the file.

That's why the W3C validator rightfully complains .. there is no attribute "price".

What can you do?

Mainly the following three things come to my mind on about how to handle such situation:

Ignore after double checking

You could simply ignore this (and only this) specific kind of W3C validation errors.

I guess that's what most devs do with ".. there is no attribute attr_name" validation errors when they already double checked, that it's a custom attribute really in use and only failing W3C validation (using a pre-HTML5 DTD), but working completely fine otherwise.

Extend the DTD

You could extend the XHTML 1.0 Strict DTD, adding custom attributes to specific elements.

Example on how to add a custom price attribute for input elements, using an internal subset:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
[
<!ATTLIST input price CDATA #IMPLIED>
]
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Test</title>
</head>
<body>
<p>
<input type="checkbox" class="checkbox product-custom-option" onclick="opConfig.reloadPrice()" 
name="options[21][]" id="options_21_2" value="27" price="0" />
</p>
</body>
</html>

Containing this internal subset, the W3C validator will validate without errors.

But, most major browsers will render an ugly ]> as a result, when internal subsets come into play.

Maybe, because they don't support nested tags (at all, or correctly), or maybe they switch to their hardwired DTDs as soon as they found an official one in the <!DOCTYPE .. >, I can't tell exactly.

To avoid the ]>, you could build a custom DTD, using the original DTD as a base, extend it with custom attributes and change the !<DOCTYPE .. > to use that custom DTD.

The crux with custom DTDs is, even though it's technically correct and the browsers won't render that ugly ]> anymore, you also can't use the W3C validator anymore. It doesn't support custom DTDs.

So, if W3C compliance is a must, your only choice is to stay with internal subsets. But then you still need to get rid of the ugly ]> somehow. And to achieve this, you could use some CSS, e.g. similiar to this:

html {
    color: transparent;
}

Be aware though, that extending DTDs can result in lots of work. You'll need to extend all element types where your custom attribute could appear. And you'd need to do this for each custom attribute, of course.

Use HTML5 data-* attributes

You could rewrite your Magento templates to use HTML5 and its data-* attributes, a way where you only have to prefix custom attribute names with data- to make them perfectly valid.

But since fully transferring Magento 1.x from XHTML 1.0 Strict to HTML5 would result in tons of complex work, I don't really consider this an option.

Afaik, even Magento 2.x will not switch to HTML5, but continue to use XHTML 1.0 Strict.

Maybe for the very same reason^^