Current language/text-direction #367
Unanswered
thewilkybarkid
asked this question in
Q&A
Replies: 1 comment
-
|
@thewilkybarkid Hi, there really is no method that can refer to the parent element, but you can re-store the value of the previous node. const posthtml = require('posthtml');
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<header dir="rtl" lang="ar">
<h1>Inherit dir="rtl" lang="ar"</h1>
</header>
</body>
</html>
`;
const plugin = function (tree) {
let dir;
let lang;
tree.walk(node => {
if (node.tag) {
if (node.attrs === undefined) {
node.attrs = {};
}
if (node.attrs.dir && (dir === undefined || node.attrs.dir !== dir)) {
dir = node.attrs.dir;
}
if (node.attrs.lang && (lang === undefined || node.attrs.lang !== lang)) {
lang = node.attrs.lang;
}
if (node.attrs.dir === undefined && dir) {
node.attrs.dir = dir;
}
if (node.attrs.lang === undefined && lang) {
node.attrs.lang = lang;
}
}
return node;
});
return tree;
}
posthtml(plugin)
.process(html)
.then(function (result) {
console.log(result.html);
});result <!DOCTYPE html>
<html lang="en">
<head lang="en">
<title lang="en">Document</title>
</head>
<body lang="en">
<header dir="rtl" lang="ar">
<h1 dir="rtl" lang="ar">Inherit dir="rtl" lang="ar"</h1>
</header>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to find out the
lang/dirfor a node when walking the tree? If not set on an element they're inherited, but I've not seen a way to find out a node's ancestors.Beta Was this translation helpful? Give feedback.
All reactions