Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /largest-contentful-paint/observe-css-generated-text.html - WPT Dashboard Interop Dashboard
<!doctype html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/largest-contentful-paint-helpers.js"></script>
<style>
#css-generated-text::before {
content: 'This is some text generated via css';
font-size: 12px;
}
#css-generated-text-attr::before {
content: attr(data-text);
font-size: 12px;
}
#css-generated-text-inline-elem::before {
content: 'This is some more text generated via css that should be displayed via a span tag';
font-size: 12px;
}
</style>
<body>
<script>
setup({"hide_test_state": true});
const runTest = (element, testName) => {
const elementId = element.id;
// The element should be atleast 12px in width
// and 100px across based on font size and text length.
const elemSizeLowerBound = 1200;
promise_test(t => {
return new Promise((resolve, reject) => {
assert_implements(window.LargestContentfulPaint,
"LargestContentfulPaint is not implemented");
const observer = new PerformanceObserver(resolve);
observer.observe({ type: 'largest-contentful-paint' });
beforeRender = performance.now();
document.body.appendChild(element);
step_timeout(() => {
reject(new Error('timeout, LCP candidate not detected'));
}, 1000)
}).then(entryList => {
assert_equals(entryList.getEntries().length, 1);
const entry = entryList.getEntries()[0];
checkTextEntry(entry, elementId, elemSizeLowerBound, beforeRender);
});
}, testName);
}
const cssGeneratedTextElem = document.createElement('p');
cssGeneratedTextElem.id = 'css-generated-text';
runTest(cssGeneratedTextElem,
"CSS generated text is observable as a LargestContentfulPaint candidate");
const cssGeneratedTextAttrElem = document.createElement('p');
cssGeneratedTextAttrElem.id = 'css-generated-text-attr';
cssGeneratedTextAttrElem.setAttribute('data-text',
'This is some text generated using content:attr() via css');
runTest(cssGeneratedTextAttrElem,
"Text generated with CSS using content:attr() is observable as a LargestContentfulPaint candidate");
const cssGeneratedTextAttrInlineElemBlockWrapper = document.createElement('div');
cssGeneratedTextAttrInlineElemBlockWrapper.id = 'css-generated-text-inline-elem-block-wrapper';
const cssGeneratedTextInlineElem = document.createElement('span');
cssGeneratedTextInlineElem.id = 'css-generated-text-inline-elem';
cssGeneratedTextAttrInlineElemBlockWrapper.appendChild(cssGeneratedTextInlineElem);
runTest(cssGeneratedTextAttrInlineElemBlockWrapper,
"CSS generated text on a inline element is observable as a LargestContentfulPaint candidate");
</script>
</body>