Skip to content

Instantly share code, notes, and snippets.

@kimhogeling
kimhogeling / undoredo.js
Created November 3, 2015 19:05
Undo/Redo with command pattern in javascript
/**
* The command supertype, which is inherited by subtype commands.
* @constructor
* @param {type} normalAction The normal action.
* @param {type} undoAction The opposite of the normal action.
* @param {type} actionValue The value, which will get passed to the action.
* @returns {Command}
*/
function Command(normalAction, undoAction, actionValue) {
this.execute = normalAction;
@iimos
iimos / xpath.js
Last active April 10, 2023 13:13
Micro function that gives xpath by element and elements by xpath.
function xpath(el) {
if (typeof el == "string") return document.evaluate(el, document, null, 0, null)
if (!el || el.nodeType != 1) return ''
if (el.id) return "//*[@id='" + el.id + "']"
var sames = [].filter.call(el.parentNode.children, function (x) { return x.tagName == el.tagName })
return xpath(el.parentNode) + '/' + el.tagName.toLowerCase() + (sames.length > 1 ? '['+([].indexOf.call(sames, el)+1)+']' : '')
}
// Usage: