dianping/cat · error · SyntaxError

@namespace not allowed here.

Error message

@namespace not allowed here.

What it means

Thrown when `@namespace` appears after rules that must follow it: `@namespace` declarations must come after `@charset`/`@import` but before any style rules (and before any element-type selectors that would rely on the namespace). The parser consumes the `@namespace` via `_namespace(false)` and throws `SyntaxError('@namespace not allowed here.')` at its token position; in non-strict mode it degrades to an 'error' event.

Source

Thrown at cat-home/src/main/webapp/assets/js/editor/worker-css.js:2069

                                break;
                            case Tokens.S:
                                this._readWhitespace();
                                break;
                            default:
                                if(!this._ruleset()){
                                    switch(tt){
                                        case Tokens.CHARSET_SYM:
                                            token = tokenStream.LT(1);
                                            this._charset(false);
                                            throw new SyntaxError("@charset not allowed here.", token.startLine, token.startCol);
                                        case Tokens.IMPORT_SYM:
                                            token = tokenStream.LT(1);
                                            this._import(false);
                                            throw new SyntaxError("@import not allowed here.", token.startLine, token.startCol);
                                        case Tokens.NAMESPACE_SYM:
                                            token = tokenStream.LT(1);
                                            this._namespace(false);
                                            throw new SyntaxError("@namespace not allowed here.", token.startLine, token.startCol);
                                        default:
                                            tokenStream.get();  //get the last token
                                            this._unexpectedToken(tokenStream.token());
                                    }

                                }
                        }
                    } catch(ex) {
                        if (ex instanceof SyntaxError && !this.options.strict){
                            this.fire({
                                type:       "error",
                                error:      ex,
                                message:    ex.message,
                                line:       ex.line,
                                col:        ex.col
                            });
                        } else {
                            throw ex;

View on GitHub (pinned to e815e74d4c)

Solutions

  1. Move `@namespace` statements directly after `@charset`/`@import` and before all style rules.
  2. If you do not actually need XML namespaces (plain HTML CSS), delete the `@namespace` line.
  3. If required, restructure so namespaced selectors live in their own file parsed/imported in correct order.
  4. Lint for at-rule ordering (@charset → @import → @namespace → rules) in CI.

Example fix

/* before */
svg|rect { fill: red; }
@namespace svg url(http://www.w3.org/2000/svg); /* not allowed here */

/* after */
@namespace svg url(http://www.w3.org/2000/svg);
svg|rect { fill: red; }
Defensive patterns

Strategy: validation

Validate before calling

function namespaceBeforeRules(css) {
  var m = css.match(/@namespace[^;]+;/);
  if (!m) return true;
  var rulesBefore = css.slice(0, m.index).replace(/@charset[^;]*;|@import[^;]+;/g, '').trim();
  return rulesBefore === '';
}

Try / catch

try {
  parser.parse(css);
} catch (ex) {
  if (/@namespace not allowed/.test(ex.message)) { moveToTop('@namespace'); return; }
  throw ex;
}

Prevention

When it happens

Trigger: `@namespace svg url(...);` placed after a ruleset, `@media`, or `@font-face`; XML-targeted stylesheets where a rule was added above the namespace; generated SVG CSS that appends namespaces at the end.

Common situations: Editing SVG-styled CSS and appending the namespace late; merge conflicts resolved by putting the namespace block below rules; tooling that injects a prefix block before a hand-written namespace.

Related errors


AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14). Data as JSON: /api/errors/2d4a1a9c469045e4. Report an issue: GitHub.