Bug 1864168 - Part 6: Use 1-origin column number in ScriptLoader and nsIScriptElement. r=smaug,devtools-reviewers,hsivonen,ochameau

Also fix opSetScriptLineAndColumnNumberAndFreeze::mColumnNumber to point the
first character of the script source, instead of '>' of the start tag.

Differential Revision: https://phabricator.services.mozilla.com/D193374
This commit is contained in:
Tooru Fujisawa 2023-11-22 11:13:56 +00:00
parent 004788b76a
commit 78e7224834
10 changed files with 51 additions and 30 deletions

View File

@ -184,7 +184,8 @@ void HTMLScriptElement::FreezeExecutionAttrs(const Document* aOwnerDoc) {
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "HTML"_ns, OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceInvalidUri", params,
nullptr, u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
nullptr, u""_ns, GetScriptLineNumber(),
GetScriptColumnNumber().oneOriginValue());
}
} else {
AutoTArray<nsString, 1> params = {u"src"_ns};
@ -192,7 +193,8 @@ void HTMLScriptElement::FreezeExecutionAttrs(const Document* aOwnerDoc) {
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "HTML"_ns, OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceEmpty", params, nullptr,
u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
u""_ns, GetScriptLineNumber(),
GetScriptColumnNumber().oneOriginValue());
}
// At this point mUri will be null for invalid URLs.

View File

@ -8,6 +8,7 @@
#define mozilla_dom_ScriptLoadContext_h
#include "js/AllocPolicy.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
#include "js/CompileOptions.h" // JS::OwningCompileOptions
#include "js/experimental/JSStencil.h" // JS::FrontendContext, JS::Stencil, JS::InstantiationStorage
#include "js/RootingAPI.h"
@ -234,7 +235,7 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
RefPtr<CompileOrDecodeTask> mCompileOrDecodeTask;
uint32_t mLineNo;
uint32_t mColumnNo;
JS::ColumnNumberOneOrigin mColumnNo;
// Set on scripts and top level modules.
bool mIsPreload;

View File

@ -919,8 +919,8 @@ static bool CSPAllowsInlineScript(nsIScriptElement* aElement,
nsIContentSecurityPolicy::SCRIPT_SRC_ELEM_DIRECTIVE,
false /* aHasUnsafeHash */, aNonce, parserCreated, element,
nullptr /* nsICSPEventListener */, u""_ns,
aElement->GetScriptLineNumber(), aElement->GetScriptColumnNumber() + 1,
&allowInlineScript);
aElement->GetScriptLineNumber(),
aElement->GetScriptColumnNumber().oneOriginValue(), &allowInlineScript);
return NS_SUCCEEDED(rv) && allowInlineScript;
}
@ -2331,8 +2331,7 @@ nsresult ScriptLoader::FillCompileOptionsForRequest(
if (aRequest->GetScriptLoadContext()->mIsInline &&
aRequest->GetScriptLoadContext()->GetParserCreated() ==
FROM_PARSER_NETWORK) {
aOptions->setColumn(JS::ColumnNumberOneOrigin::fromZeroOrigin(
aRequest->GetScriptLoadContext()->mColumnNo));
aOptions->setColumn(aRequest->GetScriptLoadContext()->mColumnNo);
}
aOptions->setIsRunOnce(true);
aOptions->setNoScriptRval(true);
@ -3416,12 +3415,15 @@ void ScriptLoader::ReportErrorToConsole(ScriptLoadRequest* aRequest,
nsIScriptElement* element =
aRequest->GetScriptLoadContext()->GetScriptElement();
uint32_t lineNo = element ? element->GetScriptLineNumber() : 0;
uint32_t columnNo = element ? element->GetScriptColumnNumber() : 0;
JS::ColumnNumberOneOrigin columnNo;
if (element) {
columnNo = element->GetScriptColumnNumber();
}
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
"Script Loader"_ns, mDocument,
nsContentUtils::eDOM_PROPERTIES, message,
params, nullptr, u""_ns, lineNo, columnNo);
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "Script Loader"_ns, mDocument,
nsContentUtils::eDOM_PROPERTIES, message, params, nullptr, u""_ns, lineNo,
columnNo.oneOriginValue());
}
void ScriptLoader::ReportWarningToConsole(
@ -3430,11 +3432,14 @@ void ScriptLoader::ReportWarningToConsole(
nsIScriptElement* element =
aRequest->GetScriptLoadContext()->GetScriptElement();
uint32_t lineNo = element ? element->GetScriptLineNumber() : 0;
uint32_t columnNo = element ? element->GetScriptColumnNumber() : 0;
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
"Script Loader"_ns, mDocument,
nsContentUtils::eDOM_PROPERTIES, aMessageName,
aParams, nullptr, u""_ns, lineNo, columnNo);
JS::ColumnNumberOneOrigin columnNo;
if (element) {
columnNo = element->GetScriptColumnNumber();
}
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "Script Loader"_ns, mDocument,
nsContentUtils::eDOM_PROPERTIES, aMessageName, aParams, nullptr, u""_ns,
lineNo, columnNo.oneOriginValue());
}
void ScriptLoader::ReportPreloadErrorsToConsole(ScriptLoadRequest* aRequest) {

View File

@ -7,6 +7,7 @@
#ifndef nsIScriptElement_h___
#define nsIScriptElement_h___
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
#include "js/loader/ScriptKind.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h"
@ -158,11 +159,11 @@ class nsIScriptElement : public nsIScriptLoaderObserver {
uint32_t GetScriptLineNumber() { return mLineNumber; }
void SetScriptColumnNumber(uint32_t aColumnNumber) {
void SetScriptColumnNumber(JS::ColumnNumberOneOrigin aColumnNumber) {
mColumnNumber = aColumnNumber;
}
uint32_t GetScriptColumnNumber() { return mColumnNumber; }
JS::ColumnNumberOneOrigin GetScriptColumnNumber() { return mColumnNumber; }
void SetIsMalformed() { mMalformed = true; }
@ -299,7 +300,7 @@ class nsIScriptElement : public nsIScriptLoaderObserver {
/**
* The start column number of the script.
*/
uint32_t mColumnNumber;
JS::ColumnNumberOneOrigin mColumnNumber;
/**
* The "already started" flag per HTML5.

View File

@ -141,7 +141,8 @@ void SVGScriptElement::FreezeExecutionAttrs(const Document* aOwnerDoc) {
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "SVG"_ns, OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceInvalidUri", params,
nullptr, u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
nullptr, u""_ns, GetScriptLineNumber(),
GetScriptColumnNumber().oneOriginValue());
}
} else {
AutoTArray<nsString, 1> params = {isHref ? u"href"_ns : u"xlink:href"_ns};
@ -149,7 +150,8 @@ void SVGScriptElement::FreezeExecutionAttrs(const Document* aOwnerDoc) {
nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "SVG"_ns, OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceEmpty", params, nullptr,
u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
u""_ns, GetScriptLineNumber(),
GetScriptColumnNumber().oneOriginValue());
}
// At this point mUri will be null for invalid URLs.

View File

@ -53,6 +53,7 @@
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/UseCounter.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
using namespace mozilla;
using namespace mozilla::dom;
@ -487,7 +488,8 @@ nsresult nsXMLContentSink::CreateElement(
aNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_SVG)) {
if (nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(element)) {
sele->SetScriptLineNumber(aLineNumber);
sele->SetScriptColumnNumber(aColumnNumber);
sele->SetScriptColumnNumber(
JS::ColumnNumberOneOrigin::fromZeroOrigin(aColumnNumber));
sele->SetCreatorParser(GetParser());
} else {
MOZ_ASSERT(nsNameSpaceManager::GetInstance()->mSVGDisabled,
@ -1322,7 +1324,7 @@ nsXMLContentSink::ReportError(const char16_t* aErrorText,
parsererror.Append((char16_t)0xFFFF);
parsererror.AppendLiteral("parsererror");
rv = HandleStartElement(parsererror.get(), noAtts, 0, (uint32_t)-1, false);
rv = HandleStartElement(parsererror.get(), noAtts, 0, (uint32_t)-1, 0);
NS_ENSURE_SUCCESS(rv, rv);
rv = HandleCharacterData(aErrorText, NS_strlen(aErrorText), false);
@ -1332,7 +1334,7 @@ nsXMLContentSink::ReportError(const char16_t* aErrorText,
sourcetext.Append((char16_t)0xFFFF);
sourcetext.AppendLiteral("sourcetext");
rv = HandleStartElement(sourcetext.get(), noAtts, 0, (uint32_t)-1, false);
rv = HandleStartElement(sourcetext.get(), noAtts, 0, (uint32_t)-1, 0);
NS_ENSURE_SUCCESS(rv, rv);
rv = HandleCharacterData(aSourceText, NS_strlen(aSourceText), false);

View File

@ -7,6 +7,7 @@
#ifndef nsXMLContentSink_h__
#define nsXMLContentSink_h__
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
#include "mozilla/Attributes.h"
#include "nsContentSink.h"
#include "nsIXMLContentSink.h"

View File

@ -227,7 +227,8 @@ nsIContentHandle* nsHtml5TreeBuilder::createElement(
}
opSetScriptLineAndColumnNumberAndFreeze operation(
content, tokenizer->getLineNumber(),
tokenizer->getColumnNumber());
// NOTE: tokenizer->getColumnNumber() points '>'.
tokenizer->getColumnNumber() + 1);
treeOp->Init(mozilla::AsVariant(operation));
nsHtml5String type =
@ -555,7 +556,8 @@ nsIContentHandle* nsHtml5TreeBuilder::createElement(
}
opSetScriptLineAndColumnNumberAndFreeze operation(
content, tokenizer->getLineNumber(),
tokenizer->getColumnNumber());
// NOTE: tokenizer->getColumnNumber() points '>'.
tokenizer->getColumnNumber() + 1);
treeOp->Init(mozilla::AsVariant(operation));
nsHtml5String url =
@ -619,7 +621,9 @@ nsIContentHandle* nsHtml5TreeBuilder::createElement(
return nullptr;
}
opSetScriptLineAndColumnNumberAndFreeze operation(
content, tokenizer->getLineNumber(), tokenizer->getColumnNumber());
content, tokenizer->getLineNumber(),
// NOTE: tokenizer->getColumnNumber() points '>'.
tokenizer->getColumnNumber() + 1);
treeOp->Init(mozilla::AsVariant(operation));
if (aNamespace == kNameSpaceID_XHTML) {
// Although we come here in cases where the value of

View File

@ -39,6 +39,7 @@
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsTextNode.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
using namespace mozilla;
using namespace mozilla::dom;
@ -988,7 +989,8 @@ nsresult nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor* aBuilder,
nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(node);
if (sele) {
sele->SetScriptLineNumber(aOperation.mLineNumber);
sele->SetScriptColumnNumber(aOperation.mColumnNumber);
sele->SetScriptColumnNumber(
JS::ColumnNumberOneOrigin(aOperation.mColumnNumber));
sele->FreezeExecutionAttrs(node->OwnerDoc());
} else {
MOZ_ASSERT(nsNameSpaceManager::GetInstance()->mSVGDisabled,

View File

@ -28,7 +28,8 @@ interface nsIExpatSink : nsISupports
* present in aAtts.
* @param aAttsCount the number of elements in aAtts.
* @param aLineNumber the line number of the start tag in the data stream.
* @param aColumnNumber the column number of the start tag in the data stream.
* @param aColumnNumber the 0-origin column number of the start tag in the
* data stream.
*/
void HandleStartElement(in wstring aName,
[array, size_is(aAttsCount)] in wstring aAtts,