神刀安全网

dominate – Convert HTML, SVG, and XML markup strings into DOM objects

dominate dominate – Convert HTML, SVG, and XML markup strings into DOM objects

Convert HTML, SVG, and XML markup strings into DOM objects.

Usage

Convert a single element HTML string into a DOM element:

const div = dominate('<div>foo</div>'); console.log(div.nodeName); // DIV

Convert a multiple element HTML string into a document fragment:

const frag = dominate('<strong>Hello</strong> <em>World</em>'); console.log(frag.nodeName); // #document-fragment

Convert plain text to a DOM text node:

const text = dominate('This is plain text.'); console.log(text.nodeName); // #text

Convert an SVG element into a DOM element:

const rect = dominate('<rect x="10" y="10" width="100" height="100"/>'); console.log(rect.nodeName); // RECT

Executes script tags within an HTML string by default:

const script = dominate('<script>console.log("foo");</script>'); document.body.appendChild(script); // console outputs "foo"

Remove script tags to prevent execution:

const el = dominate('<section><script src="foo.js"></script></section>', {scripts: false}); const script = el.querySelector('script'); // null

Use a custom document instead of the default ( window.document )

import { jsdom } from 'jsdom'; const doc = jsdom('<html><body></body></html>'); const el = dominate('<div></div>', {context: doc}); console.log(el.ownerDocument === doc); // true

Convert XML string into a DOM element:

const xml = dominate('<name>foo</name>', {type: 'xml'}); console.log(xml instanceof Element); // true console.log(xml instanceof HTMLElement); // false

Installation

Dominate is CommonJS andAMD compatible with no dependencies. You can download thedevelopment orminified version, or install it in one of the following ways:

npm install ryanmorr/dominate  bower install ryanmorr/dominate

Tests

Open test/runner.html in your browser or test with PhantomJS by issuing the following commands:

npm install npm install -g gulp gulp test

License

This project is dedicated to the public domain as described by the Unlicense .

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » dominate – Convert HTML, SVG, and XML markup strings into DOM objects

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址