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

feat: add document.createAttribute #169

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/dom/document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CTOR_KEY } from "../constructor-lock.ts";
import { Comment, Node, NodeType, Text } from "./node.ts";
import { NodeList, nodeListMutatorSym } from "./node-list.ts";
import { Element } from "./element.ts";
import { Attr, Element } from "./element.ts";
import { DocumentFragment } from "./document-fragment.ts";
import { HTMLTemplateElement } from "./elements/html-template-element.ts";
import { getSelectorEngine, SelectorApi } from "./selectors/selectors.ts";
Expand Down Expand Up @@ -255,6 +255,10 @@ export class Document extends Node {
return new Comment(data);
}

createAttribute(attribute: string): Attr {
return new Attr(null, attribute, "", CTOR_KEY);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to set the ownerDocument via the _setOwnerDocument method

}

createDocumentFragment(): DocumentFragment {
const fragment = new DocumentFragment();
fragment._setOwnerDocument(this);
Expand Down
11 changes: 11 additions & 0 deletions test/units/Document-createAttribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Document } from "../../deno-dom-wasm.ts";
import { Attr } from "../../src/dom/element.ts";
import { assertInstanceOf, assertStrictEquals as assertEquals } from "assert";

Deno.test("Document.createAttribute creates a new Attr instance", () => {
const doc = new Document();

const attr = doc.createAttribute("foo");
assertInstanceOf(attr, Attr);
assertEquals(attr.name, "foo");
});
Loading