@depup/jsdom 28.1.0-depup.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +22 -0
- package/README.md +38 -0
- package/changes.json +38 -0
- package/lib/api.js +373 -0
- package/lib/generated/event-sets.js +115 -0
- package/lib/generated/idl/AbortController.js +143 -0
- package/lib/generated/idl/AbortSignal.js +249 -0
- package/lib/generated/idl/AbstractRange.js +171 -0
- package/lib/generated/idl/AddEventListenerOptions.js +53 -0
- package/lib/generated/idl/AssignedNodesOptions.js +28 -0
- package/lib/generated/idl/Attr.js +219 -0
- package/lib/generated/idl/BarProp.js +117 -0
- package/lib/generated/idl/BeforeUnloadEvent.js +139 -0
- package/lib/generated/idl/BinaryType.js +12 -0
- package/lib/generated/idl/Blob.js +253 -0
- package/lib/generated/idl/BlobCallback.js +30 -0
- package/lib/generated/idl/BlobEvent.js +157 -0
- package/lib/generated/idl/BlobEventInit.js +43 -0
- package/lib/generated/idl/BlobPropertyBag.js +42 -0
- package/lib/generated/idl/CDATASection.js +109 -0
- package/lib/generated/idl/CanPlayTypeResult.js +12 -0
- package/lib/generated/idl/CharacterData.js +455 -0
- package/lib/generated/idl/CloseEvent.js +168 -0
- package/lib/generated/idl/CloseEventInit.js +65 -0
- package/lib/generated/idl/Comment.js +120 -0
- package/lib/generated/idl/CompositionEvent.js +219 -0
- package/lib/generated/idl/CompositionEventInit.js +32 -0
- package/lib/generated/idl/Crypto.js +148 -0
- package/lib/generated/idl/CustomElementConstructor.js +34 -0
- package/lib/generated/idl/CustomElementRegistry.js +269 -0
- package/lib/generated/idl/CustomEvent.js +206 -0
- package/lib/generated/idl/CustomEventInit.js +32 -0
- package/lib/generated/idl/DOMException.js +222 -0
- package/lib/generated/idl/DOMImplementation.js +237 -0
- package/lib/generated/idl/DOMParser.js +140 -0
- package/lib/generated/idl/DOMRect.js +276 -0
- package/lib/generated/idl/DOMRectInit.js +76 -0
- package/lib/generated/idl/DOMRectReadOnly.js +285 -0
- package/lib/generated/idl/DOMStringMap.js +299 -0
- package/lib/generated/idl/DOMTokenList.js +539 -0
- package/lib/generated/idl/DeviceMotionEvent.js +183 -0
- package/lib/generated/idl/DeviceMotionEventAcceleration.js +145 -0
- package/lib/generated/idl/DeviceMotionEventAccelerationInit.js +61 -0
- package/lib/generated/idl/DeviceMotionEventInit.js +70 -0
- package/lib/generated/idl/DeviceMotionEventRotationRate.js +145 -0
- package/lib/generated/idl/DeviceMotionEventRotationRateInit.js +61 -0
- package/lib/generated/idl/DeviceOrientationEvent.js +183 -0
- package/lib/generated/idl/DeviceOrientationEventInit.js +80 -0
- package/lib/generated/idl/Document.js +4511 -0
- package/lib/generated/idl/DocumentFragment.js +336 -0
- package/lib/generated/idl/DocumentReadyState.js +12 -0
- package/lib/generated/idl/DocumentType.js +254 -0
- package/lib/generated/idl/Element.js +3720 -0
- package/lib/generated/idl/ElementCreationOptions.js +26 -0
- package/lib/generated/idl/ElementDefinitionOptions.js +29 -0
- package/lib/generated/idl/ElementInternals.js +2152 -0
- package/lib/generated/idl/EndingType.js +12 -0
- package/lib/generated/idl/ErrorEvent.js +192 -0
- package/lib/generated/idl/ErrorEventInit.js +92 -0
- package/lib/generated/idl/Event.js +430 -0
- package/lib/generated/idl/EventHandlerNonNull.js +36 -0
- package/lib/generated/idl/EventInit.js +58 -0
- package/lib/generated/idl/EventListener.js +35 -0
- package/lib/generated/idl/EventListenerOptions.js +28 -0
- package/lib/generated/idl/EventModifierInit.js +221 -0
- package/lib/generated/idl/EventTarget.js +259 -0
- package/lib/generated/idl/External.js +130 -0
- package/lib/generated/idl/File.js +185 -0
- package/lib/generated/idl/FileList.js +298 -0
- package/lib/generated/idl/FilePropertyBag.js +33 -0
- package/lib/generated/idl/FileReader.js +468 -0
- package/lib/generated/idl/FocusEvent.js +144 -0
- package/lib/generated/idl/FocusEventInit.js +36 -0
- package/lib/generated/idl/FormData.js +468 -0
- package/lib/generated/idl/Function.js +42 -0
- package/lib/generated/idl/GetRootNodeOptions.js +31 -0
- package/lib/generated/idl/HTMLAnchorElement.js +1026 -0
- package/lib/generated/idl/HTMLAreaElement.js +825 -0
- package/lib/generated/idl/HTMLAudioElement.js +111 -0
- package/lib/generated/idl/HTMLBRElement.js +156 -0
- package/lib/generated/idl/HTMLBaseElement.js +196 -0
- package/lib/generated/idl/HTMLBodyElement.js +880 -0
- package/lib/generated/idl/HTMLButtonElement.js +525 -0
- package/lib/generated/idl/HTMLCanvasElement.js +307 -0
- package/lib/generated/idl/HTMLCollection.js +352 -0
- package/lib/generated/idl/HTMLDListElement.js +159 -0
- package/lib/generated/idl/HTMLDataElement.js +156 -0
- package/lib/generated/idl/HTMLDataListElement.js +126 -0
- package/lib/generated/idl/HTMLDetailsElement.js +159 -0
- package/lib/generated/idl/HTMLDialogElement.js +159 -0
- package/lib/generated/idl/HTMLDirectoryElement.js +159 -0
- package/lib/generated/idl/HTMLDivElement.js +156 -0
- package/lib/generated/idl/HTMLElement.js +3492 -0
- package/lib/generated/idl/HTMLEmbedElement.js +381 -0
- package/lib/generated/idl/HTMLFieldSetElement.js +332 -0
- package/lib/generated/idl/HTMLFontElement.js +239 -0
- package/lib/generated/idl/HTMLFormControlsCollection.js +318 -0
- package/lib/generated/idl/HTMLFormElement.js +661 -0
- package/lib/generated/idl/HTMLFrameElement.js +513 -0
- package/lib/generated/idl/HTMLFrameSetElement.js +711 -0
- package/lib/generated/idl/HTMLHRElement.js +323 -0
- package/lib/generated/idl/HTMLHeadElement.js +111 -0
- package/lib/generated/idl/HTMLHeadingElement.js +156 -0
- package/lib/generated/idl/HTMLHtmlElement.js +156 -0
- package/lib/generated/idl/HTMLIFrameElement.js +689 -0
- package/lib/generated/idl/HTMLImageElement.js +906 -0
- package/lib/generated/idl/HTMLInputElement.js +1931 -0
- package/lib/generated/idl/HTMLLIElement.js +204 -0
- package/lib/generated/idl/HTMLLabelElement.js +182 -0
- package/lib/generated/idl/HTMLLegendElement.js +169 -0
- package/lib/generated/idl/HTMLLinkElement.js +555 -0
- package/lib/generated/idl/HTMLMapElement.js +171 -0
- package/lib/generated/idl/HTMLMarqueeElement.js +558 -0
- package/lib/generated/idl/HTMLMediaElement.js +900 -0
- package/lib/generated/idl/HTMLMenuElement.js +159 -0
- package/lib/generated/idl/HTMLMetaElement.js +279 -0
- package/lib/generated/idl/HTMLMeterElement.js +368 -0
- package/lib/generated/idl/HTMLModElement.js +217 -0
- package/lib/generated/idl/HTMLOListElement.js +284 -0
- package/lib/generated/idl/HTMLObjectElement.js +945 -0
- package/lib/generated/idl/HTMLOptGroupElement.js +200 -0
- package/lib/generated/idl/HTMLOptionElement.js +379 -0
- package/lib/generated/idl/HTMLOptionsCollection.js +513 -0
- package/lib/generated/idl/HTMLOutputElement.js +395 -0
- package/lib/generated/idl/HTMLParagraphElement.js +156 -0
- package/lib/generated/idl/HTMLParamElement.js +279 -0
- package/lib/generated/idl/HTMLPictureElement.js +111 -0
- package/lib/generated/idl/HTMLPreElement.js +163 -0
- package/lib/generated/idl/HTMLProgressElement.js +232 -0
- package/lib/generated/idl/HTMLQuoteElement.js +176 -0
- package/lib/generated/idl/HTMLScriptElement.js +472 -0
- package/lib/generated/idl/HTMLSelectElement.js +993 -0
- package/lib/generated/idl/HTMLSlotElement.js +195 -0
- package/lib/generated/idl/HTMLSourceElement.js +340 -0
- package/lib/generated/idl/HTMLSpanElement.js +111 -0
- package/lib/generated/idl/HTMLStyleElement.js +210 -0
- package/lib/generated/idl/HTMLTableCaptionElement.js +156 -0
- package/lib/generated/idl/HTMLTableCellElement.js +733 -0
- package/lib/generated/idl/HTMLTableColElement.js +376 -0
- package/lib/generated/idl/HTMLTableElement.js +802 -0
- package/lib/generated/idl/HTMLTableRowElement.js +417 -0
- package/lib/generated/idl/HTMLTableSectionElement.js +349 -0
- package/lib/generated/idl/HTMLTemplateElement.js +124 -0
- package/lib/generated/idl/HTMLTextAreaElement.js +1210 -0
- package/lib/generated/idl/HTMLTimeElement.js +156 -0
- package/lib/generated/idl/HTMLTitleElement.js +155 -0
- package/lib/generated/idl/HTMLTrackElement.js +366 -0
- package/lib/generated/idl/HTMLUListElement.js +200 -0
- package/lib/generated/idl/HTMLUnknownElement.js +109 -0
- package/lib/generated/idl/HTMLVideoElement.js +344 -0
- package/lib/generated/idl/HashChangeEvent.js +157 -0
- package/lib/generated/idl/HashChangeEventInit.js +50 -0
- package/lib/generated/idl/Headers.js +418 -0
- package/lib/generated/idl/History.js +266 -0
- package/lib/generated/idl/InputEvent.js +168 -0
- package/lib/generated/idl/InputEventInit.js +68 -0
- package/lib/generated/idl/KeyboardEvent.js +445 -0
- package/lib/generated/idl/KeyboardEventInit.js +116 -0
- package/lib/generated/idl/Location.js +404 -0
- package/lib/generated/idl/MessageEvent.js +317 -0
- package/lib/generated/idl/MessageEventInit.js +100 -0
- package/lib/generated/idl/MimeType.js +156 -0
- package/lib/generated/idl/MimeTypeArray.js +326 -0
- package/lib/generated/idl/MouseEvent.js +595 -0
- package/lib/generated/idl/MouseEventInit.js +189 -0
- package/lib/generated/idl/MutationCallback.js +34 -0
- package/lib/generated/idl/MutationObserver.js +178 -0
- package/lib/generated/idl/MutationObserverInit.js +121 -0
- package/lib/generated/idl/MutationRecord.js +229 -0
- package/lib/generated/idl/NamedNodeMap.js +529 -0
- package/lib/generated/idl/Navigator.js +326 -0
- package/lib/generated/idl/Node.js +765 -0
- package/lib/generated/idl/NodeFilter.js +75 -0
- package/lib/generated/idl/NodeIterator.js +207 -0
- package/lib/generated/idl/NodeList.js +302 -0
- package/lib/generated/idl/OnBeforeUnloadEventHandlerNonNull.js +42 -0
- package/lib/generated/idl/OnErrorEventHandlerNonNull.js +56 -0
- package/lib/generated/idl/PageTransitionEvent.js +144 -0
- package/lib/generated/idl/PageTransitionEventInit.js +35 -0
- package/lib/generated/idl/Performance.js +142 -0
- package/lib/generated/idl/Plugin.js +359 -0
- package/lib/generated/idl/PluginArray.js +336 -0
- package/lib/generated/idl/PointerEvent.js +324 -0
- package/lib/generated/idl/PointerEventInit.js +241 -0
- package/lib/generated/idl/PopStateEvent.js +144 -0
- package/lib/generated/idl/PopStateEventInit.js +32 -0
- package/lib/generated/idl/ProcessingInstruction.js +122 -0
- package/lib/generated/idl/ProgressEvent.js +170 -0
- package/lib/generated/idl/ProgressEventInit.js +65 -0
- package/lib/generated/idl/PromiseRejectionEvent.js +157 -0
- package/lib/generated/idl/PromiseRejectionEventInit.js +42 -0
- package/lib/generated/idl/RadioNodeList.js +296 -0
- package/lib/generated/idl/Range.js +643 -0
- package/lib/generated/idl/SVGAnimatedPreserveAspectRatio.js +136 -0
- package/lib/generated/idl/SVGAnimatedRect.js +136 -0
- package/lib/generated/idl/SVGAnimatedString.js +149 -0
- package/lib/generated/idl/SVGBoundingBoxOptions.js +64 -0
- package/lib/generated/idl/SVGDefsElement.js +109 -0
- package/lib/generated/idl/SVGDescElement.js +109 -0
- package/lib/generated/idl/SVGElement.js +3157 -0
- package/lib/generated/idl/SVGGElement.js +109 -0
- package/lib/generated/idl/SVGGraphicsElement.js +139 -0
- package/lib/generated/idl/SVGMetadataElement.js +109 -0
- package/lib/generated/idl/SVGNumber.js +132 -0
- package/lib/generated/idl/SVGPreserveAspectRatio.js +196 -0
- package/lib/generated/idl/SVGRect.js +210 -0
- package/lib/generated/idl/SVGSVGElement.js +786 -0
- package/lib/generated/idl/SVGStringList.js +511 -0
- package/lib/generated/idl/SVGSwitchElement.js +109 -0
- package/lib/generated/idl/SVGSymbolElement.js +146 -0
- package/lib/generated/idl/SVGTitleElement.js +109 -0
- package/lib/generated/idl/Screen.js +180 -0
- package/lib/generated/idl/ScrollBehavior.js +12 -0
- package/lib/generated/idl/ScrollIntoViewOptions.js +45 -0
- package/lib/generated/idl/ScrollLogicalPosition.js +14 -0
- package/lib/generated/idl/ScrollOptions.js +30 -0
- package/lib/generated/idl/ScrollRestoration.js +12 -0
- package/lib/generated/idl/Selection.js +571 -0
- package/lib/generated/idl/SelectionMode.js +12 -0
- package/lib/generated/idl/ShadowRoot.js +189 -0
- package/lib/generated/idl/ShadowRootInit.js +30 -0
- package/lib/generated/idl/ShadowRootMode.js +12 -0
- package/lib/generated/idl/StaticRange.js +123 -0
- package/lib/generated/idl/StaticRangeInit.js +72 -0
- package/lib/generated/idl/Storage.js +397 -0
- package/lib/generated/idl/StorageEvent.js +318 -0
- package/lib/generated/idl/StorageEventInit.js +99 -0
- package/lib/generated/idl/StyleSheetList.js +300 -0
- package/lib/generated/idl/SubmitEvent.js +144 -0
- package/lib/generated/idl/SubmitEventInit.js +36 -0
- package/lib/generated/idl/SupportedType.js +18 -0
- package/lib/generated/idl/Text.js +170 -0
- package/lib/generated/idl/TextDecodeOptions.js +28 -0
- package/lib/generated/idl/TextDecoder.js +211 -0
- package/lib/generated/idl/TextDecoderOptions.js +43 -0
- package/lib/generated/idl/TextEncoder.js +176 -0
- package/lib/generated/idl/TextEncoderEncodeIntoResult.js +42 -0
- package/lib/generated/idl/TextTrackKind.js +12 -0
- package/lib/generated/idl/TouchEvent.js +222 -0
- package/lib/generated/idl/TouchEventInit.js +89 -0
- package/lib/generated/idl/TransitionEvent.js +170 -0
- package/lib/generated/idl/TransitionEventInit.js +65 -0
- package/lib/generated/idl/TreeWalker.js +255 -0
- package/lib/generated/idl/UIEvent.js +235 -0
- package/lib/generated/idl/UIEventInit.js +62 -0
- package/lib/generated/idl/ValidityState.js +249 -0
- package/lib/generated/idl/VisibilityState.js +12 -0
- package/lib/generated/idl/VoidFunction.js +26 -0
- package/lib/generated/idl/WebSocket.js +480 -0
- package/lib/generated/idl/WheelEvent.js +191 -0
- package/lib/generated/idl/WheelEventInit.js +71 -0
- package/lib/generated/idl/XMLDocument.js +109 -0
- package/lib/generated/idl/XMLHttpRequest.js +663 -0
- package/lib/generated/idl/XMLHttpRequestEventTarget.js +334 -0
- package/lib/generated/idl/XMLHttpRequestResponseType.js +14 -0
- package/lib/generated/idl/XMLHttpRequestUpload.js +109 -0
- package/lib/generated/idl/XMLSerializer.js +132 -0
- package/lib/generated/idl/utils.js +252 -0
- package/lib/generated/js-globals.json +332 -0
- package/lib/jsdom/browser/Window.js +952 -0
- package/lib/jsdom/browser/default-stylesheet.css +415 -0
- package/lib/jsdom/browser/not-implemented.js +20 -0
- package/lib/jsdom/browser/parser/html.js +208 -0
- package/lib/jsdom/browser/parser/index.js +37 -0
- package/lib/jsdom/browser/parser/xml.js +202 -0
- package/lib/jsdom/browser/resources/async-resource-queue.js +114 -0
- package/lib/jsdom/browser/resources/decompress-interceptor.js +184 -0
- package/lib/jsdom/browser/resources/jsdom-dispatcher.js +756 -0
- package/lib/jsdom/browser/resources/per-document-resource-loader.js +116 -0
- package/lib/jsdom/browser/resources/request-interceptor.js +171 -0
- package/lib/jsdom/browser/resources/request-manager.js +33 -0
- package/lib/jsdom/browser/resources/resource-queue.js +142 -0
- package/lib/jsdom/browser/resources/stream-handler.js +89 -0
- package/lib/jsdom/level2/style.js +65 -0
- package/lib/jsdom/level3/xpath.js +1875 -0
- package/lib/jsdom/living/aborting/AbortController-impl.js +17 -0
- package/lib/jsdom/living/aborting/AbortSignal-impl.js +131 -0
- package/lib/jsdom/living/attributes/Attr-impl.js +60 -0
- package/lib/jsdom/living/attributes/NamedNodeMap-impl.js +78 -0
- package/lib/jsdom/living/attributes.js +312 -0
- package/lib/jsdom/living/constraint-validation/DefaultConstraintValidation-impl.js +75 -0
- package/lib/jsdom/living/constraint-validation/ValidityState-impl.js +66 -0
- package/lib/jsdom/living/crypto/Crypto-impl.js +68 -0
- package/lib/jsdom/living/cssom/StyleSheetList-impl.js +38 -0
- package/lib/jsdom/living/custom-elements/CustomElementRegistry-impl.js +279 -0
- package/lib/jsdom/living/custom-elements/ElementInternals-impl.js +56 -0
- package/lib/jsdom/living/deviceorientation/DeviceMotionEventAcceleration-impl.js +7 -0
- package/lib/jsdom/living/deviceorientation/DeviceMotionEventRotationRate-impl.js +7 -0
- package/lib/jsdom/living/documents.js +15 -0
- package/lib/jsdom/living/domparsing/DOMParser-impl.js +59 -0
- package/lib/jsdom/living/domparsing/InnerHTML-impl.js +30 -0
- package/lib/jsdom/living/domparsing/XMLSerializer-impl.js +18 -0
- package/lib/jsdom/living/domparsing/parse5-adapter-serialization.js +63 -0
- package/lib/jsdom/living/domparsing/serialization.js +36 -0
- package/lib/jsdom/living/encoding/TextDecoder-impl.js +25 -0
- package/lib/jsdom/living/encoding/TextEncoder-impl.js +26 -0
- package/lib/jsdom/living/events/BeforeUnloadEvent-impl.js +12 -0
- package/lib/jsdom/living/events/BlobEvent-impl.js +14 -0
- package/lib/jsdom/living/events/CloseEvent-impl.js +10 -0
- package/lib/jsdom/living/events/CompositionEvent-impl.js +20 -0
- package/lib/jsdom/living/events/CustomEvent-impl.js +21 -0
- package/lib/jsdom/living/events/DeviceMotionEvent-impl.js +49 -0
- package/lib/jsdom/living/events/DeviceOrientationEvent-impl.js +10 -0
- package/lib/jsdom/living/events/ErrorEvent-impl.js +14 -0
- package/lib/jsdom/living/events/Event-impl.js +195 -0
- package/lib/jsdom/living/events/EventModifierMixin-impl.js +24 -0
- package/lib/jsdom/living/events/EventTarget-impl.js +438 -0
- package/lib/jsdom/living/events/FocusEvent-impl.js +9 -0
- package/lib/jsdom/living/events/HashChangeEvent-impl.js +14 -0
- package/lib/jsdom/living/events/InputEvent-impl.js +11 -0
- package/lib/jsdom/living/events/KeyboardEvent-impl.js +29 -0
- package/lib/jsdom/living/events/MessageEvent-impl.js +25 -0
- package/lib/jsdom/living/events/MouseEvent-impl.js +72 -0
- package/lib/jsdom/living/events/PageTransitionEvent-impl.js +20 -0
- package/lib/jsdom/living/events/PointerEvent-impl.js +21 -0
- package/lib/jsdom/living/events/PopStateEvent-impl.js +9 -0
- package/lib/jsdom/living/events/ProgressEvent-impl.js +14 -0
- package/lib/jsdom/living/events/PromiseRejectionEvent-impl.js +14 -0
- package/lib/jsdom/living/events/StorageEvent-impl.js +26 -0
- package/lib/jsdom/living/events/SubmitEvent-impl.js +13 -0
- package/lib/jsdom/living/events/TouchEvent-impl.js +14 -0
- package/lib/jsdom/living/events/TransitionEvent-impl.js +10 -0
- package/lib/jsdom/living/events/UIEvent-impl.js +43 -0
- package/lib/jsdom/living/events/WheelEvent-impl.js +12 -0
- package/lib/jsdom/living/fetch/Headers-impl.js +173 -0
- package/lib/jsdom/living/fetch/header-list.js +158 -0
- package/lib/jsdom/living/fetch/header-types.js +205 -0
- package/lib/jsdom/living/fetch/header-utils.js +26 -0
- package/lib/jsdom/living/file-api/Blob-impl.js +125 -0
- package/lib/jsdom/living/file-api/File-impl.js +12 -0
- package/lib/jsdom/living/file-api/FileList-impl.js +15 -0
- package/lib/jsdom/living/file-api/FileReader-impl.js +148 -0
- package/lib/jsdom/living/geometry/DOMRect-impl.js +39 -0
- package/lib/jsdom/living/geometry/DOMRectReadOnly-impl.js +72 -0
- package/lib/jsdom/living/helpers/binary-data.js +63 -0
- package/lib/jsdom/living/helpers/by-id-cache.js +67 -0
- package/lib/jsdom/living/helpers/colors.js +245 -0
- package/lib/jsdom/living/helpers/create-element.js +329 -0
- package/lib/jsdom/living/helpers/create-event-accessor.js +209 -0
- package/lib/jsdom/living/helpers/custom-elements.js +272 -0
- package/lib/jsdom/living/helpers/dates-and-times.js +270 -0
- package/lib/jsdom/living/helpers/details.js +15 -0
- package/lib/jsdom/living/helpers/encoding.js +60 -0
- package/lib/jsdom/living/helpers/events.js +24 -0
- package/lib/jsdom/living/helpers/focusing.js +104 -0
- package/lib/jsdom/living/helpers/form-controls.js +309 -0
- package/lib/jsdom/living/helpers/html-constructor.js +78 -0
- package/lib/jsdom/living/helpers/internal-constants.js +12 -0
- package/lib/jsdom/living/helpers/is-window.js +18 -0
- package/lib/jsdom/living/helpers/iterable-weak-set.js +48 -0
- package/lib/jsdom/living/helpers/json.js +7 -0
- package/lib/jsdom/living/helpers/mutation-observers.js +198 -0
- package/lib/jsdom/living/helpers/namespaces.js +16 -0
- package/lib/jsdom/living/helpers/node.js +108 -0
- package/lib/jsdom/living/helpers/number-and-date-inputs.js +195 -0
- package/lib/jsdom/living/helpers/ordered-set.js +104 -0
- package/lib/jsdom/living/helpers/page-transition-event.js +9 -0
- package/lib/jsdom/living/helpers/runtime-script-errors.js +75 -0
- package/lib/jsdom/living/helpers/shadow-dom.js +285 -0
- package/lib/jsdom/living/helpers/strings.js +168 -0
- package/lib/jsdom/living/helpers/style-rules.js +404 -0
- package/lib/jsdom/living/helpers/stylesheets.js +148 -0
- package/lib/jsdom/living/helpers/svg/basic-types.js +41 -0
- package/lib/jsdom/living/helpers/svg/render.js +46 -0
- package/lib/jsdom/living/helpers/system-colors.js +147 -0
- package/lib/jsdom/living/helpers/text.js +19 -0
- package/lib/jsdom/living/helpers/traversal.js +72 -0
- package/lib/jsdom/living/helpers/validate-names.js +65 -0
- package/lib/jsdom/living/hr-time/Performance-impl.js +22 -0
- package/lib/jsdom/living/interfaces.js +252 -0
- package/lib/jsdom/living/mutation-observer/MutationObserver-impl.js +95 -0
- package/lib/jsdom/living/mutation-observer/MutationRecord-impl.js +37 -0
- package/lib/jsdom/living/navigator/MimeType-impl.js +3 -0
- package/lib/jsdom/living/navigator/MimeTypeArray-impl.js +21 -0
- package/lib/jsdom/living/navigator/Navigator-impl.js +29 -0
- package/lib/jsdom/living/navigator/NavigatorConcurrentHardware-impl.js +8 -0
- package/lib/jsdom/living/navigator/NavigatorCookies-impl.js +7 -0
- package/lib/jsdom/living/navigator/NavigatorID-impl.js +37 -0
- package/lib/jsdom/living/navigator/NavigatorLanguage-impl.js +9 -0
- package/lib/jsdom/living/navigator/NavigatorOnLine-impl.js +7 -0
- package/lib/jsdom/living/navigator/NavigatorPlugins-impl.js +8 -0
- package/lib/jsdom/living/navigator/Plugin-impl.js +3 -0
- package/lib/jsdom/living/navigator/PluginArray-impl.js +23 -0
- package/lib/jsdom/living/node-document-position.js +10 -0
- package/lib/jsdom/living/node-type.js +16 -0
- package/lib/jsdom/living/node.js +331 -0
- package/lib/jsdom/living/nodes/CDATASection-impl.js +16 -0
- package/lib/jsdom/living/nodes/CharacterData-impl.js +118 -0
- package/lib/jsdom/living/nodes/ChildNode-impl.js +80 -0
- package/lib/jsdom/living/nodes/Comment-impl.js +20 -0
- package/lib/jsdom/living/nodes/DOMImplementation-impl.js +120 -0
- package/lib/jsdom/living/nodes/DOMStringMap-impl.js +64 -0
- package/lib/jsdom/living/nodes/DOMTokenList-impl.js +171 -0
- package/lib/jsdom/living/nodes/Document-impl.js +1000 -0
- package/lib/jsdom/living/nodes/DocumentFragment-impl.js +44 -0
- package/lib/jsdom/living/nodes/DocumentOrShadowRoot-impl.js +28 -0
- package/lib/jsdom/living/nodes/DocumentType-impl.js +24 -0
- package/lib/jsdom/living/nodes/Element-impl.js +584 -0
- package/lib/jsdom/living/nodes/ElementCSSInlineStyle-impl.js +22 -0
- package/lib/jsdom/living/nodes/ElementContentEditable-impl.js +7 -0
- package/lib/jsdom/living/nodes/GlobalEventHandlers-impl.js +81 -0
- package/lib/jsdom/living/nodes/HTMLAnchorElement-impl.js +50 -0
- package/lib/jsdom/living/nodes/HTMLAreaElement-impl.js +43 -0
- package/lib/jsdom/living/nodes/HTMLAudioElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLBRElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLBaseElement-impl.js +44 -0
- package/lib/jsdom/living/nodes/HTMLBodyElement-impl.js +12 -0
- package/lib/jsdom/living/nodes/HTMLButtonElement-impl.js +79 -0
- package/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js +136 -0
- package/lib/jsdom/living/nodes/HTMLCollection-impl.js +96 -0
- package/lib/jsdom/living/nodes/HTMLDListElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLDataElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLDataListElement-impl.js +20 -0
- package/lib/jsdom/living/nodes/HTMLDetailsElement-impl.js +35 -0
- package/lib/jsdom/living/nodes/HTMLDialogElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLDirectoryElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLDivElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLElement-impl.js +211 -0
- package/lib/jsdom/living/nodes/HTMLEmbedElement-impl.js +8 -0
- package/lib/jsdom/living/nodes/HTMLFieldSetElement-impl.js +43 -0
- package/lib/jsdom/living/nodes/HTMLFontElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLFormControlsCollection-impl.js +33 -0
- package/lib/jsdom/living/nodes/HTMLFormElement-impl.js +248 -0
- package/lib/jsdom/living/nodes/HTMLFrameElement-impl.js +266 -0
- package/lib/jsdom/living/nodes/HTMLFrameSetElement-impl.js +12 -0
- package/lib/jsdom/living/nodes/HTMLHRElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLHeadElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLHeadingElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLHtmlElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLHyperlinkElementUtils-impl.js +368 -0
- package/lib/jsdom/living/nodes/HTMLIFrameElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLImageElement-impl.js +127 -0
- package/lib/jsdom/living/nodes/HTMLInputElement-impl.js +1097 -0
- package/lib/jsdom/living/nodes/HTMLLIElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLLabelElement-impl.js +94 -0
- package/lib/jsdom/living/nodes/HTMLLegendElement-impl.js +18 -0
- package/lib/jsdom/living/nodes/HTMLLinkElement-impl.js +107 -0
- package/lib/jsdom/living/nodes/HTMLMapElement-impl.js +13 -0
- package/lib/jsdom/living/nodes/HTMLMarqueeElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLMediaElement-impl.js +138 -0
- package/lib/jsdom/living/nodes/HTMLMenuElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLMetaElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLMeterElement-impl.js +180 -0
- package/lib/jsdom/living/nodes/HTMLModElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLOListElement-impl.js +22 -0
- package/lib/jsdom/living/nodes/HTMLObjectElement-impl.js +26 -0
- package/lib/jsdom/living/nodes/HTMLOptGroupElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLOptionElement-impl.js +146 -0
- package/lib/jsdom/living/nodes/HTMLOptionsCollection-impl.js +110 -0
- package/lib/jsdom/living/nodes/HTMLOrSVGElement-impl.js +88 -0
- package/lib/jsdom/living/nodes/HTMLOutputElement-impl.js +88 -0
- package/lib/jsdom/living/nodes/HTMLParagraphElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLParamElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLPictureElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLPreElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLProgressElement-impl.js +72 -0
- package/lib/jsdom/living/nodes/HTMLQuoteElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLScriptElement-impl.js +255 -0
- package/lib/jsdom/living/nodes/HTMLSelectElement-impl.js +283 -0
- package/lib/jsdom/living/nodes/HTMLSlotElement-impl.js +59 -0
- package/lib/jsdom/living/nodes/HTMLSourceElement-impl.js +8 -0
- package/lib/jsdom/living/nodes/HTMLSpanElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLStyleElement-impl.js +76 -0
- package/lib/jsdom/living/nodes/HTMLTableCaptionElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLTableCellElement-impl.js +73 -0
- package/lib/jsdom/living/nodes/HTMLTableColElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLTableElement-impl.js +236 -0
- package/lib/jsdom/living/nodes/HTMLTableRowElement-impl.js +88 -0
- package/lib/jsdom/living/nodes/HTMLTableSectionElement-impl.js +61 -0
- package/lib/jsdom/living/nodes/HTMLTemplateElement-impl.js +67 -0
- package/lib/jsdom/living/nodes/HTMLTextAreaElement-impl.js +244 -0
- package/lib/jsdom/living/nodes/HTMLTimeElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLTitleElement-impl.js +18 -0
- package/lib/jsdom/living/nodes/HTMLTrackElement-impl.js +13 -0
- package/lib/jsdom/living/nodes/HTMLUListElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLUnknownElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/HTMLVideoElement-impl.js +17 -0
- package/lib/jsdom/living/nodes/LinkStyle-impl.js +2 -0
- package/lib/jsdom/living/nodes/Node-impl.js +1179 -0
- package/lib/jsdom/living/nodes/NodeList-impl.js +43 -0
- package/lib/jsdom/living/nodes/NonDocumentTypeChildNode-impl.js +28 -0
- package/lib/jsdom/living/nodes/NonElementParentNode-impl.js +11 -0
- package/lib/jsdom/living/nodes/ParentNode-impl.js +90 -0
- package/lib/jsdom/living/nodes/ProcessingInstruction-impl.js +22 -0
- package/lib/jsdom/living/nodes/RadioNodeList-impl.js +49 -0
- package/lib/jsdom/living/nodes/SVGDefsElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGDescElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGElement-impl.js +64 -0
- package/lib/jsdom/living/nodes/SVGGElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGGraphicsElement-impl.js +16 -0
- package/lib/jsdom/living/nodes/SVGMetadataElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGSVGElement-impl.js +40 -0
- package/lib/jsdom/living/nodes/SVGSwitchElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGSymbolElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/SVGTests-impl.js +42 -0
- package/lib/jsdom/living/nodes/SVGTitleElement-impl.js +9 -0
- package/lib/jsdom/living/nodes/ShadowRoot-impl.js +41 -0
- package/lib/jsdom/living/nodes/Slotable-impl.js +48 -0
- package/lib/jsdom/living/nodes/Text-impl.js +96 -0
- package/lib/jsdom/living/nodes/WindowEventHandlers-impl.js +14 -0
- package/lib/jsdom/living/nodes/XMLDocument-impl.js +4 -0
- package/lib/jsdom/living/range/AbstractRange-impl.js +43 -0
- package/lib/jsdom/living/range/Range-impl.js +902 -0
- package/lib/jsdom/living/range/StaticRange-impl.js +39 -0
- package/lib/jsdom/living/range/boundary-point.js +47 -0
- package/lib/jsdom/living/selection/Selection-impl.js +358 -0
- package/lib/jsdom/living/svg/SVGAnimatedPreserveAspectRatio-impl.js +24 -0
- package/lib/jsdom/living/svg/SVGAnimatedRect-impl.js +122 -0
- package/lib/jsdom/living/svg/SVGAnimatedString-impl.js +42 -0
- package/lib/jsdom/living/svg/SVGListBase.js +195 -0
- package/lib/jsdom/living/svg/SVGNumber-impl.js +48 -0
- package/lib/jsdom/living/svg/SVGPreserveAspectRatio-impl.js +100 -0
- package/lib/jsdom/living/svg/SVGRect-impl.js +143 -0
- package/lib/jsdom/living/svg/SVGStringList-impl.js +16 -0
- package/lib/jsdom/living/traversal/NodeIterator-impl.js +107 -0
- package/lib/jsdom/living/traversal/TreeWalker-impl.js +217 -0
- package/lib/jsdom/living/traversal/helpers.js +44 -0
- package/lib/jsdom/living/webidl/DOMException-impl.js +46 -0
- package/lib/jsdom/living/websockets/WebSocket-impl.js +211 -0
- package/lib/jsdom/living/webstorage/Storage-impl.js +102 -0
- package/lib/jsdom/living/window/BarProp-impl.js +10 -0
- package/lib/jsdom/living/window/External-impl.js +9 -0
- package/lib/jsdom/living/window/History-impl.js +148 -0
- package/lib/jsdom/living/window/Location-impl.js +227 -0
- package/lib/jsdom/living/window/Screen-impl.js +13 -0
- package/lib/jsdom/living/window/SessionHistory.js +163 -0
- package/lib/jsdom/living/window/navigation.js +87 -0
- package/lib/jsdom/living/window-properties.js +241 -0
- package/lib/jsdom/living/xhr/FormData-impl.js +191 -0
- package/lib/jsdom/living/xhr/XMLHttpRequest-impl.js +1057 -0
- package/lib/jsdom/living/xhr/XMLHttpRequestEventTarget-impl.js +17 -0
- package/lib/jsdom/living/xhr/XMLHttpRequestUpload-impl.js +4 -0
- package/lib/jsdom/living/xhr/multipart-form-data.js +99 -0
- package/lib/jsdom/living/xhr/xhr-sync-worker.js +51 -0
- package/lib/jsdom/living/xhr/xhr-utils.js +206 -0
- package/lib/jsdom/utils.js +105 -0
- package/lib/jsdom/virtual-console.js +46 -0
- package/package.json +143 -0
|
@@ -0,0 +1,1875 @@
|
|
|
1
|
+
/** Here is yet another implementation of XPath 1.0 in Javascript.
|
|
2
|
+
*
|
|
3
|
+
* My goal was to make it relatively compact, but as I fixed all the axis bugs
|
|
4
|
+
* the axes became more and more complicated. :-(.
|
|
5
|
+
*
|
|
6
|
+
* I have not implemented namespaces or case-sensitive axes for XML yet.
|
|
7
|
+
*
|
|
8
|
+
* How to test it in Chrome: You can make a Chrome extension that replaces
|
|
9
|
+
* the WebKit XPath parser with this one. But it takes a bit of effort to
|
|
10
|
+
* get around isolated world and same-origin restrictions:
|
|
11
|
+
* manifest.json:
|
|
12
|
+
{
|
|
13
|
+
"name": "XPathTest",
|
|
14
|
+
"version": "0.1",
|
|
15
|
+
"content_scripts": [{
|
|
16
|
+
"matches": ["http://localhost/*"], // or wildcard host
|
|
17
|
+
"js": ["xpath.js", "injection.js"],
|
|
18
|
+
"all_frames": true, "run_at": "document_start"
|
|
19
|
+
}]
|
|
20
|
+
}
|
|
21
|
+
* injection.js:
|
|
22
|
+
// goal: give my xpath object to the website's JS context.
|
|
23
|
+
var script = document.createElement('script');
|
|
24
|
+
script.textContent =
|
|
25
|
+
"document.addEventListener('xpathextend', function(e) {\n" +
|
|
26
|
+
" console.log('extending document with xpath...');\n" +
|
|
27
|
+
" e.detail(window);" +
|
|
28
|
+
"});";
|
|
29
|
+
document.documentElement.appendChild(script);
|
|
30
|
+
document.documentElement.removeChild(script);
|
|
31
|
+
var evt = document.createEvent('CustomEvent');
|
|
32
|
+
evt.initCustomEvent('xpathextend', true, true, this.xpath.extend);
|
|
33
|
+
document.dispatchEvent(evt);
|
|
34
|
+
*/
|
|
35
|
+
module.exports = core => {
|
|
36
|
+
var xpath = {};
|
|
37
|
+
|
|
38
|
+
// Helper function to deal with the migration of Attr to no longer have a nodeName property despite this codebase
|
|
39
|
+
// assuming it does.
|
|
40
|
+
function getNodeName(nodeOrAttr) {
|
|
41
|
+
return nodeOrAttr.constructor.name === 'Attr' ? nodeOrAttr.name : nodeOrAttr.nodeName;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/***************************************************************************
|
|
45
|
+
* Tokenization *
|
|
46
|
+
***************************************************************************/
|
|
47
|
+
/**
|
|
48
|
+
* The XPath lexer is basically a single regular expression, along with
|
|
49
|
+
* some helper functions to pop different types.
|
|
50
|
+
*/
|
|
51
|
+
var Stream = xpath.Stream = function Stream(str) {
|
|
52
|
+
this.original = this.str = str;
|
|
53
|
+
this.peeked = null;
|
|
54
|
+
// TODO: not really needed, but supposedly tokenizer also disambiguates
|
|
55
|
+
// a * b vs. node test *
|
|
56
|
+
this.prev = null; // for debugging
|
|
57
|
+
this.prevprev = null;
|
|
58
|
+
}
|
|
59
|
+
Stream.prototype = {
|
|
60
|
+
peek: function() {
|
|
61
|
+
if (this.peeked) return this.peeked;
|
|
62
|
+
var m = this.re.exec(this.str);
|
|
63
|
+
if (!m) return null;
|
|
64
|
+
this.str = this.str.substr(m[0].length);
|
|
65
|
+
return this.peeked = m[1];
|
|
66
|
+
},
|
|
67
|
+
/** Peek 2 tokens ahead. */
|
|
68
|
+
peek2: function() {
|
|
69
|
+
this.peek(); // make sure this.peeked is set
|
|
70
|
+
var m = this.re.exec(this.str);
|
|
71
|
+
if (!m) return null;
|
|
72
|
+
return m[1];
|
|
73
|
+
},
|
|
74
|
+
pop: function() {
|
|
75
|
+
var r = this.peek();
|
|
76
|
+
this.peeked = null;
|
|
77
|
+
this.prevprev = this.prev;
|
|
78
|
+
this.prev = r;
|
|
79
|
+
return r;
|
|
80
|
+
},
|
|
81
|
+
trypop: function(tokens) {
|
|
82
|
+
var tok = this.peek();
|
|
83
|
+
if (tok === tokens) return this.pop();
|
|
84
|
+
if (Array.isArray(tokens)) {
|
|
85
|
+
for (var i = 0; i < tokens.length; ++i) {
|
|
86
|
+
var t = tokens[i];
|
|
87
|
+
if (t == tok) return this.pop();;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
trypopfuncname: function() {
|
|
92
|
+
var tok = this.peek();
|
|
93
|
+
if (!this.isQnameRe.test(tok))
|
|
94
|
+
return null;
|
|
95
|
+
switch (tok) {
|
|
96
|
+
case 'comment': case 'text': case 'processing-instruction': case 'node':
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if ('(' != this.peek2()) return null;
|
|
100
|
+
return this.pop();
|
|
101
|
+
},
|
|
102
|
+
trypopaxisname: function() {
|
|
103
|
+
var tok = this.peek();
|
|
104
|
+
switch (tok) {
|
|
105
|
+
case 'ancestor': case 'ancestor-or-self': case 'attribute':
|
|
106
|
+
case 'child': case 'descendant': case 'descendant-or-self':
|
|
107
|
+
case 'following': case 'following-sibling': case 'namespace':
|
|
108
|
+
case 'parent': case 'preceding': case 'preceding-sibling': case 'self':
|
|
109
|
+
if ('::' == this.peek2()) return this.pop();
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
},
|
|
113
|
+
trypopnametest: function() {
|
|
114
|
+
var tok = this.peek();
|
|
115
|
+
if ('*' === tok || this.startsWithNcNameRe.test(tok)) return this.pop();
|
|
116
|
+
return null;
|
|
117
|
+
},
|
|
118
|
+
trypopliteral: function() {
|
|
119
|
+
var tok = this.peek();
|
|
120
|
+
if (null == tok) return null;
|
|
121
|
+
var first = tok.charAt(0);
|
|
122
|
+
var last = tok.charAt(tok.length - 1);
|
|
123
|
+
if ('"' === first && '"' === last ||
|
|
124
|
+
"'" === first && "'" === last) {
|
|
125
|
+
this.pop();
|
|
126
|
+
return tok.substr(1, tok.length - 2) ?? null;
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
},
|
|
130
|
+
trypopnumber: function() {
|
|
131
|
+
var tok = this.peek();
|
|
132
|
+
if (this.isNumberRe.test(tok)) return parseFloat(this.pop()) ?? null;
|
|
133
|
+
else return null;
|
|
134
|
+
},
|
|
135
|
+
trypopvarref: function() {
|
|
136
|
+
var tok = this.peek();
|
|
137
|
+
if (null == tok) return null;
|
|
138
|
+
if ('$' === tok.charAt(0)) return this.pop().substr(1) ?? null;
|
|
139
|
+
else return null;
|
|
140
|
+
},
|
|
141
|
+
position: function() {
|
|
142
|
+
return this.original.length - this.str.length;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
(function() {
|
|
146
|
+
// http://www.w3.org/TR/REC-xml-names/#NT-NCName
|
|
147
|
+
var nameStartCharsExceptColon =
|
|
148
|
+
'A-Z_a-z\xc0-\xd6\xd8-\xf6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF' +
|
|
149
|
+
'\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF' +
|
|
150
|
+
'\uFDF0-\uFFFD'; // JS doesn't support [#x10000-#xEFFFF]
|
|
151
|
+
var nameCharExceptColon = nameStartCharsExceptColon +
|
|
152
|
+
'\\-\\.0-9\xb7\u0300-\u036F\u203F-\u2040';
|
|
153
|
+
var ncNameChars = '[' + nameStartCharsExceptColon +
|
|
154
|
+
'][' + nameCharExceptColon + ']*'
|
|
155
|
+
// http://www.w3.org/TR/REC-xml-names/#NT-QName
|
|
156
|
+
var qNameChars = ncNameChars + '(?::' + ncNameChars + ')?';
|
|
157
|
+
var otherChars = '\\.\\.|[\\(\\)\\[\\].@,]|::'; // .. must come before [.]
|
|
158
|
+
var operatorChars =
|
|
159
|
+
'and|or|mod|div|' +
|
|
160
|
+
'//|!=|<=|>=|[*/|+\\-=<>]'; // //, !=, <=, >= before individual ones.
|
|
161
|
+
var literal = '"[^"]*"|' + "'[^']*'";
|
|
162
|
+
var numberChars = '[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+';
|
|
163
|
+
var variableReference = '\\$' + qNameChars;
|
|
164
|
+
var nameTestChars = '\\*|' + ncNameChars + ':\\*|' + qNameChars;
|
|
165
|
+
var optionalSpace = '[ \t\r\n]*'; // stricter than regexp \s.
|
|
166
|
+
var nodeType = 'comment|text|processing-instruction|node';
|
|
167
|
+
var re = new RegExp(
|
|
168
|
+
// numberChars before otherChars so that leading-decimal doesn't become .
|
|
169
|
+
'^' + optionalSpace + '(' + numberChars + '|' + otherChars + '|' +
|
|
170
|
+
nameTestChars + '|' + operatorChars + '|' + literal + '|' +
|
|
171
|
+
variableReference + ')'
|
|
172
|
+
// operatorName | nodeType | functionName | axisName are lumped into
|
|
173
|
+
// qName for now; we'll check them on pop.
|
|
174
|
+
);
|
|
175
|
+
Stream.prototype.re = re;
|
|
176
|
+
Stream.prototype.startsWithNcNameRe = new RegExp('^' + ncNameChars);
|
|
177
|
+
Stream.prototype.isQnameRe = new RegExp('^' + qNameChars + '$');
|
|
178
|
+
Stream.prototype.isNumberRe = new RegExp('^' + numberChars + '$');
|
|
179
|
+
})();
|
|
180
|
+
|
|
181
|
+
/***************************************************************************
|
|
182
|
+
* Parsing *
|
|
183
|
+
***************************************************************************/
|
|
184
|
+
var parse = xpath.parse = function parse(stream, a) {
|
|
185
|
+
var r = orExpr(stream,a);
|
|
186
|
+
var x, unparsed = [];
|
|
187
|
+
while (x = stream.pop()) {
|
|
188
|
+
unparsed.push(x);
|
|
189
|
+
}
|
|
190
|
+
if (unparsed.length)
|
|
191
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
192
|
+
'Position ' + stream.position() +
|
|
193
|
+
': Unparsed tokens: ' + unparsed.join(' '));
|
|
194
|
+
return r;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* binaryL ::= subExpr
|
|
199
|
+
* | binaryL op subExpr
|
|
200
|
+
* so a op b op c becomes ((a op b) op c)
|
|
201
|
+
*/
|
|
202
|
+
function binaryL(subExpr, stream, a, ops) {
|
|
203
|
+
var lhs = subExpr(stream, a);
|
|
204
|
+
if (lhs == null) return null;
|
|
205
|
+
var op;
|
|
206
|
+
while (op = stream.trypop(ops)) {
|
|
207
|
+
var rhs = subExpr(stream, a);
|
|
208
|
+
if (rhs == null)
|
|
209
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
210
|
+
'Position ' + stream.position() +
|
|
211
|
+
': Expected something after ' + op);
|
|
212
|
+
lhs = a.node(op, lhs, rhs);
|
|
213
|
+
}
|
|
214
|
+
return lhs;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Too bad this is never used. If they made a ** operator (raise to power),
|
|
218
|
+
( we would use it.
|
|
219
|
+
* binaryR ::= subExpr
|
|
220
|
+
* | subExpr op binaryR
|
|
221
|
+
* so a op b op c becomes (a op (b op c))
|
|
222
|
+
*/
|
|
223
|
+
function binaryR(subExpr, stream, a, ops) {
|
|
224
|
+
var lhs = subExpr(stream, a);
|
|
225
|
+
if (lhs == null) return null;
|
|
226
|
+
var op = stream.trypop(ops);
|
|
227
|
+
if (op) {
|
|
228
|
+
var rhs = binaryR(stream, a);
|
|
229
|
+
if (rhs == null)
|
|
230
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
231
|
+
'Position ' + stream.position() +
|
|
232
|
+
': Expected something after ' + op);
|
|
233
|
+
return a.node(op, lhs, rhs);
|
|
234
|
+
} else {
|
|
235
|
+
return lhs;// TODO
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/** [1] LocationPath::= RelativeLocationPath | AbsoluteLocationPath
|
|
239
|
+
* e.g. a, a/b, //a/b
|
|
240
|
+
*/
|
|
241
|
+
function locationPath(stream, a) {
|
|
242
|
+
return absoluteLocationPath(stream, a) ||
|
|
243
|
+
relativeLocationPath(null, stream, a);
|
|
244
|
+
}
|
|
245
|
+
/** [2] AbsoluteLocationPath::= '/' RelativeLocationPath? | AbbreviatedAbsoluteLocationPath
|
|
246
|
+
* [10] AbbreviatedAbsoluteLocationPath::= '//' RelativeLocationPath
|
|
247
|
+
*/
|
|
248
|
+
function absoluteLocationPath(stream, a) {
|
|
249
|
+
var op = stream.peek();
|
|
250
|
+
if ('/' === op || '//' === op) {
|
|
251
|
+
var lhs = a.node('Root');
|
|
252
|
+
return relativeLocationPath(lhs, stream, a, true);
|
|
253
|
+
} else {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** [3] RelativeLocationPath::= Step | RelativeLocationPath '/' Step |
|
|
258
|
+
* | AbbreviatedRelativeLocationPath
|
|
259
|
+
* [11] AbbreviatedRelativeLocationPath::= RelativeLocationPath '//' Step
|
|
260
|
+
* e.g. p/a, etc.
|
|
261
|
+
*/
|
|
262
|
+
function relativeLocationPath(lhs, stream, a, isOnlyRootOk) {
|
|
263
|
+
if (null == lhs) {
|
|
264
|
+
lhs = step(stream, a);
|
|
265
|
+
if (null == lhs) return lhs;
|
|
266
|
+
}
|
|
267
|
+
var op;
|
|
268
|
+
while (op = stream.trypop(['/', '//'])) {
|
|
269
|
+
if ('//' === op) {
|
|
270
|
+
lhs = a.node('/', lhs,
|
|
271
|
+
a.node('Axis', 'descendant-or-self', 'node', undefined));
|
|
272
|
+
}
|
|
273
|
+
var rhs = step(stream, a);
|
|
274
|
+
if (null == rhs && '/' === op && isOnlyRootOk) return lhs;
|
|
275
|
+
else isOnlyRootOk = false;
|
|
276
|
+
if (null == rhs)
|
|
277
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
278
|
+
'Position ' + stream.position() +
|
|
279
|
+
': Expected step after ' + op);
|
|
280
|
+
lhs = a.node('/', lhs, rhs);
|
|
281
|
+
}
|
|
282
|
+
return lhs;
|
|
283
|
+
}
|
|
284
|
+
/** [4] Step::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep
|
|
285
|
+
* [12] AbbreviatedStep::= '.' | '..'
|
|
286
|
+
* e.g. @href, self::p, p, a[@href], ., ..
|
|
287
|
+
*/
|
|
288
|
+
function step(stream, a) {
|
|
289
|
+
var abbrStep = stream.trypop(['.', '..']);
|
|
290
|
+
if ('.' === abbrStep) // A location step of . is short for self::node().
|
|
291
|
+
return a.node('Axis', 'self', 'node');
|
|
292
|
+
if ('..' === abbrStep) // A location step of .. is short for parent::node()
|
|
293
|
+
return a.node('Axis', 'parent', 'node');
|
|
294
|
+
|
|
295
|
+
var axis = axisSpecifier(stream, a);
|
|
296
|
+
var nodeType = nodeTypeTest(stream, a);
|
|
297
|
+
var nodeName;
|
|
298
|
+
if (null == nodeType) nodeName = nodeNameTest(stream, a);
|
|
299
|
+
if (null == axis && null == nodeType && null == nodeName) return null;
|
|
300
|
+
if (null == nodeType && null == nodeName)
|
|
301
|
+
throw new XPathException(
|
|
302
|
+
XPathException.INVALID_EXPRESSION_ERR,
|
|
303
|
+
'Position ' + stream.position() +
|
|
304
|
+
': Expected nodeTest after axisSpecifier ' + axis);
|
|
305
|
+
if (null == axis) axis = 'child';
|
|
306
|
+
if (null == nodeType) {
|
|
307
|
+
// When there's only a node name, then the node type is forced to be the
|
|
308
|
+
// principal node type of the axis.
|
|
309
|
+
// see http://www.w3.org/TR/xpath/#dt-principal-node-type
|
|
310
|
+
if ('attribute' === axis) nodeType = 'attribute';
|
|
311
|
+
else if ('namespace' === axis) nodeType = 'namespace';
|
|
312
|
+
else nodeType = 'element';
|
|
313
|
+
}
|
|
314
|
+
var lhs = a.node('Axis', axis, nodeType, nodeName);
|
|
315
|
+
var pred;
|
|
316
|
+
while (null != (pred = predicate(lhs, stream, a))) {
|
|
317
|
+
lhs = pred;
|
|
318
|
+
}
|
|
319
|
+
return lhs;
|
|
320
|
+
}
|
|
321
|
+
/** [5] AxisSpecifier::= AxisName '::' | AbbreviatedAxisSpecifier
|
|
322
|
+
* [6] AxisName::= 'ancestor' | 'ancestor-or-self' | 'attribute' | 'child'
|
|
323
|
+
* | 'descendant' | 'descendant-or-self' | 'following'
|
|
324
|
+
* | 'following-sibling' | 'namespace' | 'parent' |
|
|
325
|
+
* | 'preceding' | 'preceding-sibling' | 'self'
|
|
326
|
+
* [13] AbbreviatedAxisSpecifier::= '@'?
|
|
327
|
+
*/
|
|
328
|
+
function axisSpecifier(stream, a) {
|
|
329
|
+
var attr = stream.trypop('@');
|
|
330
|
+
if (null != attr) return 'attribute';
|
|
331
|
+
var axisName = stream.trypopaxisname();
|
|
332
|
+
if (null != axisName) {
|
|
333
|
+
var coloncolon = stream.trypop('::');
|
|
334
|
+
if (null == coloncolon)
|
|
335
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
336
|
+
'Position ' + stream.position() +
|
|
337
|
+
': Should not happen. Should be ::.');
|
|
338
|
+
return axisName;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** [7] NodeTest::= NameTest | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'
|
|
342
|
+
* [38] NodeType::= 'comment' | 'text' | 'processing-instruction' | 'node'
|
|
343
|
+
* I've split nodeTypeTest from nodeNameTest for convenience.
|
|
344
|
+
*/
|
|
345
|
+
function nodeTypeTest(stream, a) {
|
|
346
|
+
if ('(' !== stream.peek2()) {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
var type = stream.trypop(['comment', 'text', 'processing-instruction', 'node']);
|
|
350
|
+
if (null != type) {
|
|
351
|
+
if (null == stream.trypop('('))
|
|
352
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
353
|
+
'Position ' + stream.position() +
|
|
354
|
+
': Should not happen.');
|
|
355
|
+
var param = undefined;
|
|
356
|
+
if (type == 'processing-instruction') {
|
|
357
|
+
param = stream.trypopliteral();
|
|
358
|
+
}
|
|
359
|
+
if (null == stream.trypop(')'))
|
|
360
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
361
|
+
'Position ' + stream.position() +
|
|
362
|
+
': Expected close parens.');
|
|
363
|
+
return type
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function nodeNameTest(stream, a) {
|
|
367
|
+
var name = stream.trypopnametest();
|
|
368
|
+
if (name != null) return name;
|
|
369
|
+
else return null;
|
|
370
|
+
}
|
|
371
|
+
/** [8] Predicate::= '[' PredicateExpr ']'
|
|
372
|
+
* [9] PredicateExpr::= Expr
|
|
373
|
+
*/
|
|
374
|
+
function predicate(lhs, stream, a) {
|
|
375
|
+
if (null == stream.trypop('[')) return null;
|
|
376
|
+
var expr = orExpr(stream, a);
|
|
377
|
+
if (null == expr)
|
|
378
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
379
|
+
'Position ' + stream.position() +
|
|
380
|
+
': Expected expression after [');
|
|
381
|
+
if (null == stream.trypop(']'))
|
|
382
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
383
|
+
'Position ' + stream.position() +
|
|
384
|
+
': Expected ] after expression.');
|
|
385
|
+
return a.node('Predicate', lhs, expr);
|
|
386
|
+
}
|
|
387
|
+
/** [14] Expr::= OrExpr
|
|
388
|
+
*/
|
|
389
|
+
/** [15] PrimaryExpr::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
|
|
390
|
+
* e.g. $x, (3+4), "hi", 32, f(x)
|
|
391
|
+
*/
|
|
392
|
+
function primaryExpr(stream, a) {
|
|
393
|
+
var x = stream.trypopliteral();
|
|
394
|
+
if (null == x)
|
|
395
|
+
x = stream.trypopnumber();
|
|
396
|
+
if (null != x) {
|
|
397
|
+
return x;
|
|
398
|
+
}
|
|
399
|
+
var varRef = stream.trypopvarref();
|
|
400
|
+
if (null != varRef) return a.node('VariableReference', varRef);
|
|
401
|
+
var funCall = functionCall(stream, a);
|
|
402
|
+
if (null != funCall) {
|
|
403
|
+
return funCall;
|
|
404
|
+
}
|
|
405
|
+
if (stream.trypop('(')) {
|
|
406
|
+
var e = orExpr(stream, a);
|
|
407
|
+
if (null == e)
|
|
408
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
409
|
+
'Position ' + stream.position() +
|
|
410
|
+
': Expected expression after (.');
|
|
411
|
+
if (null == stream.trypop(')'))
|
|
412
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
413
|
+
'Position ' + stream.position() +
|
|
414
|
+
': Expected ) after expression.');
|
|
415
|
+
return e;
|
|
416
|
+
}
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
/** [16] FunctionCall::= FunctionName '(' ( Argument ( ',' Argument )* )? ')'
|
|
420
|
+
* [17] Argument::= Expr
|
|
421
|
+
*/
|
|
422
|
+
function functionCall(stream, a) {
|
|
423
|
+
var name = stream.trypopfuncname(stream, a);
|
|
424
|
+
if (null == name) return null;
|
|
425
|
+
if (null == stream.trypop('('))
|
|
426
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
427
|
+
'Position ' + stream.position() +
|
|
428
|
+
': Expected ( ) after function name.');
|
|
429
|
+
var params = [];
|
|
430
|
+
var first = true;
|
|
431
|
+
while (null == stream.trypop(')')) {
|
|
432
|
+
if (!first && null == stream.trypop(','))
|
|
433
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
434
|
+
'Position ' + stream.position() +
|
|
435
|
+
': Expected , between arguments of the function.');
|
|
436
|
+
first = false;
|
|
437
|
+
var param = orExpr(stream, a);
|
|
438
|
+
if (param == null)
|
|
439
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
440
|
+
'Position ' + stream.position() +
|
|
441
|
+
': Expected expression as argument of function.');
|
|
442
|
+
params.push(param);
|
|
443
|
+
}
|
|
444
|
+
return a.node('FunctionCall', name, params);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** [18] UnionExpr::= PathExpr | UnionExpr '|' PathExpr
|
|
448
|
+
*/
|
|
449
|
+
function unionExpr(stream, a) { return binaryL(pathExpr, stream, a, '|'); }
|
|
450
|
+
/** [19] PathExpr ::= LocationPath
|
|
451
|
+
* | FilterExpr
|
|
452
|
+
* | FilterExpr '/' RelativeLocationPath
|
|
453
|
+
* | FilterExpr '//' RelativeLocationPath
|
|
454
|
+
* Unlike most other nodes, this one always generates a node because
|
|
455
|
+
* at this point all reverse nodesets must turn into a forward nodeset
|
|
456
|
+
*/
|
|
457
|
+
function pathExpr(stream, a) {
|
|
458
|
+
// We have to do FilterExpr before LocationPath because otherwise
|
|
459
|
+
// LocationPath will eat up the name from a function call.
|
|
460
|
+
var filter = filterExpr(stream, a);
|
|
461
|
+
if (null == filter) {
|
|
462
|
+
var loc = locationPath(stream, a);
|
|
463
|
+
if (null == loc) {
|
|
464
|
+
throw new Error
|
|
465
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
466
|
+
'Position ' + stream.position() +
|
|
467
|
+
': The expression shouldn\'t be empty...');
|
|
468
|
+
}
|
|
469
|
+
return a.node('PathExpr', loc);
|
|
470
|
+
}
|
|
471
|
+
var rel = relativeLocationPath(filter, stream, a, false);
|
|
472
|
+
if (filter === rel) return rel;
|
|
473
|
+
else return a.node('PathExpr', rel);
|
|
474
|
+
}
|
|
475
|
+
/** [20] FilterExpr::= PrimaryExpr | FilterExpr Predicate
|
|
476
|
+
* aka. FilterExpr ::= PrimaryExpr Predicate*
|
|
477
|
+
*/
|
|
478
|
+
function filterExpr(stream, a) {
|
|
479
|
+
var primary = primaryExpr(stream, a);
|
|
480
|
+
if (primary == null) return null;
|
|
481
|
+
var pred, lhs = primary;
|
|
482
|
+
while (null != (pred = predicate(lhs, stream, a))) {
|
|
483
|
+
lhs = pred;
|
|
484
|
+
}
|
|
485
|
+
return lhs;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** [21] OrExpr::= AndExpr | OrExpr 'or' AndExpr
|
|
489
|
+
*/
|
|
490
|
+
function orExpr(stream, a) {
|
|
491
|
+
var orig = (stream.peeked || '') + stream.str
|
|
492
|
+
var r = binaryL(andExpr, stream, a, 'or');
|
|
493
|
+
var now = (stream.peeked || '') + stream.str;
|
|
494
|
+
return r;
|
|
495
|
+
}
|
|
496
|
+
/** [22] AndExpr::= EqualityExpr | AndExpr 'and' EqualityExpr
|
|
497
|
+
*/
|
|
498
|
+
function andExpr(stream, a) { return binaryL(equalityExpr, stream, a, 'and'); }
|
|
499
|
+
/** [23] EqualityExpr::= RelationalExpr | EqualityExpr '=' RelationalExpr
|
|
500
|
+
* | EqualityExpr '!=' RelationalExpr
|
|
501
|
+
*/
|
|
502
|
+
function equalityExpr(stream, a) { return binaryL(relationalExpr, stream, a, ['=','!=']); }
|
|
503
|
+
/** [24] RelationalExpr::= AdditiveExpr | RelationalExpr '<' AdditiveExpr
|
|
504
|
+
* | RelationalExpr '>' AdditiveExpr
|
|
505
|
+
* | RelationalExpr '<=' AdditiveExpr
|
|
506
|
+
* | RelationalExpr '>=' AdditiveExpr
|
|
507
|
+
*/
|
|
508
|
+
function relationalExpr(stream, a) { return binaryL(additiveExpr, stream, a, ['<','>','<=','>=']); }
|
|
509
|
+
/** [25] AdditiveExpr::= MultiplicativeExpr
|
|
510
|
+
* | AdditiveExpr '+' MultiplicativeExpr
|
|
511
|
+
* | AdditiveExpr '-' MultiplicativeExpr
|
|
512
|
+
*/
|
|
513
|
+
function additiveExpr(stream, a) { return binaryL(multiplicativeExpr, stream, a, ['+','-']); }
|
|
514
|
+
/** [26] MultiplicativeExpr::= UnaryExpr
|
|
515
|
+
* | MultiplicativeExpr MultiplyOperator UnaryExpr
|
|
516
|
+
* | MultiplicativeExpr 'div' UnaryExpr
|
|
517
|
+
* | MultiplicativeExpr 'mod' UnaryExpr
|
|
518
|
+
*/
|
|
519
|
+
function multiplicativeExpr(stream, a) { return binaryL(unaryExpr, stream, a, ['*','div','mod']); }
|
|
520
|
+
/** [27] UnaryExpr::= UnionExpr | '-' UnaryExpr
|
|
521
|
+
*/
|
|
522
|
+
function unaryExpr(stream, a) {
|
|
523
|
+
if (stream.trypop('-')) {
|
|
524
|
+
var e = unaryExpr(stream, a);
|
|
525
|
+
if (null == e)
|
|
526
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
527
|
+
'Position ' + stream.position() +
|
|
528
|
+
': Expected unary expression after -');
|
|
529
|
+
return a.node('UnaryMinus', e);
|
|
530
|
+
}
|
|
531
|
+
else return unionExpr(stream, a);
|
|
532
|
+
}
|
|
533
|
+
var astFactory = {
|
|
534
|
+
node: function() {return Array.prototype.slice.call(arguments);}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
/***************************************************************************
|
|
539
|
+
* Optimizations (TODO) *
|
|
540
|
+
***************************************************************************/
|
|
541
|
+
/**
|
|
542
|
+
* Some things I've been considering:
|
|
543
|
+
* 1) a//b becomes a/descendant::b if there's no predicate that uses
|
|
544
|
+
* position() or last()
|
|
545
|
+
* 2) axis[pred]: when pred doesn't use position, evaluate it just once per
|
|
546
|
+
* node in the node-set rather than once per (node, position, last).
|
|
547
|
+
* For more optimizations, look up Gecko's optimizer:
|
|
548
|
+
* http://mxr.mozilla.org/mozilla-central/source/content/xslt/src/xpath/txXPathOptimizer.cpp
|
|
549
|
+
*/
|
|
550
|
+
// TODO
|
|
551
|
+
function optimize(ast) {
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/***************************************************************************
|
|
555
|
+
* Evaluation: axes *
|
|
556
|
+
***************************************************************************/
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Data types: For string, number, boolean, we just use Javascript types.
|
|
560
|
+
* Node-sets have the form
|
|
561
|
+
* {nodes: [node, ...]}
|
|
562
|
+
* or {nodes: [node, ...], pos: [[1], [2], ...], lasts: [[1], [2], ...]}
|
|
563
|
+
*
|
|
564
|
+
* Most of the time, only the node is used and the position information is
|
|
565
|
+
* discarded. But if you use a predicate, we need to try every value of
|
|
566
|
+
* position and last in case the predicate calls position() or last().
|
|
567
|
+
*/
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* The NodeMultiSet is a helper class to help generate
|
|
571
|
+
* {nodes:[], pos:[], lasts:[]} structures. It is useful for the
|
|
572
|
+
* descendant, descendant-or-self, following-sibling, and
|
|
573
|
+
* preceding-sibling axes for which we can use a stack to organize things.
|
|
574
|
+
*/
|
|
575
|
+
function NodeMultiSet(isReverseAxis) {
|
|
576
|
+
this.nodes = [];
|
|
577
|
+
this.pos = [];
|
|
578
|
+
this.lasts = [];
|
|
579
|
+
this.nextPos = [];
|
|
580
|
+
this.seriesIndexes = []; // index within nodes that each series begins.
|
|
581
|
+
this.isReverseAxis = isReverseAxis;
|
|
582
|
+
this._pushToNodes = isReverseAxis ? Array.prototype.unshift : Array.prototype.push;
|
|
583
|
+
}
|
|
584
|
+
NodeMultiSet.prototype = {
|
|
585
|
+
pushSeries: function pushSeries() {
|
|
586
|
+
this.nextPos.push(1);
|
|
587
|
+
this.seriesIndexes.push(this.nodes.length);
|
|
588
|
+
},
|
|
589
|
+
popSeries: function popSeries() {
|
|
590
|
+
console.assert(0 < this.nextPos.length, this.nextPos);
|
|
591
|
+
var last = this.nextPos.pop() - 1,
|
|
592
|
+
indexInPos = this.nextPos.length,
|
|
593
|
+
seriesBeginIndex = this.seriesIndexes.pop(),
|
|
594
|
+
seriesEndIndex = this.nodes.length;
|
|
595
|
+
for (var i = seriesBeginIndex; i < seriesEndIndex; ++i) {
|
|
596
|
+
console.assert(indexInPos < this.lasts[i].length);
|
|
597
|
+
console.assert(undefined === this.lasts[i][indexInPos]);
|
|
598
|
+
this.lasts[i][indexInPos] = last;
|
|
599
|
+
}
|
|
600
|
+
},
|
|
601
|
+
finalize: function() {
|
|
602
|
+
if (null == this.nextPos) return this;
|
|
603
|
+
console.assert(0 === this.nextPos.length);
|
|
604
|
+
var lastsJSON = JSON.stringify(this.lasts);
|
|
605
|
+
for (var i = 0; i < this.lasts.length; ++i) {
|
|
606
|
+
for (var j = 0; j < this.lasts[i].length; ++j) {
|
|
607
|
+
console.assert(null != this.lasts[i][j], i + ',' + j + ':' + lastsJSON);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
this.pushSeries = this.popSeries = this.addNode = function() {
|
|
611
|
+
throw new Error('Already finalized.');
|
|
612
|
+
};
|
|
613
|
+
return this;
|
|
614
|
+
},
|
|
615
|
+
addNode: function addNode(node) {
|
|
616
|
+
console.assert(node);
|
|
617
|
+
this._pushToNodes.call(this.nodes, node)
|
|
618
|
+
this._pushToNodes.call(this.pos, this.nextPos.slice());
|
|
619
|
+
this._pushToNodes.call(this.lasts, new Array(this.nextPos.length));
|
|
620
|
+
for (var i = 0; i < this.nextPos.length; ++i) this.nextPos[i]++;
|
|
621
|
+
},
|
|
622
|
+
simplify: function() {
|
|
623
|
+
this.finalize();
|
|
624
|
+
return {nodes:this.nodes, pos:this.pos, lasts:this.lasts};
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
function eachContext(nodeMultiSet) {
|
|
628
|
+
var r = [];
|
|
629
|
+
for (var i = 0; i < nodeMultiSet.nodes.length; i++) {
|
|
630
|
+
var node = nodeMultiSet.nodes[i];
|
|
631
|
+
if (!nodeMultiSet.pos) {
|
|
632
|
+
r.push({nodes:[node], pos: [[i + 1]], lasts: [[nodeMultiSet.nodes.length]]});
|
|
633
|
+
} else {
|
|
634
|
+
for (var j = 0; j < nodeMultiSet.pos[i].length; ++j) {
|
|
635
|
+
r.push({nodes:[node], pos: [[nodeMultiSet.pos[i][j]]], lasts: [[nodeMultiSet.lasts[i][j]]]});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return r;
|
|
640
|
+
}
|
|
641
|
+
/** Matcher used in the axes.
|
|
642
|
+
*/
|
|
643
|
+
function NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase) {
|
|
644
|
+
this.nodeTypeNum = nodeTypeNum;
|
|
645
|
+
this.nodeName = nodeName;
|
|
646
|
+
this.shouldLowerCase = shouldLowerCase;
|
|
647
|
+
this.nodeNameTest =
|
|
648
|
+
null == nodeName ? this._alwaysTrue :
|
|
649
|
+
shouldLowerCase ? this._nodeNameLowerCaseEquals :
|
|
650
|
+
this._nodeNameEquals;
|
|
651
|
+
}
|
|
652
|
+
NodeMatcher.prototype = {
|
|
653
|
+
matches: function matches(node) {
|
|
654
|
+
if (0 === this.nodeTypeNum || this._nodeTypeMatches(node)) {
|
|
655
|
+
return this.nodeNameTest(getNodeName(node));
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return false;
|
|
659
|
+
},
|
|
660
|
+
_nodeTypeMatches(nodeOrAttr) {
|
|
661
|
+
if (nodeOrAttr.constructor.name === 'Attr' && this.nodeTypeNum === 2) {
|
|
662
|
+
return true;
|
|
663
|
+
}
|
|
664
|
+
return nodeOrAttr.nodeType === this.nodeTypeNum;
|
|
665
|
+
},
|
|
666
|
+
_alwaysTrue: function(name) {return true;},
|
|
667
|
+
_nodeNameEquals: function _nodeNameEquals(name) {
|
|
668
|
+
return this.nodeName === name;
|
|
669
|
+
},
|
|
670
|
+
_nodeNameLowerCaseEquals: function _nodeNameLowerCaseEquals(name) {
|
|
671
|
+
return this.nodeName === name.toLowerCase();
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
function followingSiblingHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, shift, peek, followingNode, andSelf, isReverseAxis) {
|
|
676
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
677
|
+
var nodeMultiSet = new NodeMultiSet(isReverseAxis);
|
|
678
|
+
while (0 < nodeList.length) { // can be if for following, preceding
|
|
679
|
+
var node = shift.call(nodeList);
|
|
680
|
+
console.assert(node != null);
|
|
681
|
+
node = followingNode(node);
|
|
682
|
+
nodeMultiSet.pushSeries();
|
|
683
|
+
var numPushed = 1;
|
|
684
|
+
while (null != node) {
|
|
685
|
+
if (! andSelf && matcher.matches(node))
|
|
686
|
+
nodeMultiSet.addNode(node);
|
|
687
|
+
if (node === peek.call(nodeList)) {
|
|
688
|
+
shift.call(nodeList);
|
|
689
|
+
nodeMultiSet.pushSeries();
|
|
690
|
+
numPushed++;
|
|
691
|
+
}
|
|
692
|
+
if (andSelf && matcher.matches(node))
|
|
693
|
+
nodeMultiSet.addNode(node);
|
|
694
|
+
node = followingNode(node);
|
|
695
|
+
}
|
|
696
|
+
while (0 < numPushed--)
|
|
697
|
+
nodeMultiSet.popSeries();
|
|
698
|
+
}
|
|
699
|
+
return nodeMultiSet;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/** Returns the next non-descendant node in document order.
|
|
703
|
+
* This is the first node in following::node(), if node is the context.
|
|
704
|
+
*/
|
|
705
|
+
function followingNonDescendantNode(node) {
|
|
706
|
+
if (node.ownerElement) {
|
|
707
|
+
if (node.ownerElement.firstChild)
|
|
708
|
+
return node.ownerElement.firstChild;
|
|
709
|
+
node = node.ownerElement;
|
|
710
|
+
}
|
|
711
|
+
do {
|
|
712
|
+
if (node.nextSibling) return node.nextSibling;
|
|
713
|
+
} while (node = node.parentNode);
|
|
714
|
+
return null;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** Returns the next node in a document-order depth-first search.
|
|
718
|
+
* See the definition of document order[1]:
|
|
719
|
+
* 1) element
|
|
720
|
+
* 2) namespace nodes
|
|
721
|
+
* 3) attributes
|
|
722
|
+
* 4) children
|
|
723
|
+
* [1]: http://www.w3.org/TR/xpath/#dt-document-order
|
|
724
|
+
*/
|
|
725
|
+
function followingNode(node) {
|
|
726
|
+
if (node.ownerElement) // attributes: following node of element.
|
|
727
|
+
node = node.ownerElement;
|
|
728
|
+
if (null != node.firstChild)
|
|
729
|
+
return node.firstChild;
|
|
730
|
+
do {
|
|
731
|
+
if (null != node.nextSibling) {
|
|
732
|
+
return node.nextSibling;
|
|
733
|
+
}
|
|
734
|
+
node = node.parentNode;
|
|
735
|
+
} while (node);
|
|
736
|
+
return null;
|
|
737
|
+
}
|
|
738
|
+
/** Returns the previous node in document order (excluding attributes
|
|
739
|
+
* and namespace nodes).
|
|
740
|
+
*/
|
|
741
|
+
function precedingNode(node) {
|
|
742
|
+
if (node.ownerElement)
|
|
743
|
+
return node.ownerElement;
|
|
744
|
+
if (null != node.previousSibling) {
|
|
745
|
+
node = node.previousSibling;
|
|
746
|
+
while (null != node.lastChild) {
|
|
747
|
+
node = node.lastChild;
|
|
748
|
+
}
|
|
749
|
+
return node;
|
|
750
|
+
}
|
|
751
|
+
if (null != node.parentNode) {
|
|
752
|
+
return node.parentNode;
|
|
753
|
+
}
|
|
754
|
+
return null;
|
|
755
|
+
}
|
|
756
|
+
/** This axis is inefficient if there are many nodes in the nodeList.
|
|
757
|
+
* But I think it's a pretty useless axis so it's ok. */
|
|
758
|
+
function followingHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
759
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
760
|
+
var nodeMultiSet = new NodeMultiSet(false);
|
|
761
|
+
var cursor = nodeList[0];
|
|
762
|
+
var unorderedFollowingStarts = [];
|
|
763
|
+
for (var i = 0; i < nodeList.length; i++) {
|
|
764
|
+
var node = nodeList[i];
|
|
765
|
+
var start = followingNonDescendantNode(node);
|
|
766
|
+
if (start)
|
|
767
|
+
unorderedFollowingStarts.push(start);
|
|
768
|
+
}
|
|
769
|
+
if (0 === unorderedFollowingStarts.length)
|
|
770
|
+
return {nodes:[]};
|
|
771
|
+
var pos = [], nextPos = [];
|
|
772
|
+
var started = 0;
|
|
773
|
+
while (cursor = followingNode(cursor)) {
|
|
774
|
+
for (var i = unorderedFollowingStarts.length - 1; i >= 0; i--){
|
|
775
|
+
if (cursor === unorderedFollowingStarts[i]) {
|
|
776
|
+
nodeMultiSet.pushSeries();
|
|
777
|
+
unorderedFollowingStarts.splice(i,i+1);
|
|
778
|
+
started++;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
if (started && matcher.matches(cursor)) {
|
|
782
|
+
nodeMultiSet.addNode(cursor);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
console.assert(0 === unorderedFollowingStarts.length);
|
|
786
|
+
for (var i = 0; i < started; i++)
|
|
787
|
+
nodeMultiSet.popSeries();
|
|
788
|
+
return nodeMultiSet.finalize();
|
|
789
|
+
}
|
|
790
|
+
function precedingHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
791
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
792
|
+
var cursor = nodeList.pop();
|
|
793
|
+
if (null == cursor) return {nodes:{}};
|
|
794
|
+
var r = {nodes:[], pos:[], lasts:[]};
|
|
795
|
+
var nextParents = [cursor.parentNode || cursor.ownerElement], nextPos = [1];
|
|
796
|
+
while (cursor = precedingNode(cursor)) {
|
|
797
|
+
if (cursor === nodeList[nodeList.length - 1]) {
|
|
798
|
+
nextParents.push(nodeList.pop());
|
|
799
|
+
nextPos.push(1);
|
|
800
|
+
}
|
|
801
|
+
var matches = matcher.matches(cursor);
|
|
802
|
+
var pos, someoneUsed = false;
|
|
803
|
+
if (matches)
|
|
804
|
+
pos = nextPos.slice();
|
|
805
|
+
|
|
806
|
+
for (var i = 0; i < nextParents.length; ++i) {
|
|
807
|
+
if (cursor === nextParents[i]) {
|
|
808
|
+
nextParents[i] = cursor.parentNode || cursor.ownerElement;
|
|
809
|
+
if (matches) {
|
|
810
|
+
pos[i] = null;
|
|
811
|
+
}
|
|
812
|
+
} else {
|
|
813
|
+
if (matches) {
|
|
814
|
+
pos[i] = nextPos[i]++;
|
|
815
|
+
someoneUsed = true;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
if (someoneUsed) {
|
|
820
|
+
r.nodes.unshift(cursor);
|
|
821
|
+
r.pos.unshift(pos);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
for (var i = 0; i < r.pos.length; ++i) {
|
|
825
|
+
var lasts = [];
|
|
826
|
+
r.lasts.push(lasts);
|
|
827
|
+
for (var j = r.pos[i].length - 1; j >= 0; j--) {
|
|
828
|
+
if (null == r.pos[i][j]) {
|
|
829
|
+
r.pos[i].splice(j, j+1);
|
|
830
|
+
} else {
|
|
831
|
+
lasts.unshift(nextPos[j] - 1);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return r;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/** node-set, axis -> node-set */
|
|
839
|
+
function descendantDfs(nodeMultiSet, node, remaining, matcher, andSelf, attrIndices, attrNodes) {
|
|
840
|
+
while (0 < remaining.length && null != remaining[0].ownerElement) {
|
|
841
|
+
var attr = remaining.shift();
|
|
842
|
+
if (andSelf && matcher.matches(attr)) {
|
|
843
|
+
attrNodes.push(attr);
|
|
844
|
+
attrIndices.push(nodeMultiSet.nodes.length);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (null != node && !andSelf) {
|
|
848
|
+
if (matcher.matches(node))
|
|
849
|
+
nodeMultiSet.addNode(node);
|
|
850
|
+
}
|
|
851
|
+
var pushed = false;
|
|
852
|
+
if (null == node) {
|
|
853
|
+
if (0 === remaining.length) return;
|
|
854
|
+
node = remaining.shift();
|
|
855
|
+
nodeMultiSet.pushSeries();
|
|
856
|
+
pushed = true;
|
|
857
|
+
} else if (0 < remaining.length && node === remaining[0]) {
|
|
858
|
+
nodeMultiSet.pushSeries();
|
|
859
|
+
pushed = true;
|
|
860
|
+
remaining.shift();
|
|
861
|
+
}
|
|
862
|
+
if (andSelf) {
|
|
863
|
+
if (matcher.matches(node))
|
|
864
|
+
nodeMultiSet.addNode(node);
|
|
865
|
+
}
|
|
866
|
+
// TODO: use optimization. Also try element.getElementsByTagName
|
|
867
|
+
// var nodeList = 1 === nodeTypeNum && null != node.children ? node.children : node.childNodes;
|
|
868
|
+
var nodeList = node.childNodes;
|
|
869
|
+
for (var j = 0; j < nodeList.length; ++j) {
|
|
870
|
+
var child = nodeList[j];
|
|
871
|
+
descendantDfs(nodeMultiSet, child, remaining, matcher, andSelf, attrIndices, attrNodes);
|
|
872
|
+
}
|
|
873
|
+
if (pushed) {
|
|
874
|
+
nodeMultiSet.popSeries();
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
function descenantHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, andSelf) {
|
|
878
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
879
|
+
var nodeMultiSet = new NodeMultiSet(false);
|
|
880
|
+
var attrIndices = [], attrNodes = [];
|
|
881
|
+
while (0 < nodeList.length) {
|
|
882
|
+
// var node = nodeList.shift();
|
|
883
|
+
descendantDfs(nodeMultiSet, null, nodeList, matcher, andSelf, attrIndices, attrNodes);
|
|
884
|
+
}
|
|
885
|
+
nodeMultiSet.finalize();
|
|
886
|
+
for (var i = attrNodes.length-1; i >= 0; --i) {
|
|
887
|
+
nodeMultiSet.nodes.splice(attrIndices[i], attrIndices[i], attrNodes[i]);
|
|
888
|
+
nodeMultiSet.pos.splice(attrIndices[i], attrIndices[i], [1]);
|
|
889
|
+
nodeMultiSet.lasts.splice(attrIndices[i], attrIndices[i], [1]);
|
|
890
|
+
}
|
|
891
|
+
return nodeMultiSet;
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
*/
|
|
895
|
+
function ancestorHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, andSelf) {
|
|
896
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
897
|
+
var ancestors = []; // array of non-empty arrays of matching ancestors
|
|
898
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
899
|
+
var node = nodeList[i];
|
|
900
|
+
var isFirst = true;
|
|
901
|
+
var a = [];
|
|
902
|
+
while (null != node) {
|
|
903
|
+
if (!isFirst || andSelf) {
|
|
904
|
+
if (matcher.matches(node))
|
|
905
|
+
a.push(node);
|
|
906
|
+
}
|
|
907
|
+
isFirst = false;
|
|
908
|
+
node = node.parentNode || node.ownerElement;
|
|
909
|
+
}
|
|
910
|
+
if (0 < a.length)
|
|
911
|
+
ancestors.push(a);
|
|
912
|
+
}
|
|
913
|
+
var lasts = [];
|
|
914
|
+
for (var i = 0; i < ancestors.length; ++i) lasts.push(ancestors[i].length);
|
|
915
|
+
var nodeMultiSet = new NodeMultiSet(true);
|
|
916
|
+
var newCtx = {nodes:[], pos:[], lasts:[]};
|
|
917
|
+
while (0 < ancestors.length) {
|
|
918
|
+
var pos = [ancestors[0].length];
|
|
919
|
+
var last = [lasts[0]];
|
|
920
|
+
var node = ancestors[0].pop();
|
|
921
|
+
for (var i = ancestors.length - 1; i > 0; --i) {
|
|
922
|
+
if (node === ancestors[i][ancestors[i].length - 1]) {
|
|
923
|
+
pos.push(ancestors[i].length);
|
|
924
|
+
last.push(lasts[i]);
|
|
925
|
+
ancestors[i].pop();
|
|
926
|
+
if (0 === ancestors[i].length) {
|
|
927
|
+
ancestors.splice(i, i+1);
|
|
928
|
+
lasts.splice(i, i+1);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
if (0 === ancestors[0].length) {
|
|
933
|
+
ancestors.shift();
|
|
934
|
+
lasts.shift();
|
|
935
|
+
}
|
|
936
|
+
newCtx.nodes.push(node);
|
|
937
|
+
newCtx.pos.push(pos);
|
|
938
|
+
newCtx.lasts.push(last);
|
|
939
|
+
}
|
|
940
|
+
return newCtx;
|
|
941
|
+
}
|
|
942
|
+
/** Helper function for sortDocumentOrder. Returns a list of indices, from the
|
|
943
|
+
* node to the root, of positions within parent.
|
|
944
|
+
* For convenience, the node is the first element of the array.
|
|
945
|
+
*/
|
|
946
|
+
function addressVector(node) {
|
|
947
|
+
var r = [node];
|
|
948
|
+
if (null != node.ownerElement) {
|
|
949
|
+
node = node.ownerElement;
|
|
950
|
+
r.push(-1);
|
|
951
|
+
}
|
|
952
|
+
while (null != node) {
|
|
953
|
+
var i = 0;
|
|
954
|
+
while (null != node.previousSibling) {
|
|
955
|
+
node = node.previousSibling;
|
|
956
|
+
i++;
|
|
957
|
+
}
|
|
958
|
+
r.push(i);
|
|
959
|
+
node = node.parentNode
|
|
960
|
+
}
|
|
961
|
+
return r;
|
|
962
|
+
}
|
|
963
|
+
function addressComparator(a, b) {
|
|
964
|
+
var minlen = Math.min(a.length - 1, b.length - 1), // not including [0]=node
|
|
965
|
+
alen = a.length,
|
|
966
|
+
blen = b.length;
|
|
967
|
+
if (a[0] === b[0]) return 0;
|
|
968
|
+
var c;
|
|
969
|
+
for (var i = 0; i < minlen; ++i) {
|
|
970
|
+
c = a[alen - i - 1] - b[blen - i - 1];
|
|
971
|
+
if (0 !== c)
|
|
972
|
+
break;
|
|
973
|
+
}
|
|
974
|
+
if (null == c || 0 === c) {
|
|
975
|
+
// All equal until one of the nodes. The longer one is the descendant.
|
|
976
|
+
c = alen - blen;
|
|
977
|
+
}
|
|
978
|
+
if (0 === c)
|
|
979
|
+
c = getNodeName(a) - getNodeName(b);
|
|
980
|
+
if (0 === c)
|
|
981
|
+
c = 1;
|
|
982
|
+
return c;
|
|
983
|
+
}
|
|
984
|
+
var sortUniqDocumentOrder = xpath.sortUniqDocumentOrder = function(nodes) {
|
|
985
|
+
var a = [];
|
|
986
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
987
|
+
var node = nodes[i];
|
|
988
|
+
var v = addressVector(node);
|
|
989
|
+
a.push(v);
|
|
990
|
+
}
|
|
991
|
+
a.sort(addressComparator);
|
|
992
|
+
var b = [];
|
|
993
|
+
for (var i = 0; i < a.length; i++) {
|
|
994
|
+
if (0 < i && a[i][0] === a[i - 1][0])
|
|
995
|
+
continue;
|
|
996
|
+
b.push(a[i][0]);
|
|
997
|
+
}
|
|
998
|
+
return b;
|
|
999
|
+
}
|
|
1000
|
+
/** Sort node multiset. Does not do any de-duping. */
|
|
1001
|
+
function sortNodeMultiSet(nodeMultiSet) {
|
|
1002
|
+
var a = [];
|
|
1003
|
+
for (var i = 0; i < nodeMultiSet.nodes.length; i++) {
|
|
1004
|
+
var v = addressVector(nodeMultiSet.nodes[i]);
|
|
1005
|
+
a.push({v:v, n:nodeMultiSet.nodes[i],
|
|
1006
|
+
p:nodeMultiSet.pos[i], l:nodeMultiSet.lasts[i]});
|
|
1007
|
+
}
|
|
1008
|
+
a.sort(compare);
|
|
1009
|
+
var r = {nodes:[], pos:[], lasts:[]};
|
|
1010
|
+
for (var i = 0; i < a.length; ++i) {
|
|
1011
|
+
r.nodes.push(a[i].n);
|
|
1012
|
+
r.pos.push(a[i].p);
|
|
1013
|
+
r.lasts.push(a[i].l);
|
|
1014
|
+
}
|
|
1015
|
+
function compare(x, y) {
|
|
1016
|
+
return addressComparator(x.v, y.v);
|
|
1017
|
+
}
|
|
1018
|
+
return r;
|
|
1019
|
+
}
|
|
1020
|
+
/** Returns an array containing all the ancestors down to a node.
|
|
1021
|
+
* The array starts with document.
|
|
1022
|
+
*/
|
|
1023
|
+
function nodeAndAncestors(node) {
|
|
1024
|
+
var ancestors = [node];
|
|
1025
|
+
var p = node;
|
|
1026
|
+
while (p = p.parentNode || p.ownerElement) {
|
|
1027
|
+
ancestors.unshift(p);
|
|
1028
|
+
}
|
|
1029
|
+
return ancestors;
|
|
1030
|
+
}
|
|
1031
|
+
function compareSiblings(a, b) {
|
|
1032
|
+
if (a === b) return 0;
|
|
1033
|
+
var c = a;
|
|
1034
|
+
while (c = c.previousSibling) {
|
|
1035
|
+
if (c === b)
|
|
1036
|
+
return 1; // b < a
|
|
1037
|
+
}
|
|
1038
|
+
c = b;
|
|
1039
|
+
while (c = c.previousSibling) {
|
|
1040
|
+
if (c === a)
|
|
1041
|
+
return -1; // a < b
|
|
1042
|
+
}
|
|
1043
|
+
throw new Error('a and b are not siblings: ' + xpath.stringifyObject(a) + ' vs ' + xpath.stringifyObject(b));
|
|
1044
|
+
}
|
|
1045
|
+
/** The merge in merge-sort.*/
|
|
1046
|
+
function mergeNodeLists(x, y) {
|
|
1047
|
+
var a, b, aanc, banc, r = [];
|
|
1048
|
+
if ('object' !== typeof x)
|
|
1049
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1050
|
+
'Invalid LHS for | operator ' +
|
|
1051
|
+
'(expected node-set): ' + x);
|
|
1052
|
+
if ('object' !== typeof y)
|
|
1053
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1054
|
+
'Invalid LHS for | operator ' +
|
|
1055
|
+
'(expected node-set): ' + y);
|
|
1056
|
+
while (true) {
|
|
1057
|
+
if (null == a) {
|
|
1058
|
+
a = x.shift();
|
|
1059
|
+
if (null != a)
|
|
1060
|
+
aanc = addressVector(a);
|
|
1061
|
+
}
|
|
1062
|
+
if (null == b) {
|
|
1063
|
+
b = y.shift();
|
|
1064
|
+
if (null != b)
|
|
1065
|
+
banc = addressVector(b);
|
|
1066
|
+
}
|
|
1067
|
+
if (null == a || null == b) break;
|
|
1068
|
+
var c = addressComparator(aanc, banc);
|
|
1069
|
+
if (c < 0) {
|
|
1070
|
+
r.push(a);
|
|
1071
|
+
a = null;
|
|
1072
|
+
aanc = null;
|
|
1073
|
+
} else if (c > 0) {
|
|
1074
|
+
r.push(b);
|
|
1075
|
+
b = null;
|
|
1076
|
+
banc = null;
|
|
1077
|
+
} else if (getNodeName(a) < getNodeName(b)) { // attributes
|
|
1078
|
+
r.push(a);
|
|
1079
|
+
a = null;
|
|
1080
|
+
aanc = null;
|
|
1081
|
+
} else if (getNodeName(a) > getNodeName(b)) { // attributes
|
|
1082
|
+
r.push(b);
|
|
1083
|
+
b = null;
|
|
1084
|
+
banc = null;
|
|
1085
|
+
} else if (a !== b) {
|
|
1086
|
+
// choose b arbitrarily
|
|
1087
|
+
r.push(b);
|
|
1088
|
+
b = null;
|
|
1089
|
+
banc = null;
|
|
1090
|
+
} else {
|
|
1091
|
+
console.assert(a === b, c);
|
|
1092
|
+
// just skip b without pushing it.
|
|
1093
|
+
b = null;
|
|
1094
|
+
banc = null;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
while (a) {
|
|
1098
|
+
r.push(a);
|
|
1099
|
+
a = x.shift();
|
|
1100
|
+
}
|
|
1101
|
+
while (b) {
|
|
1102
|
+
r.push(b);
|
|
1103
|
+
b = y.shift();
|
|
1104
|
+
}
|
|
1105
|
+
return r;
|
|
1106
|
+
}
|
|
1107
|
+
function comparisonHelper(test, x, y, isNumericComparison) {
|
|
1108
|
+
var coersion;
|
|
1109
|
+
if (isNumericComparison)
|
|
1110
|
+
coersion = fn.number;
|
|
1111
|
+
else coersion =
|
|
1112
|
+
'boolean' === typeof x || 'boolean' === typeof y ? fn['boolean'] :
|
|
1113
|
+
'number' === typeof x || 'number' === typeof y ? fn.number :
|
|
1114
|
+
fn.string;
|
|
1115
|
+
if ('object' === typeof x && 'object' === typeof y) {
|
|
1116
|
+
var aMap = {};
|
|
1117
|
+
for (var i = 0; i < x.nodes.length; ++i) {
|
|
1118
|
+
var xi = coersion({nodes:[x.nodes[i]]});
|
|
1119
|
+
for (var j = 0; j < y.nodes.length; ++j) {
|
|
1120
|
+
var yj = coersion({nodes:[y.nodes[j]]});
|
|
1121
|
+
if (test(xi, yj)) return true;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
return false;
|
|
1125
|
+
} else if ('object' === typeof x && x.nodes && x.nodes.length) {
|
|
1126
|
+
for (var i = 0; i < x.nodes.length; ++i) {
|
|
1127
|
+
var xi = coersion({nodes:[x.nodes[i]]}), yc = coersion(y);
|
|
1128
|
+
if (test(xi, yc))
|
|
1129
|
+
return true;
|
|
1130
|
+
}
|
|
1131
|
+
return false;
|
|
1132
|
+
} else if ('object' === typeof y && x.nodes && x.nodes.length) {
|
|
1133
|
+
for (var i = 0; i < x.nodes.length; ++i) {
|
|
1134
|
+
var yi = coersion({nodes:[y.nodes[i]]}), xc = coersion(x);
|
|
1135
|
+
if (test(xc, yi))
|
|
1136
|
+
return true;
|
|
1137
|
+
}
|
|
1138
|
+
return false;
|
|
1139
|
+
} else {
|
|
1140
|
+
var xc = coersion(x), yc = coersion(y);
|
|
1141
|
+
return test(xc, yc);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
var axes = xpath.axes = {
|
|
1145
|
+
'ancestor':
|
|
1146
|
+
function ancestor(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1147
|
+
return ancestorHelper(
|
|
1148
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, false);
|
|
1149
|
+
},
|
|
1150
|
+
'ancestor-or-self':
|
|
1151
|
+
function ancestorOrSelf(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1152
|
+
return ancestorHelper(
|
|
1153
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, true);
|
|
1154
|
+
},
|
|
1155
|
+
'attribute':
|
|
1156
|
+
function attribute(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1157
|
+
// TODO: figure out whether positions should be undefined here.
|
|
1158
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
1159
|
+
var nodeMultiSet = new NodeMultiSet(false);
|
|
1160
|
+
if (null != nodeName) {
|
|
1161
|
+
// TODO: with namespace
|
|
1162
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
1163
|
+
var node = nodeList[i];
|
|
1164
|
+
if (null == node.getAttributeNode)
|
|
1165
|
+
continue; // only Element has .getAttributeNode
|
|
1166
|
+
var attr = node.getAttributeNode(nodeName);
|
|
1167
|
+
if (null != attr && matcher.matches(attr)) {
|
|
1168
|
+
nodeMultiSet.pushSeries();
|
|
1169
|
+
nodeMultiSet.addNode(attr);
|
|
1170
|
+
nodeMultiSet.popSeries();
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
} else {
|
|
1174
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
1175
|
+
var node = nodeList[i];
|
|
1176
|
+
if (null != node.attributes) {
|
|
1177
|
+
nodeMultiSet.pushSeries();
|
|
1178
|
+
for (var j = 0; j < node.attributes.length; j++) { // all nodes have .attributes
|
|
1179
|
+
var attr = node.attributes[j];
|
|
1180
|
+
if (matcher.matches(attr)) // TODO: I think this check is unnecessary
|
|
1181
|
+
nodeMultiSet.addNode(attr);
|
|
1182
|
+
}
|
|
1183
|
+
nodeMultiSet.popSeries();
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
return nodeMultiSet.finalize();
|
|
1188
|
+
},
|
|
1189
|
+
'child':
|
|
1190
|
+
function child(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1191
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
1192
|
+
var nodeMultiSet = new NodeMultiSet(false);
|
|
1193
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
1194
|
+
var n = nodeList[i];
|
|
1195
|
+
if (n.ownerElement) // skip attribute nodes' text child.
|
|
1196
|
+
continue;
|
|
1197
|
+
if (n.childNodes) {
|
|
1198
|
+
nodeMultiSet.pushSeries();
|
|
1199
|
+
var childList = 1 === nodeTypeNum && null != n.children ?
|
|
1200
|
+
n.children : n.childNodes;
|
|
1201
|
+
for (var j = 0; j < childList.length; ++j) {
|
|
1202
|
+
var child = childList[j];
|
|
1203
|
+
if (matcher.matches(child)) {
|
|
1204
|
+
nodeMultiSet.addNode(child);
|
|
1205
|
+
}
|
|
1206
|
+
// don't have to do de-duping because children have parent,
|
|
1207
|
+
// which are current context.
|
|
1208
|
+
}
|
|
1209
|
+
nodeMultiSet.popSeries();
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
nodeMultiSet.finalize();
|
|
1213
|
+
return sortNodeMultiSet(nodeMultiSet);
|
|
1214
|
+
},
|
|
1215
|
+
'descendant':
|
|
1216
|
+
function descenant(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1217
|
+
return descenantHelper(
|
|
1218
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, false);
|
|
1219
|
+
},
|
|
1220
|
+
'descendant-or-self':
|
|
1221
|
+
function descenantOrSelf(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1222
|
+
return descenantHelper(
|
|
1223
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase, true);
|
|
1224
|
+
},
|
|
1225
|
+
'following':
|
|
1226
|
+
function following(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1227
|
+
return followingHelper(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase);
|
|
1228
|
+
},
|
|
1229
|
+
'following-sibling':
|
|
1230
|
+
function followingSibling(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1231
|
+
return followingSiblingHelper(
|
|
1232
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase,
|
|
1233
|
+
Array.prototype.shift, function() {return this[0];},
|
|
1234
|
+
function(node) {return node.nextSibling;});
|
|
1235
|
+
},
|
|
1236
|
+
'namespace':
|
|
1237
|
+
function namespace(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1238
|
+
// TODO
|
|
1239
|
+
},
|
|
1240
|
+
'parent':
|
|
1241
|
+
function parent(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1242
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
1243
|
+
var nodes = [], pos = [];
|
|
1244
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
1245
|
+
var parent = nodeList[i].parentNode || nodeList[i].ownerElement;
|
|
1246
|
+
if (null == parent)
|
|
1247
|
+
continue;
|
|
1248
|
+
if (!matcher.matches(parent))
|
|
1249
|
+
continue;
|
|
1250
|
+
if (nodes.length > 0 && parent === nodes[nodes.length-1])
|
|
1251
|
+
continue;
|
|
1252
|
+
nodes.push(parent);
|
|
1253
|
+
pos.push([1]);
|
|
1254
|
+
}
|
|
1255
|
+
return {nodes:nodes, pos:pos, lasts:pos};
|
|
1256
|
+
},
|
|
1257
|
+
'preceding':
|
|
1258
|
+
function preceding(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1259
|
+
return precedingHelper(
|
|
1260
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase);
|
|
1261
|
+
},
|
|
1262
|
+
'preceding-sibling':
|
|
1263
|
+
function precedingSibling(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1264
|
+
return followingSiblingHelper(
|
|
1265
|
+
nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase,
|
|
1266
|
+
Array.prototype.pop, function() {return this[this.length-1];},
|
|
1267
|
+
function(node) {return node.previousSibling},
|
|
1268
|
+
false, true);
|
|
1269
|
+
},
|
|
1270
|
+
'self':
|
|
1271
|
+
function self(nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase) {
|
|
1272
|
+
var nodes = [], pos = [];
|
|
1273
|
+
var matcher = new NodeMatcher(nodeTypeNum, nodeName, shouldLowerCase);
|
|
1274
|
+
for (var i = 0; i < nodeList.length; ++i) {
|
|
1275
|
+
if (matcher.matches(nodeList[i])) {
|
|
1276
|
+
nodes.push(nodeList[i]);
|
|
1277
|
+
pos.push([1]);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
return {nodes: nodes, pos: pos, lasts: pos}
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
|
|
1284
|
+
/***************************************************************************
|
|
1285
|
+
* Evaluation: functions *
|
|
1286
|
+
***************************************************************************/
|
|
1287
|
+
var fn = {
|
|
1288
|
+
'number': function number(optObject) {
|
|
1289
|
+
if ('number' === typeof optObject)
|
|
1290
|
+
return optObject;
|
|
1291
|
+
if ('string' === typeof optObject)
|
|
1292
|
+
return parseFloat(optObject); // note: parseFloat(' ') -> NaN, unlike +' ' -> 0.
|
|
1293
|
+
if ('boolean' === typeof optObject)
|
|
1294
|
+
return +optObject;
|
|
1295
|
+
return fn.number(fn.string.call(this, optObject)); // for node-sets
|
|
1296
|
+
},
|
|
1297
|
+
'string': function string(optObject) {
|
|
1298
|
+
if (null == optObject)
|
|
1299
|
+
return fn.string(this);
|
|
1300
|
+
if ('string' === typeof optObject || 'boolean' === typeof optObject ||
|
|
1301
|
+
'number' === typeof optObject)
|
|
1302
|
+
return '' + optObject;
|
|
1303
|
+
if (0 == optObject.nodes.length) return '';
|
|
1304
|
+
if (null != optObject.nodes[0].textContent)
|
|
1305
|
+
return optObject.nodes[0].textContent;
|
|
1306
|
+
return optObject.nodes[0].nodeValue;
|
|
1307
|
+
},
|
|
1308
|
+
'boolean': function booleanVal(x) {
|
|
1309
|
+
return 'object' === typeof x ? x.nodes.length > 0 : !!x;
|
|
1310
|
+
},
|
|
1311
|
+
'last': function last() {
|
|
1312
|
+
console.assert(Array.isArray(this.pos));
|
|
1313
|
+
console.assert(Array.isArray(this.lasts));
|
|
1314
|
+
console.assert(1 === this.pos.length);
|
|
1315
|
+
console.assert(1 === this.lasts.length);
|
|
1316
|
+
console.assert(1 === this.lasts[0].length);
|
|
1317
|
+
return this.lasts[0][0];
|
|
1318
|
+
},
|
|
1319
|
+
'position': function position() {
|
|
1320
|
+
console.assert(Array.isArray(this.pos));
|
|
1321
|
+
console.assert(Array.isArray(this.lasts));
|
|
1322
|
+
console.assert(1 === this.pos.length);
|
|
1323
|
+
console.assert(1 === this.lasts.length);
|
|
1324
|
+
console.assert(1 === this.pos[0].length);
|
|
1325
|
+
return this.pos[0][0];
|
|
1326
|
+
},
|
|
1327
|
+
'count': function count(nodeSet) {
|
|
1328
|
+
if ('object' !== typeof nodeSet)
|
|
1329
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1330
|
+
'Position ' + stream.position() +
|
|
1331
|
+
': Function count(node-set) ' +
|
|
1332
|
+
'got wrong argument type: ' + nodeSet);
|
|
1333
|
+
return nodeSet.nodes.length;
|
|
1334
|
+
},
|
|
1335
|
+
'id': function id(object) {
|
|
1336
|
+
var r = {nodes: []};
|
|
1337
|
+
var doc = this.nodes[0].ownerDocument || this.nodes[0];
|
|
1338
|
+
console.assert(doc);
|
|
1339
|
+
var ids;
|
|
1340
|
+
if ('object' === typeof object) {
|
|
1341
|
+
// for node-sets, map id over each node value.
|
|
1342
|
+
ids = [];
|
|
1343
|
+
for (var i = 0; i < object.nodes.length; ++i) {
|
|
1344
|
+
var idNode = object.nodes[i];
|
|
1345
|
+
var idsString = fn.string({nodes:[idNode]});
|
|
1346
|
+
var a = idsString.split(/[ \t\r\n]+/g);
|
|
1347
|
+
Array.prototype.push.apply(ids, a);
|
|
1348
|
+
}
|
|
1349
|
+
} else {
|
|
1350
|
+
var idsString = fn.string(object);
|
|
1351
|
+
var a = idsString.split(/[ \t\r\n]+/g);
|
|
1352
|
+
ids = a;
|
|
1353
|
+
}
|
|
1354
|
+
for (var i = 0; i < ids.length; ++i) {
|
|
1355
|
+
var id = ids[i];
|
|
1356
|
+
if (0 === id.length)
|
|
1357
|
+
continue;
|
|
1358
|
+
var node = doc.getElementById(id);
|
|
1359
|
+
if (null != node)
|
|
1360
|
+
r.nodes.push(node);
|
|
1361
|
+
}
|
|
1362
|
+
r.nodes = sortUniqDocumentOrder(r.nodes);
|
|
1363
|
+
return r;
|
|
1364
|
+
},
|
|
1365
|
+
'local-name': function(nodeSet) {
|
|
1366
|
+
if (null == nodeSet)
|
|
1367
|
+
return fn.name(this);
|
|
1368
|
+
if (null == nodeSet.nodes) {
|
|
1369
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1370
|
+
'argument to name() must be a node-set. got ' + nodeSet);
|
|
1371
|
+
}
|
|
1372
|
+
// TODO: namespaced version
|
|
1373
|
+
return nodeSet.nodes[0].localName;
|
|
1374
|
+
},
|
|
1375
|
+
'namespace-uri': function(nodeSet) {
|
|
1376
|
+
// TODO
|
|
1377
|
+
throw new Error('not implemented yet');
|
|
1378
|
+
},
|
|
1379
|
+
'name': function(nodeSet) {
|
|
1380
|
+
if (null == nodeSet)
|
|
1381
|
+
return fn.name(this);
|
|
1382
|
+
if (null == nodeSet.nodes) {
|
|
1383
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1384
|
+
'argument to name() must be a node-set. got ' + nodeSet);
|
|
1385
|
+
}
|
|
1386
|
+
return nodeSet.nodes[0].name;
|
|
1387
|
+
},
|
|
1388
|
+
'concat': function concat(x) {
|
|
1389
|
+
var l = [];
|
|
1390
|
+
for (var i = 0; i < arguments.length; ++i) {
|
|
1391
|
+
l.push(fn.string(arguments[i]));
|
|
1392
|
+
}
|
|
1393
|
+
return l.join('');
|
|
1394
|
+
},
|
|
1395
|
+
'starts-with': function startsWith(a, b) {
|
|
1396
|
+
var as = fn.string(a), bs = fn.string(b);
|
|
1397
|
+
return as.substr(0, bs.length) === bs;
|
|
1398
|
+
},
|
|
1399
|
+
'contains': function contains(a, b) {
|
|
1400
|
+
var as = fn.string(a), bs = fn.string(b);
|
|
1401
|
+
var i = as.indexOf(bs);
|
|
1402
|
+
if (-1 === i) return false;
|
|
1403
|
+
return true;
|
|
1404
|
+
},
|
|
1405
|
+
'substring-before': function substringBefore(a, b) {
|
|
1406
|
+
var as = fn.string(a), bs = fn.string(b);
|
|
1407
|
+
var i = as.indexOf(bs);
|
|
1408
|
+
if (-1 === i) return '';
|
|
1409
|
+
return as.substr(0, i);
|
|
1410
|
+
},
|
|
1411
|
+
'substring-after': function substringBefore(a, b) {
|
|
1412
|
+
var as = fn.string(a), bs = fn.string(b);
|
|
1413
|
+
var i = as.indexOf(bs);
|
|
1414
|
+
if (-1 === i) return '';
|
|
1415
|
+
return as.substr(i + bs.length);
|
|
1416
|
+
},
|
|
1417
|
+
'substring': function substring(string, start, optEnd) {
|
|
1418
|
+
if (null == string || null == start) {
|
|
1419
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1420
|
+
'Must be at least 2 arguments to string()');
|
|
1421
|
+
}
|
|
1422
|
+
var sString = fn.string(string),
|
|
1423
|
+
iStart = fn.round(start),
|
|
1424
|
+
iEnd = optEnd == null ? null : fn.round(optEnd);
|
|
1425
|
+
// Note that xpath string positions user 1-based index
|
|
1426
|
+
if (iEnd == null)
|
|
1427
|
+
return sString.substr(iStart - 1);
|
|
1428
|
+
else
|
|
1429
|
+
return sString.substr(iStart - 1, iEnd);
|
|
1430
|
+
},
|
|
1431
|
+
'string-length': function stringLength(optString) {
|
|
1432
|
+
return fn.string.call(this, optString).length;
|
|
1433
|
+
},
|
|
1434
|
+
'normalize-space': function normalizeSpace(optString) {
|
|
1435
|
+
var s = fn.string.call(this, optString);
|
|
1436
|
+
return s.replace(/[ \t\r\n]+/g, ' ').replace(/^ | $/g, '');
|
|
1437
|
+
},
|
|
1438
|
+
'translate': function translate(string, from, to) {
|
|
1439
|
+
var sString = fn.string.call(this, string),
|
|
1440
|
+
sFrom = fn.string(from),
|
|
1441
|
+
sTo = fn.string(to);
|
|
1442
|
+
var eachCharRe = [];
|
|
1443
|
+
var map = {};
|
|
1444
|
+
for (var i = 0; i < sFrom.length; ++i) {
|
|
1445
|
+
var c = sFrom.charAt(i);
|
|
1446
|
+
map[c] = sTo.charAt(i); // returns '' if beyond length of sTo.
|
|
1447
|
+
// copied from goog.string.regExpEscape in the Closure library.
|
|
1448
|
+
eachCharRe.push(
|
|
1449
|
+
c.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
|
|
1450
|
+
replace(/\x08/g, '\\x08'));
|
|
1451
|
+
}
|
|
1452
|
+
var re = new RegExp(eachCharRe.join('|'), 'g');
|
|
1453
|
+
return sString.replace(re, function(c) {return map[c];});
|
|
1454
|
+
},
|
|
1455
|
+
/// Boolean functions
|
|
1456
|
+
'not': function not(x) {
|
|
1457
|
+
var bx = fn['boolean'](x);
|
|
1458
|
+
return !bx;
|
|
1459
|
+
},
|
|
1460
|
+
'true': function trueVal() { return true; },
|
|
1461
|
+
'false': function falseVal() { return false; },
|
|
1462
|
+
// TODO
|
|
1463
|
+
'lang': function lang(string) { throw new Error('Not implemented');},
|
|
1464
|
+
'sum': function sum(optNodeSet) {
|
|
1465
|
+
if (null == optNodeSet) return fn.sum(this);
|
|
1466
|
+
// for node-sets, map id over each node value.
|
|
1467
|
+
var sum = 0;
|
|
1468
|
+
for (var i = 0; i < optNodeSet.nodes.length; ++i) {
|
|
1469
|
+
var node = optNodeSet.nodes[i];
|
|
1470
|
+
var x = fn.number({nodes:[node]});
|
|
1471
|
+
sum += x;
|
|
1472
|
+
}
|
|
1473
|
+
return sum;
|
|
1474
|
+
},
|
|
1475
|
+
'floor': function floor(number) {
|
|
1476
|
+
return Math.floor(fn.number(number));
|
|
1477
|
+
},
|
|
1478
|
+
'ceiling': function ceiling(number) {
|
|
1479
|
+
return Math.ceil(fn.number(number));
|
|
1480
|
+
},
|
|
1481
|
+
'round': function round(number) {
|
|
1482
|
+
return Math.round(fn.number(number));
|
|
1483
|
+
}
|
|
1484
|
+
};
|
|
1485
|
+
/***************************************************************************
|
|
1486
|
+
* Evaluation: operators *
|
|
1487
|
+
***************************************************************************/
|
|
1488
|
+
var more = {
|
|
1489
|
+
UnaryMinus: function(x) { return -fn.number(x); },
|
|
1490
|
+
'+': function(x, y) { return fn.number(x) + fn.number(y); },
|
|
1491
|
+
'-': function(x, y) { return fn.number(x) - fn.number(y); },
|
|
1492
|
+
'*': function(x, y) { return fn.number(x) * fn.number(y); },
|
|
1493
|
+
'div': function(x, y) { return fn.number(x) / fn.number(y); },
|
|
1494
|
+
'mod': function(x, y) { return fn.number(x) % fn.number(y); },
|
|
1495
|
+
'<': function(x, y) {
|
|
1496
|
+
return comparisonHelper(function(x, y) { return fn.number(x) < fn.number(y);}, x, y, true);
|
|
1497
|
+
},
|
|
1498
|
+
'<=': function(x, y) {
|
|
1499
|
+
return comparisonHelper(function(x, y) { return fn.number(x) <= fn.number(y);}, x, y, true);
|
|
1500
|
+
},
|
|
1501
|
+
'>': function(x, y) {
|
|
1502
|
+
return comparisonHelper(function(x, y) { return fn.number(x) > fn.number(y);}, x, y, true);
|
|
1503
|
+
},
|
|
1504
|
+
'>=': function(x, y) {
|
|
1505
|
+
return comparisonHelper(function(x, y) { return fn.number(x) >= fn.number(y);}, x, y, true);
|
|
1506
|
+
},
|
|
1507
|
+
'and': function(x, y) { return fn['boolean'](x) && fn['boolean'](y); },
|
|
1508
|
+
'or': function(x, y) { return fn['boolean'](x) || fn['boolean'](y); },
|
|
1509
|
+
'|': function(x, y) { return {nodes: mergeNodeLists(x.nodes, y.nodes)}; },
|
|
1510
|
+
'=': function(x, y) {
|
|
1511
|
+
// optimization for two node-sets case: avoid n^2 comparisons.
|
|
1512
|
+
if ('object' === typeof x && 'object' === typeof y) {
|
|
1513
|
+
var aMap = {};
|
|
1514
|
+
for (var i = 0; i < x.nodes.length; ++i) {
|
|
1515
|
+
var s = fn.string({nodes:[x.nodes[i]]});
|
|
1516
|
+
aMap[s] = true;
|
|
1517
|
+
}
|
|
1518
|
+
for (var i = 0; i < y.nodes.length; ++i) {
|
|
1519
|
+
var s = fn.string({nodes:[y.nodes[i]]});
|
|
1520
|
+
if (aMap[s]) return true;
|
|
1521
|
+
}
|
|
1522
|
+
return false;
|
|
1523
|
+
} else {
|
|
1524
|
+
return comparisonHelper(function(x, y) {return x === y;}, x, y);
|
|
1525
|
+
}
|
|
1526
|
+
},
|
|
1527
|
+
'!=': function(x, y) {
|
|
1528
|
+
// optimization for two node-sets case: avoid n^2 comparisons.
|
|
1529
|
+
if ('object' === typeof x && 'object' === typeof y) {
|
|
1530
|
+
if (0 === x.nodes.length || 0 === y.nodes.length) return false;
|
|
1531
|
+
var aMap = {};
|
|
1532
|
+
for (var i = 0; i < x.nodes.length; ++i) {
|
|
1533
|
+
var s = fn.string({nodes:[x.nodes[i]]});
|
|
1534
|
+
aMap[s] = true;
|
|
1535
|
+
}
|
|
1536
|
+
for (var i = 0; i < y.nodes.length; ++i) {
|
|
1537
|
+
var s = fn.string({nodes:[y.nodes[i]]});
|
|
1538
|
+
if (!aMap[s]) return true;
|
|
1539
|
+
}
|
|
1540
|
+
return false;
|
|
1541
|
+
} else {
|
|
1542
|
+
return comparisonHelper(function(x, y) {return x !== y;}, x, y);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
};
|
|
1546
|
+
var nodeTypes = xpath.nodeTypes = {
|
|
1547
|
+
'node': 0,
|
|
1548
|
+
'attribute': 2,
|
|
1549
|
+
'comment': 8, // this.doc.COMMENT_NODE,
|
|
1550
|
+
'text': 3, // this.doc.TEXT_NODE,
|
|
1551
|
+
'processing-instruction': 7, // this.doc.PROCESSING_INSTRUCTION_NODE,
|
|
1552
|
+
'element': 1 //this.doc.ELEMENT_NODE
|
|
1553
|
+
};
|
|
1554
|
+
/** For debugging and unit tests: returnjs a stringified version of the
|
|
1555
|
+
* argument. */
|
|
1556
|
+
var stringifyObject = xpath.stringifyObject = function stringifyObject(ctx) {
|
|
1557
|
+
var seenKey = 'seen' + Math.floor(Math.random()*1000000000);
|
|
1558
|
+
return JSON.stringify(helper(ctx));
|
|
1559
|
+
|
|
1560
|
+
function helper(ctx) {
|
|
1561
|
+
if (Array.isArray(ctx)) {
|
|
1562
|
+
return ctx.map(function(x) {return helper(x);});
|
|
1563
|
+
}
|
|
1564
|
+
if ('object' !== typeof ctx) return ctx;
|
|
1565
|
+
if (null == ctx) return ctx;
|
|
1566
|
+
// if (ctx.toString) return ctx.toString();
|
|
1567
|
+
if (null != ctx.outerHTML) return ctx.outerHTML;
|
|
1568
|
+
if (null != ctx.nodeValue) return ctx.nodeName + '=' + ctx.nodeValue;
|
|
1569
|
+
if (ctx[seenKey]) return '[circular]';
|
|
1570
|
+
ctx[seenKey] = true;
|
|
1571
|
+
var nicer = {};
|
|
1572
|
+
for (var key in ctx) {
|
|
1573
|
+
if (seenKey === key)
|
|
1574
|
+
continue;
|
|
1575
|
+
try {
|
|
1576
|
+
nicer[key] = helper(ctx[key]);
|
|
1577
|
+
} catch (e) {
|
|
1578
|
+
nicer[key] = '[exception: ' + e.message + ']';
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
delete ctx[seenKey];
|
|
1582
|
+
return nicer;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
var Evaluator = xpath.Evaluator = function Evaluator(doc) {
|
|
1586
|
+
this.doc = doc;
|
|
1587
|
+
}
|
|
1588
|
+
Evaluator.prototype = {
|
|
1589
|
+
val: function val(ast, ctx) {
|
|
1590
|
+
console.assert(ctx.nodes);
|
|
1591
|
+
|
|
1592
|
+
if ('number' === typeof ast || 'string' === typeof ast) return ast;
|
|
1593
|
+
if (more[ast[0]]) {
|
|
1594
|
+
var evaluatedParams = [];
|
|
1595
|
+
for (var i = 1; i < ast.length; ++i) {
|
|
1596
|
+
evaluatedParams.push(this.val(ast[i], ctx));
|
|
1597
|
+
}
|
|
1598
|
+
var r = more[ast[0]].apply(ctx, evaluatedParams);
|
|
1599
|
+
return r;
|
|
1600
|
+
}
|
|
1601
|
+
switch (ast[0]) {
|
|
1602
|
+
case 'Root': return {nodes: [this.doc]};
|
|
1603
|
+
case 'FunctionCall':
|
|
1604
|
+
var functionName = ast[1], functionParams = ast[2];
|
|
1605
|
+
if (null == fn[functionName])
|
|
1606
|
+
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
|
|
1607
|
+
'Unknown function: ' + functionName);
|
|
1608
|
+
var evaluatedParams = [];
|
|
1609
|
+
for (var i = 0; i < functionParams.length; ++i) {
|
|
1610
|
+
evaluatedParams.push(this.val(functionParams[i], ctx));
|
|
1611
|
+
}
|
|
1612
|
+
var r = fn[functionName].apply(ctx, evaluatedParams);
|
|
1613
|
+
return r;
|
|
1614
|
+
case 'Predicate':
|
|
1615
|
+
var lhs = this.val(ast[1], ctx);
|
|
1616
|
+
var ret = {nodes: []};
|
|
1617
|
+
var contexts = eachContext(lhs);
|
|
1618
|
+
for (var i = 0; i < contexts.length; ++i) {
|
|
1619
|
+
var singleNodeSet = contexts[i];
|
|
1620
|
+
var rhs = this.val(ast[2], singleNodeSet);
|
|
1621
|
+
var success;
|
|
1622
|
+
if ('number' === typeof rhs) {
|
|
1623
|
+
success = rhs === singleNodeSet.pos[0][0];
|
|
1624
|
+
} else {
|
|
1625
|
+
success = fn['boolean'](rhs);
|
|
1626
|
+
}
|
|
1627
|
+
if (success) {
|
|
1628
|
+
var node = singleNodeSet.nodes[0];
|
|
1629
|
+
ret.nodes.push(node);
|
|
1630
|
+
// skip over all the rest of the same node.
|
|
1631
|
+
while (i+1 < contexts.length && node === contexts[i+1].nodes[0]) {
|
|
1632
|
+
i++;
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
return ret;
|
|
1637
|
+
case 'PathExpr':
|
|
1638
|
+
// turn the path into an expressoin; i.e., remove the position
|
|
1639
|
+
// information of the last axis.
|
|
1640
|
+
var x = this.val(ast[1], ctx);
|
|
1641
|
+
// Make the nodeset a forward-direction-only one.
|
|
1642
|
+
if (x.finalize) { // it is a NodeMultiSet
|
|
1643
|
+
return {nodes: x.nodes};
|
|
1644
|
+
} else {
|
|
1645
|
+
return x;
|
|
1646
|
+
}
|
|
1647
|
+
case '/':
|
|
1648
|
+
// TODO: don't generate '/' nodes, just Axis nodes.
|
|
1649
|
+
var lhs = this.val(ast[1], ctx);
|
|
1650
|
+
console.assert(null != lhs);
|
|
1651
|
+
var r = this.val(ast[2], lhs);
|
|
1652
|
+
console.assert(null != r);
|
|
1653
|
+
return r;
|
|
1654
|
+
case 'Axis':
|
|
1655
|
+
// All the axis tests from Step. We only get AxisSpecifier NodeTest,
|
|
1656
|
+
// not the predicate (which is applied later)
|
|
1657
|
+
var axis = ast[1],
|
|
1658
|
+
nodeType = ast[2],
|
|
1659
|
+
nodeTypeNum = nodeTypes[nodeType],
|
|
1660
|
+
shouldLowerCase = true, // TODO: give option
|
|
1661
|
+
nodeName = ast[3] && shouldLowerCase ? ast[3].toLowerCase() : ast[3];
|
|
1662
|
+
nodeName = nodeName === '*' ? null : nodeName;
|
|
1663
|
+
if ('object' !== typeof ctx) return {nodes:[], pos:[]};
|
|
1664
|
+
var nodeList = ctx.nodes.slice(); // TODO: is copy needed?
|
|
1665
|
+
var r = axes[axis](nodeList /*destructive!*/, nodeTypeNum, nodeName, shouldLowerCase);
|
|
1666
|
+
return r;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
};
|
|
1670
|
+
var evaluate = xpath.evaluate = function evaluate(expr, doc, context) {
|
|
1671
|
+
//var astFactory = new AstEvaluatorFactory(doc, context);
|
|
1672
|
+
var stream = new Stream(expr);
|
|
1673
|
+
var ast = parse(stream, astFactory);
|
|
1674
|
+
var val = new Evaluator(doc).val(ast, {nodes: [context]});
|
|
1675
|
+
return val;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
/***************************************************************************
|
|
1679
|
+
* DOM interface *
|
|
1680
|
+
***************************************************************************/
|
|
1681
|
+
var XPathException = xpath.XPathException = function XPathException(code, message) {
|
|
1682
|
+
var e = new Error(message);
|
|
1683
|
+
e.name = 'XPathException';
|
|
1684
|
+
e.code = code;
|
|
1685
|
+
return e;
|
|
1686
|
+
}
|
|
1687
|
+
XPathException.INVALID_EXPRESSION_ERR = 51;
|
|
1688
|
+
XPathException.TYPE_ERR = 52;
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
var XPathEvaluator = xpath.XPathEvaluator = function XPathEvaluator() {}
|
|
1692
|
+
XPathEvaluator.prototype = {
|
|
1693
|
+
createExpression: function(expression, resolver) {
|
|
1694
|
+
return new XPathExpression(expression, resolver);
|
|
1695
|
+
},
|
|
1696
|
+
createNSResolver: function(nodeResolver) {
|
|
1697
|
+
// TODO
|
|
1698
|
+
},
|
|
1699
|
+
evaluate: function evaluate(expression, contextNode, resolver, type, result) {
|
|
1700
|
+
var expr = new XPathExpression(expression, resolver);
|
|
1701
|
+
return expr.evaluate(contextNode, type, result);
|
|
1702
|
+
}
|
|
1703
|
+
};
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
var XPathExpression = xpath.XPathExpression = function XPathExpression(expression, resolver, optDoc) {
|
|
1707
|
+
var stream = new Stream(expression);
|
|
1708
|
+
this._ast = parse(stream, astFactory);
|
|
1709
|
+
this._doc = optDoc;
|
|
1710
|
+
}
|
|
1711
|
+
XPathExpression.prototype = {
|
|
1712
|
+
evaluate: function evaluate(contextNode, type, result) {
|
|
1713
|
+
if (null == contextNode.nodeType)
|
|
1714
|
+
throw new Error('bad argument (expected context node): ' + contextNode);
|
|
1715
|
+
var doc = contextNode.ownerDocument || contextNode;
|
|
1716
|
+
if (null != this._doc && this._doc !== doc) {
|
|
1717
|
+
throw new core.DOMException(
|
|
1718
|
+
core.DOMException.WRONG_DOCUMENT_ERR,
|
|
1719
|
+
'The document must be the same as the context node\'s document.');
|
|
1720
|
+
}
|
|
1721
|
+
var evaluator = new Evaluator(doc);
|
|
1722
|
+
var value = evaluator.val(this._ast, {nodes: [contextNode]});
|
|
1723
|
+
if (XPathResult.NUMBER_TYPE === type)
|
|
1724
|
+
value = fn.number(value);
|
|
1725
|
+
else if (XPathResult.STRING_TYPE === type)
|
|
1726
|
+
value = fn.string(value);
|
|
1727
|
+
else if (XPathResult.BOOLEAN_TYPE === type)
|
|
1728
|
+
value = fn['boolean'](value);
|
|
1729
|
+
else if (XPathResult.ANY_TYPE !== type &&
|
|
1730
|
+
XPathResult.UNORDERED_NODE_ITERATOR_TYPE !== type &&
|
|
1731
|
+
XPathResult.ORDERED_NODE_ITERATOR_TYPE !== type &&
|
|
1732
|
+
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE !== type &&
|
|
1733
|
+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE !== type &&
|
|
1734
|
+
XPathResult.ANY_UNORDERED_NODE_TYPE !== type &&
|
|
1735
|
+
XPathResult.FIRST_ORDERED_NODE_TYPE !== type)
|
|
1736
|
+
throw new core.DOMException(
|
|
1737
|
+
core.DOMException.NOT_SUPPORTED_ERR,
|
|
1738
|
+
'You must provide an XPath result type (0=any).');
|
|
1739
|
+
else if (XPathResult.ANY_TYPE !== type &&
|
|
1740
|
+
'object' !== typeof value)
|
|
1741
|
+
throw new XPathException(
|
|
1742
|
+
XPathException.TYPE_ERR,
|
|
1743
|
+
'Value should be a node-set: ' + value);
|
|
1744
|
+
return new XPathResult(doc, value, type);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
var XPathResult = xpath.XPathResult = function XPathResult(doc, value, resultType) {
|
|
1749
|
+
this._value = value;
|
|
1750
|
+
this._resultType = resultType;
|
|
1751
|
+
this._i = 0;
|
|
1752
|
+
|
|
1753
|
+
// TODO: we removed mutation events but didn't take care of this. No tests fail, so that's nice, but eventually we
|
|
1754
|
+
// should fix this, preferably by entirely replacing our XPath implementation.
|
|
1755
|
+
// this._invalidated = false;
|
|
1756
|
+
// if (this.resultType === XPathResult.UNORDERED_NODE_ITERATOR_TYPE ||
|
|
1757
|
+
// this.resultType === XPathResult.ORDERED_NODE_ITERATOR_TYPE) {
|
|
1758
|
+
// doc.addEventListener('DOMSubtreeModified', invalidate, true);
|
|
1759
|
+
// var self = this;
|
|
1760
|
+
// function invalidate() {
|
|
1761
|
+
// self._invalidated = true;
|
|
1762
|
+
// doc.removeEventListener('DOMSubtreeModified', invalidate, true);
|
|
1763
|
+
// }
|
|
1764
|
+
// }
|
|
1765
|
+
}
|
|
1766
|
+
XPathResult.ANY_TYPE = 0;
|
|
1767
|
+
XPathResult.NUMBER_TYPE = 1;
|
|
1768
|
+
XPathResult.STRING_TYPE = 2;
|
|
1769
|
+
XPathResult.BOOLEAN_TYPE = 3;
|
|
1770
|
+
XPathResult.UNORDERED_NODE_ITERATOR_TYPE = 4;
|
|
1771
|
+
XPathResult.ORDERED_NODE_ITERATOR_TYPE = 5;
|
|
1772
|
+
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE = 6;
|
|
1773
|
+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE = 7;
|
|
1774
|
+
XPathResult.ANY_UNORDERED_NODE_TYPE = 8;
|
|
1775
|
+
XPathResult.FIRST_ORDERED_NODE_TYPE = 9;
|
|
1776
|
+
var proto = {
|
|
1777
|
+
// XPathResultType
|
|
1778
|
+
get resultType() {
|
|
1779
|
+
if (this._resultType) return this._resultType;
|
|
1780
|
+
switch (typeof this._value) {
|
|
1781
|
+
case 'number': return XPathResult.NUMBER_TYPE;
|
|
1782
|
+
case 'string': return XPathResult.STRING_TYPE;
|
|
1783
|
+
case 'boolean': return XPathResult.BOOLEAN_TYPE;
|
|
1784
|
+
default: return XPathResult.UNORDERED_NODE_ITERATOR_TYPE;
|
|
1785
|
+
}
|
|
1786
|
+
},
|
|
1787
|
+
get numberValue() {
|
|
1788
|
+
if (XPathResult.NUMBER_TYPE !== this.resultType)
|
|
1789
|
+
throw new XPathException(XPathException.TYPE_ERR,
|
|
1790
|
+
'You should have asked for a NUMBER_TYPE.');
|
|
1791
|
+
return this._value;
|
|
1792
|
+
},
|
|
1793
|
+
get stringValue() {
|
|
1794
|
+
if (XPathResult.STRING_TYPE !== this.resultType)
|
|
1795
|
+
throw new XPathException(XPathException.TYPE_ERR,
|
|
1796
|
+
'You should have asked for a STRING_TYPE.');
|
|
1797
|
+
return this._value;
|
|
1798
|
+
},
|
|
1799
|
+
get booleanValue() {
|
|
1800
|
+
if (XPathResult.BOOLEAN_TYPE !== this.resultType)
|
|
1801
|
+
throw new XPathException(XPathException.TYPE_ERR,
|
|
1802
|
+
'You should have asked for a BOOLEAN_TYPE.');
|
|
1803
|
+
return this._value;
|
|
1804
|
+
},
|
|
1805
|
+
get singleNodeValue() {
|
|
1806
|
+
if (XPathResult.ANY_UNORDERED_NODE_TYPE !== this.resultType &&
|
|
1807
|
+
XPathResult.FIRST_ORDERED_NODE_TYPE !== this.resultType)
|
|
1808
|
+
throw new XPathException(
|
|
1809
|
+
XPathException.TYPE_ERR,
|
|
1810
|
+
'You should have asked for a FIRST_ORDERED_NODE_TYPE.');
|
|
1811
|
+
return this._value.nodes[0] || null;
|
|
1812
|
+
},
|
|
1813
|
+
get invalidIteratorState() {
|
|
1814
|
+
if (XPathResult.UNORDERED_NODE_ITERATOR_TYPE !== this.resultType &&
|
|
1815
|
+
XPathResult.ORDERED_NODE_ITERATOR_TYPE !== this.resultType)
|
|
1816
|
+
return false;
|
|
1817
|
+
return !!this._invalidated;
|
|
1818
|
+
},
|
|
1819
|
+
get snapshotLength() {
|
|
1820
|
+
if (XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE !== this.resultType &&
|
|
1821
|
+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE !== this.resultType)
|
|
1822
|
+
throw new XPathException(
|
|
1823
|
+
XPathException.TYPE_ERR,
|
|
1824
|
+
'You should have asked for a ORDERED_NODE_SNAPSHOT_TYPE.');
|
|
1825
|
+
return this._value.nodes.length;
|
|
1826
|
+
},
|
|
1827
|
+
iterateNext: function iterateNext() {
|
|
1828
|
+
if (XPathResult.UNORDERED_NODE_ITERATOR_TYPE !== this.resultType &&
|
|
1829
|
+
XPathResult.ORDERED_NODE_ITERATOR_TYPE !== this.resultType)
|
|
1830
|
+
throw new XPathException(
|
|
1831
|
+
XPathException.TYPE_ERR,
|
|
1832
|
+
'You should have asked for a ORDERED_NODE_ITERATOR_TYPE.');
|
|
1833
|
+
if (this.invalidIteratorState)
|
|
1834
|
+
throw new core.DOMException(
|
|
1835
|
+
core.DOMException.INVALID_STATE_ERR,
|
|
1836
|
+
'The document has been mutated since the result was returned');
|
|
1837
|
+
return this._value.nodes[this._i++] || null;
|
|
1838
|
+
},
|
|
1839
|
+
snapshotItem: function snapshotItem(index) {
|
|
1840
|
+
if (XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE !== this.resultType &&
|
|
1841
|
+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE !== this.resultType)
|
|
1842
|
+
throw new XPathException(
|
|
1843
|
+
XPathException.TYPE_ERR,
|
|
1844
|
+
'You should have asked for a ORDERED_NODE_SNAPSHOT_TYPE.');
|
|
1845
|
+
return this._value.nodes[index] || null;
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
1848
|
+
// so you can access ANY_TYPE etc. from the instances:
|
|
1849
|
+
XPathResult.prototype = Object.create(XPathResult,
|
|
1850
|
+
Object.keys(proto).reduce(function (descriptors, name) {
|
|
1851
|
+
descriptors[name] = Object.getOwnPropertyDescriptor(proto, name);
|
|
1852
|
+
return descriptors;
|
|
1853
|
+
}, {
|
|
1854
|
+
constructor: {
|
|
1855
|
+
value: XPathResult,
|
|
1856
|
+
writable: true,
|
|
1857
|
+
configurable: true
|
|
1858
|
+
}
|
|
1859
|
+
}));
|
|
1860
|
+
|
|
1861
|
+
core.XPathException = XPathException;
|
|
1862
|
+
core.XPathExpression = XPathExpression;
|
|
1863
|
+
core.XPathResult = XPathResult;
|
|
1864
|
+
core.XPathEvaluator = XPathEvaluator;
|
|
1865
|
+
|
|
1866
|
+
core.Document.prototype.createExpression =
|
|
1867
|
+
XPathEvaluator.prototype.createExpression;
|
|
1868
|
+
|
|
1869
|
+
core.Document.prototype.createNSResolver =
|
|
1870
|
+
XPathEvaluator.prototype.createNSResolver;
|
|
1871
|
+
|
|
1872
|
+
core.Document.prototype.evaluate = XPathEvaluator.prototype.evaluate;
|
|
1873
|
+
|
|
1874
|
+
return xpath; // for tests
|
|
1875
|
+
};
|