Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions cjs/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ exports.isNotParsing = isNotParsing;
const parseFromString = (document, isHTML, markupLanguage) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

const foreignObjects = [];
const ownerSVGElements = [];

let node = document;
let ownerSVGElement = null;
let parsingCData = false;

notParsing = false;
Expand All @@ -60,14 +62,17 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
const { length: slength } = ownerSVGElements;
const { length: flength } = foreignObjects;
if (slength && (!flength || (foreignObjects[flength - 1].ownerSVGElement !== ownerSVGElements[slength - 1]))) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
node.ownerSVGElement = ownerSVGElements[slength - 1];
if (name === 'foreignobject' || name === 'FOREIGNOBJECT') foreignObjects.push(node);
create = false;
}
else if (name === 'svg' || name === 'SVG') {
ownerSVGElement = document.createElementNS(SVG_NAMESPACE, name);
node = append(node, ownerSVGElement, active);
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
ownerSVGElements[slength] = node;
create = false;
}
else if (active) {
Expand Down Expand Up @@ -105,8 +110,10 @@ const parseFromString = (document, isHTML, markupLanguage) => {

// </tagName>
onclosetag() {
if (isHTML && node === ownerSVGElement)
ownerSVGElement = null;
if (isHTML) {
pop(node, ownerSVGElements);
pop(node, foreignObjects);
}
node = node.parentNode;
}
}, {
Expand All @@ -123,3 +130,9 @@ const parseFromString = (document, isHTML, markupLanguage) => {
return document;
};
exports.parseFromString = parseFromString;

const pop = (node, list) => {
let { length } = list;
if (length && node === list[length - 1])
list.pop();
};
27 changes: 20 additions & 7 deletions esm/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export const isNotParsing = () => notParsing;
export const parseFromString = (document, isHTML, markupLanguage) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

const foreignObjects = [];
const ownerSVGElements = [];

let node = document;
let ownerSVGElement = null;
let parsingCData = false;

notParsing = false;
Expand All @@ -58,14 +60,17 @@ export const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
const { length: slength } = ownerSVGElements;
const { length: flength } = foreignObjects;
if (slength && (!flength || (foreignObjects[flength - 1].ownerSVGElement !== ownerSVGElements[slength - 1]))) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
node.ownerSVGElement = ownerSVGElements[slength - 1];
if (name === 'foreignobject' || name === 'FOREIGNOBJECT') foreignObjects.push(node);
create = false;
}
else if (name === 'svg' || name === 'SVG') {
ownerSVGElement = document.createElementNS(SVG_NAMESPACE, name);
node = append(node, ownerSVGElement, active);
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
ownerSVGElements[slength] = node;
create = false;
}
else if (active) {
Expand Down Expand Up @@ -103,8 +108,10 @@ export const parseFromString = (document, isHTML, markupLanguage) => {

// </tagName>
onclosetag() {
if (isHTML && node === ownerSVGElement)
ownerSVGElement = null;
if (isHTML) {
pop(node, ownerSVGElements);
pop(node, foreignObjects);
}
node = node.parentNode;
}
}, {
Expand All @@ -120,3 +127,9 @@ export const parseFromString = (document, isHTML, markupLanguage) => {

return document;
};

const pop = (node, list) => {
let { length } = list;
if (length && node === list[length - 1])
list.pop();
};
7 changes: 7 additions & 0 deletions test/svg/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ try {
assert(false, true, 'facades should not be instantiable');
}
catch (OK) {}


const {parseHTML} = global[Symbol.for('linkedom')];

const html = '<svg><foreignObject><div></div><span>x</span></foreignObject></svg>';

assert(parseHTML(html).document.toString().includes('<div></div>'), true, 'foreignObject');
27 changes: 20 additions & 7 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2272,8 +2272,10 @@ const attribute = (element, end, attribute, value, active) => {
const parseFromString = (document, isHTML, markupLanguage) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

const foreignObjects = [];
const ownerSVGElements = [];

let node = document;
let ownerSVGElement = null;
let parsingCData = false;

const content = new Parser({
Expand All @@ -2287,14 +2289,17 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
const { length: slength } = ownerSVGElements;
const { length: flength } = foreignObjects;
if (slength && (!flength || (foreignObjects[flength - 1].ownerSVGElement !== ownerSVGElements[slength - 1]))) {
node = append$2(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
node.ownerSVGElement = ownerSVGElements[slength - 1];
if (name === 'foreignobject' || name === 'FOREIGNOBJECT') foreignObjects.push(node);
create = false;
}
else if (name === 'svg' || name === 'SVG') {
ownerSVGElement = document.createElementNS(SVG_NAMESPACE, name);
node = append$2(node, ownerSVGElement, active);
node = append$2(node, document.createElementNS(SVG_NAMESPACE, name), active);
ownerSVGElements[slength] = node;
create = false;
}
else if (active) {
Expand Down Expand Up @@ -2332,8 +2337,10 @@ const parseFromString = (document, isHTML, markupLanguage) => {

// </tagName>
onclosetag() {
if (isHTML && node === ownerSVGElement)
ownerSVGElement = null;
if (isHTML) {
pop(node, ownerSVGElements);
pop(node, foreignObjects);
}
node = node.parentNode;
}
}, {
Expand All @@ -2348,6 +2355,12 @@ const parseFromString = (document, isHTML, markupLanguage) => {
return document;
};

const pop = (node, list) => {
let { length } = list;
if (length && node === list[length - 1])
list.pop();
};

const htmlClasses = new Map;

const registerHTMLClass = (names, Class) => {
Expand Down
Loading