Commit 788931ce authored by Nicolas Chevobbe's avatar Nicolas Chevobbe
Browse files

Bug 1825208 - [devtools] Fix pretty printing inline script on first line....

Bug 1825208 - [devtools] Fix pretty printing inline script on first line. r=devtools-reviewers,ochameau.

If a `<script>` is on the first line of the HTML file, we were miscalculating
its position in the HTML file because there is not previous line breaks.
A test case is added to fix this.

Differential Revision: https://phabricator.services.mozilla.com/D173929
parent ca983070
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -169,12 +169,12 @@ async function prettyPrintHtmlFile({
    // Here we want to get the index of the last line break before the script tag.
    // In allLineBreaks, this would be the item at (script tag line - 1)
    // Since sourceInfo.sourceStartLine is 1-based, we need to get the item at (sourceStartLine - 2)
    const previousLineBreakIndexInHtmlText =
    const indexAfterPreviousLineBreakInHtml =
      sourceInfo.sourceStartLine > 1
        ? allLineBreaks[sourceInfo.sourceStartLine - 2].index
        ? allLineBreaks[sourceInfo.sourceStartLine - 2].index + 1
        : 0;
    const startIndex =
      previousLineBreakIndexInHtmlText + sourceInfo.sourceStartColumn + 1;
      indexAfterPreviousLineBreakInHtml + sourceInfo.sourceStartColumn;
    const endIndex = startIndex + sourceInfo.sourceLength;
    const scriptText = htmlFileText.substring(startIndex, endIndex);
    DevToolsUtils.assert(
+29 −0
Original line number Diff line number Diff line
@@ -145,6 +145,35 @@ add_task(async function() {
  ok(true, "…at the expected location");
});

add_task(async function prettyPrintSingleLineDataUrl() {
  const TEST_URL = `data:text/html,<meta charset=utf8><script>{"use strict"; globalThis.foo = function() {}}</script>`;
  const PRETTY_PRINTED_URL = `${TEST_URL}:formatted`;
  const dbg = await initDebuggerWithAbsoluteURL(TEST_URL);

  await selectSource(dbg, TEST_URL);
  clickElement(dbg, "prettyPrintButton");

  const prettySource = await waitForSource(dbg, PRETTY_PRINTED_URL);
  await waitForSelectedSource(dbg, prettySource);
  const prettyPrintedSource = findSourceContent(dbg, PRETTY_PRINTED_URL);

  ok(prettyPrintedSource, "Pretty-printed source exists");

  info("Check that the HTML file was pretty-printed as expected");
  const expectedPrettyHtml = `<meta charset=utf8><script>
{
  'use strict';
  globalThis.foo = function () {
  }
}
</script>`;
  is(
    prettyPrintedSource.value,
    expectedPrettyHtml,
    "HTML file is pretty printed as expected"
  );
});

/**
 * Return the expected pretty-printed HTML. Lines starting with ➤ indicate breakable
 * lines for easier maintenance.
+1 −1
Original line number Diff line number Diff line
@@ -824,7 +824,7 @@ async function selectSource(dbg, url, line, column) {
    createLocation({ source, line, column }),
    { keepContext: false }
  );
  return waitForSelectedSource(dbg, url);
  return waitForSelectedSource(dbg, source);
}

async function closeTab(dbg, url) {
+3 −1
Original line number Diff line number Diff line
@@ -2206,7 +2206,9 @@ class ThreadActor extends Actor {
          ...content.substring(0, scriptStartOffset).matchAll("\n"),
        ];
        const startLine = 1 + allLineBreaks.length;
        const startColumn = scriptStartOffset - allLineBreaks.at(-1).index - 1;
        const startColumn =
          scriptStartOffset -
          (allLineBreaks.length ? allLineBreaks.at(-1).index - 1 : 0);

        // Don't create a source if we already found one for this script
        if (