capricorn86 / happy-dom Goto Github PK
View Code? Open in Web Editor NEWA JavaScript implementation of a web browser without its graphical user interface
License: MIT License
A JavaScript implementation of a web browser without its graphical user interface
License: MIT License
Two xml sibling tags with no endtag could look like this when parsed:
<path/> <path/>
But when innerHTML is generated, they will suddenly have a parent-child relationship:
<path> <path></path> </path>
HTMLUnknownElement
is uncommonly used, but it's important sometimes, such as
https://github.com/EasyWebApp/WebCell/blob/c3adfc711bbb472c4194609536b212a52d7c02a3/source/utility.ts#L73
happy-dom 0.11.0+ lost it...
Sizzle is a selector engine used by jQuery. It was there before querySelectorAll
and similar methods. Now, it uses querySelectorAll
if available.
I've seen there are changes in happy-dom selectors on v1.0.0
branch, but maybe it's still valuable to use Sizzle? I gave it a quick try with happy-dom, but unfortunately, Sizzle expects window
object to be available. We could experiment with it further (I think even contributing in Sizzle isn't a problem), but the question is - is this worth it? What do you think?
I have a function that uses the classList to add/remove class names.
I mistakenly added a two classes as one string with a space to separate them, the test PASSED but in production it failed:
"Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('a b') contains HTML space characters, which are not valid in tokens"
//func.js
export function applyClassName({
oldValue,
newValue,
element,
defaultValue = ""
}) {
if (newValue) {
if (oldValue) {
element.classList.remove(oldValue);
}
element.classList.add(newValue);
} else if (oldValue) {
element.classList.remove(oldValue);
element.classList.add(defaultValue);
}
}
//func.test.js
import { Window } from "happy-dom";
import { applyClassName } from "./func";
const window = new Window();
const document = window.document;
test.only("Test space within class name", () => {
const myDiv = document.createElement("div");
applyClassName({newValue: "a b", element: myDiv});
expect(myDiv.className).toBe("a b");
});
I expect the test to fail because it will throw an exception, but using happy-dom it dosen't, it passes.
Running tests for enhook, getting the following.
# react
(node:5736) UnhandledPromiseRejectionWarning: TypeError: Right-hand side of 'instanceof' is not an object
at getActiveElementDeep (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:8903:18)
at getSelectionInformation (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:8934:21)
at prepareForCommit (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:9447:26)
at commitRootImpl (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:24973:5)
at unstable_runWithPriority (C:\projects\enhook\node_modules\scheduler\cjs\scheduler.development.js:818:12)
at runWithPriority$2 (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:12130:10)
at commitRoot (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:24889:3)
at finishSyncRender (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:24296:3)
at performSyncWorkOnRoot (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:24274:9)
at scheduleUpdateOnFiber (C:\projects\enhook\node_modules\react-dom\cjs\react-dom.development.js:23665:7)
(node:5736) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
Not sure what's the reason, due to incredibly obscure nature of react I can't even figure out where the error is coming from.
I'll just leave it here in hope somebody else will stumble upon it too, with less vague context.
when trying to get the slot element I receive null!
//<div class="card-footer"><slot name="footer"></slot></div>
console.info(`${this.shadowRoot.querySelector(".card-footer")}`);// => the div element
console.info(`${this.shadowRoot.querySelector(".card-footer > slot")}`);// => null
console.info(`${this.shadowRoot.querySelector(".card-footer slot")}`);// => the slot element
For some weird reason CustomEvent is the same object as Event, but looking at the code they are different. CustomEvent has detail in it and extends Event.
This was found in jest-environment-happy-dom, so it might not happen when only using happy-dom.
The callback attributeChangedCallback
is being called on ANY attribute change, not only on the observed ones as expected.
here is a jest test to help you reproduce
import { Window } from "happy-dom";
class MyElem extends HTMLElement {
attributeChangedCallback(attribute, oldValue, newValue) {}
static get observedAttributes() {
return ['my-attr'];
}
}
const window = new Window();
const document = window.document;
let myElem;
window.customElements.define("my-elem", MyElem);
beforeEach(() => {
document.body.innerHTML = "<my-elem id='my-elem'></my-elem>";
myElem = document.querySelector("#my-elem");
});
describe("Make sure component is created", () => {
//SUCCESS
test("’Make sure <my-elem> is defined", () => {
expect(myElem).not.toBeNull();
});
//SUCCESS
test("’Make sure changing an observed attribute will triiger change callback", () => {
const spy = jest.spyOn(myElem, 'attributeChangedCallback');
myElem.setAttribute("my-attr", "css-class");
expect(spy).toHaveBeenCalled();
});
//FAILS
test("’Make sure changing non observed attribute will not triiger change callback", () => {
const spy = jest.spyOn(myElem, 'attributeChangedCallback');
myElem.setAttribute("class", "css-class");
expect(spy).not.toHaveBeenCalled();
});
//FAILS
test("’Make sure changing non observed attribute will not triiger change callback", () => {
const spy = jest.spyOn(myElem, 'attributeChangedCallback');
myElem.setAttribute("style", "display: block;");
expect(spy).not.toHaveBeenCalled();
});
//FAILS
test("’Make sure changing non observed attribute will not triiger change callback", () => {
const spy = jest.spyOn(myElem, 'attributeChangedCallback');
myElem.setAttribute("id", "other-id");
expect(spy).not.toHaveBeenCalled();
});
});
I can't find a mention of firstElementChild
in this repository, which is defined in the ParentNode interface. Is this supported by happy-dom?
This is a really weird one. Not sure if it is a bug or if there is something wrong in the app itself.
The class "HTMLElement" should implement the official Typescript "HTMLElement" e.g. because of two reasons:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.