Skip to content

Fragment delimiters at column 0 are not removed from the displayed source #165

Description

@javier-godoy

A begin-block / end-block delimiter that starts at column 0 is blanked but its line is not removed, leaving a stray empty line in the displayed source. Delimiters that are indented (which is the case for every fragment in the current demos) are removed correctly, which is why this has gone unnoticed.

Steps to reproduce

Add a fragment to a demo source with the delimiters unindented:

public class SampleDemo extends Div {
  public SampleDemo() {
// begin-block first
    Div first = new Div(new Text("First"));
    add(first);
// end-block
    add(other);
  }
}

Expected (same as when the delimiters are indented):

    Div first = new Div(new Text("First"));
    add(first);
    add(other);

Actual — one blank line for each delimiter:

    Div first = new Div(new Text("First"));

    add(other);

If the delimiter is additionally preceded by a blank line, the result is two consecutive blank lines instead of one.

Cause

process() clears the delimiter's comment node and then calls trimEnd(i-1) to remove the newline and indentation preceding it, in code-viewer.ts:

node.textContent = node.textContent.replaceAll(/\n[\t\x20]+$/g, '');

The [\t\x20]+ requires at least one space or tab after the newline, so the rule implemented is "remove the delimiter's line if it is indented" rather than "remove the delimiter's line". When the delimiter is at column 0 the preceding text node is exactly "\n", nothing matches, and the newline survives.

Suggested fix

Make the indentation optional:

-node.textContent = node.textContent.replaceAll(/\n[\t\x20]+$/g, '');
+node.textContent = node.textContent.replaceAll(/\n[\t\x20]*$/g, '');

The match stays anchored at $, so this removes exactly the delimiter's own newline and never a blank line written by the author. It is a no-op for every indented delimiter, i.e. for all fragments currently in the repository.

Notes

This is independent of #98, but it becomes unavoidable there: CSS has no line comments and rules normally start at column 0, so every CSS fragment delimiter hits this path.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions