Vue.js produces excessive white spaces in the generated HTML

I am struggling with a problem regarding the rendering of strings using Vue. At the moment, if an HTML tag is opened and closed on different lines like this:

<span class="description">
    {{ text }}
</span>

it ends up being displayed as follows:

<span class="description">
            text
          </span>

This results in

document.querySelector('.description').textContent
returning a string that contains unnecessary white spaces. For example:

"
          Text
        "

I discovered that if you open and close the HTML tag containing the string on the same line, the issue disappears. For instance:

<span class="description">{{ text }}</span>

Question: Is there a way to instruct Vue to render text without extra spaces without reformatting all actual HTML code, which means keeping tags opened and closed on separate lines? (I'm not the only one working on this project) Alternatively, is there a method to enforce formatting as shown in the last example? We currently use ESLint and Prettier, but I haven't been able to find anything that fits my specific scenario.

Thank you!

More details: Why do these spaces matter to me? In our selenium tests, we occasionally locate elements by text, and the presence of these spaces makes it challenging. Additionally, while I recognize that I could use CSS selectors, some elements lack unique selectors, so I rely on locating them by text.

Package.json

  // Dependencies list here

.prettierrc

{
  "trailingComma": "none",
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true,
  "htmlWhiteSpaceSensitivity": "ignore"
}

.eslintrc.js

// Configuration for ESLint goes here

Answer №1

Perhaps your specific situation is more complex, but when dealing with tags that do not include additional markup, I recommend using v-text whenever you can:

<div class="description" v-text="text" />

Take note of the self-closing <div /> which is not mandatory but emphasizes that this element does not have any internal markup.

Answer №2

One way to eliminate white spaces is by using the trim method in JavaScript.

let str = '   sample ';

console.log(str.trim(), str);

let desc = document.getElementsByClassName('description')[0];

console.log(desc.textContent, desc.textContent.trim());
<span class="description">
            text
          </span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmit a data element from the user interface to the server side without relying on the

I have developed a MEAN stack application. The backend of the application includes a file named api.js: var express = require('express') var router = express.Router(); var body = 'response.send("hello fixed")'; var F = new Function (" ...

Error Encountered in Vue.js when Trying to Access a Nested

Below is a snippet of my route code from app.js let routes = [{ path: "/dashboard", component: require("./components/Dashboard.vue") }, { path: "/tour", component: require("./components/Index.vue"), children: [{ name: &apos ...

Turn off dolly feature in CameraControls

I'm working with a Jupiter component that displays a 3D model of Jupiter. I'm facing an issue where I can't find a way to disable zoom when the user scrolls. import * as THREE from "three"; import React, { useRef, useEffect } from ...

Incorporate an assortment of facial features into BufferGeometry using three.js

If I have a BufferGeometry, I can easily assign its vertices using an array of type Float32Array with the following code snippet: geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); However, is there a way to set the f ...

Is it possible to generate an array of all matches found by a regular expression

I am trying to utilize the match-all package in order to match CSS selectors and generate an array of all of them. Here is the code snippet. const matchAll = require("match-all"); let s = `.u-br, .u-nr { blah blah } .u-tr { blah .blah }`; consol ...

Skip using bootstrap-vue icons by utilizing the Webpack IgnorePlugin

After analyzing with Webpack bundle analyzer, I discovered that the icons from the bootstrap-vue package are a whopping 535kb in size. Knowing this, I've decided not to utilize them in my project. I attempted to exclude the entire package using a web ...

Employing JavaScript to set a variable that changes dynamically

In my code, I have a functionality that allows for changing different dropdowns with similar ending IDs. In other parts of the code, I have assigned variables rl and rl_extra as well as rs and rs_extra. Now, when the var prefix is determined to be either ...

Is there a way to determine a path connecting 3D coordinates?

Understanding this concept is better with visual aids. I have a set of green points located here: https://i.sstatic.net/V8VEC.png My goal is to determine several points along the red line shown below: https://i.sstatic.net/b26AK.png Although this illus ...

The div is incorrect and causing the label and input to move in the wrong direction

Whenever I try to adjust the position of the current-grade-name-input, the entire grade-point-input moves along with it inexplicably. /* final grade calculator: (wanted-grade - (current-grade * (1 - final%))) / final% Ex: have a 80 an ...

What methods work effectively for adjusting the nvd3 x domain range to accommodate fluctuating time intervals?

What is the most efficient method for setting the x domain of an nvd3 chart to display a specific time period, such as the last hour, in a real-time line chart? Essentially, I need to continuously add new data points (y values with corresponding timestamps ...

How can one address the issue of undefined data within table cells?

I have encountered an issue while reading and displaying an XML file on a webpage using a JavaScript script. The problem arises when the page loads, and the information inside the cells of the table shows as "UNDEFINED". The intended display should include ...

What are the different kinds of properties that can be used in Vue.js

When working with Javascript, I can create arrays that hold different types of data elements like: var ex = ['name', 12, true]; console.log(ex); In Vue JS, when defining props for a component within a single file template, I have the option to ...

When new text is added to Div, the first line is not displayed

One of the divs on my page has an ID of "TrancriptBox" and contains multiple lines of text. When I scroll through it on my iPad, everything works fine. However, if I scroll and then change the text within that div, it doesn't display the first line o ...

The confusion surrounding navigation in Angular's single-page applications

I'm struggling to grasp how Angular handles routing in my single-page application. Here's my issue: When I type in the url: localhost:3000/streams/ I expect the 'streams' page to load. My understanding was this: My express serve ...

Moving information from Ajax to PHP

I'm experiencing an issue with sending data from AJAX to PHP on the same site, "testpage.php". The PHP script doesn't seem to be displaying the data being sent. Using jQuery/Ajax: <script src="http://code.jquery.com/jquery-latest.js" type="t ...

discord.js embed message not displaying text correctly in Upper and Lower case

I created a custom "test" command and I want it to be case-insensitive so that it works regardless of whether the user types -test, -Test, -TEST, etc. Currently, it only responds to lowercase -test commands. I tried changing the toLowerCase(); method to to ...

Leverage the power of JavaScript by combining it with the .on and .editableselect methods

I am facing an issue with a select input that I am trying to make both editable and trigger an ajax request using the on change JS function. However, only one of the functions is working as expected because I have used the same id for the select input twic ...

Store the JSON reply as a fixed variable

Recently, I have been delving into ReactJS and I've encountered a challenge of saving a JSON array as a 'const'. I have attempted the following approach: fetch(url) .then(response => response.json()) .then(json => { this.setSt ...

Begin a TypeScript project within the IntelliJ IDEA 2019.2 Community edition

Creating a TypeScript project in IntelliJ IDEA 2019.2 Community edition I'm trying to setting up a TypeScript project in IntelliJ IDEA 2019.2 Community edition so I can easily navigate through the classes, but I can't seem to find the option in ...

passing data from the view to the controller

When I choose an option from the dropdown menu, the selected value is not being passed to the controller action method. Although the selected value is binding in Ajax, it is not binding in the controller action method. Check out our page <div class="ro ...