Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rudimentary innerText function. #3584

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 125 additions & 0 deletions lib/jsdom/living/domparsing/InnerText-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"use strict";

const { getDeclarationForElement } = require("../helpers/style-rules.js");
const { ELEMENT_NODE, TEXT_NODE } = require("../node-type");
const { getChildNodes } = require("./parse5-adapter-serialization");

function getComputedStyle(element, property) {
return getDeclarationForElement(element).getPropertyValue(property);
}

function isRendered(element) {
return (
!element.getAttributeNS(null, "hidden") &&
getComputedStyle(element, "display") !== "none" &&
getComputedStyle(element, "visibility") !== "hidden" &&
element.tagName !== "IMG" &&
element.tagName !== "NOSCRIPT"
);
}

function renderedTextCollect(node) {
let items = [];

if (node.nodeType === ELEMENT_NODE && !isRendered(node)) {
return items;
}
for (const childNode of getChildNodes(node)) {
items.push(...renderedTextCollect(childNode));
}
if (node.nodeType === TEXT_NODE) {
let text = node.textContent || "";
if (!node.parentElement) {
return items;
}
switch (getComputedStyle(node.parentElement, "text-transform")) {
case "uppercase": {
text = text.toUpperCase();
break;
}
case "lowercase": {
text = text.toLowerCase();
break;
}
case "capitalize": {
text = text
.split(" ")
.map(word => word.length >= 1 ? word[0].toUpperCase() + word.slice(1) : word)
.join(" ");
break;
}
}
text = text.trim();
if (text) {
items.push(text);
}
} else if (node.nodeType === ELEMENT_NODE) {
if (node._localName === "br") {
items.push("\n");
}
if (
getComputedStyle(node, "display") === "table-cell" &&
node.parentElement?.lastChild !== node
) {
items.push("\u0009");
}
if (
getComputedStyle(node, "display") === "table-row" &&
node.parentElement?.lastChild !== node
) {
items.push("\n");
}
if (node._localName === "p") {
items = [2, ...items, 2];
}

if (["block", "flex", "table", "grid"].includes(node.style.display)) {
items = [1, ...items, 1];
}
}
return items;
}

// https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute
exports.implementation = class InnerTextImpl {
get innerText() {
if (!isRendered(this)) {
return this.textContent;
}
let results = [];
for (const node of getChildNodes(this)) {
const current = renderedTextCollect(node);
results.push(...current);
}
results = results.filter(Boolean);
while (typeof results[0] === "number") {
results.shift();
}
while (typeof results.at(-1) === "number") {
results.pop();
}

let currentRun = [];
const reducedResults = [];

for (const result of results) {
if (typeof result === "number") {
currentRun.push(result);
} else {
if (currentRun.length > 0) {
reducedResults.push(Math.max(currentRun));
}
currentRun = [];
reducedResults.push(result);
}
}

results = reducedResults.map(result => typeof result === "number" ? "\n".repeat(result) : result);
return results.join("\n");
}
set innerText(text) {
text = text.replace(/[&<>'"]/g, r => "&#" + r.charCodeAt(0) + ";");
text = text.replace("\n", "<br />");
this.innerHTML = text;
}
};
5 changes: 5 additions & 0 deletions lib/jsdom/living/domparsing/InnerText.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface mixin InnerText {
[CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
};

HTMLElement includes InnerText;
2 changes: 2 additions & 0 deletions lib/jsdom/living/nodes/HTMLElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const MouseEvent = require("../generated/MouseEvent");
const ElementCSSInlineStyleImpl = require("./ElementCSSInlineStyle-impl").implementation;
const GlobalEventHandlersImpl = require("./GlobalEventHandlers-impl").implementation;
const HTMLOrSVGElementImpl = require("./HTMLOrSVGElement-impl").implementation;
const InnerTextImpl = require("../domparsing/InnerText-impl").implementation;
const { firstChildWithLocalName } = require("../helpers/traversal");
const { isDisabled } = require("../helpers/form-controls");
const { fireAnEvent } = require("../helpers/events");
Expand Down Expand Up @@ -154,6 +155,7 @@ class HTMLElementImpl extends ElementImpl {
mixin(HTMLElementImpl.prototype, ElementCSSInlineStyleImpl.prototype);
mixin(HTMLElementImpl.prototype, GlobalEventHandlersImpl.prototype);
mixin(HTMLElementImpl.prototype, HTMLOrSVGElementImpl.prototype);
mixin(HTMLElementImpl.prototype, InnerTextImpl.prototype);

module.exports = {
implementation: HTMLElementImpl
Expand Down
24 changes: 24 additions & 0 deletions test/api/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ describe("API: JSDOM class's methods", () => {

assert.deepEqual(dom.nodeLocation(node), undefined);
});
it("should return the correct text from innerText", () => {
const source = `<p id="source">
<style>
#source {
color: red;
}
#text {
text-transform: uppercase;
}
</style>
<span id="text">
Take a look at<br />
how this text<br />
is interpreted below.
</span>
<span style="display:none">HIDDEN TEXT</span>
</p>`;
const expectedResult = `TAKE A LOOK AT
HOW THIS TEXT
IS INTERPRETED BELOW.`;
const dom = new JSDOM(source);
const result = dom.window.document.getElementById("source").innerText;
assert.strictEqual(result, expectedResult);
});
});

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