Detalhes do pacote

xmldom-qsa

zeligzhou165.6kMIT1.1.3

Based on @xmldom/xmldom with some minor enhancements. Add querySelector, querySelectorAll and match method to Document and Element.

w3c, dom, xml, parser

readme (leia-me)

xmldom-qsa

This is based on xmldom with some minor enhancements.

xmldom-qsa add querySelector, querySelectorAll and match method to Document and Element. And you can write some more methods to these elements by add prototype methods to Node.

xmldom is a javascript ponyfill to provide the following APIs that are present in modern browsers to other runtimes:

  • convert an XML string into a DOM tree
    new DOMParser().parseFromString(xml, mimeType) => Document
  • create, access and modify a DOM tree
    new DOMImplementation().createDocument(...) => Document
  • serialize a DOM tree back into an XML string
    new XMLSerializer().serializeToString(node) => string

The target runtimes xmldom supports are currently Node >= v10 (ES5) and Rhino (not tested as part of CI).

When deciding how to fix bugs or implement features, xmldom tries to stay as close as possible to the various related specifications/standards. As indicated by the version starting with 0., this implementation is not feature complete and some implemented features differ from what the specifications describe. Issues and PRs for such differences are always welcome, even when they only provide a failing test case.

This project was forked from it's original source in 2019, more details about that transition can be found in the CHANGELOG.

Usage

Install:

npm install xmldom-qsa

Enhancements:


doc.documentElement.querySelector('.clazz');

var Node = require('xmldom-qsa/dom').Node;
Node.prototype.addAttr = function(x,y) { ... }
doc.documentElement.addAttr('x','y');

Example:

In NodeJS

const { DOMParser } = require('xmldom-qsa')

const doc = new DOMParser().parseFromString(
    '<xml xmlns="a" xmlns:c="./lite">\n' +
        '\t<child>test</child>\n' +
        '\t<child></child>\n' +
        '\t<child/>\n' +
        '</xml>',
    'text/xml'
)
doc.documentElement.setAttribute('x', 'y')
doc.documentElement.setAttributeNS('./lite', 'c:x', 'y2')
console.info(doc)

const nsAttr = doc.documentElement.getAttributeNS('./lite', 'x')
console.info(nsAttr)

Note: in Typescript ~and ES6~(see #316) you can use the import approach, as follows:

import { DOMParser } from 'xmldom-qsa'

API Reference

  • DOMParser:

     parseFromString(xmlsource,mimeType)
    • options extension by xmldom (not DOM standard!!)

      //added the options argument
      new DOMParser(options)
      
      //errorHandler is supported
      new DOMParser({
        /**
         * locator is always need for error position info
         */
        locator:{},
        /**
         * you can override the errorHandler for xml parser
         * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
         */
        errorHandler:{warning:function(w){console.warn(w)},error:callback,fatalError:callback}
        //only callback model
        //errorHandler:function(level,msg){console.log(level,msg)}
      })
  • XMLSerializer

     serializeToString(node)

    DOM level2 method and attribute:

  • Node

    readonly class properties (aka NodeType), these can be accessed from any Node instance node: if (node.nodeType === node.ELEMENT_NODE) {...

    1. ELEMENT_NODE (1)
    2. ATTRIBUTE_NODE (2)
    3. TEXT_NODE (3)
    4. CDATA_SECTION_NODE (4)
    5. ENTITY_REFERENCE_NODE (5)
    6. ENTITY_NODE (6)
    7. PROCESSING_INSTRUCTION_NODE (7)
    8. COMMENT_NODE (8)
    9. DOCUMENT_NODE (9)
    10. DOCUMENT_TYPE_NODE (10)
    11. DOCUMENT_FRAGMENT_NODE (11)
    12. NOTATION_NODE (12)

    attribute:

    • nodeValue | prefix

    readonly attribute:

    • nodeName | nodeType | parentNode | childNodes | firstChild | lastChild | previousSibling | nextSibling | attributes | ownerDocument | namespaceURI | localName

    method:

    • insertBefore(newChild, refChild)
    • replaceChild(newChild, oldChild)
    • removeChild(oldChild)
    • appendChild(newChild)
    • hasChildNodes()
    • cloneNode(deep)
    • normalize()
    • isSupported(feature, version)
    • hasAttributes()
  • DOMException

    extends the Error type thrown as part of DOM API.

    readonly class properties:

    • INDEX_SIZE_ERR (1)
    • DOMSTRING_SIZE_ERR (2)
    • HIERARCHY_REQUEST_ERR (3)
    • WRONG_DOCUMENT_ERR (4)
    • INVALID_CHARACTER_ERR (5)
    • NO_DATA_ALLOWED_ERR (6)
    • NO_MODIFICATION_ALLOWED_ERR (7)
    • NOT_FOUND_ERR (8)
    • NOT_SUPPORTED_ERR (9)
    • INUSE_ATTRIBUTE_ERR (10)
    • INVALID_STATE_ERR (11)
    • SYNTAX_ERR (12)
    • INVALID_MODIFICATION_ERR (13)
    • NAMESPACE_ERR (14)
    • INVALID_ACCESS_ERR (15)

    attributes:

    • code with a value matching one of the above constants.
  • DOMImplementation

    method:

    • hasFeature(feature, version)
    • createDocumentType(qualifiedName, publicId, systemId)
    • createDocument(namespaceURI, qualifiedName, doctype)
  • Document : Node

    readonly attribute:

    • doctype | implementation | documentElement

    method:

    • createElement(tagName)
    • createDocumentFragment()
    • createTextNode(data)
    • createComment(data)
    • createCDATASection(data)
    • createProcessingInstruction(target, data)
    • createAttribute(name)
    • createEntityReference(name)
    • getElementsByTagName(tagname)
    • importNode(importedNode, deep)
    • createElementNS(namespaceURI, qualifiedName)
    • createAttributeNS(namespaceURI, qualifiedName)
    • getElementsByTagNameNS(namespaceURI, localName)
    • getElementById(elementId)
  • DocumentFragment : Node

  • Element : Node

    readonly attribute:

    • tagName

    method:

    • getAttribute(name)
    • setAttribute(name, value)
    • removeAttribute(name)
    • getAttributeNode(name)
    • setAttributeNode(newAttr)
    • removeAttributeNode(oldAttr)
    • getElementsByTagName(name)
    • getAttributeNS(namespaceURI, localName)
    • setAttributeNS(namespaceURI, qualifiedName, value)
    • removeAttributeNS(namespaceURI, localName)
    • getAttributeNodeNS(namespaceURI, localName)
    • setAttributeNodeNS(newAttr)
    • getElementsByTagNameNS(namespaceURI, localName)
    • hasAttribute(name)
    • hasAttributeNS(namespaceURI, localName)
  • Attr : Node

    attribute:

    • value

    readonly attribute:

    • name | specified | ownerElement
  • NodeList

    readonly attribute:

    • length

    method:

    • item(index)
  • NamedNodeMap

    readonly attribute:

    • length

    method:

    • getNamedItem(name)
    • setNamedItem(arg)
    • removeNamedItem(name)
    • item(index)
    • getNamedItemNS(namespaceURI, localName)
    • setNamedItemNS(arg)
    • removeNamedItemNS(namespaceURI, localName)
  • CharacterData : Node

    method:

    • substringData(offset, count)
    • appendData(arg)
    • insertData(offset, arg)
    • deleteData(offset, count)
    • replaceData(offset, count, arg)
  • Text : CharacterData

    method:

    • splitText(offset)
  • CDATASection

  • Comment : CharacterData

  • DocumentType

    readonly attribute:

    • name | entities | notations | publicId | systemId | internalSubset
  • Notation : Node

    readonly attribute:

    • publicId | systemId
  • Entity : Node

    readonly attribute:

    • publicId | systemId | notationName
  • EntityReference : Node

  • ProcessingInstruction : Node

    attribute:

    • data readonly attribute:
    • target

DOM level 3 support:

  • Node

    attribute:

    • textContent

    method:

    • isDefaultNamespace(namespaceURI)
    • lookupNamespaceURI(prefix)

DOM extension by xmldom

  • [Node] Source position extension;

    attribute:

    • lineNumber //number starting from 1
    • columnNumber //number starting from 1

Specs

The implementation is based on several specifications:

Overview of related specifications and their relations

DOM Parsing and Serialization

From the W3C DOM Parsing and Serialization (WD 2016) xmldom provides an implementation for the interfaces:

  • DOMParser
  • XMLSerializer

Note that there are some known deviations between this implementation and the W3 specifications.

Note: The latest version of this spec has the status "Editors Draft", since it is under active development. One major change is that the definition of the DOMParser interface has been moved to the HTML spec

DOM

The original author claims that xmldom implements [DOM Level 2] in a "fully compatible" way and some parts of [DOM Level 3], but there are not enough tests to prove this. Both Specifications are now superseded by the [DOM Level 4 aka Living standard] wich has a much broader scope than xmldom.

xmldom implements the following interfaces (most constructors are currently not exposed):

  • Attr
  • CDATASection
  • CharacterData
  • Comment
  • Document
  • DocumentFragment
  • DocumentType
  • DOMException (constructor exposed)
  • DOMImplementation (constructor exposed)
  • Element
  • Entity
  • EntityReference
  • LiveNodeList
  • NamedNodeMap
  • Node (constructor exposed)
  • NodeList
  • Notation
  • ProcessingInstruction
  • Text

more details are available in the (incomplete) API Reference section.

HTML

xmldom does not have any goal of supporting the full spec, but it has some capability to parse, report and serialize things differently when "detecting HTML" (by checking the default namespace). There is an upcoming change to better align the implementation with the latest specs, related to https://github.com/xmldom/xmldom/issues/203.

SAX, XML, XMLNS

xmldom has an own SAX parser implementation to do the actual parsing, which implements some interfaces in alignment with the Java interfaces SAX defines:

  • XMLReader
  • DOMHandler

There is an idea/proposal to make it possible to replace it with something else in https://github.com/xmldom/xmldom/issues/55

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

0.8.7

Fixed

  • properly parse closing where the last attribute has no value #485 / #486

Thank you, @bulandent, for your contributions

0.7.10

Fixed

  • properly parse closing where the last attribute has no value #485 / #486

Thank you, @bulandent, for your contributions

0.8.6

Fixed

  • Properly check nodes before replacement #457 / #455 / #456

Thank you, @edemaine, @pedro-l9, for your contributions

0.8.5

Fixed

  • fix: Restore ES5 compatibility #452 / #453

Thank you, @fengxinming, for your contributions

0.8.4

Fixed

  • Security: Prevent inserting DOM nodes when they are not well-formed CVE-2022-39353 In case such a DOM would be created, the part that is not well-formed will be transformed into text nodes, in which xml specific characters like < and > are encoded accordingly. In the upcoming version 0.9.0 those text nodes will no longer be added and an error will be thrown instead. This change can break your code, if you relied on this behavior, e.g. multiple root elements in the past. We consider it more important to align with the specs that we want to be aligned with, considering the potential security issues that might derive from people not being aware of the difference in behavior. Related Spec: https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity

Thank you, @frumioj, @cjbarth, @markgollnick for your contributions

0.8.3

Fixed

  • Avoid iterating over prototype properties #437 / #436

Thank you, @Supraja9726 for your contributions

0.8.2

Fixed

  • fix(dom): Serialize &gt; as specified (#395) #58

Other

  • docs: Add nodeType values to public interface description #396
  • test: Add executable examples for node and typescript #317
  • fix(dom): Serialize &gt; as specified #395
  • chore: Add minimal Object.assign ponyfill #379
  • docs: Refine release documentation #378
  • chore: update various dev dependencies

Thank you @niklasl, @cburatto, @SheetJSDev, @pyrsmk for your contributions

0.8.1

Fixes

  • Only use own properties in entityMap #374

Docs

  • Add security policy #365
  • changelog: Correct contributor name and link #366
  • Describe release/publish steps #358, #376
  • Add snyk package health badge #360

0.8.0

Fixed

  • Normalize all line endings according to XML specs 1.0 and 1.1 \ BREAKING CHANGE: Certain combination of line break characters are normalized to a single \n before parsing takes place and will no longer be preserved.
  • XMLSerializer: Preserve whitespace character references #284 / #310 \ BREAKING CHANGE: If you relied on the not spec compliant preservation of literal \t, \n or \r in attribute values. To preserve those you will have to create XML that instead contains the correct numerical (or hexadecimal) equivalent (e.g. &#x9;, &#xA;, &#xD;).
  • Drop deprecated exports DOMImplementation and XMLSerializer from lib/dom-parser.js #53 / #309 BREAKING CHANGE: Use the one provided by the main package export.
  • dom: Remove all links as part of removeChild #343 / #355

Chore

  • ci: Restore latest tested node version to 16.x #325
  • ci: Split test and lint steps into jobs #111 / #304
  • Pinned and updated devDependencies

Thank you @marrus-sh, @victorandree, @mdierolf, @tsabbay, @fatihpense for your contributions

0.7.5

Commits

Fixes:

0.7.4

Commits

Fixes:

  • Restore ability to parse __prototype__ attributes #315 Thank you, @dsimpsonOMF

0.7.3

Commits

Fixes:

  • Add doctype when parsing from string #277 / #301
  • Correct typo in error message #294 Thank you, @rrthomas

Refactor:

  • Improve exports & require statements, new main package entry #233

Docs:

  • Fix Stryker badge #298
  • Fix link to help-wanted issues #299

Chore:

  • Execute stryker:dry-run on branches #302
  • Fix stryker config #300
  • Split test and lint scripts #297
  • Switch to stryker dashboard owned by org #292

0.7.2

Commits

Fixes:

  • Types: Add index.d.ts to packaged files #288 Thank you, @forty

0.7.1

Commits

Fixes:

  • Types: Copy types from DefinitelyTyped #283 Thank you, @kachkaev

Chore:

  • package.json: remove author, maintainers, etc. #279

0.7.0

Commits

Due to #271 this version was published as

  • unscoped xmldom package to github (git tags 0.7.0 and 0.7.0+unscoped)
  • scoped xmldom-qsa package to npm (git tag 0.7.0+scoped) For more details look at #278

Fixes:

  • Security: Misinterpretation of malicious XML input CVE-2021-32796
  • Implement Document.getElementsByClassName as specified #213, thank you, @ChALkeR
  • Inherit namespace prefix from parent when required #268
  • Handle whitespace in closing tags #267
  • Update DOMImplementation according to recent specs #210 BREAKING CHANGE: Only if you "passed features to be marked as available as a constructor arguments" and expected it to "magically work".
  • No longer serializes any namespaces with an empty URI #244 (related to #168 released in 0.6.0) BREAKING CHANGE: Only if you rely on "unsetting" a namespace prefix by setting it to an empty string
  • Set localName as part of Document.createElement #229, thank you, @rrthomas

CI

  • We are now additionally running tests against node v16
  • Stryker tests on the master branch now run against node v14

Docs

  • Describe relations with and between specs: #211, #247

0.6.0

Commits

Fixes

  • Stop serializing empty namespace values like xmlns:ds="" #168 BREAKING CHANGE: If your code expected empty namespaces attributes to be serialized. Thank you, @pdecat and @FranckDepoortere
  • Escape < to &lt; when serializing attribute values #198 / #199

0.5.0

Commits

Fixes

  • Avoid misinterpretation of malicious XML input - GHSA-h6q6-9hqw-rwfv (CVE-2021-21366)

    • Improve error reporting; throw on duplicate attribute\ BREAKING CHANGE: It is currently not clear how to consistently deal with duplicate attributes, so it's also safer for our users to fail when detecting them. It's possible to configure the DOMParser.errorHandler before parsing, to handle those errors differently.

      To accomplish this and also be able to verify it in tests I needed to

      • create a new Error type ParseError and export it
      • Throw ParseError from errorHandler.fatalError and prevent those from being caught in XMLReader.
      • export DOMHandler constructor as __DOMHandler
    • Preserve quotes in DOCTYPE declaration Since the only purpose of parsing the DOCTYPE is to be able to restore it when serializing, we decided that it would be best to leave the parsed publicId and systemId as is, including any quotes. BREAKING CHANGE: If somebody relies on the actual unquoted values of those ids, they will need to take care of either single or double quotes and the right escaping. (Without this change this would not have been possible because the SAX parser already dropped the information about the quotes that have been used in the source.)

      https://www.w3.org/TR/2006/REC-xml11-20060816/#dtd https://www.w3.org/TR/2006/REC-xml11-20060816/#IDAX1KS (External Entity Declaration)

  • Fix breaking preprocessors' directives when parsing attributes #171

  • fix(dom): Escape ]]&gt; when serializing CharData #181
  • Switch to (only) MIT license (drop problematic LGPL license option) #178
  • Export DOMException; remove custom assertions; etc. #174

Docs

  • Update MDN links in readme.md #188

0.4.0

Commits

Fixes

  • BREAKING Restore &nbsp; behavior from v0.1.27 #67
  • BREAKING Typecheck source param before parsing #113
  • Include documents in package files list #156
  • Preserve doctype with sysid #144
  • Remove ES6 syntax from getElementsByClassName #91
  • Revert "Add lowercase of åäö in entityMap" due to duplicate entries #84
  • fix: Convert all line separators to LF #66

Docs

  • Update CHANGELOG.md through version 0.3.0 #63
  • Update badges #78
  • Add .editorconfig file #104
  • Add note about import #79
  • Modernize & improve the example in readme.md #81

CI

  • Add Stryker Mutator #70
  • Add Stryker action to update dashboard #77
  • Add Node GitHub action workflow #64
  • add & enable eslint #106
  • Use eslint-plugin-es5 to enforce ES5 syntax #107
  • Recover vows tests, drop proof tests #59
  • Add jest tessuite and first tests #114
  • Add jest testsuite with xmltest cases #112
  • Configure Renovate #108
  • Test European HTML entities #86
  • Updated devDependencies

Other

  • Remove files that are not of any use #131, #65, #33

0.3.0

Commits

0.2.1

Commits

  • Correct homepage, repository and bugs URLs in package.json.

0.2.0

Commits

0.1.31

Commits

The patch versions (v0.1.29 - v0.1.31) that have been released on the v0.1.x branch, to reflect the changed maintainers, are branched off from v0.1.27 so they don't include the breaking changes introduced in xmldom-alpha@v0.1.28:

Maintainer changes

After the last commit to the original repository https://github.com/jindw/xmldom on the 9th of May 2017, the first commit to https://github.com/xmldom/xmldom is from the 19th of December 2019. The fork has been announced in the original repository on the 2nd of March 2020.

The versions listed below have been published to one or both of the following packages:

It is currently not planned to continue publishing the xmldom-alpha package.

The new maintainers did not invest time to understand changes that led to the last xmldom version 0.1.27 published by the original maintainer, but consider it the basis for their work. A timeline of all the changes that happened from that version until 0.3.0 is available in https://github.com/xmldom/xmldom/issues/62. Any related questions should be asked there.

0.1.28

Commits

Published by @jindw on the 9th of May 2017 as

0.1.27

Published by @jindw on the 28th of Nov 2016 as

  • xmldom@0.1.27
  • xmldom-alpha@0.1.27

  • Various bug fixes.

0.1.26

Published on the 18th of Nov 2016 as xmldom@0.1.26

  • Details unknown

0.1.25

Published on the 18th of Nov 2016 as

  • xmldom@0.1.25

  • Details unknown

0.1.24

Published on the 27th of November 2016 as

  • xmldom@0.1.24
  • xmldom-alpha@0.1.24

  • Added node filter.

0.1.23

Published on the 5th of May 2016 as

  • xmldom-alpha@0.1.23

  • Add namespace support for nest node serialize.

  • Various other bug fixes.

0.1.22

  • Merge XMLNS serialization.
  • Remove \r from source string.
  • Print namespaces for child elements.
  • Switch references to nodeType to use named constants.
  • Add nodelist toString support.

0.1.21

  • Fix serialize bug.

0.1.20

  • Optimize invalid XML support.
  • Add toString sorter for attributes output.
  • Add html self closed node button.
  • Add * NS support for getElementsByTagNameNS.
  • Convert attribute's value to string in setAttributeNS.
  • Add support for HTML entities for HTML docs only.
  • Fix TypeError when Document is created with DocumentType.

0.1.19

0.1.18

  • Add default ns support.
  • parseFromString now renders entirely plain text documents as textNode.
  • Enable option to ignore white space on parsing.

0.1.17

Details missing for this and potential earlier version

0.1.16

  • Correctly handle multibyte Unicode greater than two byts. #57. #56.
  • Initial unit testing and test coverage. #53. #46. #19.
  • Create Bower component.json #52.

0.1.8

  • Add: some test case from node-o3-xml(excludes xpath support)
  • Fix: remove existed attribute before setting (bug introduced in v0.1.5)
  • Fix: index direct access for childNodes and any NodeList collection(not w3c standard)
  • Fix: remove last child bug