summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/rendering/widgets/field-sizing-textarea.html
blob: 114d60c72d02329c8572d1308e86c5dc37bd55c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
textarea {
  font-family: monospace;
}

.disable-default {
  field-sizing: content;
}

.small-placeholder {
  font-size: 32px;
}
.small-placeholder::placeholder {
  font-size: 16px;
}

.large-placeholder {
  font-size: 20px;
}
.large-placeholder::placeholder {
  font-size: 40px;
}
</style>
<body>
<div id="container"></div>
<script>
const DISABLE = 'class="disable-default"';
const LONG_TEXT = 'The quick brown fox jumps over the lazy dog.';

function addElements(source) {
  const container = document.querySelector('#container');
  container.innerHTML = source + source;
  container.lastElementChild.classList.add('disable-default');
  return {
    fixed: container.firstElementChild,
    content: container.lastElementChild
  };
}

function addTwoElements(source1, source2) {
  const container = document.querySelector('#container');
  container.innerHTML = source1 + source2;
  return {
    e1: container.firstElementChild,
    e2: container.lastElementChild
  };
}

test(() => {
  let pair = addElements('<textarea></textarea>');
  // Historically a <textarea> has approximately 20ch x 2lh size by default.
  // A <textarea> with field-sizing:content must be samller than the default.
  assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addTwoElements(`<textarea ${DISABLE}></textarea>`, `<textarea ${DISABLE}>foo</textarea>`);
  assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);
  assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);
}, 'Empty value');

test(() => {
  let pair = addElements('<textarea cols="10" rows="3"></textarea>');
  assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);
}, 'Empty value with cols/rows');

test(() => {
  let pair = addElements(`<textarea>${LONG_TEXT}</textarea>`);
  assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  pair = addElements(`<textarea placeholder="${LONG_TEXT}"></textarea>`);
  assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
}, 'Auto width and auto height with a long text');

test(() => {
  let pair = addElements('<textarea style="width:20ch; height:2lh"></textarea>');
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="width:20ch; height:2lh">${LONG_TEXT}</textarea>`);
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="width:20ch; height:2lh" placeholder="${LONG_TEXT}"></textarea>`);
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);
}, 'Explicit width and heigth');

test(() => {
  let pair = addElements('<textarea style="width:20ch"></textarea>');
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="width:20ch">${LONG_TEXT}</textarea>`);
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_greater_than(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="width:20ch" placeholder="${LONG_TEXT}"></textarea>`);
  assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_greater_than(pair.content.offsetHeight, pair.fixed.offsetHeight);
}, 'Explicit width and auto height');

test(() => {
  let pair = addElements('<textarea style="height:2lh"></textarea>');
  assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="height:2lh">${LONG_TEXT}</textarea>`);
  assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);

  pair = addElements(`<textarea style="height:2lh" placeholder="${LONG_TEXT}"></textarea>`);
  assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);
  assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);
}, 'Explicit height and auto width');

test(() => {
  let pair = addTwoElements(`<textarea class="disable-default small-placeholder"></textarea>`,
                            `<textarea class="disable-default small-placeholder" placeholder="foo"></textarea>`);
  assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);
  assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);
}, 'Text caret is taller than the placeholder');

test(() => {
  let pair = addTwoElements(`<textarea class="disable-default large-placeholder"></textarea>`,
                            `<textarea class="disable-default large-placeholder" placeholder="foo"></textarea>`);
  assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth, 'width');
  assert_less_than(pair.e1.offsetHeight, pair.e2.offsetHeight, 'height');
}, 'Text caret is shorter than the placeholder');

test(() => {
   const container = document.querySelector('#container');
   container.innerHTML = '<textarea></textarea>';
   const element = container.firstChild;
   const w = element.offsetWidth;
   const h = element.offsetHeight;
   element.classList.add('disable-default');
   assert_less_than(element.offsetWidth, w);
   assert_less_than(element.offsetHeight, h);
}, 'Update field-sizing property dynamically');
</script>
</body>