The applyAcl
function takes an ACL and an HTML document, makes elements matching the ACL selectors editable, and returns the modified HTML. This is likely used to control which parts of a webpage users can edit.
npm run import -- "apply acl to html"
var importer = require('../Core')
var {selectDom} = importer.import("select tree")
// scan using an acl list, similar to easylist?
// TODO: accept formats:
// {"selector": "selector"}
// {"glob-url@selector": "glob-template-path@selector"}
// {"selector": "html-file@selector"}
// {"selector": "html-file@xpath"} ?
// {"glob-file": {"glob-url"...} || ["selector"]}
const paths = JSON.parse('[]');
function applyAcl(acl, doc) {
if(typeof doc === 'string') {
doc = selectDom('*', doc)
}
if(typeof acl === 'string') {
acl = [acl]
}
var body = selectDom('//body', doc)
if(body) {
// add content editable to -acl list elements
acl.forEach(i => {
var els = selectDom([i], body)
els.forEach(el => {
el.setAttribute('contenteditable', 'contenteditable')
})
})
return doc
} else {
throw Error(`Not found ${url}`)
}
}
module.exports = applyAcl
const { selectDom } = require('../Core/importers/selectTree');
const supportedFormats = {
JSON: {
selector: (json) => JSON.parse(json),
'glob-url@selector': (json) => ({
url: JSON.parse(json)[0],
selector: JSON.parse(json)[1],
}),
'html-file@selector': (json) => ({
file: JSON.parse(json)[0],
selector: JSON.parse(json)[1],
}),
'html-file@xpath': (json) => ({
file: JSON.parse(json)[0],
xpath: JSON.parse(json)[1],
}),
'glob-file': (json) => {
const formats = JSON.parse(json).formats;
return formats.map((format) => {
if (format.type === 'glob-url') {
return {
url: format.glob,
selector: format.selector,
};
} else if (format.type === 'glob-file') {
return {
file: format.glob,
};
}
throw new Error(`Unsupported format type: ${format.type}`);
});
},
},
};
async function applyAcl(acl, doc) {
if (typeof doc ==='string') {
doc = await selectDom('*', doc);
}
if (typeof acl ==='string') {
acl = [acl];
}
const body = await selectDom('//body', doc);
if (!body) {
throw new Error('Not found');
}
const aclElements = await selectDom(acl, body);
aclElements.forEach((el) => {
el.setAttribute('contenteditable', 'true');
});
return doc;
}
module.exports = applyAcl;
This code defines a function applyAcl
that takes an ACL (Access Control List) and an HTML document as input.
Here's a breakdown:
Input Handling:
DOM Manipulation:
selectDom
function (imported from a Core
module) to select elements from the HTML document based on the provided ACL selectors.contenteditable
attribute to true
, making the element editable.Error Handling:
//body
element is not found in the HTML document, it throws an error.Output:
Purpose:
The code essentially aims to make specific elements within an HTML document editable based on a predefined ACL. This could be used in a web application where you want to allow users to modify certain parts of the content while restricting editing in other areas.
Let me know if you have any more questions.