Skip to content

Enable the unicorn/prefer-dom-node-append ESLint plugin rule #15029

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

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"unicorn/no-useless-spread": "error",
"unicorn/prefer-at": "error",
"unicorn/prefer-date-now": "error",
"unicorn/prefer-dom-node-append": "error",
"unicorn/prefer-dom-node-remove": "error",
"unicorn/prefer-string-starts-ends-with": "error",

Expand Down
9 changes: 9 additions & 0 deletions examples/node/domstubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ DOMElement.prototype = {
this.setAttribute(name, value);
},

append: function DOMElement_append(...elements) {
const childNodes = this.childNodes;
for (const element of elements) {
if (!childNodes.includes(element)) {
childNodes.push(element);
}
}
},

appendChild: function DOMElement_appendChild(element) {
const childNodes = this.childNodes;
if (!childNodes.includes(element)) {
Expand Down
4 changes: 2 additions & 2 deletions examples/text-only/pdf2svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function buildSVG(viewport, textContent) {
text.setAttribute("transform", "matrix(" + tx.join(" ") + ")");
text.setAttribute("font-family", style.fontFamily);
text.textContent = textItem.str;
svg.appendChild(text);
svg.append(text);
});
return svg;
}
Expand All @@ -57,7 +57,7 @@ async function pageLoaded() {
const textContent = await page.getTextContent();
// building SVG and adding that to the DOM
const svg = buildSVG(viewport, textContent);
document.getElementById("pageContainer").appendChild(svg);
document.getElementById("pageContainer").append(svg);
// Release page resources.
page.cleanup();
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/chromium/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function updateObjectElement(elem) {
if (!iframe || !iframe.__inserted_by_pdfjs) {
iframe = createFullSizeIframe();
elem.textContent = "";
elem.appendChild(iframe);
elem.append(iframe);
iframe.__inserted_by_pdfjs = true;
}
iframe.src = getEmbeddedViewerURL(elem.data);
Expand Down
6 changes: 3 additions & 3 deletions extensions/chromium/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function renderBooleanPref(shortDescription, description, prefName) {
storageArea.set(pref);
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").appendChild(wrapper);
document.getElementById("settings-boxes").append(wrapper);

function renderPreference(value) {
checkbox.checked = value;
Expand All @@ -172,7 +172,7 @@ function renderEnumPref(shortDescription, prefName) {
storageArea.set(pref);
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").appendChild(wrapper);
document.getElementById("settings-boxes").append(wrapper);

function renderPreference(value) {
select.value = value;
Expand All @@ -189,7 +189,7 @@ function renderDefaultZoomValue(shortDescription) {
});
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").appendChild(wrapper);
document.getElementById("settings-boxes").append(wrapper);

function renderPreference(value) {
value = value || "auto";
Expand Down
54 changes: 27 additions & 27 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class AnnotationElement {
trigger = document.createElement("div");
trigger.style.height = container.style.height;
trigger.style.width = container.style.width;
container.appendChild(trigger);
container.append(trigger);
}

const popupElement = new PopupElement({
Expand All @@ -466,7 +466,7 @@ class AnnotationElement {
// Position the popup next to the annotation's container.
popup.style.left = container.style.width;

container.appendChild(popup);
container.append(popup);
}

/**
Expand Down Expand Up @@ -607,15 +607,15 @@ class LinkAnnotationElement extends AnnotationElement {
return this._renderQuadrilaterals("linkAnnotation").map(
(quadrilateral, index) => {
const linkElement = index === 0 ? link : link.cloneNode();
quadrilateral.appendChild(linkElement);
quadrilateral.append(linkElement);
return quadrilateral;
}
);
}

this.container.className = "linkAnnotation";
if (isBound) {
this.container.appendChild(link);
this.container.append(link);
}

return this.container;
Expand Down Expand Up @@ -828,7 +828,7 @@ class TextAnnotationElement extends AnnotationElement {
this._createPopup(image, this.data);
}

this.container.appendChild(image);
this.container.append(image);
return this.container;
}
}
Expand Down Expand Up @@ -1234,7 +1234,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
this._setBackgroundColor(element);
this._setDefaultPropertiesFromJS(element);

this.container.appendChild(element);
this.container.append(element);
return this.container;
}
}
Expand Down Expand Up @@ -1319,7 +1319,7 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
this._setBackgroundColor(element);
this._setDefaultPropertiesFromJS(element);

this.container.appendChild(element);
this.container.append(element);
return this.container;
}
}
Expand Down Expand Up @@ -1408,7 +1408,7 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
this._setBackgroundColor(element);
this._setDefaultPropertiesFromJS(element);

this.container.appendChild(element);
this.container.append(element);
return this.container;
}
}
Expand Down Expand Up @@ -1490,7 +1490,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
optionElement.setAttribute("selected", true);
addAnEmptyEntry = false;
}
selectElement.appendChild(optionElement);
selectElement.append(optionElement);
}

let removeEmptyEntry = null;
Expand Down Expand Up @@ -1595,7 +1595,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
const optionElement = document.createElement("option");
optionElement.textContent = displayValue;
optionElement.value = exportValue;
selectElement.appendChild(optionElement);
selectElement.append(optionElement);
}
if (selectElement.options.length > 0) {
selectElement.options[0].selected = true;
Expand Down Expand Up @@ -1668,7 +1668,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
this._setBackgroundColor(selectElement);
this._setDefaultPropertiesFromJS(selectElement);

this.container.appendChild(selectElement);
this.container.append(selectElement);
return this.container;
}
}
Expand Down Expand Up @@ -1734,7 +1734,7 @@ class PopupAnnotationElement extends AnnotationElement {
this.container.style.left = `${popupLeft}px`;
this.container.style.top = `${popupTop}px`;

this.container.appendChild(popup.render());
this.container.append(popup.render());
return this.container;
}
}
Expand Down Expand Up @@ -1781,7 +1781,7 @@ class PopupElement {
const title = document.createElement("h1");
title.dir = this.titleObj.dir;
title.textContent = this.titleObj.str;
popup.appendChild(title);
popup.append(title);

// The modification date is shown in the popup instead of the creation
// date if it is available and can be parsed correctly, which is
Expand All @@ -1796,7 +1796,7 @@ class PopupElement {
date: dateObject.toLocaleDateString(),
time: dateObject.toLocaleTimeString(),
});
popup.appendChild(modificationDate);
popup.append(modificationDate);
}

if (
Expand All @@ -1811,7 +1811,7 @@ class PopupElement {
popup.lastChild.className = "richText popupContent";
} else {
const contents = this._formatContents(this.contentsObj);
popup.appendChild(contents);
popup.append(contents);
}

if (!Array.isArray(this.trigger)) {
Expand All @@ -1826,7 +1826,7 @@ class PopupElement {
}
popup.addEventListener("click", this._hide.bind(this, true));

wrapper.appendChild(popup);
wrapper.append(popup);
return wrapper;
}

Expand All @@ -1845,9 +1845,9 @@ class PopupElement {
const lines = str.split(/(?:\r\n?|\n)/);
for (let i = 0, ii = lines.length; i < ii; ++i) {
const line = lines[i];
p.appendChild(document.createTextNode(line));
p.append(document.createTextNode(line));
if (i < ii - 1) {
p.appendChild(document.createElement("br"));
p.append(document.createElement("br"));
}
}
return p;
Expand Down Expand Up @@ -1957,7 +1957,7 @@ class LineAnnotationElement extends AnnotationElement {
line.setAttribute("stroke", "transparent");
line.setAttribute("fill", "transparent");

svg.appendChild(line);
svg.append(line);
this.container.append(svg);

// Create the popup ourselves so that we can bind it to the line instead
Expand Down Expand Up @@ -2004,7 +2004,7 @@ class SquareAnnotationElement extends AnnotationElement {
square.setAttribute("stroke", "transparent");
square.setAttribute("fill", "transparent");

svg.appendChild(square);
svg.append(square);
this.container.append(svg);

// Create the popup ourselves so that we can bind it to the square instead
Expand Down Expand Up @@ -2051,7 +2051,7 @@ class CircleAnnotationElement extends AnnotationElement {
circle.setAttribute("stroke", "transparent");
circle.setAttribute("fill", "transparent");

svg.appendChild(circle);
svg.append(circle);
this.container.append(svg);

// Create the popup ourselves so that we can bind it to the circle instead
Expand Down Expand Up @@ -2106,7 +2106,7 @@ class PolylineAnnotationElement extends AnnotationElement {
polyline.setAttribute("stroke", "transparent");
polyline.setAttribute("fill", "transparent");

svg.appendChild(polyline);
svg.append(polyline);
this.container.append(svg);

// Create the popup ourselves so that we can bind it to the polyline
Expand Down Expand Up @@ -2199,7 +2199,7 @@ class InkAnnotationElement extends AnnotationElement {
// instead of to the entire container (which is the default).
this._createPopup(polyline, data);

svg.appendChild(polyline);
svg.append(polyline);
}

this.container.append(svg);
Expand Down Expand Up @@ -2376,7 +2376,7 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
this._createPopup(trigger, this.data);
}

this.container.appendChild(trigger);
this.container.append(trigger);
return this.container;
}

Expand Down Expand Up @@ -2471,15 +2471,15 @@ class AnnotationLayer {
}
if (Array.isArray(rendered)) {
for (const renderedElement of rendered) {
div.appendChild(renderedElement);
div.append(renderedElement);
}
} else {
if (element instanceof PopupAnnotationElement) {
// Popup annotation elements should not be on top of other
// annotation elements to prevent interfering with mouse events.
div.prepend(rendered);
} else {
div.appendChild(rendered);
div.append(rendered);
}
}
}
Expand Down Expand Up @@ -2557,7 +2557,7 @@ class AnnotationLayer {

const { firstChild } = element;
if (!firstChild) {
element.appendChild(canvas);
element.append(canvas);
} else if (firstChild.nodeName === "CANVAS") {
element.replaceChild(canvas, firstChild);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ function loadScript(src, removeScriptElement = false) {
script.onerror = function () {
reject(new Error(`Cannot load script at: ${script.src}`));
};
(document.head || document.documentElement).appendChild(script);
(document.head || document.documentElement).append(script);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class AnnotationEditorLayer {
editor.parent = this;
if (editor.div && editor.isAttachedToDOM) {
editor.div.remove();
this.div.appendChild(editor.div);
this.div.append(editor.div);
}
}

Expand All @@ -248,7 +248,7 @@ class AnnotationEditorLayer {

if (!editor.isAttachedToDOM) {
const div = editor.render();
this.div.appendChild(div);
this.div.append(div);
editor.isAttachedToDOM = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ class FreeTextEditor extends AnnotationEditor {
style.fontSize = `calc(${this.#fontSize}px * var(--zoom-factor))`;
style.color = this.#color;

this.div.appendChild(this.editorDiv);
this.div.append(this.editorDiv);

this.overlayDiv = document.createElement("div");
this.overlayDiv.classList.add("overlay", "enabled");
this.div.appendChild(this.overlayDiv);
this.div.append(this.overlayDiv);

// TODO: implement paste callback.
// The goal is to sanitize and have something suitable for this
Expand Down
2 changes: 1 addition & 1 deletion src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class InkEditor extends AnnotationEditor {
#createCanvas() {
this.canvas = document.createElement("canvas");
this.canvas.className = "inkEditorCanvas";
this.div.appendChild(this.canvas);
this.div.append(this.canvas);
this.ctx = this.canvas.getContext("2d");
}

Expand Down
6 changes: 3 additions & 3 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BaseFontLoader {
styleElement.id = `PDFJS_FONT_STYLE_TAG_${this.docId}`;
this._document.documentElement
.getElementsByTagName("head")[0]
.appendChild(styleElement);
.append(styleElement);
}
const styleSheet = styleElement.sheet;
styleSheet.insertRule(rule, styleSheet.cssRules.length);
Expand Down Expand Up @@ -345,9 +345,9 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
const span = this._document.createElement("span");
span.textContent = "Hi";
span.style.fontFamily = name;
div.appendChild(span);
div.append(span);
}
this._document.body.appendChild(div);
this._document.body.append(div);

isFontReady(loadTestFontId, () => {
div.remove();
Expand Down
Loading