From 886a160c6a7ba22fa55b88261f8961f5d56a504a Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 4 Jan 2018 22:02:50 +0100 Subject: [PATCH] Strip leading/trailing whitespace from CSS exprs When tokenising CSS expressions we now strip leading and trailing whitespace from the input string. This is performed without any checks as a check + `String#strip` ended up being slower compared to just running `String#strip`. On top of that we cache expressions anyway, so the overhead of `String#strip` is very small. Fixes https://gitlab.com/yorickpeterse/oga/issues/187 --- lib/oga/css/lexer.rl | 2 +- spec/oga/css/lexer_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 spec/oga/css/lexer_spec.rb diff --git a/lib/oga/css/lexer.rl b/lib/oga/css/lexer.rl index 3ef0a45..75e81ac 100644 --- a/lib/oga/css/lexer.rl +++ b/lib/oga/css/lexer.rl @@ -23,7 +23,7 @@ module Oga # @param [String] data The data to lex. def initialize(data) - @data = data + @data = data.strip end # Gathers all the tokens for the input and returns them as an Array. diff --git a/spec/oga/css/lexer_spec.rb b/spec/oga/css/lexer_spec.rb new file mode 100644 index 0000000..d00e347 --- /dev/null +++ b/spec/oga/css/lexer_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe Oga::CSS::Lexer do + it 'ignores leading and trailing whitespace' do + expect(lex_css(' foo ')).to eq([[:T_IDENT, 'foo']]) + end +end