public void Sample1() { const string css = "div, p.foo { font-style: bold; }"; var subject = new CssLexer(); var tokens = subject.GetTokens(css).Where(t => t.Value != "").ToArray(); Check.That(tokens).ContainsExactly( new Token(0, TokenTypes.Name.Tag, "div"), new Token(3, TokenTypes.Operator, ","), new Token(4, TokenTypes.Text, " "), new Token(5, TokenTypes.Name.Tag, "p"), new Token(6, TokenTypes.Name.Class, ".foo"), new Token(10, TokenTypes.Text, " "), new Token(11, TokenTypes.Punctuation, "{"), new Token(12, TokenTypes.Text, " "), new Token(13, TokenTypes.Name.Builtin, "font-style"), new Token(23, TokenTypes.Operator, ":"), new Token(24, TokenTypes.Text, " "), new Token(25, TokenTypes.Name.Builtin, "bold"), new Token(29, TokenTypes.Punctuation, ";"), new Token(30, TokenTypes.Text, " "), new Token(31, TokenTypes.Punctuation, "}") ); }
public override Value ReadStartValue(CssLexer lexer) { // Read off the at: lexer.Read(); // Read the name now: string name = lexer.ReadString(); // Skip any junk: lexer.SkipJunk(); // Map to an at rule: CssAtRule rule = CssAtRules.GetLocal(name); if (rule == null) { // Unrecognised - enter error recovery mode: lexer.ErrorRecovery(); // Done: return(null); } // Create the representitive unit: AtRuleUnit at = new AtRuleUnit(); at.AtRule = rule; // Now in @ mode: rule.SetupParsing(lexer); return(at); }
static void Main(string[] args) { var ms = new MemoryStream(); ms.Write(Encoding.UTF8.GetBytes(@"p { color: red; text-align: center; } #myItems { list-style: square url(http://www.example.com/image.png); background: url(""banner.png"") #00F no-repeat fixed; }")); ms.Seek(0, SeekOrigin.Begin); using (var r = new StreamReader(ms)) using (CssLexer lex = new CssLexer(r)) { while (true) { var n = lex.Next().Result; Console.WriteLine($"{n.Type}; [{n.Value}] [{n.Representation}]"); if (n.Type == TokenType.EOF) { break; } } } Console.Read(); }
public override void OnValueReady(CssLexer lexer) { int count = Count; if (count <= 6) { // 2D matrix. Matrix[0] = Get(0, 1f); // Scale X Matrix[1] = -Get(1, 0f); // Skew Y Matrix[4] = -Get(2, 0f); // Skew X Matrix[5] = Get(3, 1f); // Scale Y return; } // 3D matrix (Hackey! Got to somehow apply a valid mapping from CSS space to world space) Matrix[0] = Get(0, 1f); // Scale X Matrix[1] = -Get(1, 0f); // Skew Y Matrix[2] = Get(2, 0f); Matrix[3] = Get(3, 0f); Matrix[4] = -Get(4, 0f); // Skew X Matrix[5] = Get(5, 1f); // Scale Y Matrix[6] = Get(6, 0f); Matrix[7] = -Get(7, 0f); Matrix[8] = Get(8, 0f); Matrix[9] = Get(9, 0f); Matrix[10] = Get(10, 1f); // Scale Z Matrix[11] = Get(11, 0f); // Perspective Matrix[15] = Get(15, 1f); }
public override void OnValueReady(CssLexer lexer) { if (Count == 0) { return; } Counter = this[0].Text; }
public override void OnValueReady(CssLexer lexer) { // Get the var name: string name = this[0].Text; if (name != null) { Value = lexer.GetVariable(name.ToLower()); } }
public override void OnValueReady(CssLexer lexer) { int length = Count; if (length == 0) { return; } bool mapAll = true; // If any of the parameters are decimals or are just 0 and 1, remap them to INTS. for (int i = 0; i < length; i++) { // Get the decimal value [either a percentage or a 0-1 value]: DecimalUnit dec = this[i] as DecimalUnit; if (dec == null) { continue; } if (dec.RawValue > 1f) { // No remapping required. mapAll = false; break; } } // Re-map the parameters now: for (int i = 0; i < length; i++) { // Get the decimal value: DecimalUnit dec = this[i] as DecimalUnit; if (dec == null) { continue; } if (i == 3 || dec.RawValue < 1f || mapAll) { } else { dec.RawValue /= 255f; } } }
public override Value ReadStartValue(CssLexer lexer) { // Skip the [: lexer.Read(); // Note that we do not read off the close bracket. // This is so the lexer knows it's done reading the value // and can terminate accordingly. SquareBracketUnit block = new SquareBracketUnit(); // Create the value: ValueSet cValue = new ValueSet(); List <Css.Value> values = new List <Css.Value>(); // Keep reading single values until a ] is reached. while (lexer.Peek() != '\0') { // Skip junk: lexer.SkipJunk(); // Read the value: Css.Value value = lexer.ReadSingleValue(); if (value.Type == ValueType.Text) { if (value.Text == "]") { break; } } values.Add(value); } cValue.Count = values.Count; // Copy over: for (int i = 0; i < values.Count; i++) { cValue[i] = values[i]; } block.Value = cValue; return(block); }
public override void OnValueReady(CssLexer lexer) { if (Count == 0) { return; } Counter = this[0].Text; if (Count == 1) { return; } // Separator: Separator = this[1].Text; }
public override void OnValueReady(CssLexer lexer) { // Figure out nth and offset. Value nth = this[0]; if (nth.Text == "odd") { Nth = 2; } else if (nth.Text == "even") { Nth = 2; ChildOffset = 1; } else if (nth is DecimalUnit) { ChildOffset = nth.GetInteger(null, null); } else { Nth = nth.GetInteger(null, null); } int count = Count; if (count == 1) { return; } if (count == 2) { ChildOffset = this[1].GetInteger(null, null); } else if (count == 3) { string op = this[1].Text; ChildOffset = this[2].GetInteger(null, null); if (op == "-") { ChildOffset = -ChildOffset; } } }
public override void OnValueReady(CssLexer lexer) { // The parameter set is now treated as an interpretable set of commands. // Compact them into a single "operator": if (Operators == null) { RequireOperators(); } Operator = BuildOperator(this); if (Operator == this) { Operator = null; } }
public string EditText(string text) { AntlrInputStream inputStream = new AntlrInputStream(text); ITokenSource lexer; IVisitorTree visitor; IChangeTokenSource editorTokens; lexer = new CssLexer(inputStream); visitor = new CssVisitorEditNameSelector(factoryNames, lexer.TokenFactory); editorTokens = new BaseCssEditTokens(visitor); lexer = editorTokens.Edit(lexer); CommonTokenStream cs = new CommonTokenStream(lexer); cs.Fill(); return(cs.GetText()); }
public override Value ReadStartValue(CssLexer lexer) { // Skip the hash: lexer.Read(); if (lexer.SelectorMode) { // Just a hash (ID selector): return(new TextUnit("#")); } // Read the hex string now: string hex = lexer.ReadString(); ColourUnit result = new ColourUnit(); result.SetHex(hex); return(result); }
public override void OnValueReady(CssLexer lexer) { // Get the var name: string name = base[0].Text; if (name.StartsWith("--")) { name = name.Substring(2); } if (name != null) { Value = lexer.GetVariable(name.ToLower()); } if (Value == null) { Value = new Units.DecimalUnit(0); } }
public override void OnValueReady(CssLexer lexer) { // Create the set: List <SelectorMatcher> multi = new List <SelectorMatcher>(); for (int i = 0; i < Count; i++) { // Read and add: SelectorMatcher sm = Css.CssLexer.ReadSelectorMatcher(this[i]); if (sm == null) { // Entire any should be ignored. return; } multi.Add(sm); } Matchers = multi.ToArray(); }
public override void OnValueReady(CssLexer lexer) { int length = Count; if (length < 3) { // Bad call! return; } // Re-map the parameters now: float h = this[0].GetRawDecimal() / 360f; float s = this[1].GetRawDecimal(); float l = this[2].GetRawDecimal(); if (s == 0f) { this[0].SetRawDecimal(l); this[1].SetRawDecimal(l); this[2].SetRawDecimal(l); } else { float q; if (l < 0.5f) { q = l * (1f + s); } else { q = l + s - l * s; } float p = 2f * l - q; this[0].SetRawDecimal(HueToRgb(p, q, h + 1f / 3f)); this[1].SetRawDecimal(HueToRgb(p, q, h)); this[2].SetRawDecimal(HueToRgb(p, q, h - 1f / 3f)); } }
public override void OnValueReady(CssLexer lexer) { // Map to radians: RawValue *= 2f * Mathf.PI; }
public override void OnValueReady(CssLexer lexer) { // Map to 0-1 range: RawValue /= 100f; }
public override void OnValueReady(CssLexer lexer) { // Read the index: Index = this[0].GetInteger(null, null); }
public override void OnValueReady(CssLexer lexer) { }
public static List <String> rewrite(java.io.Reader content, Uri source, ILinkRewriter rewriter, java.io.Writer writer, bool extractImports) { List <String> imports = new List <string>(); CharProducer producer = CharProducer.Factory.create(content, new InputSource(new java.net.URI(source.ToString()))); CssLexer lexer = new CssLexer(producer); try { bool inImport = false; while (lexer.hasNext()) { Token token = lexer.next(); if (extractImports) { if (token.type == CssTokenType.SYMBOL && token.text.ToLower().Equals("@import")) { inImport = true; continue; } if (inImport) { if (token.type == CssTokenType.URI) { Match matcher = urlMatcher.Match(token.text); if (matcher.Success) { imports.Add(matcher.Groups[2].Value.Trim()); } } else if (token.type != CssTokenType.SPACE && token.type != CssTokenType.PUNCTUATION) { inImport = false; } } if (!inImport) { writer.write(token.text); } } else { if (token.type == CssTokenType.URI) { writer.write(rewriteLink(token, source, rewriter)); continue; } writer.write(token.text); } } writer.flush(); } catch (ParseException pe) { pe.printStackTrace(); } catch (Exception ioe) { throw ioe; } return(imports); }
public override void OnValueReady(CssLexer lexer) { string code = this[0].Text; LanguageCode = code.ToLower(); }
public static List<String> rewrite(java.io.Reader content, Uri source, ILinkRewriter rewriter, java.io.Writer writer, bool extractImports) { List<String> imports = new List<string>(); CharProducer producer = CharProducer.Factory.create(content, new InputSource(new java.net.URI(source.ToString()))); CssLexer lexer = new CssLexer(producer); try { bool inImport = false; while (lexer.hasNext()) { Token token = lexer.next(); if (extractImports) { if (token.type == CssTokenType.SYMBOL && token.text.ToLower().Equals("@import")) { inImport = true; continue; } if (inImport) { if (token.type == CssTokenType.URI) { Match matcher = urlMatcher.Match(token.text); if (matcher.Success) { imports.Add(matcher.Groups[2].Value.Trim()); } } else if (token.type != CssTokenType.SPACE && token.type != CssTokenType.PUNCTUATION) { inImport = false; } } if (!inImport) { writer.write(token.text); } } else { if (token.type == CssTokenType.URI) { writer.write(rewriteLink(token, source, rewriter)); continue; } writer.write(token.text); } } writer.flush(); } catch (ParseException pe) { pe.printStackTrace(); } catch (Exception ioe) { throw ioe; } return imports; }
public override void SetupParsing(CssLexer lexer) { // Load as a property map so we can grab the literal "identity" properties: lexer.PropertyMapMode = true; }
public override Value ReadStartValue(CssLexer lexer) { // No longer in selector mode: lexer.SelectorMode = false; // Skip the {: lexer.Read(); if (lexer.PropertyMapMode) { // Clear: lexer.PropertyMapMode = false; // Create map: PropertyMapUnit map = new PropertyMapUnit(); // Create the style: Style mapStyle = new Style(lexer.Scope); // Read the values: mapStyle.LoadProperties(lexer, delegate(Style s, string pName, Css.Value value){ return(map.OnReadProperty(s, pName, value)); }); map.Style = mapStyle; return(map); } // Result: SelectorBlockUnit block = new SelectorBlockUnit(); if (lexer.AtRuleMode) { // Clear at rule mode: lexer.AtRuleMode = false; List <Rule> rules = null; lexer.SkipJunk(); while (lexer.Peek() != '}' && lexer.Peek() != '\0') { Rule[] set; Rule single = lexer.ReadRules(out set); if (single == null) { if (set != null) { if (rules == null) { rules = new List <Rule>(); } for (int x = 0; x < set.Length; x++) { rules.Add(set[x]); } } } else { if (rules == null) { rules = new List <Rule>(); } rules.Add(single); } lexer.SkipJunk(); } block.Rules = rules; } else { // Create the style: Style style = new Style(lexer.Scope); // Read the values: style.LoadProperties(lexer, null); block.Style = style; } // Note that we do not read off the close bracket. // This is so the lexer knows it's done reading the value // and can terminate accordingly. return(block); }
public override void OnValueReady(CssLexer lexer) { // Map to pixels: RawValue *= (float)PowerUI.ScreenInfo.CssPixelDpi; }
/// <summary>True if this @ rule uses nested selectors. Media and keyframes are two examples.</summary> public override void SetupParsing(CssLexer lexer) { lexer.AtRuleMode = true; }
public override void OnValueReady(CssLexer lexer) { // Map to hz: RawValue *= 1000f; }
public override void OnValueReady(CssLexer lexer) { // Map to pixels: RawValue *= 12f * PowerUI.ScreenInfo.CssPixelDpi / 72f; }
public override void OnValueReady(CssLexer lexer) { // Map to DPI: RawValue /= 2.54f; }
public override void OnValueReady(CssLexer lexer) { // Get the direction: Direction = this[0].GetInteger(null, null); }