Skip to content

Commit

Permalink
Fixed wrong tailored text generation in getTailoredTextOrDefault (mic…
Browse files Browse the repository at this point in the history
…rosoft#119)

* Fixed wrong tailored text generation in getTailoredTextOrDefault

* skip if length of the text is lower then tail

* Enabled all tests

* Changed way to identify if string is longer than ellipses

* Fixed condition

* Extended tests
  • Loading branch information
AleksSavelev authored Feb 14, 2024
1 parent d2b7e4e commit 4929cd3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 6.1.0
* Fixed wrong tailored text generation in getTailoredTextOrDefault

## 6.0.3
* Update powerbi-visuals-utils-testutils to 6.0.3

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-utils-formattingutils",
"version": "6.0.3",
"version": "6.1.0",
"description": "FormattingUtils",
"main": "lib/src/index.js",
"module": "lib//src/index.js",
Expand Down
11 changes: 7 additions & 4 deletions src/textMeasurementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,20 @@ export function getTailoredTextOrDefault(textProperties: TextProperties, maxWidt
ensureDOM();

const strLength: number = textProperties.text.length;

if (strLength === 0) {
return textProperties.text;
}

let width: number = measureSvgTextWidth(textProperties);

if (width < maxWidth) {
return textProperties.text;
}

const ellipsesWidth: number = measureSvgTextWidth(textProperties, ellipsis);
if (ellipsesWidth >= width) {
return textProperties.text;
}

// Create a copy of the textProperties so we don't modify the one that's passed in.
const copiedTextProperties = Prototype.inherit(textProperties);

Expand All @@ -297,7 +300,7 @@ export function getTailoredTextOrDefault(textProperties: TextProperties, maxWidt
let i = ellipsis.length;

while (min <= max) {
// num | 0 prefered to Math.floor(num) for performance benefits
// num | 0 preferred to Math.floor(num) for performance benefits
i = (min + max) / 2 | 0;

copiedTextProperties.text = text.substring(0, i);
Expand All @@ -322,7 +325,7 @@ export function getTailoredTextOrDefault(textProperties: TextProperties, maxWidt
i--;
}

return text.substring(ellipsis.length, i) + ellipsis;
return textProperties.text.substring(0, i - ellipsis.length) + ellipsis;
}

/**
Expand Down
67 changes: 53 additions & 14 deletions test/textMeasurementServiceTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,17 @@ describe("Text measurement service", () => {
});

describe("getTailoredTextOrDefault", () => {
it("without ellipsis", () => {
const defaultTextProperties: TextProperties = {
fontFamily: "Arial",
fontSize: "11px",
fontWeight: "700",
fontStyle: "italic",
whiteSpace: "nowrap",
fontVariant: "normal"
}
it("long text without ellipsis", () => {
let properties: TextProperties = {
fontFamily: "Arial",
fontSize: "11px",
fontWeight: "700",
fontStyle: "italic",
whiteSpace: "nowrap",
fontVariant: "normal",
...defaultTextProperties,
text: "PowerBI rocks!",
};

Expand All @@ -252,14 +255,9 @@ describe("Text measurement service", () => {
expect(properties).toEqual(originalProperties);
});

it("with ellipsis", () => {
it("long text with ellipsis", () => {
let properties: TextProperties = {
fontFamily: "Arial",
fontSize: "11px",
fontWeight: "700",
fontStyle: "italic",
whiteSpace: "nowrap",
fontVariant: "normal",
...defaultTextProperties,
text: "PowerBI rocks!",
};

Expand All @@ -271,6 +269,47 @@ describe("Text measurement service", () => {
expect(stringExtensions.startsWithIgnoreCase(text, "Pow")).toBeTruthy();
expect(properties).toEqual(originalProperties);
});

it("long text replaced with ellipsis", () => {
const MAX_WIDTH = 0;
const ORIGINAL_TEXT = "PowerBI rocks!";
const EXPECTED_TEXT = "...";
const properties: TextProperties = {
...defaultTextProperties,
text: ORIGINAL_TEXT,
};

const result = textMeasurementService.getTailoredTextOrDefault(properties, MAX_WIDTH);

expect(result).toEqual(EXPECTED_TEXT);
})

it("choose best text by width", () => {
const MAX_WIDTH = 3;
const ORIGINAL_TEXT = "..m";
const EXPECTED_TEXT = "...";
const properties: TextProperties = {
...defaultTextProperties,
text: ORIGINAL_TEXT,
};

const result = textMeasurementService.getTailoredTextOrDefault(properties, MAX_WIDTH);

expect(result).toEqual(EXPECTED_TEXT);
})

it("short string without ellipsis", () => {
const MAX_WIDTH = 3;
const ORIGINAL_TEXT = "1";

const properties: TextProperties = {
...defaultTextProperties,
text: ORIGINAL_TEXT,
}
const result = textMeasurementService.getTailoredTextOrDefault(properties, MAX_WIDTH);

expect(result).toEqual(ORIGINAL_TEXT);
})
});

describe("svgEllipsis", () => {
Expand Down

0 comments on commit 4929cd3

Please sign in to comment.