Skip to content

Commit

Permalink
[text-autospace] Extract getCharAdvances
Browse files Browse the repository at this point in the history
This patch extracts a JavaScript function from
`text-autospace-001.html`and share it with
`text-spacing-trim-combinations-001.html`.

Bug: 1463890, 1463891
Change-Id: I509fc6948d5242e970674f9e65ada48d27ab408c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4835287
Auto-Submit: Koji Ishii <[email protected]>
Reviewed-by: Lingqi Chi <[email protected]>
Commit-Queue: Lingqi Chi <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1191928}
  • Loading branch information
kojiishi authored and chromium-wpt-export-bot committed Sep 4, 2023
1 parent dc5ea80 commit 92313a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
25 changes: 25 additions & 0 deletions css/css-text/support/get-char-advances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

/**
* Returns an array of advances (widths) for all characters in the descendants
* of the specified element.
*/
function getCharAdvances(element) {
const range = document.createRange();
let advances = [];
for (const node of element.childNodes) {
const nodeType = node.nodeType;
if (nodeType === Node.TEXT_NODE) {
const text = node.nodeValue;
for (let i = 0; i < text.length; ++i) {
range.setStart(node, i);
range.setEnd(node, i + 1);
const bounds = range.getBoundingClientRect();
advances.push(bounds.width);
}
} else if (nodeType === Node.ELEMENT_NODE) {
advances = advances.concat(getCharAdvances(node));
}
}
return advances;
}
23 changes: 1 addition & 22 deletions css/css-text/text-autospace/text-autospace-001.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../support/get-char-advances.js"></script>
<style>
.test {
font-family: Ahem;
Expand All @@ -17,28 +18,6 @@
<div class="test" expect="3">国。XX国</div>
<div class="test first-line" expect="1,3,6,8">国国XX国<br>国国XX国</div>
<script>
const range = document.createRange();

// Get advances of all characters in the descendants.
function getCharAdvances(element) {
let advances = [];
for (const node of element.childNodes) {
const nodeType = node.nodeType;
if (nodeType === Node.TEXT_NODE) {
const text = node.nodeValue;
for (let i = 0; i < text.length; ++i) {
range.setStart(node, i);
range.setEnd(node, i + 1);
const bounds = range.getBoundingClientRect();
advances.push(bounds.width);
}
} else if (nodeType === Node.ELEMENT_NODE) {
advances = advances.concat(getCharAdvances(node));
}
}
return advances;
}

for (const element of document.getElementsByClassName('test')) {
const advances = getCharAdvances(element);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<link rel="help" href="https://drafts.csswg.org/css-text-4/#text-spacing-trim-property">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../support/get-char-advances.js"></script>
<style>
#container {
/* This test prefers fonts with the `halt` feature. */
Expand All @@ -17,25 +18,17 @@
<script>
const range = document.createRange();

function assertNode(textNode, expect) {
const text = textNode.nodeValue;
const results = [];
for (let i = 0; i < text.length; ++i) {
range.setStart(textNode, i);
range.setEnd(textNode, i + 1);
const rect = range.getBoundingClientRect();
results.push(rect.width >= 8 ? 'F' : 'H');
}
function assertNode(element, expect) {
const advances = getCharAdvances(element);
const results = advances.map(advance => advance >= 8 ? 'F' : 'H');
const result = results.join('');
assert_equals(result, expect);
}

for (const element of document.querySelectorAll('div[expect]')) {
const textNode = element.firstChild;
const text = textNode.nodeValue;
test(() => {
let expect = element.getAttribute('expect');
assertNode(textNode, expect);
}, `"${text}"`);
assertNode(element, expect);
}, `"${element.innerHTML}"`);
}
</script>

0 comments on commit 92313a3

Please sign in to comment.