Open Bug 1921672 Opened 1 year ago Updated 1 year ago

Undeclared variable can be assigned in strict mode if html element with same id

Categories

(Core :: DOM: Core & HTML, task, P3)

Firefox 130
task

Tracking

()

UNCONFIRMED

People

(Reporter: FormularSumo, Unassigned)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0

Steps to reproduce:

Run the following test case code

<!DOCTYPE html>
<html>
<body>
<div id='test'></div>
<script>
"use strict";
test = "text";
console.log(test)
</script>
</body>
</html>

Actual results:

Firefox allows the variable test to be assigned without being declared using a keyword. This is because test is also the id of a html element, and prior to being (re-)assigned is a reference to that element. After (re-)assigning test, it can be set to any other value such as in this case "text", even though it hasn't been declared in the Javascript code.

Expected results:

When strict mode is on, references to html elements shouldn't be able to be (re-)assigned without a variable declaration. This is the current behaviour in Chromium-based browsers, which is what led me to notice this. I'd forgotten to use a "let" declaration in a project, and Firefox (and Webkit-based browsers) ran the code fine in strict mode, while Chromium ones returned "Uncaught ReferenceError: test is not defined", which I think is the correct behaviour here.

This is probably better placed into "DOM: Core & HTML", because it involves Named Properties which is a HTML thing.

Component: JavaScript Engine → DOM: Core & HTML

Peter, who would be the right person to look into this?

Flags: needinfo?(peterv)

So I think Chrome is not following the spec here, these properties are writable and configurable https://webidl.spec.whatwg.org/#named-properties-object-getownproperty:~:text=Set%20desc.%5B%5BWritable%5D%5D%20to%20true%20and%20desc.%5B%5BConfigurable%5D%5D%20to%20true. File a spec bug if you think this behaviour should change.

I'll leave this open so we can add a wpt.

Severity: -- → S3
Type: defect → task
Flags: needinfo?(peterv)
Priority: -- → P3
You need to log in before you can comment on or make changes to this bug.