-
Notifications
You must be signed in to change notification settings - Fork 0
Nilbog class
A Nilbog
instance assigns observers to prevent certain mutating actions on DOM elements.
For a more detailed explanation, see Conflict Management.
Safe mode keeps a record of every observer assigned by an instance of Nilbog
. This prevents conflicts if there are multiple observers trying to operate on the same mutated element.
- No infinite feedback loops between multiple observers
- Foreign scripts could potentially find the list of observers and disconnect all of them.
- Will only track within the instance. So if another instance of
Nilbog
is created, there's still the potential for a feedback loop.
const nilbog = new Nilbog()
const nilbog = new Nilbog(false) // safe mode: off
-
safe
(optional, default:true
) - Prevents conflicts between multiple Nilbog observers.
Prevents creation of elements matching selector
.
-
selector
(required) - Elements matching this will not be allowed to be created.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease.
-
Observer
instance
Prevents deletion of elements matching selector
.
-
selector
(required) - Elements matching this will not be allowed to be deleted.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease. -
onTreeDeletion
(optional, default:false
) - If a parent of a matched element is deleted, what should Nilbog do?-
false
- Do nothing. It's gone forever. -
"recreate-full-tree"
- Recreate the entire tree of the deleted parent element. -
"recreate-direct-path"
- Recreate the path(s) leading to the matched element(s), removing unnecessary relatives. -
"place-at-top-level"
- Place matched elements where the deleted parent element was.
-
-
Observer
instance
Prevents modification of inner text of elements matching selector
.
-
selector
(required) - Elements matching this will not be allowed to have their inner text modified.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease.
-
Observer
instance
Prevents actions on attributes of elements matching selector
.
-
selector
(required) - Elements matching this will have their attributes protected.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease. -
rules
(optional, default:{ allow: false, prevent: true }
) - Prevent/allow creation, deletion, and/or modification of specific or all attributes.
-
Observer
instance
This will refuse to act on changes to the class
attribute. Use protectClasses
instead.
Prevents actions on classes of elements matching selector
.
-
selector
(required) - Elements matching this will not be allowed to have their classes protected.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease. -
rules
(optional, default:{ allow: false, prevent: true }
) - Prevent/allow creation or deletion of specific or all classes.
-
Observer
instance
Prevents deletion and protects text, classes, and attributes of element.
-
selector
(required) - Elements matching this will not be allowed to be mutated.
-
parent
(optional, default:document.documentElement
) - Element to watch on. If this element is destroyed, observation will cease.
-
Object
:-
preventDelete
:Observer
-
protectText
:Observer
-
protectClasses
:Observer
-
protectAttributes
:Observer
-
-
safe
(typeboolean
) - Is safe mode on? -
conflictManager
(typeConflictManager
) - Resolves conflicts when safe mode is on.
Nilbog uses jQuery-like selectors. It can either be a CSS-style selector (div#id.class
), an HTML element (document.getElementById('id')
), or a list of elements and/or selectors.
The protectClasses
and protectAttributes
observers share a ruleset based configuration. Both offer discrimination based on creation and deletion. protectAttributes
additionally offers modification. By default, everything is prevented with no allowances. Check out the examples section below to see how ruleset configuration works.
// Default ruleset when `rules` is undefined
nilbog.protectAttributes(selector, {
rules: {
allow: false, // No allowances
prevent: true // Prevent everything
}
})
// Above is equivalent to:
nilbog.protectAttributes(selector, {
rules: {
allow: {
create: false, // Don't allow creation
delete: false, // Don't allow deletion
modify: false // Don't allow modification
},
prevent: {
create: true, // Prevent all creation
delete: true, // Prevent all deletion
modify: true // Prevent all modification
}
})
// Exception-based rules
nilbog.protectAttributes(selector, {
rules: {
allow: {
create: false, // Don't allow creation
delete: false, // Don't allow deletion
modify: ['attr-1', 'attr-2'] // Allow modification of these attributes
},
prevent: true // Prevent everything
})
nilbog.protectAttributes(selector, {
rules: {
allow: {
create: true, // Allow creation
delete: true, // Allow deletion
modify: ['attr-1', 'attr-2'] // Allow modification of only these attributes
},
prevent: {
create: ['attr-3'], // Prevent creation of only this attribute
delete: false, // Don't prevent deletion
modify: true // Prevent modification
}
}
})