Skip to content

Commit

Permalink
Merge pull request #1364 from vivliostyle/fix/attr-selector-wildcard-ns
Browse files Browse the repository at this point in the history
fix: Attribute selectors with wildcard namespace not worked
  • Loading branch information
MurakamiShinyu authored Jul 18, 2024
2 parents a965c77 + b7c45f7 commit 6e04fd9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
75 changes: 58 additions & 17 deletions packages/core/src/vivliostyle/css-cascade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const supportedNamespaces = {
"http://www.idpf.org/2007/ops": true,
"http://www.w3.org/1999/xhtml": true,
"http://www.w3.org/2000/svg": true,
"http://www.w3.org/1998/Math/MathML": true,
};

export const coupledPatterns = [
Expand Down Expand Up @@ -898,15 +899,48 @@ export class CheckNamespaceAction extends ChainedAction {
}
}

function checkAttribute(
element: Element,
ns: string | null,
name: string,
pred: (element: Element, ns: string, name: string) => boolean,
): boolean {
if (!element) {
return false;
}
if (ns !== null) {
return pred(element, ns, name);
}
// For wildcard namespace
for (const qname of element.getAttributeNames()) {
if (qname === name || qname.endsWith(`:${name}`)) {
if (
pred(
element,
qname === name ? "" : element.lookupNamespaceURI(qname.split(":")[0]),
name,
)
) {
return true;
}
}
}
return false;
}

export class CheckAttributePresentAction extends ChainedAction {
constructor(public readonly ns: string, public readonly name: string) {
super();
}

override apply(cascadeInstance: CascadeInstance): void {
if (
cascadeInstance.currentElement &&
cascadeInstance.currentElement.hasAttributeNS(this.ns, this.name)
checkAttribute(
cascadeInstance.currentElement,
this.ns,
this.name,
(element, ns, name) => element.hasAttributeNS(ns, name),
)
) {
this.chained.apply(cascadeInstance);
}
Expand All @@ -924,9 +958,12 @@ export class CheckAttributeEqAction extends ChainedAction {

override apply(cascadeInstance: CascadeInstance): void {
if (
cascadeInstance.currentElement &&
cascadeInstance.currentElement.getAttributeNS(this.ns, this.name) ==
this.value
checkAttribute(
cascadeInstance.currentElement,
this.ns,
this.name,
(element, ns, name) => element.getAttributeNS(ns, name) == this.value,
)
) {
this.chained.apply(cascadeInstance);
}
Expand Down Expand Up @@ -956,14 +993,16 @@ export class CheckNamespaceSupportedAction extends ChainedAction {
}

override apply(cascadeInstance: CascadeInstance): void {
if (cascadeInstance.currentElement) {
const ns = cascadeInstance.currentElement.getAttributeNS(
if (
checkAttribute(
cascadeInstance.currentElement,
this.ns,
this.name,
);
if (ns && supportedNamespaces[ns]) {
this.chained.apply(cascadeInstance);
}
(element, ns, name) =>
!!supportedNamespaces[element.getAttributeNS(ns, name)],
)
) {
this.chained.apply(cascadeInstance);
}
}

Expand All @@ -986,14 +1025,16 @@ export class CheckAttributeRegExpAction extends ChainedAction {
}

override apply(cascadeInstance: CascadeInstance): void {
if (cascadeInstance.currentElement) {
const attr = cascadeInstance.currentElement.getAttributeNS(
if (
checkAttribute(
cascadeInstance.currentElement,
this.ns,
this.name,
);
if (attr && attr.match(this.regexp)) {
this.chained.apply(cascadeInstance);
}
(element, ns, name) =>
!!element.getAttributeNS(ns, name)?.match(this.regexp),
)
) {
this.chained.apply(cascadeInstance);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/vivliostyle/css-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1780,8 +1780,9 @@ export class Parser {
}
token = tokenizer.token();
if (token.type == TokenType.BAR) {
ns = text ? this.namespacePrefixToURI[text] : text;
if (ns == null) {
ns = text && this.namespacePrefixToURI[text];
// if ns === null, it's wildcard namespace
if (ns === undefined) {
this.actions = actionsErrorSelector;
handler.error("E_CSS_UNDECLARED_PREFIX", token);
tokenizer.consume();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/vivliostyle/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function isFlowRoot(element: Element): boolean {
* https://drafts.csswg.org/css2/visuren.html#dis-pos-flo
*/
export function blockify(display: Css.Ident): Css.Ident {
const displayStr = display.toString();
const displayStr = display?.toString() || "block";
let blockifiedStr: string;
switch (displayStr) {
case "inline-flex":
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/vivliostyle/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,6 @@ export function getMathJaxHub(): object {
return null;
}

export function checkMathJax(): void {
if (getMathJaxHub()) {
CssCascade.supportedNamespaces[Base.NS.MATHML] = true;
}
}

export const supportedMediaTypes = {
"application/xhtml+xml": true,
"image/jpeg": true,
Expand Down Expand Up @@ -779,7 +773,6 @@ export class OPFDoc {
public readonly pubURL: string,
) {
this.documentURLTransformer = this.createDocumentURLTransformer();
checkMathJax();
}

// FIXME: TS4055
Expand Down

0 comments on commit 6e04fd9

Please sign in to comment.